我使用dma(LAN-TCP)将音频从micro发送到PC:
while (1) {
U32 max;
int r,i;
main_TcpNet ();
if(tcpSend & sendBuffer)
{
if(selectBuffer)
{
send_datalog(ADC_RegularConvertedValueTab2,sizeof(ADC_RegularConvertedValueTab2));
sendBuffer = 0;
}
else
{
send_datalog(ADC_RegularConvertedValueTab,sizeof(ADC_RegularConvertedValueTab));
sendBuffer = 0;
}
main_TcpNet ();
}
}
}
我需要实时播放.这是我到目前为止使用NAudio所做的事情:
byte[] recBuff = new byte[1400];
public void OnDataReceived(IAsyncResult asyn)
{
try
{
SocketPacket theSockId = (SocketPacket)asyn.AsyncState;
int iRx = theSockId.thisSocket.EndReceive(asyn);
recBuff [count]= theSockId.dataBuffer[0];
count++;
if (count >= 1400)
{
//--------------------------------------------------------------------
for (int i = 0; i < 1400; i += 2)
recieveSound[i / 2] = recBuff[i] + (recBuff[i + 1] * 256); //turn back to 16bit
//--------------------------------------------------------------------
foreach(int data in recieveSound)
sound.Add(data);
//----------------------------------
if (playStauts)
{
if (firstplay)
{
IWaveProvider provider = new RawSourceWaveStream(
new MemoryStream(recBuff), new WaveFormat());
_waveOut.Init(provider);
_waveOut.Play();
//playThread.Start();
//firstplay = false;
}
}
else
{
player.Stop();
}
count = 0; //RESET THE RecBuff
}
//---------------------------------------------------------------
}
catch (ObjectDisposedException)
{
System.Diagnostics.Debugger.Log(0, "1", "\nOnDataReceived: Socket has been closed\n");
}
catch (SocketException se)
{
MessageBox.Show(se.Message);
}
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
if (m_clientSocket != null)
{
m_clientSocket.Close();
m_clientSocket = null;
}
Close();
}
private void frmMain_Load(object sender, EventArgs e)
{
playThread = new Thread(new ThreadStart(play));
player = new SoundPlayer(filePath);
toolStriplbIP.Text = "Your IP: " + GetIP();
btnDisconnect.Enabled = false;
}
#region Palying Sound
private void btnPlay_Click(object sender, EventArgs e)
{
try
{
//Array.Clear(sound, 0, sound.Count);
buffCount = 0;
offsetSound = 0;
sound.Clear();
Object objData = "7";
byte[] byData = System.Text.Encoding.ASCII.GetBytes(objData.ToString());
if (m_clientSocket != null)
m_clientSocket.Send(byData);
playStauts = true;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void btnPause_Click(object sender, EventArgs e)
{
playStauts = false;
}
#endregion
public void play()
{
while(true){
using (SoundPlayer player = new SoundPlayer(filePath))
{
//????????
}
}
}
我只是在第二节听到像一个bijilion Buzzes.但是当我保存然后播放它时,我听到这首歌非常清晰而响亮.
怎么了?如何在字节数增长时播放它?
这对我有用吗?
byte[] bytes = new byte[1400];
IWaveProvider provider = new RawSourceWaveStream(
new MemoryStream(bytes), new WaveFormat());
_waveOut.Init(provider);
_waveOut.Play();
解决方法:
首先,您使用的是默认的WaveFormat,它可能正确也可能不正确.源格式和目标格式之间的分歧肯定会给您带来问题.
一旦你确定WaveFormat是正确的,我建议使用BufferedWaveProvider作为wave播放器的输入而不是MemoryStream,如下所示:
WaveFormat Format = new WaveFormat(/* fill in the right parameters here */);
BufferedWaveProvider Provider = new BufferedWaveProvider(Foramt);
然后,只要您对recBuff感到满意,只需调用Provider.AddSamples将数据放入BufferedWaveProvider,然后由WaveOut播放器拾取.
还有一些其他的陌生感.你一次只收到一个字节吗?看起来这就是你的异步处理程序正在做的事情.这可能不是最好的事情,因为这将导致大量的上下文切换.如果您一次收到多个字节,那么您只抓住第一个字节而忽略其余字节.这无疑会在播放过程中产生“意外”的声音.