How to record and analyze audio in real-time?

17 views (last 30 days)
Hi all,
I would like to have audio recorded continously, and at each time interval, grab the audio data and analyze the data without interrupting the recording.
I can imagine using two threads, one for recording, the other for analyzing. I know how to do recording and analyzing, but I don't know how to implement two threads. Does matlab support multiple threads for this kind of task? Could someone give me an example of how to do this? I would greatly appreciate that.
Thanks,
  13 Comments
Oskar Kilgus
Oskar Kilgus on 29 Jun 2022
From @Geoff Hayes linked code
i tried to transfer this to pitch analysis. The problem with pitch seems to be the windowlength/overlaplength so you cant plot the XData/YData due to different sizes/shapes.
Do you guys have an idea how to make that work?
function [recorder] = myAudioRecording(Fs,durationSecs)
% add an extra half-second so that we get the full duration in our
% processing
durationSecs = durationSecs + 0.5;
% index of the last sample obtained from our recording
lastSampleIdx = 0;
% start time of the recording
atTimSecs = 0;
% create the audio recorder
recorder = audiorecorder(Fs,8,1);
% assign a timer function to the recorder
set(recorder,'TimerPeriod',1,'TimerFcn',@audioTimerCallback);
% create a figure with two subplots (changed it to one)
hFig = figure;
hAxes1 = subplot(1,1,1);
% create the graphics handles to the data that will be plotted on each
% axes (changed it to one)
hPlot1 = plot(hAxes1,NaN,NaN);
drawnow;
% start the recording
record(recorder,durationSecs);
% define the timer callback
function audioTimerCallback(hObject,~)
% get the sample data
samples = getaudiodata(hObject);
% skip if not enough data
if length(samples)<lastSampleIdx+1+Fs
return;
end
% extract the samples that we have not performed pitch on
X = samples(lastSampleIdx+1:lastSampleIdx+Fs);
% compute the pitch
Y = pitch(X,Fs);
% plot the data
t = linspace(0,1-1/Fs,Fs) + atTimSecs; %t wird zu einem zeitvektor von einer sekunde in 8000 Fs Teilen
set(hPlot1,'XData',t,'YData',Y);
% increment the time in seconds "counter"
atTimSecs = atTimSecs + 1;
end
% do not exit function until the figure has been deleted
waitfor(hFig);
end
This is the slightly changed code where i tried to do the exact same thing with pitch-plot. With fs of 8000, t becomes a 1x8000 vector, Y becomes a 95x1 vector.
Thanks :)
Walter Roberson
Walter Roberson on 29 Jun 2022
If you record
half_samples = round(durationseconds * frequency / 2)
per frame, then the previous buffer plus the newest buffer concatenated together make a full current buffer. (If the overlap is not half of a buffer then the buffer strategy can be modified.)

Sign in to comment.

Accepted Answer

Geoff Hayes
Geoff Hayes on 9 Oct 2014
Yanping - when I've used the audio recorder object to record from my microphone, I just add a timer object to the recorder. Then (for example) every second, the timer fires, and I process the most recent one second of data (since the lsat time the timer fired). Something like
recorder = audiorecorder(8192,8,1);
set(recorder,'TimerPeriod',1,'TimerFcn',{@audioTimer});

More Answers (1)

Nissim Gean
Nissim Gean on 19 Jun 2018
Geoff - According to my understanding, in order to start recording and/or "fire" the timer we need to add a third line to your suggestion above, which is:
record( recorder );
But then in order to grab the audio data we need first to stop the recorder object and start it again (after grabbing it) in the audioTimer callback function for the next iteration, something like this:
function audioTimer(recorder)
...
stop(recorder);
data = getaudiodata(recorder);
record(recorder);
...
Since we stop/start the recorder every time for frame-by-frame analysis we cannot get a continuous recording. Could you please explain how exactly you grab the audio data and analyze the data without interrupting the recording?
Thanks!
  8 Comments
Walter Roberson
Walter Roberson on 9 Aug 2020
That code defines the durationSecs parameter if you do not pass anything in. Are you sure that the error is inside the function, and not when you try to call the function? What parameters are you passing to the function?
Hamza Ashraf
Hamza Ashraf on 9 Aug 2020
oki i got that i was not passing durationSecs to the fucntion. i have another problem if you can help. i want to pass these following samples in above code to a trained neural network instead of taking fft.
X = samples(lastSampleIdx+1:lastSampleIdx+Fs);
can you tell me how to do that

Sign in to comment.

Categories

Find more on Graphics Performance in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!