0
votes

I'm starting to write a Windows Service that will convert G.722 audio files into WAV files and I'm planning on using the NAudio library.

After looking at the NAudio demos, I've found that I will need to use the G722Codec to decode the audio data from the file but I'm having trouble figuring out how to read the G722 file. Which reader should I use?\

The G722 files are 7 kHz.

I'm working my way through the Pluralsight course for NAudio but it would be great to get a small code sample.

1
Are the G.722 files not already in a WAV container?Mark Heath
I tried the WaveFileReader class but got an error. I won't be able to get the exact error message for a couple of days because I haven't got a sample file at home to test.Anthony
could it just be raw G.722? If so, a RawSourceStream would do the trickMark Heath
It's created by Commend ComREC. I'll try the RawSourceStream on Monday & see how it goes. I'll let you know how it goes. Thanks for your helpAnthony

1 Answers

2
votes

I got it working with the RawSourceWaveStreambut then tried to simply read the bytes of the file, decode using the G722 Codec and write the bytes out to a wave file. It worked.

    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;
    }