Azzera filtri
Azzera filtri

Simultaneously play and record a signal with audioPlayerRecorder

19 visualizzazioni (ultimi 30 giorni)
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

Risposte (2)

Sulaymon Eshkabilov
Sulaymon Eshkabilov il 24 Dic 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)
  1 Commento
Lorenzo Lellini
Lorenzo Lellini il 25 Dic 2022
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…

Accedi per commentare.


Jimmy Lapierre
Jimmy Lapierre il 3 Gen 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);

Categorie

Scopri di più su Audio I/O and Waveform Generation in Help Center e File Exchange

Prodotti


Release

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by