Simultaneously play and record a signal with audioPlayerRecorder
10 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(2 个)
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)
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);
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Audio I/O and Waveform Generation 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!