-- | Enumeration of SC3 server commands.
module Sound.SC3.Server.Command.Enum where

import Data.List {- base -}
import Data.Maybe {- base -}

import Sound.OSC.Packet {- hosc -}

-- | SC3 server commands are strings.
type SC3_Command = String

-- | Enumerate server command numbers.
sc3_cmd_enumeration :: [(SC3_Command,Int)]
sc3_cmd_enumeration :: [(SC3_Command, Int)]
sc3_cmd_enumeration =
    [(SC3_Command
"/notify",Int
1)
    ,(SC3_Command
"/status",Int
2)
    ,(SC3_Command
"/quit",Int
3)
    ,(SC3_Command
"/cmd",Int
4)
    -- /d = synthdef
    ,(SC3_Command
"/d_recv",Int
5)
    ,(SC3_Command
"/d_load",Int
6)
    ,(SC3_Command
"/d_loadDir",Int
7)
    ,(SC3_Command
"/d_freeAll",Int
8)
    -- /s = synth
    ,(SC3_Command
"/s_new",Int
9)
    -- /n = node
    ,(SC3_Command
"/n_trace",Int
10)
    ,(SC3_Command
"/n_free",Int
11)
    ,(SC3_Command
"/n_run",Int
12)
    ,(SC3_Command
"/n_cmd",Int
13)
    ,(SC3_Command
"/n_map",Int
14)
    ,(SC3_Command
"/n_set",Int
15)
    ,(SC3_Command
"/n_setn",Int
16)
    ,(SC3_Command
"/n_fill",Int
17)
    ,(SC3_Command
"/n_before",Int
18)
    ,(SC3_Command
"/n_after",Int
19)
    -- /u = ugen
    ,(SC3_Command
"/u_cmd",Int
20)
    -- /g = group
    ,(SC3_Command
"/g_new",Int
21)
    ,(SC3_Command
"/g_head",Int
22)
    ,(SC3_Command
"/g_tail",Int
23)
    ,(SC3_Command
"/g_freeAll",Int
24)
    -- /c = control
    ,(SC3_Command
"/c_set",Int
25)
    ,(SC3_Command
"/c_setn",Int
26)
    ,(SC3_Command
"/c_fill",Int
27)
    -- /b = buffer
    ,(SC3_Command
"/b_alloc",Int
28)
    ,(SC3_Command
"/b_allocRead",Int
29)
    ,(SC3_Command
"/b_read",Int
30)
    ,(SC3_Command
"/b_write",Int
31)
    ,(SC3_Command
"/b_free",Int
32)
    ,(SC3_Command
"/b_close",Int
33)
    ,(SC3_Command
"/b_zero",Int
34)
    ,(SC3_Command
"/b_set",Int
35)
    ,(SC3_Command
"/b_setn",Int
36)
    ,(SC3_Command
"/b_fill",Int
37)
    ,(SC3_Command
"/b_gen",Int
38)
    --
    ,(SC3_Command
"/dumpOSC",Int
39)
    -- _get
    ,(SC3_Command
"/c_get",Int
40)
    ,(SC3_Command
"/c_getn",Int
41)
    ,(SC3_Command
"/b_get",Int
42)
    ,(SC3_Command
"/b_getn",Int
43)
    ,(SC3_Command
"/s_get",Int
44)
    ,(SC3_Command
"/s_getn",Int
45)
    -- _query
    ,(SC3_Command
"/n_query",Int
46)
    ,(SC3_Command
"/b_query",Int
47)
    --
    ,(SC3_Command
"/n_mapn",Int
48)
    ,(SC3_Command
"/s_noid",Int
49)
    --
    ,(SC3_Command
"/g_deepFree",Int
50)
    ,(SC3_Command
"/clearSched",Int
51)
    --
    ,(SC3_Command
"/sync",Int
52)
    --
    ,(SC3_Command
"/d_free",Int
53)
    -- _channel
    ,(SC3_Command
"/b_allocReadChannel",Int
54)
    ,(SC3_Command
"/b_readChannel",Int
55)
    -- _tree
    ,(SC3_Command
"/g_dumpTree",Int
56)
    ,(SC3_Command
"/g_queryTree",Int
57)
    -- error
    ,(SC3_Command
"/error",Int
58)
    -- _args
    ,(SC3_Command
"/s_newargs",Int
59)
    --
    ,(SC3_Command
"/n_mapa",Int
60)
    ,(SC3_Command
"/n_mapan",Int
61)
    ,(SC3_Command
"/n_order",Int
62)
    ]

