How to make real time audio recording of 1 sec in matlab?

4 次查看(过去 30 天)
I am trying to record 1 sec audio from microphone and save it into a .wav file along with time stamps. However, when output is verified , the code is not recording per sec instead there is a delay of 2 to 4 sec between two sucessive recordings. Here is the code. I dont know what is going wrong and why there is time delay between two recording audio files
for i=1:25
Fs=44100;
disp('Recording Thread')
DataBaseDir='C:\Path';
recorder = audiorecorder(44100,16,1,1);
recordblocking(recorder,1);
y=getaudiodata(recorder);
file = sprintf('%s.wav', datetime(("now"),Format="HH mm ss"));
filename=[DataBaseDir '\' file];
audiowrite(filename,y,Fs);
end

采纳的回答

Walter Roberson
Walter Roberson 2023-3-3
It takes time to construct the recording object. It takes time to retrieve the recorded samples. It takes time to write the samples to file.
You should switch to using Audio System Toolbox and using the audio device recorder system object, which can run continuously.
  3 个评论
Walter Roberson
Walter Roberson 2023-3-3
%tested
DataBaseDir = '.'; %or as appropriate
Fs = 44100;
recorder = audioDeviceReader('SampleRate', Fs, ...
'BitDepth', '16-bit integer', ...
'NumChannels', 1, ...
'SamplesPerFrame', Fs);
writer = dsp.AudioFileWriter('SampleRate',Fs);
for i=1:25
filename = fullfile(DataBaseDir, sprintf('%s.wav', datetime("now",Format="HH mm ss")));
release(writer);
writer.Filename = filename;
y = recorder();
writer(y);
end
release(reader)
release(writer) %necessary to flush last frame to file
This takes a while to get going.

请先登录,再进行评论。

更多回答(0 个)

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by