hosc-0.15: Haskell Open Sound Control

Safe Haskell | Safe
Language | Haskell98

Sound.OSC.Type

Contents

- Time
- Datum
- Message
- Bundle
- Packet
- Address Query
- Pretty printing
- Parser

Description

Alegbraic data types for OSC datum and packets.

Synopsis

- type Time = Double
- immediately :: Time
- type Datum_Type = Char
- type ASCII = ByteString
- ascii :: String -> ASCII
- ascii_to_string :: ASCII -> String
- data MIDI = MIDI Word8 Word8 Word8 Word8
- data Datum
  - = Int32 {
    - d_int32 :: Int32
    }
  - | Int64 {
    - d_int64 :: Int64
    }
  - | Float {
    - d_float :: Float
    }
  - | Double {
    - d_double :: Double
    }
  - | ASCII_String {
    - d_ascii_string :: ASCII
    }
  - | Blob {
    - d_blob :: ByteString
    }
  - | TimeStamp {
    - d_timestamp :: Time
    }
  - | Midi {
    - d_midi :: MIDI
    }
- datum_tag :: Datum -> Datum_Type
- datum_integral :: Integral i => Datum -> Maybe i
- datum_floating :: Floating n => Datum -> Maybe n
- class Datem a where
- int32 :: Integral n => n -> Datum
- int64 :: Integral n => n -> Datum
- float :: Real n => n -> Datum
- double :: Real n => n -> Datum
- string :: String -> Datum
- midi :: (Word8, Word8, Word8, Word8) -> Datum
- type Address_Pattern = String
- data Message = Message {
  - messageAddress :: Address_Pattern
  - messageDatum :: [Datum]
  }
- message :: Address_Pattern -> [Datum] -> Message
- descriptor :: [Datum] -> ASCII
- descriptor_tags :: ASCII -> ASCII
- data Bundle = Bundle {
  - bundleTime :: Time
  - bundleMessages :: [Message]
  }
- bundle :: Time -> [Message] -> Bundle
- data Packet
  - = Packet_Message {
    - packetMessage :: Message
    }
  - | Packet_Bundle {
    - packetBundle :: Bundle
    }
- p_bundle :: Time -> [Message] -> Packet
- p_message :: Address_Pattern -> [Datum] -> Packet
- packetTime :: Packet -> Time
- packetMessages :: Packet -> [Message]
- packet_to_bundle :: Packet -> Bundle
- packet_to_message :: Packet -> Maybe Message
- packet_is_immediate :: Packet -> Bool
- at_packet :: (Message -> a) -> (Bundle -> a) -> Packet -> a
- message_has_address :: Address_Pattern -> Message -> Bool
- bundle_has_address :: Address_Pattern -> Bundle -> Bool
- packet_has_address :: Address_Pattern -> Packet -> Bool
- type FP_Precision = Maybe Int
- floatPP :: RealFloat n => Maybe Int -> n -> String
- timePP :: FP_Precision -> Time -> String
- vecPP :: Show a => [a] -> String
- datumPP :: FP_Precision -> Datum -> String
- messagePP :: FP_Precision -> Message -> String
- bundlePP :: FP_Precision -> Bundle -> String
- packetPP :: FP_Precision -> Packet -> String
- readMaybe :: Read a => String -> Maybe a
- parse_datum :: Datum_Type -> String -> Maybe Datum

# Time

type Time = Double Source #

`NTP` time in real-valued (fractional) form.

immediately :: Time Source #

Constant indicating a bundle to be executed immediately.

# Datum

type Datum_Type = Char Source #

Type enumerating Datum categories.

type ASCII = ByteString Source #

Type for ASCII strings (strict `Char`8 `ByteString`).

ascii :: String -> ASCII Source #

Type-specialised `pack`.

ascii_to_string :: ASCII -> String Source #

Type-specialised `unpack`.

data MIDI Source #

Four-byte midi message.

Constructors

MIDI Word8 Word8 Word8 Word8 |

Instances

