Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Prepare a minimum of 12 sound files and each file should contain one or more non

ID: 3669740 • Letter: P

Question

Prepare a minimum of 12 sound files and each file should contain one or more non-offensive voice sentences. Famous voices can be downloaded from www.wavsource.com. Write a MATLAB program that allows the player to manually select any sound file from a pre-selected group of sound files (that are already saved in the Homework 3 directory) and play the sound. The program should allow the player to select a sound file to play from the group of files continuously until the player picks a sound file that says “goodbye”. When the program ends, print the total number of sound files played in the command window. Add an auto play function to the program, so that when the program starts if the player wishes, the program will randomly select a sound file to play, wait for a random number of seconds, and randomly select another sound file to play. The program can end randomly after a specific sound file (i.e. goodbye) is played. When the program ends, print the total number of sound files played in the command window.

Explanation / Answer

Getting some pre-recorded sound files (digital sound data)

You will first need the following sound files.  Right click on these and choose “Save Link As…” and put these in a folder and make sure you use the pull down menu option File àSet Path to put that folder in the MATLAB path (or simply put the .wav files in your correct working directory.

road.wav

hootie.wav

lunch.au

flute.wav

tenorsax.wav

mutedtrumpet.wav

Loading Sound files into MATLAB

We want to read the digital sound data from the .wav file into an array in our MATLAB workspace.  We can then listen to it, plot it, manipulate, etc.  Use the following command at the MATLAB prompt:

[road,fs]=wavread('road.wav'); % loads “the long and winding road” clip

The array road now contains the stereo sound data and fs is the sampling frequency.  This data is sampled at the same rate as that on a music CD (fs=44,100 samples/second).

See the size of road:  size(road)

The left and right channel signals are the two columns of the road array:

left=road(:,1);

right=road(:,2);

Let’s plot the left data versus time.  Note that the plot will look solid because there are so many data points and the screen resolution can’t show them all.  This picture shows you where the signal is strong and weak over time.

time=(1/44100)*length(left);

t=linspace(0,time,length(left));

plot(t,left)

xlabel('time (sec)');

ylabel('relative signal strength')

Let’s plot a small portion so you can see some details

time=(1/44100)*2000;

t=linspace(0,time,2000);

plot(t,left(1:2000))

xlabel('time (sec)');

ylabel('relative signal strength')

Let’s listen to the data (plug in your headphones).  Click on the speaker icon in the lower right hand corner of your screen to adjust the volume. Enter these commands below one at a time.  Wait until the sound stops from one command before you enter another sound command!

soundsc(left,fs)       % plays left channel as mono

soundsc(right,fs)    % plays right channel mono (sound nearly the same)

soundsc(road,fs)     % plays stereo (ahhh…)

Another audio format is the .au file format.  These files are read in using

[lunch,fs2]=auread('lunch.au');

soundsc(lunch,fs2);

To save an array as a .wav file, use wavwrite( ). Use auwrite( ) for .au format output.