Sampling Windows Audio Data in MATLAB
This module will walk you through the coding process of saving input audio data from a Windows sound card.
Setup
The first thing you need to do is set how often you would like MATLAB to access your sound card, and how fast you want it to do so. This multiplying these two values together will give you the total size of your sample space. The code below is how we initialized our data input.
refreshrate = .04644; % sec
samplerate = 44100; % Hz
ai = analoginput('winsound', 1); %windows addchannel(ai,[1 2]); %two channels
samplerate = setverify(ai, 'SampleRate', samplerate);
Trigger
Next, you must set a command structure to let MATLAB know when to access the sound data. In the case of our graphical equalizer we wanted MATLAB to sample until we told it to stop so we created an infinite trigger loop. To do this you must set the triggers on your input class.
ai.TimerPeriod = refreshrate;
spt = round(samplerate * refreshrate);
ai.SamplesPerTrigger = spt;
set(ai, 'TriggerRepeat', Inf);
set(ai, 'TimerFcn' , @getdata);
start(ai);
Storing Data and Stopping
Now that you have begun to sample the soundcard, you have to store the sampled data in a buffer for analysis. You need to flush the data in your acquisition structure or you will suffer serious memory leaks. Also, the try and catch structure allows the loading of empty values if the peekdata is empty (a type of error suppression).
try
timesig = peekdata(ai,samples);
flushdata(ai)
catch
timesig = [];
end
Stopping the data acquisition is simple:
stop(ai)
0 comments:
Post a Comment