我使用 NAudio 在 C# 中编写了一个方法,该方法成功输出 2 声道声音,但在 6 声道声音中失败。
使用 WaveOut 时,我得到“未处理 MmException:调用 waveOutOpen 的无效参数”
这是我的代码:
public class AudioOutput {
private WaveOut latestAudioOut = null;
private WaveMemoryStream latestMemoryStream = null;
public void PlayAudio (byte[][] buffers, WaveFormat format) {
if (latestAudioOut != null) {
latestAudioOut.Stop();
latestAudioOut.Dispose();
}
latestAudioOut = new WaveOut();
if (latestMemoryStream != null) {
latestMemoryStream.Dispose();
}
int longestChannelLength = 0;
foreach (byte[] b in buffers) {
if (b != null) if (b.Length > longestChannelLength) longestChannelLength = b.Length;
}
byte[][] finalisedbuffers = new byte[buffers.Length][];
for (int i = 0; i < buffers.Length; i ++) {
finalisedbuffers[i] = new byte[longestChannelLength];
if (buffers[i] != null) buffers[i].CopyTo(finalisedbuffers[i], 0);
}
buffers = finalisedbuffers;
byte[] interLeavedBuffer = new byte[longestChannelLength * buffers.Length];
int bytesPerSample = format.BitsPerSample / 8;
int frameLength = buffers.Length * bytesPerSample;
int numberOfFrames = longestChannelLength / bytesPerSample;
int position = 0;
int frameStart = 0;
for (int f = 0; f < numberOfFrames; f ++) {
for (int c = 0; c < buffers.Length; c ++) {
for (int b = 0; b < bytesPerSample; b ++) {
interLeavedBuffer[position] = buffers[c][frameStart + b];
position ++;
}
}
frameStart += bytesPerSample;
}
MemoryStream bufferStream = new MemoryStream(interLeavedBuffer);
latestMemoryStream = new WaveMemoryStream(bufferStream, format);
latestAudioOut.Init(latestMemoryStream);
latestAudioOut.Play();
}