Clear Filters
Clear Filters

Error when recording multi-channel audio with loopback

9 views (last 30 days)
I am trying to record multi-channel audio where one channel records audio from a microphone and two channels record "internal" sound (i.e., loopback) that is played via MATLAB to headphones. I am working with a digital audio interface (Focusrite Scarlett), ASIO drivers, Windows 10 and MATLAB R2022b.
With the code below I get the following error message:
Error using audioDeviceWriter/setup
The buffer size parameter of the audio input device must match the buffer size parameter of the audio output device.
Error in audioDeviceWriter/setupImpl
I have checked the buffer size parameter using asiosettings and also the settings of the Focusrite and they are set to the same value. I would assume that this applies to both the outputs and inputs of the Focusrite so I cannot explain the error message.
%% define properties of digital audio interface and recording set-up
device = 'Focusrite USB ASIO';
driver = 'ASIO';
Fs = 44100; % sampling frequency
%% set up device reader (for recording)
deviceReader = audioDeviceReader('SampleRate',Fs, ...
'Device',device, ...
'BitDepth','24-bit integer', ...
'Driver',driver, ...
'NumChannels',6, ...
'SamplesPerFrame', Fs/4);
setup(deviceReader);
%% load audio file and set up device writer (for audio output)
fileReader = dsp.AudioFileReader('metronome90bpm.wav');
fileInfo = audioinfo('metronome90bpm.wav');
deviceWriter = audioDeviceWriter('SampleRate',fileInfo.SampleRate, ...
'Device',device, ...
'BitDepth','16-bit integer', ...
'Driver',driver);
setup(deviceWriter, zeros(fileReader.SamplesPerFrame, ...
fileInfo.NumChannels));
Each section on its own does its job, i.e., setting up the device reader and then recording something OR setting up the device writer and playing the audio.
I have tried the multichannel recording (with microphone + loopback) when playing an audiofile in the background (so not in MATLAB) with windows media player and then starting a recording using the device reader and that works fine.

Accepted Answer

Jimmy Lapierre
Jimmy Lapierre on 15 Dec 2022
Hi Charlotte,
When using the reader and writer at the same time, some settings like frame size must match.
Here, fileReader.SamplesPerFrame (1024) does not match the deviceReader SamplesPerFrame (Fs/4).
Maybe you intend to set fileReader SamplesPerFrame to Fs/4 as well.
P.S. might be easier to use audioPlayerRecorder in this case.
  2 Comments
Charlotte Mock
Charlotte Mock on 16 Dec 2022
Edited: Charlotte Mock on 16 Dec 2022
Thanks, Jimmy! It solved the problem.
Regarding using audioPlayerRecorder, as far as I know it doesn't allow loopback...?
Jimmy Lapierre
Jimmy Lapierre on 16 Dec 2022
You might need to specify channel mapping properties to specify which channels you need, but I'm not sure I see what the limitation is.

Sign in to comment.

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!