You are to write the following two programs. Use my WavReader.java library, and look at the other programs for ideas on how to proceed.
Create an array of short (short []) to hold the data, fill it in with a loop (using Math.sin() to create the sine wave), and then use WavReader.writeWav() to create the file.
The formula for a sine wave of frequency f is sin(2πfx). Use π = Math.PI.
Note, though, that for filling a discrete array
short[] samples you will need to express
the frequency f in terms of the sampling rate (because the values samples[i]
are expressed in terms of the sampling rate). To make this conversion, f should
be the original frequency (in Hertz) divided by the sampling rate
(in samples per second).
Be sure you use floating-point division!
(You will also use the array index i instead of x, and multiply
the sine result by the volume level.)
Those registered for graduate
credit should also generate sawtooth, triangle, and square waves. Which
one sounds the most musically interesting? This
is easiest if you write saw(t), tri(t), and sqr(t) functions with
period 1 (not 2π!), in which case you will set samples[i] =
saw(freq*i). To find saw(t), etc, first, scale t so that it is in the
range [0,1]: t
= t - Math.floor(t). Then shift so that t is in the range [-1/2, 1/2]:
if (t>1/2) t = t-1. Then saw(t) = 2*t; sqr(t) = 1.0 if t>=0, -1.0
otherwise, and the formuas for the t>0 and t<0 halves of tri(t)
are 1-4*t and 1+4*t, respectively.
To mix WavReader objects wr1, wr2, ... wrn (constructed from the corresponding files), form the sum
sample[i] = wr1.readValue() + wr2.readValue() + ... + wrn.readValue();
Note that you'll also have to divide by N, the number of files, in order
to avoid overrunning the 32767 16-bit-maximum-sample-value limit.
You will need to create an array of WavReader objects, one for each file, so you can read from each file in turn, and compute the above sum in a loop.
The files listed on the command line will appear in the String[] args parameter to main. The number of files is args.length; the number of input files is N=args.length-1, the output file name is args[N] and the input files are args[0] ... args[N-1].
Undergraduates only: you may implement this so that it takes
exactly two input files, rather than an arbitrary number. In this case,
you don't need an array of WavReaders, just two of them.