Eq MIDI Source # |
Methods (==) :: MIDI -> MIDI -> Bool # (/=) :: MIDI -> MIDI -> Bool #
Read MIDI Source # |
Methods readsPrec :: Int -> ReadS MIDI # readList :: ReadS [MIDI] # readPrec :: ReadPrec MIDI # readListPrec :: ReadPrec [MIDI] #
Show MIDI Source # |
Methods showsPrec :: Int -> MIDI -> ShowS # show :: MIDI -> String # showList :: [MIDI] -> ShowS #
Datem MIDI Source # |
Methods d_put :: MIDI -> Datum Source # d_get :: Datum -> Maybe MIDI Source #

data Datum Source #

The basic elements of OSC messages.

Constructors

Int32 |

Fields

- d_int32 :: Int32

Int64 |

Fields

- d_int64 :: Int64

Float |

Fields

- d_float :: Float

Double |

Fields

- d_double :: Double

ASCII_String |

Fields

- d_ascii_string :: ASCII

Blob |

Fields

- d_blob :: ByteString

TimeStamp |

Fields

- d_timestamp :: Time

Midi |

Fields

- d_midi :: MIDI

Instances

Eq Datum Source # |
Methods (==) :: Datum -> Datum -> Bool # (/=) :: Datum -> Datum -> Bool #
Read Datum Source # |
Methods readsPrec :: Int -> ReadS Datum # readList :: ReadS [Datum] # readPrec :: ReadPrec Datum # readListPrec :: ReadPrec [Datum] #
Show Datum Source # |
Methods showsPrec :: Int -> Datum -> ShowS # show :: Datum -> String # showList :: [Datum] -> ShowS #

datum_tag :: Datum -> Datum_Type Source #

Single character identifier of an OSC datum.

datum_integral :: Integral i => Datum -> Maybe i Source #

`Datum` as `Integral` if `Int32` or `Int64`.

    let d = [Int32 5,Int64 5,Float 5.5,Double 5.5]
    in map datum_integral d == [Just (5::Int),Just 5,Nothing,Nothing]

datum_floating :: Floating n => Datum -> Maybe n Source #

`Datum` as `Floating` if `Int32`, `Int64`, `Float`, `Double` or `TimeStamp`.

    let d = [Int32 5,Int64 5,Float 5,Double 5,TimeStamp 5]
    in Data.Maybe.mapMaybe datum_floating d == replicate 5 (5::Double)

class Datem a where Source #

Class for translating to and from `Datum`. There are instances for the direct `Datum` field types.

    d_put (1::Int32) == Int32 1
    d_put (1::Int64) == Int64 1
    d_put (1::Float) == Float 1
    d_put (1::Double) == Double 1
    d_put (C.pack "str") == ASCII_String (C.pack "str")
    d_put (B.pack [37,37]) == Blob (B.pack [37,37])
    d_put (MIDI 0 0 0 0) == Midi (MIDI 0 0 0 0)

There are also instances for standard Haskell types.

    d_put (1::Int) == Int64 1
    d_put (1::Integer) == Int64 1

Minimal complete definition

d_put, d_get

Methods

d_put :: a -> Datum Source #

d_get :: Datum -> Maybe a Source #

Instances

Datem Double Source # |
Methods d_put :: Double -> Datum Source # d_get :: Datum -> Maybe Double Source #
Datem Float Source # |
Methods d_put :: Float -> Datum Source # d_get :: Datum -> Maybe Float Source #
Datem Int Source # |
Methods d_put :: Int -> Datum Source # d_get :: Datum -> Maybe Int Source #
Datem Int32 Source # |
Methods d_put :: Int32 -> Datum Source # d_get :: Datum -> Maybe Int32 Source #
Datem Int64 Source # |
Methods d_put :: Int64 -> Datum Source # d_get :: Datum -> Maybe Int64 Source #
Datem Integer Source # |
Methods d_put :: Integer -> Datum Source # d_get :: Datum -> Maybe Integer Source #
Datem ByteString Source # |
Methods d_put :: ByteString -> Datum Source # d_get :: Datum -> Maybe ByteString Source #
Datem ByteString Source # |
Methods d_put :: ByteString -> Datum Source # d_get :: Datum -> Maybe ByteString Source #
Datem MIDI Source # |
Methods d_put :: MIDI -> Datum Source # d_get :: Datum -> Maybe MIDI Source #

int32 :: Integral n => n -> Datum Source #

