Issue with trying to play and record audio simultaneously

21 views (last 30 days)
Hi guys, For my project I need to play a wav file from my speaker and while it's playing I want to record it with a microphone simultaneously. I'm using the audioPlayerRecorder which I believe is what I need to use. This is what I've done so far, however I'm not quite sure if its correct.
clear all
clc
filereader=dsp.AudioFileReader('chirp_2.wav'); % this is the wav file saved in my Computer
fs=filereader.SampleRate;
filewriter= audioDeviceWriter(fs);
apr=audioPlayerRecorder('SampleRate',fs);
while ~isDone(filereader)
audioToPlay = filereader();
audioRecorded = apr(audioToPlay);
filewriter(audioToPlay);
end
release(apr);
release(filewriter);
release(filereader);
mic1=getaudiodata(apr);
mic2=getaudiodata(filewriter);
When I run this, it runs fine so I'm assuming that both speaker and microphone are working synchronously?
My issue is, I want to plot both the audio from speaker and the recorded audio. I'm trying to convert the object to a numerical array through the getaudiodata function however having I get an error saying "Unrecognized function or variable getaudiodata()".
Does anyone know how to plot both the speaker and microphone audio?

Accepted Answer

Walter Roberson
Walter Roberson on 24 Jul 2020
getaudiodata() is only for audiorecorder() https://www.mathworks.com/help/matlab/ref/audiorecorder.html which is a routine that records audio and holds on to it until it is asked for.
You are using the system-object based routines such as audioPlayerRecorder. Those do not hold on to all the data: they only hold on to one buffer-worth. The audio data can be obtained by using step() -- or, exactly the way you did, by invoking the variable storing the object as-if it were a function. One buffer at a time.
  8 Comments
Victor Dunira
Victor Dunira on 12 Aug 2020
Have you installed ASIO driver, it recognised my laptops microphone and speakers after installation.
Walter Roberson
Walter Roberson on 12 Aug 2020
ASIO does not exist for MacOS. ;-)
And it is true that I do not have a simultaneous input/output audio device. I had been thinking of my speakers and built-in mic as one combine unit, but really they are not at all.

Sign in to comment.

More Answers (0)

Categories

Find more on Audio I/O and Waveform Generation in Help Center and File Exchange

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!