带有NAudio的六声道输出? (或者支持其他.NET库的建议?)

时间:2013-05-21 09:27:41

标签: c# .net output naudio

我在C#中使用NAudio编写了一个成功输出2声道声音但在6声道声音失败的方法。

使用WaveOut时,我得到'MmException未处理:InvalidParameter调用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();
    }

2 个答案:

答案 0 :(得分:1)

我认为您需要使用WaveFormatExtensible代替WaveFormat

答案 1 :(得分:0)

我知道这似乎是一个滑稽的回应,实际上并没有解决你的特定问题,但我需要在几年前在我自己的项目中提供广泛的音频支持。

在评估了我能找到的所有C#兼容库(包括NAudio)后,我选择使用BASS Library from Un4seen Developments这个库非常棒,并且对C#提供了全面而广泛的支持。

它可以处理我曾经抛出的所有内容,并支持丰富的插件环境和VST支持。使用BASS实现您的需求应该非常直接,支持论坛非常好。