How do you play multiple sound files simultaneously?

10 views (last 30 days)
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

Accepted Answer

Walter Roberson
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
Matthew
Matthew on 29 Feb 2012
Thanks again for the help its appreciated
Walter Roberson
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.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!