This project has moved and is read-only. For the latest updates, please go here.

How to input more the one audio files to play it simultaneously for real time analysis with NAudio

Aug 26, 2016 at 11:35 PM
Hello, 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, same way as it shown below, but at the same time, to get current peaks value 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 here, but I found nothing, some examples does not works for me, seems need some setups, so to check everything, better get practical and direct 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 analysis 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;
        }
    }
}
Aug 27, 2016 at 10:55 AM
"Console.ReadLine" doesn´t block execution, so you almost instantly dispose the WaveOutEvent instance.

To analyze the samples simply extent "outputStream.ToSampleProvider" and pass it withevents to a "NotifyingSampleProvider". Then Init and play the NotifyingSampleProvider. NotifyingSampleProvider will then raise an event on each sample (pair).
Aug 27, 2016 at 3:41 PM
Hello, thank you for your feedback, as I've said I'm new, and it would be very helpful, if you can show me with given example how to do it following your instructions just for two file inputs real time for example, because I have tried as you said, but seems I'm doing something wrong.