我有一个生成32位浮点数(大字节序)的应用程序。如果我将它们写入文件,然后在Audacity中打开它们,则文件将正确播放。 我正在尝试使用NAudio播放流。如果我创建了一个包含24k样本,32位和2通道的WaveFormat,我确实会听到噪音,尽管由于格式错误,流当然不会呈现,如果我创建正确的格式(IeeeFloatWave),那么我听不到任何声音所有。我知道样本可以正确到达,因为我可以将它们保存到磁盘上,但是我无法播放它们。有人看到我在做什么是错误的吗?
已更新并解决-喂食程序几乎没有大到小端的变化
FloatTo16位提供者在播放之前转换格式
private bool _streamActive = false;
private BufferedWaveProvider bufferedWaveProvider = null;
private WaveFloatTo16Provider waveFloatTo16Provider = null;
private WaveOut waveOut = null;
private WaveFormat waveFormat=null;
// ProcessSound is fed incoming byte packets
// (28 bytes header plus 1024 bytes audio)
// by background thread
// Data is converted from little to big endian in background thread
public void ProcessSound(byte[] rxData)
{
// get data length
int datalen = rxData.Length;
// check to activate player
if (_streamActive == true)
{
// add samples to buffer ('28' allows for header information)
bufferedWaveProvider.AddSamples(rxData, 28, datalen - 28);
return;
}
// start it going
waveFormat = WaveFormat.CreateIeeeFloatWaveFormat(24000, 2);
// create buffer to allow samples to be added
bufferedWaveProvider = new BufferedWaveProvider(waveFormat);
// convert from 32 bit float to 16 bit PCM
waveFloatTo16Provider = new WaveFloatTo16Provider(bufferedWaveProvider);
// add samples to buffer
bufferedWaveProvider.AddSamples(rxData, 28, datalen - 28);
// create waveOut player
waveOut = new WaveOut();
waveOut.Init(waveFloatTo16Provider);
waveOut.Volume = 0.25f;
waveOut.Play();
// mark stream is active
_streamActive = true;
}