csabahruska / proteaaudio

Simple audio library for Windows, Linux, OSX. Supports Mp3, Ogg, Wav playback and multichannel mixing.
28 stars 8 forks source link

MP3 support #12

Closed nomeata closed 1 year ago

nomeata commented 1 year ago

I’m considering using proteaaudio in one of my projects where I currently shell out to sox. One feature I’ll probably miss is support for MP3 in addition to WAV and OGG. Is that by any chance on your road map and I can just wait for that, or would I have to work around it anyways?

csabahruska commented 1 year ago

IMO I could add MP3 support. This lib looks promising: https://github.com/lieff/minimp3 I prefer single file solutions without external dependencies.

nomeata commented 1 year ago

That would be great!

csabahruska commented 1 year ago

I've added MP3 support. Please check it and if it works for you then I'll make a new release on Hackage. https://github.com/csabahruska/proteaaudio/commit/aa482067333b01361cc370571c900b76e2267fbb

nomeata commented 1 year ago

Works flawlessly!

In my application I need to handle different formats without knowing their filename, and will use logic like this

sampleFromMemory :: B.ByteString -> Float -> IO Sample
sampleFromMemory bs
  | B.take 4 bs == B.pack "RIFF" = sampleFromMemoryWav (B.toStrict bs)
  | B.take 4 bs == B.pack "OggS" = sampleFromMemoryOgg (B.toStrict bs)
  | B.take 3 bs == B.pack "ID3" || B.take 2 bs `elem`
      [B.pack "\xFF\xFB", B.pack "\xFF\xF3", B.pack "\xFF\xF2"]
  = sampleFromMemoryMp3 (B.toStrict bs)
  | otherwise = error "Could not detect audio format"
 where magic = B.take 4 bs

If you think that logic would actually be good to have in proteaaudio itself, I’ll of course prefer that :-)

Besides that I think I’m good for now, thanks!

csabahruska commented 1 year ago

Released proteaaudio-0.10.1 with mp3 support and sampleFromMemory.