我是音频编程和套接字编程的新手。我正在尝试使用NAudio AsioOut类和套接字通过网络发送吉他信号。
这是接收者和发送者的源代码。
看起来接收器确实从发送器获取字节数组,但我听到的只是白噪声。
接收者源代码
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);
}
}
}
}
发件人源代码
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):发件人代码和接收器代码已更新。现在我可以听到来自接收器的吉他信号,但听起来非常扭曲。我做错了什么?
P.S。我正在台式电脑上使用M-Audio Fast Track USB声卡。通过网络将ASIO4ALL驱动程序从那台信号转移到我的笔记本电脑上。
答案 0 :(得分:1)
您的接收器假定传入的音频是16位,但使用ASIO,它可以很容易地为24或32位。实际上,您的发送代码似乎假定为32位。首先尝试在接收端使用IEEE float WaveFormat