Search code examples
c#audioreal-timenaudio

How to play two audio files simultaneously with real time size value with NAudio


the first I'm working with NAudio, so need your help

Same way as I got here real-time from default audio source, how can I get two audio wav or mp3 files, in this case must be direct inputs I guess, to play it both simultaneously with real-time value. Just same way as it shown below, but at the same time, to get current peaks size from each. What and how I have to use for this goal in my C# Windows Form Application. I was looking for in the sources, but I found nothing, some examples does not works for me, seems need some setups, so to check everything, better get practical and direct example or advice

using NAudio.CoreAudioApi;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace XX_9_REAL_TIME_AUDIO
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            MMDeviceEnumerator enumerator = new MMDeviceEnumerator();
            var devices = enumerator.EnumerateAudioEndPoints(DataFlow.All, DeviceState.Active);
            comboBox1.Items.AddRange(devices.ToArray());
        }

        private void timer_Tick(object sender, EventArgs e)
        {
            if (comboBox1.SelectedItem != null)
            {
                var device = (MMDevice)comboBox1.SelectedItem;
                progressBar1.Value = (int) (Math.Round(device.AudioMeterInformation.MasterPeakValue * 100 + 0.5));


            }
        }
    }
}

Here I got wav file, but have no idea how to get real time value for it and how to play another one at the same time:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NAudio.Wave;

namespace NAudioTest
{
    class Program
    {
        static IWavePlayer waveout;
        static WaveStream outputStream;
        static string filename = null;

        static void Main(string[] args)
        {
            waveout = new WaveOutEvent();

            filename ="C:\\folder\\track.wav";

            outputStream = CreateInputStream(filename);

            try
            {
                waveout.Init(outputStream);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error while loading output");
                Console.WriteLine("Details: " + ex.Message);
                Console.ReadLine();
                return;
            }

            Console.WriteLine("Press [Enter] to start playback");
            Console.ReadLine();

            waveout.Play(); //this stops after 1 sec. should it play until i hit enter cause of the next line?

            Console.WriteLine("Press [Enter] to abort");
            Console.ReadLine();
            waveout.Dispose();
            Console.ReadLine();
        }


        static WaveStream CreateInputStream(string name)
        {
            WaveChannel32 inputStream;
            if (name.EndsWith(".wav"))
            {
                WaveStream readerStream = new WaveFileReader(name);
                if (readerStream.WaveFormat.Encoding != WaveFormatEncoding.Pcm)
                {
                    readerStream = WaveFormatConversionStream.CreatePcmStream(readerStream);
                    readerStream = new BlockAlignReductionStream(readerStream);
                }

                if (readerStream.WaveFormat.BitsPerSample != 16)
                {
                    var format = new WaveFormat(readerStream.WaveFormat.SampleRate, 16, readerStream.WaveFormat.Channels);
                    readerStream = new WaveFormatConversionStream(format, readerStream);
                }
                inputStream = new WaveChannel32(readerStream);
            }
            else
            {
                throw new InvalidOperationException("Invalid extension");
            }
            return inputStream;
        }
    }
}

Solution

  • To play multiple files simultaneously (which is what I think you are asking to do), you should create an AudioFileReader for each of the input files. And then connect each of those as inputs to a MixingSampleProvider. Then initialize your output audio device with the MixingSampleProvider