NAudio now playing FLAC files!

Dec 24, 2010 at 3:15 PM
Edited Dec 24, 2010 at 3:16 PM

Hi,

After adding playback support for WMA and OGG, FLAC was next on my plan.

I've just added FLAC playback support (FLACFileReader) that allows NAudio to play FLAC files (currently 16-bit only).

It seems to be working just fine (I also tested reposition).

The code is using the official libFlac C DLL (which has BSD license - so it should be fine for NAudio Library) using a P/Invokes wrapper named LibFlacSharp.

My library (NAudioFlac) has an MIT license.

 

@Mark - if you find this code usable, please feel free to integrate the FLACFileReader to NAudio. Once/If you do this I'll get rid of my library like we did with WMA.

Also, LibFlacSharp has an encoding interface which I did not use - you might want to write a FLAC writer, it should be easy and add a cool loseless encoding feature to NAudio.

 

A binary demo is available at:

http://code.google.com/p/practicesharp/downloads/detail?name=NAudioFLAC.zip

The latest sources are available from Practice# SVN: (under Trunk\NAudioFLAC folder)

Browsing: http://code.google.com/p/practicesharp/source/browse/#svn%2Ftrunk%2FNAudioFLAC

Or to checkout: http://code.google.com/p/practicesharp/source/checkout

 

Thanks

Yuval Naveh

Coordinator
Jan 5, 2011 at 10:48 AM

thanks for doing this Yuval. I might consider incorporating it into the NAudio demo project at some point. I'll also try to get a link to this on the NAudio home page

Mark

Oct 17, 2013 at 3:35 AM
Hello,
I'm trying to make my player playing .flac. However, when the code "fReader = new FLACFileReader(fileName);" is run, I receive the following:

"An unhandled exception of type 'System.ApplicationException' occurred in NAudioFLAC.dll
Additional information: FLAC: Could not Could not process single!
"

Then, I'm unable to play .flac songs.

Could someone help me?
Mar 11 at 6:34 AM
bump

I also have the same problem as N_Neto. Why is this happening?
Coordinator
Mar 12 at 12:23 PM
you may have to ask Yuval Naveh at the practice sharp site, as he wrote this code. You could perhaps try to load your FLAC file into his Practice Sharp app and see if that has the same issue
Mar 13 at 3:53 PM
I am using a couple of different techniques to get Flac support in Naudio. Naudio 1.7 support windows media foundation. So once you install a codec to support flac in media center you can then ask Naudio to play it for you through media foundation.

Here are a couple of link to codecs and howto install:

http://sourceforge.net/projects/mfflac/ <-- Main one I currently use (Support for multiple bit rates)

http://www.howtogeek.com/howto/13843/how-to-play-flac-files-in-windows-7-media-center-player/

Good luck!

BTW I can provide code to playback Flac's through media foundation interface in Naudio if needed.
Mar 15 at 10:35 PM
Some extra info: I'm using NaudioFLAC by Yuval and I'm getting the same exception ("Could not Could not process single!") when running my player from visual studio in debug. When launching the executable outside visual studio it works fine though.

There is however a bad seeking bug. I'm trying to understand: there seems to be some translation needed between the way flac handles seeking compared to Naudio. I might contact Yuval about this.
Mar 17 at 2:05 PM
Hi guys,
Sorry for the late response, I'm pretty busy at work.

I already emailed rogdart and asked for more evidence - I need the flac file and full steps to reproduce it.
NAudioFlac is a wrapper over libFlac (C++ open source project).
The version I'm currently using is 1.2. It could be a bug there as well.
Also Practice# (and the demos in that project) were using NAudio 1.6.
Yesterday I upgrade NAudio to 1.7 and also moved to a more modern .NET runtime (.NET 3.5 Client Profile).

With the flac files I tested, I'm unable to reproduce seeking issues.

Thanks
Yuval
Mar 18 at 3:47 AM
Edited Mar 18 at 2:36 PM
I managed to re-produce the problem.
It is not a private case - the get current position logic was done incorrectly in FLACReader/NAudioFlac.
The root cause is (well me) the weird and inconsistent way libFlac works, in particular the FLAC__stream_decoder_get_decode_position() function that returns the byte position in the DECODED frame (i.e. after decompression) which is useless for time position calculations.
I figured out how to get the current playing sample number in a different way.
My fixed version sets and gets proper time values - still under testing.
Once I verify this fix is good, I will commit to Practice# SVN - To update NAudioFlac and also Practice# App.

