conduit-audio-lame 0.1.0.1 → 0.1.1
raw patch · 2 files changed
+17/−9 lines, 2 files
Files
conduit-audio-lame.cabal view
@@ -1,5 +1,5 @@ name: conduit-audio-lame-version: 0.1.0.1+version: 0.1.1 author: Michael Tolly maintainer: miketolly@gmail.com license: LGPL
src/Data/Conduit/Audio/LAME.hs view
@@ -14,19 +14,26 @@ import qualified Data.ByteString as B import Foreign import qualified System.IO as IO+import Control.Monad.Trans.Class (lift) --- | Saves an audio stream to an MP3 file.+-- | Saves an audio stream to an MP3 file. Uses the default VBR mode. sinkMP3 :: (MonadResource m) => FilePath -> A.AudioSource m Float -> m ()-sinkMP3 fp (A.AudioSource s r c _) = (C.$$) s+sinkMP3 fp = sinkMP3WithHandle fp $ \lame -> liftIO $ L.check $ L.setVBR lame L.VbrDefault++-- | Lets you perform arbitrary setup on the `lame` handle before encoding.+-- You must select a quality somewhere in the setup action,+-- such as with 'L.setVBR'.+sinkMP3WithHandle :: (MonadResource m) => FilePath -> (L.LAME -> m ()) -> A.AudioSource m Float -> m ()+sinkMP3WithHandle fp setup (A.AudioSource s r c _) = (C.$$) s $ C.bracketP L.init (L.check . L.close) $ \lame -> C.bracketP (IO.openBinaryFile fp IO.WriteMode) IO.hClose $ \fout -> do let o = liftIO . L.check o $ L.setInSamplerate lame $ round r- unless (c `elem ` [1, 2]) $ error $- "sinkMP3: only 1 or 2 channels are supported (" ++ show c ++ " given)"+ unless (c `elem ` [1, 2]) $ err $+ "only 1 or 2 channels are supported (" ++ show c ++ " given)" o $ L.setNumChannels lame c- o $ L.setVBR lame L.VbrDefault+ lift $ setup lame o $ L.initParams lame fix $ \loop -> C.await >>= \mx -> case mx of Nothing -> liftIO $ do@@ -37,7 +44,7 @@ IO.hSeek fout IO.AbsoluteSeek 0 tags <- allocaArray 100000 $ \buf -> do len <- L.getLametagFrame lame (castPtr buf) 100000- when (len < 0) $ error "sinkMP3: couldn't get lame tag frame (buffer too small)"+ when (len < 0) $ err "couldn't get lame tag frame (buffer too small)" B.packCStringLen (buf, fromIntegral len) B.hPutStr fout tags Just v -> do@@ -48,8 +55,8 @@ len <- case c of 1 -> L.encodeBufferIeeeFloat lame (castPtr p) nullPtr nsamples (castPtr buf) mp3bufsize _ -> L.encodeBufferInterleavedIeeeFloat lame (castPtr p) nsamples (castPtr buf) mp3bufsize- when (len < 0) $ error $- "sinkMP3: encode function returned " ++ show len ++ "; " ++ case len of+ when (len < 0) $ err $+ "encode function returned " ++ show len ++ "; " ++ case len of -1 -> "mp3buf was too small" -2 -> "malloc() problem" -3 -> "lame_init_params() not called"@@ -58,3 +65,4 @@ B.packCStringLen (buf, len) B.hPutStr fout bs loop+ where err = error . ("Conduit.Audio.LAME.sinkMP3WithHandle: " ++)