How do you play multiple sound files simultaneously?
10 views (last 30 days)
Show older comments
Matthew
on 9 Feb 2012
Commented: Walter Roberson
on 20 Apr 2023
Hi, Are you able to play multiple sound clips in matlab simultaneously? I have tried using the sound(...) function and wavplay(...) function by calling them one after another in the following manner:
wavplay(soundClip1,Fs) wavplay(soundClip2,Fs) wavplay(soundClip3,Fs)
but this only plays the files in succession, not at the same time. The work-around I have been using is to sum the data in each array into one:
mergedClip = soundClip1+soundClip2+soundClip3; wavplay(mergedClip,Fs)
Does anyone know if there is a way to call a matlab audio function so the separate files play together?
Thanks, Matt
0 Comments
Accepted Answer
Walter Roberson
on 9 Feb 2012
There is no method provided in MATLAB to play multiple sounds simultaneously. There is no method provided in MATLAB to synchronize multiple audio or video events. Or even to synchronize an audio or video event against some trigger or timer. Or to determine a maximum latency for an audio or video event.
The closest that is available is to use a very recent version of a 64 bit MATLAB, a National Instruments (NI) Digital to Analog converter, the new session-based interface, multiple channels synchronized together, and queue the audio, with the analog output feeding in to some kind of analog mixer. (Possibly it could also be done with the legacy interface and non-NI devices; it should work in principle but I have not yet located specific assurances that the channels would be synchronized.)
Other than that... averaging the signals is the method. Not just adding them together, as that would usually saturate the output. mean() is suitable for the task.
2 Comments
Walter Roberson
on 20 Apr 2023
In the years since the above answer was posted, MATLAB added audioplayer, which has a play() method. If you use play() rather than playblocking() then anything else that is being play() for a different audioplayer() object will be played at the same time. However, there will not be any synchronization between the objects.
If the sound were going to different output devices, then these days you could use https://www.mathworks.com/help/audio/ref/audiodevicewriter-system-object.html audioDeviceWriter to keep closer synchronization (but still decidedly imperfect)
For single device if you require synchronization, using mean() is still the way to go.
More Answers (0)
See Also
Categories
Find more on Audio and Video Data in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!