Gil SH
Gil SH

Reputation: 1

NAudio.net - Trying to send 2 channels from multi channel inpot to a sterio input

Please help. I am a begginer. I am trying to make a tool that will devide a multi channel input to multipul Stereo outputs. To start i am trying to just sent the first two channels of a 6-channel input to a stereo output the sound comes out distorted

Heare is my code - PLEASE HELP!

public partial class Form1 : Form
{
    private WasapiCapture inputCupture;
    WasapiLoopbackCapture waveIn;
    private BufferedWaveProvider waveBuffer;
    private MediaFoundationResampler resampler;
    private ChannelDivider channelDivider;
    private BufferedWaveProvider waveOutBuffer;

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        
        

        MMDevice inputDevice = null;
        MMDevice outputDevice = null;

        MMDevice[] AudioDevices = new MMDeviceEnumerator().EnumerateAudioEndPoints(DataFlow.All, DeviceState.Active).ToArray();
        foreach (MMDevice device in AudioDevices)
        {
            string deviceType = device.DataFlow == DataFlow.Capture ? "INPUT" : "OUTPUT";
            string deviceLabel = $"{deviceType}: {device.FriendlyName}";
            Console.WriteLine(deviceLabel);
            if (device.FriendlyName.Contains("Line 2")) inputDevice = device;
            if (device.FriendlyName.Contains("DELL")) outputDevice = device;
        }
        Console.WriteLine("input: " + inputDevice.FriendlyName);
        Console.WriteLine("output: " + outputDevice.FriendlyName );

  

        inputCupture = inputDevice.DataFlow == DataFlow.Render ? new WasapiLoopbackCapture(inputDevice) : new WasapiCapture(inputDevice, true, 10);
        Console.WriteLine("ch: " +inputCupture.WaveFormat.Channels);
        inputCupture.DataAvailable += OnDataAvailable;
        WaveFormat f = inputCupture.WaveFormat;

        channelDivider = new ChannelDivider(f);


        inputCupture.StartRecording();


        WaveFormat w = new WaveFormat(inputCupture.WaveFormat.SampleRate, inputCupture.WaveFormat.BitsPerSample, 2);
        //w = new WaveFormat(44100, 16, 2);
        Console.WriteLine("format: " + w.SampleRate + "," + w.BitsPerSample);
        waveOutBuffer = new BufferedWaveProvider(w);

        waveOutBuffer.DiscardOnBufferOverflow = true;
        WasapiOut waveOut = new WasapiOut(outputDevice, AudioClientShareMode.Shared,true, 60);
        waveOut.Init(waveOutBuffer);
     
        waveOut.Play();
       

    }

    

    private void OnDataAvailable(object sender, WaveInEventArgs e)
    {
        
        //waveOutBuffer.AddSamples(e.Buffer, 0, e.BytesRecorded);

        //for each channel
        int channel = 1;
        for (int i = 0; i < e.BytesRecorded; i += inputCupture.WaveFormat.BitsPerSample)
        {
            if (channel <= 2)
            {
                waveOutBuffer.AddSamples(e.Buffer, i, inputCupture.WaveFormat.BitsPerSample);
            }

            channel++;
            if (channel > inputCupture.WaveFormat.Channels) channel = 1;
           
        }

    }

    private void Form1_FormClosed(object sender, FormClosedEventArgs e)
    {
        inputCupture.StopRecording();
        inputCupture.Dispose();
    }
}

I tryed to devided it into multible channels by taking each set of bits from a sample (BitsPerSample), thinking that this is how the sample holds the different data for the different channels.

Upvotes: 0

Views: 16

Answers (0)

Related Questions