# How do I play and record audio at the same time?

So this question is more related to Matlab itself.

Some background info: I'm trying out the Exponential Sine Sweep method to obtain the Room Impulse Response with my laptop speakers and microphone for testing.

I can generate the Sine sweep perfectly but I have trouble playing and recording it without a lot of lag. I'm now trying to use "audioPlayerRecorder" from the Audio System Toolbox but when I do:

f1 = 80;       % Start frequency (Hz)
f2 = 22000;    % Stop frequency (Hz)
T = 3;         % Sweep duration in seconds
fs = 44100;    % Sampling frequency

[sweep, inverse] = ESS_generator(f1, f2, T, fs);

playRec = audioPlayerRecorder(fs);
recording = playRec(sweep);


I get an error on the last line saying: Error using coder.internal.assert (line 33) No full-duplex audio device detected

But I know that my laptop is able to use the microphone and speakers simultaneously so I'm thinking maybe the error gets thrown because of a missing configuration?

I'm running the code on Linux with the ALSA audio drivers. I'm using external speakers through the AUX audio port, and the default laptop microphone.

• I don't know if Matlab is capable of doing that. What I have done in the past is to use Matlab to play the sound, while another process (sox in my case) does the recording. – MBaz Feb 14 '19 at 17:20
• May be simulink could do that (simultaneous playback and recording)? have you checked its documentation as well ? – Fat32 Feb 15 '19 at 23:17
• @MBaz , How 'real-time' is this method? Since I assume you have a delay between the playing the audio using matlab and starting the recording. – Mistergrave Feb 17 '19 at 13:18
• @Fat32, I'll check this out. Thanks! – Mistergrave Feb 17 '19 at 13:18
• @Mistergrave It is real time in the sense that both streams are playing at the specified sampling rate. There is of course a delay, but that is unrelated to the system being 'real time". See this paper for more details: ieeexplore.ieee.org/document/5739249 – MBaz Feb 17 '19 at 17:13

I suspect that your problem is to do with having your speakers and microphone on different ALSA audio devices or subdevices.

You need to find the available sound cards for recording and playback on your system. The following commands will tell you this information :

aplay -l
arecord -l


You will need to define a default full duplex sound card using your choice of devices you see in the output to the above commands.

The way to define this full duplex card (made of two different soundcard devices) is to define it in your ~/.asoundrc file. This example shows you how to do that, however I will repeat it here.

The requirement is that you use plughw as your device name because the clocks on the two sound cards will drift and you will need resampling of some form. But you can play with this option.

So for example if you want to take input from a device on the first card (hw:0) and output to the second device on the second card (hw:1,1) then you would write the following in your ~/.asoundrc file :

pcm.!default {
type asym
playback.pcm "plughw:1,1"
capture.pcm  "plughw:0"
}
ctl.!default {
type hw
card 1
}


Hopefully now Matlab sees a full duplex card as the default device and allows full duplex operation.

New contributor
Matt is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.