MATLAB: Record audio and get data in real time

audio recorder real time

Hello, i want to make a recorder in real time, and i get data every 0.01 second with recording parameters(Fs = 8000, nbits = 16 bits, nbChannel = 1), so i want to get the 80 samples (every 0.01) second to analyse them and apply my work. i used audiorecorder object but no result, when i try to get data it is empty. Thank you

Best Answer

dz - a couple of things to note. You don't need to declare some of your variables as global if you nest your VADML function within a parent function. For example,
function mainScript
dt = [];
c = 0;
dtt = [];
% create the timer
% ...
function VADML(h,~)
dt = ....;
% etc.
end
end
Using globals is fine, but you can avoid it with nested functions and (as in above) there is no need to return dt from VADML since you are updating a local variable in the parent function workspace.
I suspect that you aren't getting any data because you are using record instead of recordblocking. The latter does not return control until recording completes. Using record will not block and so once you call it once, the code will continue on its way and exit the main function or script before it has a chance to do anything. So you want to block to prevent this from happening.
I'm a little unclear on the code in your VADML function
function VADML(h,~)
% Get data from each sample

dt = getaudiodata(h);
% compute energy
c=0;
if mod(length(dt),80)==0
c=c+1;
dtt=dt(1:80);
end
end
Why do you reset c each time this function is called? Won't dtt be the same on each iteration as you are always grabbing the first 80 samples? Note that getaudiodata returns all of the collected data to that point in time. So dt will "grow" on each iteration: 80, 160, 320, etc. I suspect that you want to extract the latter 80 samples.
Finally, the 0.01 second rate may be too ambitious given that you are trying to extract some data and process it. If I just do
function VADML(h,~)
% Get data from each sample
dt = getaudiodata(h) ;
c = c + 1
end
I find that c iterates from 1 to 151 (using R2014a on a MacBook Pro) and not the expected 200 times. You may need to relax this 0.01 second requirement and bump this up to 0.1 seconds.
Related Question