Y=wavread('filename');we can replay the sound with the command
sound(Y,22050);provided the computer has a sound card.
This now allows us to create our own sounds. For example the sequence of commands
t=linspace(0,4,40000); y=sin(400*2*pi*t); sound(y,10000);will create a sound of 400 Hz that lasts 4 seconds. We used a sampling rate of 10000 here, since this seems sufficient for a sound of 400 cycles per second, i.e. in each cycle the function
As a little advanced exercise we will write a program that plays a C-major scale. To start we need to find the frequencies of the individual sounds. A middle A is usually used to tune instruments. That note has a frequency of 440 Hz. The base A has half the frequency, and there are twelve notes in the octave between a base A and a middle A. Similar the octave between a low C (the start of the C-major scale) and the high c its end contains twelve notes (including the so called half-tones).
|
In order to sound a C for 2 seconds, we write the following program:
t=linspace(0,2,20000); y=sin(262*2*pi*t); sound(y,10000);and call it ``cee.m''. We can write similar programs for all other notes and then the C-major scale
pause; cee; pause; dee; pause; ee; pause; eff; pause; gee; pause; aa; pause; bee; pause; hcee;call it ``scale .m''. The pause commands are requests for key board input. Pressing any key will move the program to the next line. Typing
scalewill allow us to play a C-major scale by pressing any key to move to the next note. An obvious improvement would be to allow for different length of the notes.