Type generalised `Int32`.

    int32 (1::Int32) == int32 (1::Integer)
    d_int32 (int32 (maxBound::Int32)) == maxBound
    int32 (((2::Int) ^ (64::Int))::Int) == Int32 0

int64 :: Integral n => n -> Datum Source #

Type generalised `Int64`.

    int64 (1::Int32) == int64 (1::Integer)
    d_int64 (int64 (maxBound::Int64)) == maxBound

float :: Real n => n -> Datum Source #

Type generalised `Float`.

    float (1::Int) == float (1::Double)
    floatRange (undefined::Float) == (-125,128)
    isInfinite (d_float (float (encodeFloat 1 256 :: Double))) == True

double :: Real n => n -> Datum Source #

Type generalised `Double`.

    double (1::Int) == double (1::Double)
    double (encodeFloat 1 256 :: Double) == Double 1.157920892373162e77

string :: String -> Datum Source #

`ASCII_String` of `pack`.

    string "string" == ASCII_String (C.pack "string")

midi :: (Word8, Word8, Word8, Word8) -> Datum Source #

Four-tuple variant of `Midi` `.` `MIDI`.

    midi (0,0,0,0) == Midi (MIDI 0 0 0 0)

# Message

type Address_Pattern = String Source #

OSC address pattern. This is strictly an ASCII value, but it is very common to pattern match on addresses and matching on `ByteString` requires `OverloadedStrings`.

data Message Source #

An OSC message.

Constructors

Message |

Fields

- messageAddress :: Address_Pattern
- messageDatum :: [Datum]

Instances

Eq Message Source # |
Methods (==) :: Message -> Message -> Bool # (/=) :: Message -> Message -> Bool #
Read Message Source # |
Methods readsPrec :: Int -> ReadS Message # readList :: ReadS [Message] # readPrec :: ReadPrec Message # readListPrec :: ReadPrec [Message] #
Show Message Source # |
Methods showsPrec :: Int -> Message -> ShowS # show :: Message -> String # showList :: [Message] -> ShowS #
OSC Message Source # |
Methods toPacket :: Message -> Packet Source # fromPacket :: Packet -> Maybe Message Source #

message :: Address_Pattern -> [Datum] -> Message Source #

`Message` constructor. It is an `error` if the `Address_Pattern` doesn't conform to the OSC specification.

descriptor :: [Datum] -> ASCII Source #

Message argument types are given by a descriptor.

    C.unpack (descriptor [Int32 1,Float 1,string "1"]) == ",ifs"

descriptor_tags :: ASCII -> ASCII Source #

Descriptor tags are `comma` prefixed.

# Bundle

data Bundle Source #

An OSC bundle.

Constructors

Bundle |

Fields

- bundleTime :: Time
- bundleMessages :: [Message]

Instances

Eq Bundle Source # |
Methods (==) :: Bundle -> Bundle -> Bool # (/=) :: Bundle -> Bundle -> Bool #
Ord Bundle Source # | OSC `Bundle`s can be ordered (time ascending).
Methods compare :: Bundle -> Bundle -> Ordering # (<) :: Bundle -> Bundle -> Bool # (<=) :: Bundle -> Bundle -> Bool # (>) :: Bundle -> Bundle -> Bool # (>=) :: Bundle -> Bundle -> Bool # max :: Bundle -> Bundle -> Bundle # min :: Bundle -> Bundle -> Bundle #
Read Bundle Source # |
Methods readsPrec :: Int -> ReadS Bundle # readList :: ReadS [Bundle] # readPrec :: ReadPrec Bundle # readListPrec :: ReadPrec [Bundle] #
Show Bundle Source # |
Methods showsPrec :: Int -> Bundle -> ShowS # show :: Bundle -> String # showList :: [Bundle] -> ShowS #
OSC Bundle Source # |
Methods toPacket :: Bundle -> Packet Source # fromPacket :: Packet -> Maybe Bundle Source #

bundle :: Time -> [Message] -> Bundle Source #

`Bundle` constructor. It is an `error` if the `Message` list is empty.

# Packet

data Packet Source #

An OSC `Packet` is either a `Message` or a `Bundle`.

Constructors

Packet_Message |

Fields

- packetMessage :: Message

