record audio and get data in real time

10 views (last 30 days)
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
  2 Comments
Geoff Hayes
Geoff Hayes on 27 May 2016
dz - please show the code that you have written to use the audio player. Have you created a GUI to start the recording and display the results in real-time? Provide some context as to what you are attempting.
Geoff Hayes
Geoff Hayes on 28 May 2016
dz's answer moved here
clear all
clc
close all
global dt dtt c
%%Create a timer object:
time_rec = 2;
recObj = audiorecorder(8000,16,1);
set(recObj,'TimerPeriod',0.01,'TimerFcn',@VADML);
record(recObj,time_rec)
%%below function is to try to read data by samples for each 0.01 sec
function [dt] = VADML(h,~)
global dt dtt c
% 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

Sign in to comment.

Accepted Answer

Geoff Hayes
Geoff Hayes on 28 May 2016
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.
  4 Comments
Geoff Hayes
Geoff Hayes on 28 May 2016
Perhaps try this
function [sampleData] = mainScript
sampleData = {};
atSample = 1;
atPeriod = 1;
fs = 8000;
timerPer = 0.01;
sampleSize = floor(fs*timerPer);
time_rec = 5; % total time for recording
recObj = audiorecorder(fs,16,1); % create rec object
set(recObj,'TimerPeriod',timerPer,'TimerFcn',@VADML);
recordblocking(recObj,time_rec);
function VADML(h,~)
samples = getaudiodata(h);
sampleData{atPeriod} = samples(atSample:sampleSize+atSample-1);
atPeriod = atPeriod + 1;
atSample = atSample + sampleSize;
end
end
We ensure that we only get 80 samples each time the callback fires but it may still lag. When I tried this just now, in the five seconds, I was only able to get the samples for the first 3 seconds or so...
David Clark
David Clark on 16 Jul 2019
Thank you for this answer! It really helped with a project I am working on. (Simultaneously sampling EKG data while recording audio).

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!