Simultaneously play and record a signal with audioPlayerRecorder

조회 수: 16 (최근 30일)
Lorenzo Lellini
Lorenzo Lellini 2022년 12월 24일
편집: Jimmy Lapierre 2025년 8월 21일
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

답변 (2개)

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2022년 12월 24일
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)
  댓글 수: 1
Lorenzo Lellini
Lorenzo Lellini 2022년 12월 25일
Thank you for your answer but I need to simultaneously play from a specific audio device channel and record from another one. I am currently not be able to manage the audioPlayerRecorder tool. I don’t know if there is another way for doing that…

댓글을 달려면 로그인하십시오.


Jimmy Lapierre
Jimmy Lapierre 2023년 1월 3일
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
Jimmy Lapierre
Jimmy Lapierre 2025년 8월 21일
편집: Jimmy Lapierre 2025년 8월 21일
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)

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Audio I/O and Waveform Generation에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by