NAudio AsioOut + Sockets

烂漫一生 提交于 2019-12-25 06:59:52

问题


I'm newbie in audio programming and in sockets programming. I'm trying to send guitar signal over a network using NAudio AsioOut class and Sockets.

Here's source code of Receiver and Sender.

It seems like the receiver really gets the bytes array from sender, but all what I hear is white noise.

Receiver source code

    using System;
using System.Net;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using System.Threading;
using NAudio.Utils;
using NAudio.Wave;
using NAudio.Wave.SampleProviders;

namespace Reciever
{
    internal class Program
    {
        private static Socket Listener;
        private static Socket Accepter;
        private static AsioOut Output;
        private static BufferedWaveProvider OutputBuffer;
        [STAThread]
        private static void Main(string[] args)
        {
            IPHostEntry ipHostEntry = Dns.GetHostEntry("192.168.1.4");
            IPAddress ipAddr = ipHostEntry.AddressList[2];
            IPEndPoint ipEndPoint = new IPEndPoint(ipAddr, 7777);
            Listener = new Socket(ipAddr.AddressFamily, SocketType.Stream, ProtocolType.Tcp);

            Console.WriteLine("Слушатель готов по адресу:{0} ", ipEndPoint);
            Listener.Bind(ipEndPoint);
            Listener.Listen(10);
            Accepter = Listener.Accept();
            Console.WriteLine("Клиент с адресом {0} подключен", Accepter.RemoteEndPoint);

            Output = new AsioOut();
            Output.ShowControlPanel();
            Console.Read();
            OutputBuffer = new BufferedWaveProvider(WaveFormat.CreateIeeeFloatWaveFormat(44100, 2));

            Output.Init(OutputBuffer);

            Thread playing = new Thread(new ThreadStart(Output.Play));
            Thread listening = new Thread(new ThreadStart(Listening));

            playing.Start();
            listening.Start();
        }

        public static void Listening()
        {
            while (true)
            {
                byte[] buffer = new byte[65538];
                int ReceivedData = Accepter.Receive(buffer);
                OutputBuffer.AddSamples(buffer, 0, ReceivedData);
            }
        }
    }
}

Sender source code

    using System;
using System.Net;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using NAudio.Wave;


namespace Sender
{
    class Program
    {
        private static Socket sck;

        [STAThread]
        static void Main(string[] args)
        {
            AsioOut asioout = new AsioOut();

            IPHostEntry ipHostEntry = Dns.GetHostEntry("192.168.1.2");
            IPAddress ipAddr = ipHostEntry.AddressList[1];
            IPEndPoint ipEndPoint = new IPEndPoint(ipAddr, 7777);
            sck = new Socket(ipEndPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
            BufferedWaveProvider buffer = new BufferedWaveProvider(WaveFormat.CreateIeeeFloatWaveFormat(44100,2));
            sck.Bind(ipEndPoint);
            try
            {
                sck.Connect("192.168.1.4", 7777);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Соединие не установлено");
                Console.Read();
                return;
            }
            Console.WriteLine("Соединение установлено");
            asioout.InitRecordAndPlayback(buffer, 2, 44100);
            asioout.AudioAvailable += new EventHandler<AsioAudioAvailableEventArgs>(asioout_AudioAvailable);
            asioout.Play();
            Console.Read();
        }

        private static void asioout_AudioAvailable(object sender, AsioAudioAvailableEventArgs e)
        {
            var samples = e.GetAsInterleavedSamples();
            byte[] OutputBuffer = new byte[e.SamplesPerBuffer*4];
            byte[] SendingBuffer = new byte[e.SamplesPerBuffer*4];
            Console.WriteLine(e.SamplesPerBuffer*4);
            for (int i = 0; i < e.InputBuffers.Length; i++)
            {
                Marshal.Copy(e.InputBuffers[i], OutputBuffer, 0, e.SamplesPerBuffer*4);
                Buffer.BlockCopy(samples, 0, SendingBuffer, 0, e.SamplesPerBuffer*4);
                Marshal.Copy(OutputBuffer, 0, e.OutputBuffers[i], e.SamplesPerBuffer * 4);

            }

            sck.Send(SendingBuffer);
            e.WrittenToOutputBuffers = true;
        }
    }
}

UPD.1 (07.10.2015): Sender code and Receiver code are updated. Now I can hear the guitar signal from receiver, but it sounds very distorted. What I do wrong?

P.S. I'm using M-Audio Fast Track USB sound card on my desktop PC. The signal goes from that one to my laptop with ASIO4ALL driver over the network.


回答1:


Your receiver assumes that the incoming audio is 16 bit, but with ASIO, it could easily be 24 or 32 bit. In fact, your sending code seems to assume 32 bit. So start by trying to use an IEEE float WaveFormat on the receiving end



来源:https://stackoverflow.com/questions/31229827/naudio-asioout-sockets

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!