我有一些输出我的数据作为音频流。
我已经上传了sound snipped正弦波,并带有不良噪音效果。
我认为这个问题与数据包之间存在一个小暂停有关,并且会以某种方式产生这种低,相当规则的流行音
AudioMonitor类通过SetBuffer获取其数据,每个250ms aprrox。 ~6000个样本。
编辑 *似乎是数据无法无缝进入的问题。播放一个软件包后,会暂停,waveprovider空闲并发出点击噪音。< / p>
在没有准备好样品的时候,是否可以保持沉默?*
using System;
using NAudio.Wave; // Version 1.7.0.13
namespace aws
{
public class AudioMonitor
{
private BufferedWaveProvider waveProvider;
private WaveOut wavePlayer;
int audioFilter = 0;
int sampleRate = 31250;
int resolution = 8;
private void CreateAudio()
{
if (wavePlayer != null)
{
StopAudio();
wavePlayer.Dispose();
wavePlayer = null;
waveProvider = null;
}
waveProvider = new BufferedWaveProvider(new WaveFormat(sampleRate, resolution, 1));
waveProvider.BufferLength = 31250; // standard value: 31250
wavePlayer = new WaveOut();
wavePlayer.DesiredLatency = 300; // standard value: 300
wavePlayer.NumberOfBuffers = 2; // standard value: 2
wavePlayer.Init(waveProvider);
}
public bool StartAudio()
{
CreateAudio();
if (wavePlayer != null)
{
wavePlayer.Play();
return true;
}
return false;
}
public void StopAudio()
{
if (wavePlayer != null)
{
wavePlayer.Stop();
}
}
public PlaybackState GetPlaybackState()
{
if (wavePlayer != null)
{
return wavePlayer.PlaybackState;
}
return PlaybackState.Stopped;
}
public void SetBuffer(byte[] b)
{
int indexWrite = 0, indexBuffer = 0, sizeBuffer;
byte[] nbuffer = b;
sizeBuffer = b.Length;
// valid data
for (indexWrite = indexBuffer; indexWrite < (indexBuffer + sizeBuffer); ++indexWrite)
{
nbuffer[indexWrite] = (Byte)((nbuffer[indexWrite]) + 128);
if ((nbuffer[indexWrite] < (128 + audioFilter)) && (nbuffer[indexWrite] > (128 - audioFilter)))
nbuffer[indexWrite] = 128;
}
try
{
waveProvider.AddSamples(nbuffer, indexBuffer, sizeBuffer);
}
catch (Exception e){
Console.WriteLine(e.StackTrace);
}
}
public void SetBuffer(double[] d, double fac)
{
byte[] tmpBuf = new byte[d.Length];
for(int i = 0; i < tmpBuf.Length; i++)
{
tmpBuf[i] = (byte)(d[i] / fac);
}
SetBuffer(tmpBuf);
}
public void SetAudioFilter(int f)
{
audioFilter = f*3; // min 0 max 60
}
public void Finalize()
{
if (wavePlayer != null)
{
wavePlayer.Stop();
wavePlayer.Dispose();
}
}
}
}