MATLAB Answers

Audiorecorder level trigger starting record

9 views (last 30 days)
Benoit Meriot
Benoit Meriot on 1 Jun 2016
Answered: Brian on 21 Aug 2019
Hello everyone I'm trying to use Matlab to record sound from two microphones. I'm using an audiorecorder object to do so
Here's the part of my code concerned :
recObj = audiorecorder(48000,16,2);
recordblocking(recObj, 10);
y=getaudiodata(recObj);
I would like to use a level trigger to start the recording for a fixed time. Is there a function to do it ? instead of using recordblocking

Answers (2)

Brian
Brian on 21 Aug 2019
I know this is three years late, but in case anybody else comes here looking for a solution, you can use a combination of record (instead of record blocking), pause, and getaudiodata if you are using an audiorecorder object. The audiorecorder continues to record while you collect and analyze the data. See sample code below.
triggerLevel=1; % Level to trigger the recording
preRecord=0.1; % Time to record prior to peak
postRecord=0.5; % Time to record after peak
fs=96000; nBits=16;
recObj=audiorecorder(fs,nBits,1,0);
record(recObj) % Start recording
pause(0.5) % Wait half a second
tempdata=getaudiodata(recObj); % Grab data
while max(abs(tempdata))<triggerLevel % Check to see if you've hit the trigger level
pause(0.5) % Keep recording and gathering data if you haven't
tempdata=getaudiodata(recObj);
end
pause(postRecord) % Pause for the specified time
stop(recObj) % Stop recording
finaldata=getaudiodata(recObj);
[~,I]=max(abs(finaldata)); % Find the location of the peak
windowSamples=fs*[preRecord postRecord]+I; % Get window
dataout=finaldata(windowsamples(1):windowsamples(2));
I would recommend including a timeout function so you don't get into an infinite loop, but this is the guts. I hope to have a function submitted in the File Exchange soon, so I will update if it gets posted.

Pavel Dey
Pavel Dey on 9 Jun 2016
I think you may refer to this link , which answers a similar query.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!