How to play two audio files simultaneously using dsp.AudioPlayer??

8 views (last 30 days)
I want to play two audio files simultaneously by using dsp.AudioPlayer.
hmfr1 = dsp.AudioFileReader(filename1);
hmfr2 = dsp.AudioFileReader(filename2);
hap = dsp.AudioPlayer('DeviceName','Speakers (SoundMAX Integrated Digital Audio)',...
'SampleRate',hmfr1.SampleRate,'QueueDuration',3);
while ~isDone(hmfr2)
audio1 = step(hmfr1);
step(hap, audio1);
audio2 = step(hmfr2);
step(hap,audio2);
end
But the sound that it plays is totally distorted. Does anyone could help me with that?? Thanks!!!
  1 Comment
Simone Casale
Simone Casale on 5 May 2015
You have to sum audio1 and audio2 and then play the sum.
while ~isDone(hmfr2)
audio1 = step(hmfr1);
audio2 = step(hmfr2);
audioMix= audio1+audio2;
step(hap,audioMix);
end
To avoid possible truncation during playback ensure that the while loop is based on the longer of the two files.

Sign in to comment.

Answers (1)

Basilio Furest
Basilio Furest on 27 Aug 2019
The audio may sounds distorted because there is data drop when the audio files are loaded and played, the file size can also influence data drop. You will have to make some compromises with the device properties. Check out this link https://www.mathworks.com/help/dsp/ref/dsp.audioplayer-system-object.html for more info, specifically in the box labeled "Queue Duration" where the equation and explanation for latency is described. The you can change e.g. buffer size to decrease data loss by increasing latency.
I have a related issue.. I have two instances of matlab that are simultaneously running and both use different channels of the same dsp.AudioPlayer object to play sounds. However, I get an error when a sound is to be played through both channels at the same time, i.e. the audio object is being used by both instances at the same time. Is there a workaround for this? This problem did not use to occur on older interfaces that were no ASIO-based sound cards with which one could use the audioplayer function and adjust the gain of the channel for independent line output.

Community Treasure Hunt

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

Start Hunting!