-- Copyright 2013 Evan Laforge
-- This program is distributed under the terms of the GNU General Public
-- License 3.0, see COPYING or http://www.gnu.org/licenses/gpl-3.0.txt

-- | These instances would be better in "Midi.Midi", but that would be
-- a circular import with "Midi.Encode".  And putting them in "Midi.Encode"
-- would make them a bit too hidden.
module Midi.Instances where
import qualified Util.Serialize as Serialize
import qualified Midi.Encode as Encode
import qualified Midi.Midi as Midi


instance Serialize.Serialize Midi.ReadMessage where
    put :: Putter ReadMessage
put (Midi.ReadMessage ReadDevice
a RealTime
b Message
c) =
        Putter ReadDevice
forall a. Serialize a => Putter a
Serialize.put ReadDevice
a PutM () -> PutM () -> PutM ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Putter RealTime
forall a. Serialize a => Putter a
Serialize.put RealTime
b PutM () -> PutM () -> PutM ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Putter Message
forall a. Serialize a => Putter a
Serialize.put Message
c
    get :: Get ReadMessage
get = ReadDevice -> RealTime -> Message -> ReadMessage
Midi.ReadMessage (ReadDevice -> RealTime -> Message -> ReadMessage)
-> Get ReadDevice -> Get (RealTime -> Message -> ReadMessage)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>
        Get ReadDevice
forall a. Serialize a => Get a
Serialize.get Get (RealTime -> Message -> ReadMessage)
-> Get RealTime -> Get (Message -> ReadMessage)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Get RealTime
forall a. Serialize a => Get a
Serialize.get Get (Message -> ReadMessage) -> Get Message -> Get ReadMessage
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Get Message
forall a. Serialize a => Get a
Serialize.get

instance Serialize.Serialize Midi.WriteMessage where
    put :: Putter WriteMessage
put (Midi.WriteMessage WriteDevice
a RealTime
b Message
c) =
        Putter WriteDevice
forall a. Serialize a => Putter a
Serialize.put WriteDevice
a PutM () -> PutM () -> PutM ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Putter RealTime
forall a. Serialize a => Putter a
Serialize.put RealTime
b PutM () -> PutM () -> PutM ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Putter Message
forall a. Serialize a => Putter a
Serialize.put Message
c
    get :: Get WriteMessage
get = WriteDevice -> RealTime -> Message -> WriteMessage
Midi.WriteMessage (WriteDevice -> RealTime -> Message -> WriteMessage)
-> Get WriteDevice -> Get (RealTime -> Message -> WriteMessage)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>
        Get WriteDevice
forall a. Serialize a => Get a
Serialize.get Get (RealTime -> Message -> WriteMessage)
-> Get RealTime -> Get (Message -> WriteMessage)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Get RealTime
forall a. Serialize a => Get a
Serialize.get Get (Message -> WriteMessage) -> Get Message -> Get WriteMessage
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Get Message
forall a. Serialize a => Get a
Serialize.get

instance Serialize.Serialize Midi.Message where
    put :: Putter Message
put = Putter ByteString
forall a. Serialize a => Putter a
Serialize.put Putter ByteString -> (Message -> ByteString) -> Putter Message
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Message -> ByteString
Encode.encode
    get :: Get Message
get = ByteString -> Message
Encode.decode (ByteString -> Message) -> Get ByteString -> Get Message
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get ByteString
forall a. Serialize a => Get a
Serialize.get