11{-# LANGUAGE FlexibleContexts #-}
2-
2+ -- | This module contains the high-level API for encoding Opus audio.
33module Codec.Audio.Opus.Encoder
44 ( -- * Encoder
55 Encoder , OpusException (.. )
@@ -22,11 +22,12 @@ import qualified Data.ByteString as BS
2222import qualified Data.ByteString.Lazy as BL
2323import Foreign
2424
25- -- | Encoder State
25+ -- | Encoder. Internally, it holds a pointer to the libopus encoder state and
26+ -- a pointer to the (potential) last Opus error code.
2627newtype Encoder = Encoder (ForeignPtr EncoderT , ForeignPtr ErrorCode )
2728 deriving (Eq , Ord , Show )
2829
29- -- | allocates and initializes an encoder state .
30+ -- | Allocates and initializes an encoder.
3031opusEncoderCreate :: (HasEncoderConfig cfg , MonadIO m ) => cfg -> m Encoder
3132opusEncoderCreate cfg = liftIO $ do
3233 let cs = if isStereo then 2 else 1
@@ -39,14 +40,16 @@ opusEncoderCreate cfg = liftIO $ do
3940 let enc = Encoder (e', err)
4041 opusLastError enc >>= maybe (pure enc) throwM
4142
42-
43-
4443-- | Encode an Opus frame.
4544opusEncode
4645 :: (HasStreamConfig cfg , MonadIO m )
47- => Encoder -- ^ 'Encoder' state
48- -> cfg -- ^ max data bytes
49- -> ByteString -- ^ input signal (interleaved if 2 channels)
46+ => Encoder
47+ -- ^ 'Encoder' state
48+ -> cfg
49+ -- ^ The stream configuration that specifies the frame size, the output size,
50+ -- and the encoder configuration (sampling rate, channels, coding mode).
51+ -> ByteString
52+ -- ^ Input signal (interleaved if 2 channels)
5053 -> m ByteString
5154opusEncode e cfg i =
5255 let fs = cfg ^. streamFrameSize
@@ -62,13 +65,22 @@ opusEncode e cfg i =
6265 if l < 0 then throwM OpusInvalidPacket else
6366 BS. packCStringLen ol
6467
68+ -- | Encode an Opus frame. Returns a lazy 'BL.ByteString'.
6569opusEncodeLazy :: (HasStreamConfig cfg , MonadIO m )
66- => Encoder -- ^ 'Encoder' state
70+ => Encoder
71+ -- ^ 'Encoder' state
6772 -> cfg
68- -> ByteString -- ^ input signal (interleaved if 2 channels)
73+ -- ^ The stream configuration that specifies the frame size, the output size,
74+ -- and the encoder configuration (sampling rate, channels, coding mode).
75+ -> ByteString
76+ -- ^ Input signal (interleaved if 2 channels)
6977 -> m BL. ByteString
7078opusEncodeLazy e cfg = fmap BL. fromStrict . opusEncode e cfg
7179
80+
81+ -- | For use with 'ResourceT' or any other monad that implements 'MonadResource'.
82+ -- Safely allocate an 'Encoder' that will be freed upon exiting the monad, an
83+ -- exception, or an explicit call to 'Control.Monad.Trans.Resource.release'.
7284withOpusEncoder :: (HasEncoderConfig cfg ) => MonadResource m
7385 => cfg
7486 -> (Encoder -> IO () )
@@ -77,21 +89,22 @@ withOpusEncoder cfg a =
7789 snd <$> allocate (opusEncoderCreate cfg) a
7890
7991
80- -- | Frees an 'Encoder'. Is normaly called automaticly
81- -- when 'Encoder' gets out of scope
92+ -- | Frees an 'Encoder'.
8293opusEncoderDestroy :: MonadIO m => Encoder -> m ()
8394opusEncoderDestroy (Encoder (e, err)) = liftIO $
8495 finalizeForeignPtr e >> finalizeForeignPtr err
8596
8697
87- -- | get last error from encoder
98+ -- | Get the last error from the encoder.
8899opusLastError :: MonadIO m => Encoder -> m (Maybe OpusException )
89100opusLastError (Encoder (_, fp)) =
90101 liftIO $ (^? _ErrorCodeException) <$> withForeignPtr fp peek
91102
103+ -- | An 'EncoderAction' is an IO action that uses a 'EncoderT' for its operation.
92104type EncoderAction a = Ptr EncoderT -> IO a
93105
94- -- | Run an 'EncoderAction'.
106+ -- | Run an 'EncoderAction' using an 'Encoder', returning either 'OpusException'
107+ -- for errors or the result of the action.
95108withEncoder' :: MonadIO m =>
96109 Encoder -> EncoderAction a -> m (Either OpusException a )
97110withEncoder' e@ (Encoder (fp_a, _)) m = liftIO $
@@ -100,7 +113,7 @@ withEncoder' e@(Encoder (fp_a, _)) m = liftIO $
100113 le <- opusLastError e
101114 pure $ maybe (Right r) Left le
102115
103- -- | Run an 'EncoderAction'. Might throw an 'OpusException'
116+ -- | Run an 'EncoderAction'. Might throw an 'OpusException' if the action fails.
104117runEncoderAction :: (MonadIO m , MonadThrow m ) =>
105118 Encoder -> EncoderAction a -> m a
106119runEncoderAction e m = withEncoder' e m >>= either throwM pure
0 commit comments