Simultaneously play and record a signal with audioPlayerRecorder
20 views (last 30 days)
Show older comments
I need to play and record a sinesweep signal selecting input and output audio card channels and the folder where it should save the recording.
I am trying to use audioPlayerRecorder I have seen its tutorial page, but it is not clear how to properly set the IN/OUT channels and how to make it works...
Moreover, once I create the sinesweep signal, I save it on the disk and I use dsp.AudioFileReader to open it as in the tutorial page. Is it possible direcltly to play the generated signal without saving on the disk?
Here is the code:
%% SIGNAL GENERATION
Fs = 44100;
sweep = sweeptone(2,1,Fs);
% Save it on disk
fileName = 'SineSweep.wav';
disp('Save file on disk...')
audiowrite(fileName, sweep, Fs);
%% AUDIO CARD CHANNEL ID INPUT & OUTPUT SELECTION
info = audiodevinfo;
% OUTPUT
msg2 = "Output Channel Selection";
opts2 = string(zeros(1, length(info.output)));
for i = 1:length(info.output)
opts2(i) = string(info.output(i).Name);
end
choice2 = menu(msg2,opts2);
disp(opts2(choice2));
for i = 1:length(info.output)
if choice2 == i
ID_device_output = double(info.output(i).ID);
end
end
% INPUT
msg3 = "Iutput Channel Selection";
opts3 = string(zeros(1, length(info.input)));
for i = 1:length(info.input)
opts3(i) = string(info.input(i).Name);
end
choice3 = menu(msg3,opts3);
disp(opts3(choice3));
for i = 1:length(info.input)
if choice3 == i
ID_device_input = double(info.input(i).ID);
end
end
disp (['Output Audio Channel ID: ', num2str(ID_device_output)]);
disp (['Input Audio Channel ID: ', num2str(ID_device_input)]);
clear choice2; clear msg2; clear opts2;
clear choice3; clear msg3; clear opts3;
%% PLAY & REC -> my problems are in this section...
fileReader = dsp.AudioFileReader('SineSweep.wav', ...
'SamplesPerFrame',512);
fs = fileReader.SampleRate;
fileWriter = dsp.AudioFileWriter('SineSweep.wav', ...
'SampleRate',fs);
playRec = audioPlayerRecorder(Fs,'BitDepth','8-bit integer');
playrec.PlayerChannelMapping = [ID_device_output,ID_device_input]; % it does not work...
while ~isDone(fileReader)
audioToPlay = fileReader();
[audioRecorded,nUnderruns,nOverruns] = playRec(audioToPlay);
fileWriter(audioRecorded)
if nUnderruns > 0
fprintf('Audio player queue was underrun by %d samples.\n',nUnderruns);
end
if nOverruns > 0
fprintf('Audio recorder queue was overrun by %d samples.\n',nOverruns);
end
end
0 Comments
Answers (2)
Sulaymon Eshkabilov
on 24 Dec 2022
Yes, you can read or listen your created sound or existing one using sound() fcn, e.g.:
Fs = 44100;
sweep = sweeptone(2,1,Fs);
sound(sweep, Fs)
Jimmy Lapierre
on 3 Jan 2023
To specify IN/OUT channels, use channel mapping. For example, to play on channels 1+2, and record on 4:
audioPlayerRecorder("PlayerChannelMapping",1:2,"RecorderChannelMapping",4)
To avoid using a file for the sweep, consider using dsp.AsyncBuffer.
buf = dsp.AsyncBuffer(size(sweep,1));
write(buf,sweep);
You can then read it bit by bit:
audioToPlay = read(buf,512);
1 Comment
Jimmy Lapierre
on 21 Aug 2025
Edited: Jimmy Lapierre
on 21 Aug 2025
Starting with R2025a, the new audiostreamer object is the easiest way to do measurements like this.
For example, to measure an I.R. with an ASIO device using channel 3 (in and out):
fs = 44100;
as = audiostreamer(Mode="full-duplex",SampleRate=fs,...
Driver="ASIO",PlayerChannels=3,RecorderChannels=3);
sweep = sweeptone(2,1,fs);
rec = playrec(as,sweep);
ir = impzest(sweep,rec);
t = (1:length(ir))/fs;
plot(t,ir)
See Also
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!