如何使用NAudio将WasapiCapture的原始音频重新采样到g711 mulaw?

时间:2019-07-10 22:19:54

标签: c# naudio

我正在尝试使用NAudio来使客户端模拟软件电话,以通过捕获本地麦克风/扬声器设备来发送g.711 MuLaw格式的电话RTP数据包,但是此过程缺少一些没有做的步骤感觉与过时的信息。 MuLaw与MediaFoundationResampler和WdlResampler不兼容,ACM重采样器使音频质量完全乱码,下面的代码将我带到PCM,但是从那里没有任何信息可以解决。是否应该在这里添加一个低通滤波器?您是否应该根据2013 article(无论如何与MFR不兼容)将WasapiCapture事件中的原始字节数据转换为16位?

我没有使用音频的知识或经验,因此整个过程对我来说是陌生的,因此需要从何处着手进行指导,因为only closest answer并未实际发布他们如何“解决”它

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="video-container"></div>

1 个答案:

答案 0 :(得分:0)

在尝试使用该库并从各种渠道窃取了几个不同的建议之后,我最终得到了一些最终可行的方法,因此希望这可以使其他人省去很多麻烦。

从本质上来说,我不得不滚动自己的IWaveProvider并进行各种自定义过滤器,直到它起作用为止。

public class MuLawResamplerProvider : IWaveProvider
{
    public WaveFormat WaveFormat => WaveFormat.CreateMuLawFormat(8000, 1);

    private BufferedWaveProvider waveBuffer;

    private IWaveProvider ieeeToPcm;

    private byte[] sourceBuffer;

    /// <summary>
    /// Converts from 32-bit Ieee Floating-point format to MuLaw 8khz 8-bit 1 channel.
    /// Used for WasapiCapture and WasapiLoopbackCapture.
    /// </summary>
    /// <param name="audio">The raw audio stream.</param>
    /// <param name="inputFormat">The input format.</param>
    public MuLawResamplerProvider(byte[] stream, WaveFormat inputFormat)
    {
        // Root buffer provider.
        waveBuffer = new BufferedWaveProvider(inputFormat);
        waveBuffer.DiscardOnBufferOverflow = false;
        waveBuffer.ReadFully = false;
        waveBuffer.AddSamples(stream, 0, stream.Length);

        var sampleStream = new WaveToSampleProvider(waveBuffer);

        // Stereo to mono filter.
        var monoStream = new StereoToMonoSampleProvider(sampleStream)
        {
            LeftVolume = 2.0f,
            RightVolume = 2.0f
        };

        // Downsample to 8000 filter.
        var resamplingProvider = new WdlResamplingSampleProvider(monoStream, 8000);

        // Convert to 16-bit in order to use ACM or MuLaw tools.
        ieeeToPcm = new SampleToWaveProvider16(resamplingProvider);

        sourceBuffer = new byte[ieeeToPcm.WaveFormat.AverageBytesPerSecond];
    }

    /// <summary>
    /// Reset the buffer to the starting position with a new stream.
    /// </summary>
    /// <param name="stream">New stream to initialize.</param>
    public void Reset(byte[] stream)
    {
        waveBuffer.ClearBuffer();
        waveBuffer.AddSamples(stream, 0, stream.Length);
    }

    /// <summary>
    /// Converts the 16-bit ACM stream to 8-bit MuLaw on read.
    /// </summary>
    /// <param name="destinationBuffer">The destination buffer to output into.</param>
    /// <param name="offset">The offset to store at.</param>
    /// <param name="readingCount">The requested size to read.</param>
    /// <returns></returns>
    public int Read(byte[] destinationBuffer, int offset, int readingCount)
    {
        // Source buffer has twice as many items as the output array.
        var sizeOfPcmBuffer = readingCount * 2;
        sourceBuffer = BufferHelpers.Ensure(sourceBuffer, sizeOfPcmBuffer);
        var sourceBytesRead = ieeeToPcm.Read(sourceBuffer, 0, sizeOfPcmBuffer);
        var samplesRead = sourceBytesRead / 2;

        var outIndex = 0;
        for (var n = 0; n < sizeOfPcmBuffer; n += 2)
        {
            destinationBuffer[outIndex++] = MuLawEncoder.LinearToMuLawSample(BitConverter.ToInt16(sourceBuffer, offset + n));
        }

        return samplesRead;
    }
}

然后您就可以用它来做简单的事情

        var resampler = new MuLawResamplerProvider(deviceStream.ToArray(), ActiveWasapiCaptureDevice.WaveFormat);

        // Write it out to a local file
        string path = @"C:\Temp\" + Guid.NewGuid().ToString() + ".wav";
        WaveFileWriter.CreateWaveFile(path, resampler);
        resampler.Reset(deviceStream.ToArray());

        // Write it into memory for other uses
        MemoryStream outputStream = new MemoryStream();
        WaveFileWriter.WriteWavFileToStream(outputStream, resampler);