繁体   English   中英

无法使用 OpusDotNet 对 WaveIn 数据进行编码和解码

[英]Can't Encode and Decode WaveIn data with OpusDotNet

我正在尝试弄清楚 NAudio 输入的编码和解码。 出于测试目的,我在同一个 scope 中进行这两种操作,这意味着它不会通过网络或文件压缩等方式进行 go。

以下是我开始使用 NAudio 录制的方法:

private static WaveIn input;
private static BufferedWaveProvider waveProvider;
private static WaveOut output;

public static void StartRecording(ComboBox inputs, ComboBox outputs) {
    input = new WaveIn {
        DeviceNumber = ((AudioDevice)inputs.SelectedItem).id,
        BufferMilliseconds = 25
    };
    input.WaveFormat = new WaveFormat(48000, 1);
    input.RecordingStopped += RecordingStopped;
    input.DataAvailable += DataAvailable;

    waveProvider = new BufferedWaveProvider(input.WaveFormat);

    output = new WaveOut {
        DeviceNumber = ((AudioDevice)outputs.SelectedItem).id,
        DesiredLatency = 100
    };
    output.Init(waveProvider);
    output.PlaybackStopped += PlaybackStopped;

    input.StartRecording();
    output.Play();
}

DataAvailable方法中,我尝试使编码和解码工作,但到目前为止没有成功。

private static void DataAvailable(object sender, WaveInEventArgs e) {
    using OpusEncoder encoder = new OpusEncoder(OpusDotNet.Application.Audio, 48000, 1);
    byte[] opusBytes = encoder.Encode(e.Buffer, e.BytesRecorded, out int encodedLength);

    using OpusDecoder decoder = new OpusDecoder(48000, 1);
    byte[] output = decoder.Decode(opusBytes, encodedLength, out int decodedLength);

    waveProvider.AddSamples(output, 0, decodedLength);
}

我得到的错误是"The frame size must be one of the following: 2.5, 5, 10, 20, 40 or 60."

如果我删除编码和解码部分并将e.Buffer发送到我的 WaveProvider 中,它工作正常并且质量也很好。

I'm using the OpusDotNet Nuget package ( https://github.com/mrphil2105/OpusDotNet ) and I also tried using the Concentus package ( https://github.com/lostromb/concentus ) but no success on both.

这里有些东西我基本上不明白,但我不知道是什么。

编辑:我发现我需要将缓冲区大小设置为 20,因为这是帧大小,否则会影响它。 现在它可以编译了,但音频非常不稳定/尖刺。

我通过将 OpusEncoder 和 OpusDecoder 放在 DataReceived 的 scope 之外解决了我的问题,使它们成为全局的。

这是有道理的,每次接收数据时初始化和处理编码器和解码器听起来并不那么好。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM