Skip to content

Example - Loading Audio Buffer #114

Answered by manuelxmarquez
h1cks asked this question in Q&A
Discussion options

You must be logged in to vote

I wish I had seen this before I wrote a parser. For anyone dealing in wave files, here is some quick code to load a PCM wave file.

private static void ParseWaveFile(byte[] data, out WaveFormat format, out byte[] audioData)
{
    var headerRiff = Encoding.ASCII.GetString(data, 0, 4);

    if (headerRiff != "RIFF")
        throw new ArgumentException("RIFF header not found.");

    var fileSize = BitConverter.ToInt32(data, 4);

    if (fileSize != data.Length - 8)
        throw new ArgumentException("Invalid file size.");

    var headerFormat = Encoding.ASCII.GetString(data, 8, 4);

    if (headerFormat != "WAVE")
        throw new ArgumentException("File is not a WAVE file.");

    var su…

Replies: 2 comments 1 reply

Comment options

You must be logged in to vote
1 reply
@h1cks
Comment options

Answer selected by h1cks
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants