简体   繁体   English

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

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

I'm trying to figure out Encoding and Decoding an NAudio input.我正在尝试弄清楚 NAudio 输入的编码和解码。 For testing purposes, I'm doing both in the same scope, which means it doesn't go trough network or file compression etc.出于测试目的,我在同一个 scope 中进行这两种操作,这意味着它不会通过网络或文件压缩等方式进行 go。

Here's how I start recording with NAudio:以下是我开始使用 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();
}

And inside the DataAvailable method I try and make the Encoding and Decoding work but with so far no success.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 error I'm getting is "The frame size must be one of the following: 2.5, 5, 10, 20, 40 or 60."我得到的错误是"The frame size must be one of the following: 2.5, 5, 10, 20, 40 or 60."

If I remove the encoding and decoding part and just send e.Buffer into my WaveProvider, it works fine and the quality is good too.如果我删除编码和解码部分并将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. 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.

There's something I fundamentally don't understand here but I can't figure out what.这里有些东西我基本上不明白,但我不知道是什么。

Edit: I've figured out that I need to set my Buffer size to 20, as that's the Frame Size, or it affects it.编辑:我发现我需要将缓冲区大小设置为 20,因为这是帧大小,否则会影响它。 Now it compiles, but the audio is very choppy/spiky.现在它可以编译了,但音频非常不稳定/尖刺。

I've solved my problem by putting the OpusEncoder and OpusDecoder outside of DataReceived's scope, making them global.我通过将 OpusEncoder 和 OpusDecoder 放在 DataReceived 的 scope 之外解决了我的问题,使它们成为全局的。

Which makes sense, initializing and disposing of both an Encoder and Decoder every time you received data doesn't sound that good.这是有道理的,每次接收数据时初始化和处理编码器和解码器听起来并不那么好。

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

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