Expand All Collapse All


Synth
Should I implement the ceiling and floor functions?
No. You can use Math.floor(). Math.ceil().
How do I compute xy in Java?
The method call Math.pow(x, y) returns x raised to the power y, where x and y are floating-point numbers. Note that x ^ y will not work.
How do I generate a uniformly random floating-point number in a specified interval?
If a and b are of type double, StdRandom.uniformDouble(a, b) returns a uniformly random floating-point number between a and b.
Which value should I use for the mathematical constant π?
Use the constant Math.PI.
How can I avoid hardwiring the constant 44100 in my program?
Use the constant StdAudio.SAMPLE_RATE.
Any suggestions for testing the functions that return arrays?
Print the samples to standard output (instead of playing on standard audio). Consider writing a private helper method to print the elements in an array. Also, to avoid being overwhelmed with output, use a small duration. For example, a duration of 0.00021 seconds will produce arrays of length 10.

For the square() function, we suggest a using a frequency that's divisible by 44,100 since it will result in a repeating pattern. For example, with a frequency of 22,050 Hz, the samples will alternate between +1.0 and –1.0.

My program doesn't make any sound when the frequency is lower than 20 Hz or higher than 20,000 Hz. Why not?
The range of human hearing is approximately 20 Hz to 20,000 Hz, although it varies by individual.
The sineWave(), squareWave(), and sawWave() have repeated code. Is there anyway to restructure my program to avoid duplicating code?
Yes, but that would require learning about functional programming, in which you can pass functions to other functions.
MySoundWave
My sound isn't 10 seconds long. How can I make it longer?
You can play the sound repeatedly in a short loop. Of, if the sound depends on a frequency parameter, you can generate the sound at different frequencies (such as a major scale) and play each sound.
I'd like to use my sound to play a song. How can I do that?
One approach is to read MIDI note numbers and durations from standard input; create the corresponding sound waves; and play them. You can convert from a MIDI note number n to frequency f using the following formula:

\(f \; = \; 2^{\, (n - 69) \, / \, 12} \; \times \; 440\)

This would make a nice function.

Which other types of sound waves are commonly used as building blocks in synthesizers?