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

How to play two streams in two different channels?

Nov 11, 2013 at 4:52 AM
Edited Nov 11, 2013 at 6:41 AM
Sorry for my english :)

What i should do, that two different streams start playing in one moment?
Here's the image of main form:
IMAGE
By this code, it starts playing with some delay, after some time when i'm click "play".
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        AsioOut player1, player2;
        Mp3FileReader reader1, reader2;
        WaveChannel32 stream1, stream2; 

        private void Obut_Click(object sender, EventArgs e)
        {
            var ofd = new OpenFileDialog();
            ofd.Filter = "Audio files|*.wav;*.mp3";
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                this.reader1 = new Mp3FileReader(ofd.FileName);
                this.stream1 = new WaveChannel32(reader1);
                VolumeUpdate1();
                DriverUptade1();
                this.player1.Init(this.stream1);
                
            }
        }

        private void Obut2_Click(object sender, EventArgs e)
        {
            var ofd = new OpenFileDialog();
            ofd.Filter = "Audio files|*.wav;*.mp3";
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                this.reader2 = new Mp3FileReader(ofd.FileName);
                this.stream2 = new WaveChannel32(reader2);
                VolumeUpdate2();
                DriverUptade2();
                this.player2.Init(this.stream2);
            }
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            Cleanup1();
            foreach (var device in AsioOut.GetDriverNames())
            {
                this.Dbox.Items.Add(device);
            }
            if (this.Dbox.Items.Count > 0)
            {
                this.Dbox.SelectedIndex = 0;
            }
        }

        private void VolumeUpdate1()
        {
            if (Vol1.Value != 0)
            {
                stream1.Volume = (float)Vol1.Value * 0.01F;
            }
            else
            {
                stream1.Volume = 0.0F;
            }            
        }

        private void VolumeUpdate2()
        {
            if (Vol2.Value != 0)
            {
                stream2.Volume = (float)Vol2.Value * 0.01F;
            }
            else
            {
                stream2.Volume = 0.0F;
            }
        }

        private void Cleanup1()
        {
            if (this.player1 != null)
            {
                this.player1.Dispose();
                this.player1 = null;
            }
            if (this.reader1 != null)
            {
                this.reader1.Dispose();
                this.reader1 = null;
            }
        }

        private void DriverUptade1()
        {
            if (player1 != null)
            {
                player1.Dispose();
                player1 = null;
            }

            if (player1 == null)
            {
                player1 = new AsioOut(Dbox.Text);
                player1.ChannelOffset = Convert.ToInt32(ChOfset1.Text);
            }
        }

        private void DriverUptade2()
        {
            if (player2 != null)
            {
                player2.Dispose();
                player2 = null;
            }

            if (player2 == null)
            {
                player2 = new AsioOut(Dbox.Text);
                player2.ChannelOffset = Convert.ToInt32(ChOfset2.Text);
            }
        }

        private void PBut_Click(object sender, EventArgs e)
        {
            try
            {
                Play();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            Cleanup1();
        }

        private void Play()
        {
            this.reader1.Position = 0;
            this.reader2.Position = 0;
            this.player1.Play();
            this.player2.Play();
        }

        private void SBut_Click(object sender, EventArgs e)
        {
            player1.Stop();
            player2.Stop();
        }

        private void Vol_Scroll(object sender, EventArgs e)
        {
            VolumeUpdate1();
        }

        private void Vol2_Scroll(object sender, EventArgs e)
        {
            VolumeUpdate2();
        }
    }
}
Coordinator
Nov 11, 2013 at 8:57 AM
for perfect synchronization, you should mix the two together (e.g. using MixingSampleProvider) and use a single output device
Nov 11, 2013 at 11:13 AM
Ok, but if i use a single output, how can i select different channels for first track (0,1) and for second (2,3)?
Coordinator
Nov 12, 2013 at 4:48 PM
try the MultiplexingWaveProvider/MultiplexingSampleProvider for multi-channel output. To be honest I am surprised that your soundcard driver lets you open multiple instances of ASIO out for the same device.
Nov 12, 2013 at 7:45 PM
It's my mistake. My card not support it. This causes an exception in this case. Thanks for the advice, MultiplexingWaveProvider looks like exactly what I need.