簡體   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