Math.floor()
.
Math.ceil()
.
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.
a
and b
are of type double
,
StdRandom.uniformDouble(a, b)
returns a uniformly random floating-point number between a
and b
.
Math.PI
.
StdAudio.SAMPLE_RATE
.
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.
sineWave()
, squareWave()
, and sawWave()
have repeated code. Is there anyway to restructure my program to avoid duplicating code?\(f \; = \; 2^{\, (n - 69) \, / \, 12} \; \times \; 440\)
This would make a nice function.
Here is a triangle wave whose frequency is 4 Hz:
Here is a pulse wave whose frequency is 4 Hz and duty cycle is 25%:
However, two sinusoids of the same frequency but different phases can interact (interfere) with other sound waves differently. To model this, a sound synthesizer would typically add a phase parameter \(\phi\) (between 0 and \(2 \pi\) radians) to the sine wave:
\( y(t) \; = \; \sin ( 2 \pi \, f \, t \, + \, \phi)\)