Thanks
Yuval
Mar 24 at 1:03 AM
The issue was fixed.
I committed the code to the SVN repository of Practice# in Google Code

Thanks
Yuval
Coordinator
Mar 28 at 12:58 PM
thanks Yuval
Mar 28 at 1:50 PM
Edited Mar 28 at 2:24 PM
Hi Mark,
Unfortunately I was celebrating too soon.
Since I moved to .NET 3.5 and NAudio 1.7, but same libFlac, I'm now experiencing similar problems to what rgodart described.
FLAC plays fines (including re-positioning) inside Visual Studio (Debug or Release) but once run outside of VS it fails - the decoder_process_single function fails with a "BAD FRAME" error. This seems to be due to a failed CRC check on the in-memory frame, and I'm not sure what is the root cause for that.
One suspicion I have is memory alignment - i.e. buffers aligned differently in VS vs outside of VS, causing zeros to be padded before or after the unmanaged flac buffer memory.

I wish we had a fully managed (.NET/C#) flac library.

I'm going to write a small console test app, without NAudio, that tries to reproduce the libFlac issue.

Any ideas or help on this are welcome.

Yuval
Mar 28 at 4:45 PM
UPDATE:
I have this small console libFlac test. Very minimal, no NAudio.
In VS it works, outside fails. Both for .NET 2.0 and .NET 3.5.

For .NET 4.0 and 4.5 it fails in a similar way inside VS..

Even for .NET 2.0 and .NET 3.5 the compile has to be done to x86. It fails on "Bad Image" for x64.
Mar 29 at 4:13 PM
This is almost certainly due to incompatibilities between the CLR's memory allocator and libFlac. The debugging allocator in CLR 2.0 (3.5) just happens to be compatible...

You might try looking at what the FlacBox guys have done...
Mar 29 at 4:17 PM
Thanks. This makes sense.

rgodart and I are working on it together.
He will try to compile the latest version of libFlac (C code) and see if it works.

If not, we will probably find a managed .NET solution.
FlacBox is one option. I'm not too happy to use it as it is not based on the original libFlac logic.
Another is to use jFlac, a Java port if libFlac, and convert it into .NET.
I started testing jFlac and it works great so far (in Java/Eclipse).
Mar 30 at 5:51 PM
Looked at FlacBox.
I don't see any references to SeekTable, SeekPosition or seek in general.
Lack of these features is a big minus, and adding them is not straight forward.

I did manage to quickly add seek functionality to jFlac, which follows the libFlac implementation and seems more mature.
Apr 2 at 1:39 PM
Edited Apr 2 at 1:40 PM
UPDATE: rgodart found a link to a .NET port of jFlac - https://wp7flacplayer.codeplex.com/
Great find!
I checked out the code, it was built for Windows Phone 7.
Converted it to "standard" .NET library and it seems to be working fine. I can see correct meta data and also PCM coming out of the decoder.
I'm going to clean it up then test it with NAudioFlac, ,my NAudio plugin decoder for FLAC.
A seek() method is missing but I think it shouldn't be hard to add. I as mentioned in the previous post I added it to jFlac, to porting it to .NET should be feasible.

Then if all goes well, I'll get rid of the native C libFlac.dll and move to a fully memory managed FLAC solution.
Apr 2 at 3:52 PM
I am looking forward to your new code library yuvalnv. Thanks for all your work on this. Hopefully there will be support for 96k/24bit FLAC files.
Apr 2 at 4:56 PM
That would be awesome.. i'll look forward to this!
Oct 6 at 2:23 PM
Is 64 bit version of LibFlac.dll available ?

Thanks,
Praveen
Nov 25 at 2:13 PM
Edited Nov 26 at 8:30 AM
Hello,

I also searched for a full managed flac decoder and asked to include some of my suggested solutions into NAudio.
There ARE already PURE .NET decoders out there!!!

Please read on here: NAudio Discussion Thread

Kind regards

Freefall


EDIT: I´ve managed a full .NET solution by using the cscore.codeplex.com source.