Telecom programs 1 and 2

Due: June 8

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.

Program 1: oscillator

The oscillator program should generate a specific sine tone for a specified length of time. All the parameters should be towards the beginning of the program so they can be easily changed. You are to assume that the output format is 16-bit mono. You will need to specify: The frequency should be "exact"; that is, if the wavelength is 15.36 sample times then do not round off to 15 sample times.

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.)

Program 2: mixer

Write a program to take a list of .wav files on the command line and combine them into a single output file by adding corresponding components. The last file named on the command line should be the name of the output file.

To mix files F1, F2, ... Fn, form the sum
      sample[i] = F1.readValue() + F2.readValue() + ... + Fn.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.

Demo

Use your oscillator program to generate two sound files 2-5 seconds long that have frequencies differing by 1-2 Hertz (eg 440 and 442). Use your mixer to mix them. Play the result. You should clearly hear the "beat frequency", equal to the difference in the file frequencies (eg 2 Hertz, or 2 beats/sec).