The emphasis on this chapter is getting started with capturing and using audio within MATLAB. This includes recording and playing back directly within MATLAB using the built-in sound tools, although we note that specialised audio tools such as the excellent free and open source Audacity tool for Linux, Mac OS-X and Windows are usually preferable because they are more intuitive and immediate in operation.
However there are many times when manipulating audio within MATLAB is useful - one of those is when running a perception-based test that couples a user interace (or display of information) with audio input/output.
Setting up and recording sound directly in MATLAB starts with creating an audio object, and then recording to it:
aro=audiorecorder(16000,16,1); record(aro);
In fact we can pause and resume recording just as easily:
pause(10); %waits 10 seconds before next command pause(aro); %this pauses the recording resume(aro); %this resumes recording
before we stop and then replay the audio to test it:
stop(aro); %this ends the recording session play(aro);
Acutally making use of the recorded audio data, we need to convert this into a standard MATLAB array which we can then process just like any other vector (or array) of information in MATLAB (note: it will be a [1xL] vector for a mono recording of duration
speech=getaudiodata(aro, 'double');
As discussed in section 2.1.3, it is often more convenient to make use of sound files, and so we can load from a Wave format file, and save to another one very easily
[y,fs]=audioread('soundfile.wav'); audiowrite('another_soundfile.wav',y,fs);
Where
To save readers from having to type everything in by hand, the following listings are some of the functions and longer code snippets found in Chapter 2. They can be selected on this page using your mouse, copied, and then pasted into the MATLAB command line, or into the MATLAB editor.
function [s]=tonegen(Ft, Fs, Td) s=sin([1:Fs*Td]*2*pi*Ft/Fs);
cx=cceps(x.*hamming(length(x))); stem(abs(cx)); axis tight; xlabel('Cepstral coefficient') ylabel('Log magnitude')
gensnd=[]; %initialise an empty array for t=0:0.1:4 fr=ModF(1+floor(t*Fs)); % use floor() since index must be integer gensnd=[gensnd,tonegen(fr,8000,0.1)]; % concatenates new tones to end of gensnd end soundsc(gensnd,Fs); % listen then view it spectrogram(gensnd,128,0,128,Fs,'yaxis');
function [snd]=freqgen(frc, Fs) th=0; fr=frc*2*pi/Fs; for si=1:length(fr) th=th+fr(si); snd(si)=sin(th); th=unwrap(th); end
C=tonegen(261.63, 8000, 2); E=tonegen(329.63, 8000, 2); G=tonegen(783.99, 8000, 2); B=tonegen(987.77, 8000, 2); soundsc(C+E+G+B, 8000);