Decoding FLAC audio files in C#

FlacReader + WavWriter

As a follow-up to my previous article on FLAC and encoding uncompressed audio, I have further developed the Wav2Flac library and added FLAC decoding to the WAVE container. Some key points of this aspect of the WavFlacTest library are:

  • Support for 16- and 24-bit audio streams
  • Support for virtually all FLAC channel mappings: mono, stereo, 5.1, 7.1, etc.
  • The resulting file is a bit-for-bit copy of the original audio stream

All source code and the compiled 32-bit FLAC library can be downloaded from here.

Known limitations

  • Due to the wave format used being 32-bit, uncompressed files greater than 2GB in size might not play in full in some players.
  • Even though FlacReader can receive metadata callbacks, the data is not interpreted. This can be further developed using the FLAC API documentation.

15 Responses to “Decoding FLAC audio files in C#”  

  1. 1 Stefano Corti

    OK The Wav 2 Flac encoder (even if you have to declare the calling convention in DllImport under VS2010 in order to avoid the “StackImbalance” exception)
    The Wav 2 Flac doesn’t work (I use VS2010) - I always get the error in the console “Cannot Open File Stream”
    I don’t know how to fix it!
    Please help!

    Stefano (IT)

  2. 2 Stefano Corti

    I also get the error in the console application CANNOT PROCESS UNTIL EOF

  3. 3 OneBuckFilms

    Thank you for posting your code here.

    It is extremely useful to see this, and I’m using the code for a personal project.

    I might humbly suggest perhaps implementing an event system for reporting status and progress within those callbacks, so that the classes can be separated from the console class, as the code can be used in non-console applications (perhaps a Windows Forms app, which is what I’m developing).

    I may try and make such changes myself, and converting the code to VB.NET for myself.

    Really nice work wrapping the FLAC and WAV APIs.

  4. 4 Ray

    For those running into errors and had to convert the project from VS20XX to VS2010+, make sure that you’re not building for “Any CPU”. Create build configuration for x86, and it should work out for you.

  5. 5 Kris

    Is it possible to have a 16000 wav sample rate and output to a 8000 flac sample rate file without the slowed output?

    Thanks
    Kris

  6. 6 Kris

    Is there a 64 bit version of LibFlac.dll?

    Thanks
    Kris

  7. 7 CJ

    WP7 System doesn’t support DLLImport, so would you be so kind to let me know how can I decode a Flac file?

  8. 8 Stanimir Stoyanov

    Kris, CJ: I believe there’s a .NET implementation of FLAC which should be helpful by not relying on the official DLL. See http://code.google.com/p/sharpflac/downloads/list

  9. 9 CJ

    Thanks for your kindness and immediate response. But the project you suggestted only shows how to read the inner-tag of a flac, which is not enough. In my opinion, I should learn to convert a flac stream/btye[] into a wav/mp3 stream/byte[] so that the WP7 mediaelment could load it and play it. Reading the inner-tag could make a player elegant but the problem is I couldn’t even make a player work. :-( However, I appreciate you very much and I’m sorry for my poor English. :-)

  10. 10 KL

    Is there some new information about the CANNOT PROCESS UNTIL EOF Error? Have the same issue, but only when running without debugging, and can not fix it (I use build configuration for x86). Seems to be a Visual Studio Version problem, will try to install an older version later and test it.

  11. 11 KL

    Ok, changing to visual Studio 2008 did not help. Also tried your sample Code without opening it in Studio on 3 different PCs (XP/7). Additional i tried the process_single method. I get always this error when reading the flac file. It always reads the first block, but then fails at the second block.

  12. 12 Timothée BOSSON

    Hey,

    About the “Cannot process until EOF” error, I think I’ve found the solution.

    I had the same error and my test project target framework was .NET Framework 4 Client Profile. I noticed that Stanimir’s test project target framework was .NET Framework 2.0 so I tried to build my project for older version of .NET Framework. I tried 3.5 and it worked.

    Hope it helped ;)

    /Tim

  13. 13 Levi

    Hello,
    Top job on your project - you have potentially saved me hours however;

    I have got your project work and have imported the required bits into my project and all appeared to be working
    My application takes a stream from microphone passes it through your flac writer then writes it to the Google continuous speech api - i then look to the output stream. Put it all together - should work..
    anyway - it didnt! - i started to debug it - i noticed that if stream any flac file through it converted from a wav file it works fine however if i convert wav to flac using your code it doesnt work.
    i have tested your generated flac file and it appears okay - also showing identically to a working version of the file in spectro
    ive tried the following:
    Audacity - wav to flac - works
    FLAC frontend - wav to flac - works
    vtmblip library (although no streaming option!) - works

    As i said your generated flac file tests okay and plays but it will not pass through my application like the others.
    sample rate = 16k
    bit rate = 16
    channels = 1

    Ive only started learning about this today so forgive me if ive missed something - ive tried playing around with ur settings but cant get it going either (FlacWriter.cs)

    Please could you advice a solution
    Thanks again very much
    Levi

  14. 14 Levi

    Hello Again,
    Ive pulled the flac writer apart and pretty much rewritten it - im still getting the same problem tho.
    I’ve been through the documentation and all appears fine.
    I noticed though that the file sizes were different and on closer inspection the compression ratios are different (40% vs 10%) - the only thing different i can see which could be causing this is the average bitrate? - orignal file size both are the same
    I cant see anywhere in the documentation how to lower the average bitrate from 235 (orignal file had 256 [wav]) to working file: 110Kbps
    Any help would be hugely appreciated
    Thanks
    Levi

  15. 15 master131

    If anyone is pulling their hair out regarding to why this code fails on .NET 4 and above, make sure you apply the Cdecl calling convention to all the imports, for example:

    [DllImport(Dll, CallingConvention = CallingConvention.Cdecl)]
    static extern IntPtr FLAC__stream_decoder_new();

    Afterwards, add the following enum to the FlacReader.cs file:

    enum WriteStatus
    {
    Continue,
    Abort
    }

    Now, change the return type of the Write function and the WriteCallback delegate to WriteStatus. Add the following line to the end of the Write function:

    return WriteStatus.Continue;

    Now it should work. Originally, the return type was void and because of .NET 4’s improved P/Invoke engine, this caused the stack to imbalance and throw this error.

Leave a Reply