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

4 次查看(过去 30 天)
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.

回答(2 个)

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

Jimmy Lapierre
Jimmy Lapierre 2021-5-3
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.

类别

Help CenterFile Exchange 中查找有关 Simulation, Tuning, and Visualization 的更多信息

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by