Next: , Previous: Read in and write out a soundfile, Up: Top


3 Playing multiple sound files at once

Problem

You want to output multiple sound files to the audio device on your system all at the same time.

Solution

Make a network with SoundFileSources and put all these SoundFileSources inside a Fanout.

Discussion

To do this, you need a network with SoundFileSources for each of the input sources. You then put all these SoundFileSources inside a Fanout, this will play all the sound files simultaneously.

The output of the Fanout is a stacked array of all the data from the audio files, you need to combine these together using a Sum object.

Finally, you output all of them to an AudioSink

The network will look like:

     - Series
       - Fanout
         - SoundFileSource
         - SoundFileSource
         - SoundFileSource
       - Sum
       - AudioSink

The code will look like:

     MarSystemManager mng;
     MarSystem* playbacknet = mng.create("Series", "playbacknet");
     MarSystem* fanout = mng.create("Fanout", "fanout");
     
     // Create the SoundFileSources
     fanout->addMarSystem(mng.create("SoundFileSource", "src1"));
     fanout->addMarSystem(mng.create("SoundFileSource", "src2"));
     fanout->addMarSystem(mng.create("SoundFileSource", "src3"));
     
     // Assign filenames to the SoundFileSources
     fanout->updctrl("SoundFileSource/src1/mrs_string/filename",file1);
     fanout->updctrl("SoundFileSource/src2/mrs_string/filename",file2);
     fanout->updctrl("SoundFileSource/src3/mrs_string/filename",file3);
     
     // Add the fanout to the main network
     playbacknet->addMarSystem(fanout);
     
     // Sum up all of the fanouts
     playbacknet->addMarSystem(mng.create("Sum", "sum"));
     
     // Create the output file which is a SoundFileSink
     playbacknet->addMarSystem(mng.create("AudioSink", "dest"));
     playbacknet->updctrl("AudioSink/dest/mrs_bool/initAudio", true);
     
     while (!done) {
       playbacknet->tick();
     }