listener sound in partcular sound card

May 23, 2011 at 2:05 PM

Hello,

I am a beginner in c #. I have a code that is speaking, the sound output of the computer's sound card. But this code does not allow me to out the sound in a specific external sound card.I have used devicenumber but the problem is not solved yet.

Has ANYONE can help me improve my code so that it can hear the sound listen in a particular external sound card.

it is possible to call the device in the parameter function:Streaming Form(int deviceNumber )and how????  

Thank you in advance.

Here is the code:


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NAudio.Wave;
using System.Runtime.InteropServices;

namespace PaGa
{
    public partial class StreamingForm : Form{

      


        public StreamingForm()
             
        {
            InitializeComponent();
            
            
        }
         
         

         private const string mmdll = "winmm.dll";
         [DllImport(mmdll)]
         public static extern int waveOutGetNumDevs();

        /// <summary>
		/// Clean up any resources being used.
		/// </summary>
		

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		//[STAThread]
		

		private PaGa.WaveOutPlayer m_Player;
		private PaGa.WaveInRecorder m_Recorder;
		private PaGa.FifoStream m_Fifo = new PaGa.FifoStream();

		private byte[] m_PlayBuffer;
		private byte[] m_RecBuffer;

		private void Filler(IntPtr data, int size)
		{
			if (m_PlayBuffer == null || m_PlayBuffer.Length < size)
				m_PlayBuffer = new byte[size];
			if (m_Fifo.Length >= size)
				m_Fifo.Read(m_PlayBuffer, 0, size);
			else
				for (int i = 0; i < m_PlayBuffer.Length; i++)
					m_PlayBuffer[i] = 0;
			System.Runtime.InteropServices.Marshal.Copy(m_PlayBuffer, 0, data, size);
		}

		private void DataArrived(IntPtr data, int size)
		{
			if (m_RecBuffer == null || m_RecBuffer.Length < size)
				m_RecBuffer = new byte[size];
			System.Runtime.InteropServices.Marshal.Copy(data, m_RecBuffer, 0, size);
			m_Fifo.Write(m_RecBuffer, 0, m_RecBuffer.Length);
		}

		private void Stop()
		{
			if (m_Player != null)
				try
				{
					m_Player.Dispose();
				}
				finally
				{
					m_Player = null;
				}
			if (m_Recorder != null)
				try
				{
					m_Recorder.Dispose();
				}
				finally
				{
					m_Recorder = null;
				}
			m_Fifo.Flush(); // clear all pending data
		}

        private void Start()
		{
			//Stop();
			//try
			//{
				PaGa.WaveFormatt fmt = new PaGa.WaveFormatt(44100, 16, 2);
				m_Player = new PaGa.WaveOutPlayer(-1, fmt, 16384, 3, new PaGa.BufferFillEventHandler(Filler));
				m_Recorder = new PaGa.WaveInRecorder(0, fmt, 16384, 3, new PaGa.BufferDoneEventHandler(DataArrived));
               // wave = new WaveOut();
                
           // stop previous sounds before starting
           
            
			//}
			//catch
			//{
				//Stop();
				//throw;
			//}
		}

		private void StreamingForm_Closing(object sender, System.ComponentModel.CancelEventArgs e)
		{
			Stop();
		}

		private void StartButton_Click(object sender, System.EventArgs e)
		{
			Start();
		}

		private void StopButton_Click(object sender, System.EventArgs e)
		{
			Stop();
		}
       

        


	}
}

    

Hello,

I am a beginner in c #. I have a code that is speaking, the sound output of the computer's sound card. But this code does not allow me to out the sound in a specific external sound card.I have used devicenumber but the problem is not solved yet.

Has ANYONE can help me improve my code so that it can hear the sound listen in a particular external sound card.


Thank you in advance.

Here is the code:

Coordinator
May 23, 2011 at 2:29 PM

You do not appear to be using NAudio. If I were to guess, it would be to change the first parameter of the WaveOutPlayer constructor to be the output device number you wish to play through.

Mark

May 23, 2011 at 3:05 PM

 

But I want it to be in  the parameter of "streamingForm"  function  for
can call this function to the outside. Do you can help me do that markheath please.

Coordinator
May 23, 2011 at 3:08 PM

Just save it in a class member

private int deviceNumber;

public StreamingForm(int deviceNumber)
{
    this.deviceNumber = deviceNumber;
    InitializeComponent();
}

then you can use it in the constructor for WaveOutPlayer further on

Mark

        public StreamingForm()
             
        {
            InitializeComponent();
            
            
        }
private int deviceNumber;
public StreamingForm(int deviceNumber)             
{
    this.deviceNumber = deviceNumber;
    InitializeComponent();
}
May 23, 2011 at 3:22 PM

 

thank you for your reply. I tried with this method but the sound always comes out of the same external sound card if I change the device number.

May 23, 2011 at 3:29 PM

 

I think it's the right approach but it lacks something to make it work