-- | Lookup command number in 'sc3_cmd_enumeration'.
--
-- > map sc3_cmd_number ["/b_alloc","/s_new"] == [Just 28,Just 9]
sc3_cmd_number :: SC3_Command -> Maybe Int
sc3_cmd_number :: SC3_Command -> Maybe Int
sc3_cmd_number = (SC3_Command -> [(SC3_Command, Int)] -> Maybe Int)
-> [(SC3_Command, Int)] -> SC3_Command -> Maybe Int
forall a b c. (a -> b -> c) -> b -> a -> c
flip SC3_Command -> [(SC3_Command, Int)] -> Maybe Int
forall a b. Eq a => a -> [(a, b)] -> Maybe b
lookup [(SC3_Command, Int)]
sc3_cmd_enumeration

-- | 'isJust' of 'sc3_cmd_number'.
known_sc3_cmd :: SC3_Command -> Bool
known_sc3_cmd :: SC3_Command -> Bool
known_sc3_cmd = Maybe Int -> Bool
forall a. Maybe a -> Bool
isJust (Maybe Int -> Bool)
-> (SC3_Command -> Maybe Int) -> SC3_Command -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SC3_Command -> Maybe Int
sc3_cmd_number

-- | List of asynchronous server commands, ie. commands that reply with @/done@.
async_cmds :: [SC3_Command]
async_cmds :: [SC3_Command]
async_cmds =
    [SC3_Command
"/b_alloc"
    ,SC3_Command
"/b_allocRead"
    ,SC3_Command
"/b_allocReadChannel"
    ,SC3_Command
"/b_close"
    ,SC3_Command
"/b_free"
    ,SC3_Command
"/b_read"
    ,SC3_Command
"/b_readChannel"
    ,SC3_Command
"/b_write"
    ,SC3_Command
"/b_zero"
    ,SC3_Command
"/d_load"
    ,SC3_Command
"/d_loadDir"
    ,SC3_Command
"/d_recv"
    ,SC3_Command
"/notify"
    ,SC3_Command
"/quit"
    ,SC3_Command
"/sync"]

-- | 'True' if 'Message' is an asynchronous 'Message'.
--
-- > import Sound.SC3
-- > map isAsync [b_close 0,n_set1 0 "0" 0,status] == [True,False,False]
isAsync :: Message -> Bool
isAsync :: Message -> Bool
isAsync (Message SC3_Command
a [Datum]
_) = SC3_Command
a SC3_Command -> [SC3_Command] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [SC3_Command]
async_cmds

-- | Asynchronous commands are at the left.  This function should
-- preserve the ordering of both branches.
--
-- > partition_async [b_close 0,n_set1 0 "0" 0]
partition_async :: [Message] -> ([Message],[Message])
partition_async :: [Message] -> ([Message], [Message])
partition_async = (Message -> Bool) -> [Message] -> ([Message], [Message])
forall a. (a -> Bool) -> [a] -> ([a], [a])
partition Message -> Bool
isAsync

-- | Types & names for @b_info@ message fields.
b_info_fields :: [(String,String)]
b_info_fields :: [(SC3_Command, SC3_Command)]
b_info_fields =
    [(SC3_Command
"int",SC3_Command
"buffer-id")
    ,(SC3_Command
"int",SC3_Command
"frame-count")
    ,(SC3_Command
"int",SC3_Command
"channels-count")
    ,(SC3_Command
"float",SC3_Command
"sample-rate")]

-- | Type, name and value descriptors for fields of @n_info@ message.
n_info_fields :: [(String,String,String)]
n_info_fields :: [(SC3_Command, SC3_Command, SC3_Command)]
n_info_fields =
    [(SC3_Command
"int",SC3_Command
"node-id",SC3_Command
"")
    ,(SC3_Command
"int",SC3_Command
"parent group-id",SC3_Command
"-1 = nil")
    ,(SC3_Command
"int",SC3_Command
"previous node-id",SC3_Command
"-1 = nil")
    ,(SC3_Command
"int",SC3_Command
"next node-id",SC3_Command
"-1 = nil")
    ,(SC3_Command
"int",SC3_Command
"node-type",SC3_Command
"0 = synth,1 = group")
    ,(SC3_Command
"int",SC3_Command
"head node-id",SC3_Command
"-1 = nil")
    ,(SC3_Command
"int",SC3_Command
"tail node-id",SC3_Command
"-1 = nil")]