Packet_Bundle |

Fields

- packetBundle :: Bundle

Instances

Eq Packet Source # |
Methods (==) :: Packet -> Packet -> Bool # (/=) :: Packet -> Packet -> Bool #
Read Packet Source # |
Methods readsPrec :: Int -> ReadS Packet # readList :: ReadS [Packet] # readPrec :: ReadPrec Packet # readListPrec :: ReadPrec [Packet] #
Show Packet Source # |
Methods showsPrec :: Int -> Packet -> ShowS # show :: Packet -> String # showList :: [Packet] -> ShowS #
OSC Packet Source # |
Methods toPacket :: Packet -> Packet Source # fromPacket :: Packet -> Maybe Packet Source #

p_bundle :: Time -> [Message] -> Packet Source #

`Packet_Bundle` `.` `bundle`.

p_message :: Address_Pattern -> [Datum] -> Packet Source #

`Packet_Message` `.` `message`.

packetTime :: Packet -> Time Source #

The `Time` of `Packet`, if the `Packet` is a `Message` this is `immediately`.

packetMessages :: Packet -> [Message] Source #

Retrieve the set of `Message`s from a `Packet`.

packet_to_bundle :: Packet -> Bundle Source #

If `Packet` is a `Message` add `immediately` timestamp, else `id`.

packet_to_message :: Packet -> Maybe Message Source #

If `Packet` is a `Message` or a `Bundle` with an immediate time tag and with one element, return the `Message`, else `Nothing`.

packet_is_immediate :: Packet -> Bool Source #

Is `Packet` immediate, ie. a `Bundle` with timestamp `immediately`, or a plain Message.

at_packet :: (Message -> a) -> (Bundle -> a) -> Packet -> a Source #

Variant of `either` for `Packet`.

# Address Query

message_has_address :: Address_Pattern -> Message -> Bool Source #

Does `Message` have the specified `Address_Pattern`.

bundle_has_address :: Address_Pattern -> Bundle -> Bool Source #

Do any of the `Message`s at `Bundle` have the specified `Address_Pattern`.

packet_has_address :: Address_Pattern -> Packet -> Bool Source #

Does `Packet` have the specified `Address_Pattern`, ie. `message_has_address` or `bundle_has_address`.

# Pretty printing

type FP_Precision = Maybe Int Source #

Perhaps a precision value for floating point numbers.

floatPP :: RealFloat n => Maybe Int -> n -> String Source #

Variant of `showFFloat` that deletes trailing zeros.

    map (floatPP (Just 4)) [1,pi] == ["1.0","3.1416"]

timePP :: FP_Precision -> Time -> String Source #

Pretty printer for `Time`.

    timePP (Just 4) (1/3) == "0.3333"

vecPP :: Show a => [a] -> String Source #

Pretty printer for vectors.

    vecPP [1::Int,2,3] == "<1,2,3>"

datumPP :: FP_Precision -> Datum -> String Source #

Pretty printer for `Datum`.

    let d = [Int32 1,Float 1.2,string "str",midi (0,0x90,0x40,0x60)]
    in map datumPP d ==  ["1","1.2","\"str\"","<0,144,64,96>"]

messagePP :: FP_Precision -> Message -> String Source #

Pretty printer for `Message`.

bundlePP :: FP_Precision -> Bundle -> String Source #

Pretty printer for `Bundle`.

packetPP :: FP_Precision -> Packet -> String Source #

Pretty printer for `Packet`.

# Parser

readMaybe :: Read a => String -> Maybe a Source #

Variant of `read`.

parse_datum :: Datum_Type -> String -> Maybe Datum Source #

Given `Datum_Type` attempt to parse `Datum` at `String`.

    parse_datum 'i' "42" == Just (Int32 42)
    parse_datum 'h' "42" == Just (Int64 42)
    parse_datum 'f' "3.14159" == Just (Float 3.14159)
    parse_datum 'd' "3.14159" == Just (Double 3.14159)
    parse_datum 's' "\"pi\"" == Just (string "pi")
    parse_datum 'b' "[112,105]" == Just (Blob (B.pack [112,105]))
    parse_datum 'm' "(0,144,60,90)" == Just (midi (0,144,60,90))