Recorded Audio is Shorter than Expected

3 views (last 30 days)
I am trying to use dsp tool box to do real-time audio recording. I use tic toc to time it. However, recorded audio is always shorter than desired time. I am wondering what might cause it. Here is my coded:
close all;
clear all;
clc
frameLength = 1024;
fs=48000;
audioFrameLength=1024;
fileWriter = dsp.AudioFileWriter( ...
'Filename', 'test_mic_array.wav', ...
'SampleRate',fs,...
'DataType','int32');
deviceWriter=audioDeviceWriter(...
'SampleRate', fs,...
'BitDepth','24-bit integer')
deviceReader = audioDeviceReader(...
'Device', 'miniDSP ASIO Driver',...
'Driver', 'ASIO', ...
'SampleRate', fs, ...
'NumChannels', 8 ,...
'SamplesPerFrame', audioFrameLength,...
'BitDepth', '32-bit float',...
'OutputDataType','int32');
setup(deviceReader)
scope = timescope( ...
'SampleRate',fs, ...
'TimeSpan',2, ...
'BufferLength',fs*2*2, ...
'YLimits',[-2^32,2^32], ...
'TimeSpanOverrunAction',"Scroll");
reverb = reverberator( ...
'SampleRate', 48000, ...
'PreDelay',0.5, ...
'WetDryMix',0.4);
% fprintf('Latency due to device buffer: %f seconds.\n',deviceReader.SamplesPerFrame/deviceReader.SampleRate)
disp('Recording now.')
tic
while toc < 20
acquiredAudio = deviceReader();
acquiredAudio=bitshift(acquiredAudio,10);
scope(acquiredAudio)
deviceWriter(acquiredAudio);
fileWriter(acquiredAudio);
end
disp('Recording complete.')
release(fileWriter)
release(deviceReader)
release(deviceWriter)
release(reverb)
release(scope)

Accepted Answer

jibrahim
jibrahim on 11 Mar 2022
Hi Xiwen,
I suspect the scope is taking a long time to set up the first time you call it.
Consider adding this code outside of the for loop to remove this overhead:
setup(scope,zeros(frameLength,1,'int32'))
scope.show

More Answers (0)

Categories

Find more on Audio I/O and Waveform Generation 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!