module Sound.OSC.Datum.Datem where
import qualified Data.ByteString.Lazy as Lazy
import qualified Data.ByteString.Char8 as Char8
import Data.Int
import Data.Maybe
import Data.Word
import Sound.OSC.Datum
import Sound.OSC.Time
class Datem a where
d_put :: a -> Datum
d_get :: Datum -> Maybe a
instance Datem Int32 where
d_put = Int32
d_get d = case d of {Int32 x -> Just x;_ -> Nothing}
instance Datem Int64 where
d_put = Int64
d_get d = case d of {Int64 x -> Just x;_ -> Nothing}
instance Datem Int where
d_put = Int64 . fromIntegral
d_get = datum_integral
instance Datem Integer where
d_put = Int64 . fromIntegral
d_get = datum_integral
instance Datem Float where
d_put = Float
d_get d = case d of {Float x -> Just x;_ -> Nothing}
instance Datem Double where
d_put = Double
d_get d = case d of {Double x -> Just x;_ -> Nothing}
instance Datem Char8.ByteString where
d_put = ASCII_String
d_get d = case d of {ASCII_String x -> Just x;_ -> Nothing}
instance Datem Lazy.ByteString where
d_put = Blob
d_get d = case d of {Blob x -> Just x;_ -> Nothing}
instance Datem MIDI where
d_put = Midi
d_get d = case d of {Midi x -> Just x;_ -> Nothing}
d_get_err :: Datem a => Datum -> a
d_get_err = fromMaybe (error "d_get") . d_get
datum_int32 :: Datum -> Maybe Int32
datum_int32 = d_get
datum_int64 :: Datum -> Maybe Int64
datum_int64 = d_get
datum_float :: Datum -> Maybe Float
datum_float = d_get
datum_double :: Datum -> Maybe Double
datum_double = d_get
datum_ascii :: Datum -> Maybe ASCII
datum_ascii = d_get
datum_string :: Datum -> Maybe String
datum_string = fmap Char8.unpack . datum_ascii
datum_blob :: Datum -> Maybe Lazy.ByteString
datum_blob = d_get
datum_timestamp :: Datum -> Maybe Time
datum_timestamp d = case d of {TimeStamp x -> Just x;_ -> Nothing}
datum_midi :: Datum -> Maybe MIDI
datum_midi = d_get
datum_sequence :: Datum -> Maybe [Word8]
datum_sequence d =
case d of
ASCII_String s -> Just (map (fromIntegral . fromEnum) (Char8.unpack s))
Blob s -> Just (Lazy.unpack s)
Midi (MIDI p q r s) -> Just [p,q,r,s]
_ -> Nothing