This project has moved. For the latest updates, please go here.

Mixing two mp3 or wav files

Oct 21, 2011 at 4:50 PM

Hi,

I've two mp3 files let's say 1.mp3 and 2.mp3. Is it possible to do following with NAudio?

1. Take first file i.e. 1.mp3. Play it for 10 seconds.

2. Now play that file i.e. 1.mp3 in background with volume of 10% and parallelly start playing 2.mp3 with full volume until it's not completed.

3. Now again play 1.mp3 for 10 seconds with full volume.

Please help me to achieve this. Can someone provide me example of achieving this. Any help is appreciated.

Thank you.

Viresh

Oct 25, 2011 at 8:08 PM

I am VERY interested in this exact same feature.  Thanks !

George

Coordinator
Oct 26, 2011 at 4:08 PM

There are no code examples doing exactly what you want, but it is perfectly possible to achieve. In NAudio you need to build up a graph of WaveStreams. There will be a mixer stream that will have two inputs in your examples. Then you would create a WaveStream derived class that can delay the start of your MP3, and limit it to 10 seconds (override the Read method to return 0s until you are ready to start, and then read from the real MP3 file stream until the 10 seconds are up).

In the latest source code there is a simple drum machine demo, which isn't exactly what you are after, but it will give a feel for how you can sequence sounds to appear at the right moment.

Mark

Nov 1, 2011 at 5:36 AM
Edited Nov 1, 2011 at 5:37 AM

Hi Mark,

Thanks for your reply.  Following is the code which I'm using but can you please tell me how can I control volume of both mp3 at specific interval? I can able to start 2nd mp3 after 10 seconds but I want to decrease the volume of 1st mp3 and want to increase the volume of 2nd mp3. Also how can I repeat 1st mp3 in background, if it's length is less than 2nd mp3?

            var background = new WaveFileReader("1.wav");
            var message = new WaveFileReader("message.wav");

            var mixer = new WaveMixerStream32();
            mixer.AutoStop = true;

            var messageOffset = background.TotalTime;
            var messageOffsetted = new WaveOffsetStream(
           message, TimeSpan.FromSeconds(10), TimeSpan.Zero, message.TotalTime.Subtract(TimeSpan.FromSeconds(30)));

            var background32 = new WaveChannel32(background);
            background32.PadWithZeroes = false;
            background32.Volume = 0.3f;

            mixer.AddInputStream(background32);

            var message32 = new WaveChannel32(messageOffsetted);
            message32.PadWithZeroes = false;
            message32.Volume = 0.7f;
            mixer.AddInputStream(message32);

            WaveFileWriter.CreateWaveFile("mycomposed.wav", new Wave32To16Stream(mixer));

Regards,

Viresh