使用NAudio将g722音频转换为WAV

时间:2015-01-30 01:25:30

标签: naudio

我开始编写一个Windows服务,将G.722音频文件转换为WAV文件,我打算使用NAudio库。

在看了NAudio演示之后,我发现我需要使用G722Codec来解码文件中的音频数据,但是我无法弄清楚如何读取G722文件。我应该使用哪个读者?\

G722文件为7 kHz。

我正在通过针对NAudio的Pluralsight课程,但是获得一个小代码样本会很棒。

1 个答案:

答案 0 :(得分:2)

我使用了RawSourceWaveStream,但后来尝试简单地读取文件的字节,使用G722 Codec进行解码并将字节写入波形文件。有效。

    private readonly G722CodecState _state = new G722CodecState(64000, G722Flags.SampleRate8000);
    private readonly G722Codec _codec = new G722Codec();
    private readonly WaveFormat _waveFormat = new WaveFormat(8000, 1);

    public MainWindow()
    {
        InitializeComponent();

        var data = File.ReadAllBytes(@"C:\Recordings\000-06Z_chunk00000.g722");
        var output = Decode(data, 0, data.Length);

        using (WaveFileWriter waveFileWriter = new WaveFileWriter(@"C:\Recordings\000-06Z_chunk00000.wav", _waveFormat))
        {
            waveFileWriter.Write(output, 0, output.Length);
        }
    }

    private byte[] Decode(byte[] data, int offset, int length)
    {
        if (offset != 0)
        {
            throw new ArgumentException("G722 does not yet support non-zero offsets");
        }
        int decodedLength = length * 4;
        var outputBuffer = new byte[decodedLength];
        var wb = new WaveBuffer(outputBuffer);
        int decoded = _codec.Decode(_state, wb.ShortBuffer, data, length);
        return outputBuffer;
    }
相关问题