summaryrefslogtreecommitdiff
path: root/src/Trivstream/Types.hs
blob: 6dbd9495e326919f3c51ac6065df56d3d088de77 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE GeneralizedNewtypeDeriving, StandaloneDeriving, DeriveGeneric #-}

module Trivstream.Types
  ( AudioConfig(..)
  , AudioBackend(..)
  , SampleRate(..)
  , Configuration(..)
  , Mode (..)
  , module Data.Default.Class
  ) where


import Control.Lens
import Control.Lens.TH
import Data.Default.Class (Default(..))
import Data.Serialize (Serialize)
import GHC.Generics (Generic)

import Foreign.C.Types (CInt(..))

import Sound.Pulse.Simple (ChannelPosition(..), ChannelPan(..))
import Sound.JACK ()

import Network.Socket (Family(..), SocketType(..), ProtocolNumber(..), SockAddr(..))


deriving instance Generic ChannelPan
instance Serialize ChannelPan

deriving instance Generic ChannelPosition
instance Serialize ChannelPosition


data AudioBackend = Pulse | Jack
  deriving (Enum, Eq, Generic, Show, Read)

instance Default AudioBackend where
  def = Pulse

instance Serialize AudioBackend


-- | Numeric instances consider this value to be in `kHz`
newtype SampleRate = SampleRate Int
  deriving (Eq, Ord, Enum, Num, Real, Integral, Generic)
makePrisms ''SampleRate

instance Default SampleRate where
  def = 44100

instance Serialize SampleRate


data AudioConfig = AudioConfig
  { _aBackend  :: AudioBackend
  , _aChannels :: [Maybe ChannelPosition]
  , _aRate     :: Maybe SampleRate
  } deriving (Generic)
makeLenses ''AudioConfig

instance Default AudioConfig where
  def = AudioConfig
    { _aBackend  = def
    , _aChannels = [Just $ ChannelNormal PanLeft, Just $ ChannelNormal PanRight]
    , _aRate     = Just def
    }

instance Serialize AudioConfig


data Mode = Server | Client
  deriving (Enum, Eq, Generic, Show, Read)

instance Default Mode where
  def = Server

instance Serialize Mode


data Configuration = Configuration
  { _cMode       :: Mode
  , _cSocketDesc :: Maybe (Family, SocketType, ProtocolNumber, SockAddr)
  , _cAudio      :: AudioConfig
  } deriving (Generic)
makeLenses ''Configuration

instance Default Configuration where
  def = Configuration
    { _cMode       = def
    , _cSocketDesc = Nothing
    , _cAudio      = def
    }