MATLAB Answers

How do I play more than one sound file concurrently with audioDeviceWriter?

1 view (last 30 days)
Joel Sieber
Joel Sieber on 15 Apr 2021
Answered: Jimmy Lapierre on 3 May 2021
Hi,
Is it possible to play more than one sound file at the same time using audioDeviceWriter? I am using the following snippet of code to play note recordings when midi keys are pressed:
msgs = midireceive(app.device1);
for i = 1:numel(msgs)
msg = msgs(i);
if isNoteOn(msg) == true && msg.Velocity > 0
% select note file
if msg.Note == 48
app.fileReader = dsp.AudioFileReader('OrganArchive\Processed Audio\Swell\Oboe\Audio\Close\C1\S.wav');
else
app.fileReader = dsp.AudioFileReader('OrganArchive\Processed Audio\Swell\Oboe\Audio\Close\A2\S.wav');
end
% Play sound
while ~isDone(app.fileReader) && msg.Velocity > 0
audioData = app.fileReader();
app.deviceWriter(audioData);
drawnow;
if msg.Velocity == 0 || isNoteOff(msg) == true
break;
end
end
My problem is that when more than one key is pressed, they are played one after the other rather than at the same time. How can I get them to play together?
Thanks in advance and please don't hesitate to ask any questions.

Answers (2)

Ignacio Laude
Ignacio Laude on 15 Apr 2021
Check this out https://www.mathworks.com/help/audio/ref/audioplayerrecorder-system-object.html

Jimmy Lapierre
Jimmy Lapierre on 3 May 2021
If you use audioDeviceWriter (or audioPlayerRecorder), you need to manage each frame yourself, including adding any signals that need to be played at the same time. You might also need to break down your sound into smaller frames for your player to be more responsive.
It might be easier to use sound, which can play multiple sounds at the same time. Or audioplayer, if you maintain multiple copies of the object.

Community Treasure Hunt

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

Start Hunting!