| 1 | <section id="udpserver"> |
| 2 | <title>UDP Server</title> |
| 3 | <para> |
| 4 | Csound includes a fully-functional UDP server, which can accept a |
| 5 | range of commands, including Open Sound Control, and/or |
| 6 | orchestra. |
| 7 | </para> |
| 8 | <bridgehead>Overview</bridgehead> |
| 9 | <para> |
| 10 | The UDP server can be started with the following option: |
| 11 | </para> |
| 12 | <literallayout> |
| 13 | --port=N |
| 14 | </literallayout> |
| 15 | <para> |
| 16 | where N indicates a port number to listen to UDP messages. Csound does not necessarily need to be |
| 17 | given a CSD or orchestra, although it is also possible to do so. |
| 18 | </para> |
| 19 | <bridgehead>Commands</bridgehead> |
| 20 | <para> |
| 21 | Commands take the form of an opcode followed by one or more |
| 22 | arguments. The following commands are accepted by the server: |
| 23 | </para> |
| 24 | <literallayout> |
| 25 | &[line event] |
| 26 | </literallayout> |
| 27 | <para> |
| 28 | Sends in a line event [live event]. Multiple events can be send on multiple |
| 29 | lines. Use this command for single or multiple events that do |
| 30 | not need preprocessing. |
| 31 | </para> |
| 32 | <literallayout> |
| 33 | $[score] |
| 34 | </literallayout> |
| 35 | <para> |
| 36 | Sends in a score [score], to which most preprocessing (except for tempo) can |
| 37 | be applied. Use this option for larger blocks of score events. |
| 38 | </para> |
| 39 | <literallayout> |
| 40 | @[channel_name] [value] |
| 41 | </literallayout> |
| 42 | <para> |
| 43 | Set a control channel [channel_name] with the value [value]. |
| 44 | Example: if the csd contains the line ' chn_k "freq", 440' then send '@freq 330' sets "freq" to 330 |
| 45 | </para> |
| 46 | <literallayout> |
| 47 | %[channel_name] [string] |
| 48 | </literallayout> |
| 49 | <para> |
| 50 | Set a string channel [channel_name] with the string [string]. |
| 51 | </para> |
| 52 | <literallayout> |
| 53 | :@[channel_name] [address] [port] |
| 54 | </literallayout> |
| 55 | <para> |
| 56 | Request the value of the control channel [channel_name] to be |
| 57 | sent as a string via UDP to address [address] port [port]. The |
| 58 | string will contain the channel name followed by two colons (::) |
| 59 | and its current value. |
| 60 | </para> |
| 61 | <literallayout> |
| 62 | :%[channel_name] [address] [port] |
| 63 | </literallayout> |
| 64 | <para> |
| 65 | Request the contents of the string channel [channel_name] to be |
| 66 | sent as a string via UDP to address [address] port [port]. The |
| 67 | string will contain the channel name followed by two colons (::) |
| 68 | and its current contents. |
| 69 | </para> |
| 70 | <literallayout> |
| 71 | /[OSC message] |
| 72 | </literallayout> |
| 73 | <para> |
| 74 | A command starting with a slash "/" is interpreted as an Open Sound |
| 75 | Control (OSC) Message. The data contents of the command are expected |
| 76 | to adhere to the OSC format. See the section below on OSC for more details. |
| 77 | </para> |
| 78 | |
| 79 | <bridgehead>Orchestra code</bridgehead> |
| 80 | |
| 81 | <para> |
| 82 | In addition to the above commands, the UDP server also accepts a |
| 83 | string containing orchestra code, which is compiled immediately. |
| 84 | The orchestra string is not prefixed with any special command |
| 85 | opcode. This string should be sent in a single UDP message. |
| 86 | For example: scoreline_i "i 2 0 1 550 60" |
| 87 | </para> |
| 88 | |
| 89 | <para> |
| 90 | If the orchestra code length exceeds the number of characters that |
| 91 | are possible in a single message, it can be broken in separate |
| 92 | messages. For this to work, the whole orchestra code needs to |
| 93 | enclosed in brackets ({ }). The open brackets ({) starts the server |
| 94 | taking the code in and the close brackets (}) sends the code for |
| 95 | compilation. This way the code can be sent in multiple messages. |
| 96 | </para> |
| 97 | |
| 98 | <bridgehead>Closing the server</bridgehead> |
| 99 | <para> |
| 100 | The server (and Csound) can be closed with one of the following |
| 101 | commands: |
| 102 | </para> |
| 103 | <literallayout> |
| 104 | ##close## |
| 105 | </literallayout> |
| 106 | <para> |
| 107 | or |
| 108 | </para> |
| 109 | <literallayout> |
| 110 | !!close!! |
| 111 | </literallayout> |
| 112 | |
| 113 | <bridgehead>Open Sound Control</bridgehead> |
| 114 | |
| 115 | <para> |
| 116 | The UDP server can work as an OSC server as it can parse OSC messages |
| 117 | sent in from remote clients. Any incoming data block starting with a slash ("/") |
| 118 | is expected to be an OSC message. The server parses messages in two |
| 119 | groups: system and user-defined. |
| 120 | </para> |
| 121 | |
| 122 | <para> System messages: these are defined by addresses starting with |
| 123 | "/csound" and used for global engine commands. The following are the |
| 124 | currently-defined system messages: |
| 125 | </para> |
| 126 | |
| 127 | <literallayout> |
| 128 | /csound/compile "s" [string] |
| 129 | </literallayout> |
| 130 | <para> |
| 131 | Compiles the string passed as the message data. |
| 132 | </para> |
| 133 | |
| 134 | <literallayout> |
| 135 | /csound/event "s" [string] |
| 136 | </literallayout> |
| 137 | <para> |
| 138 | Sends an event defined by the string passed as the message data. |
| 139 | </para> |
| 140 | |
| 141 | <literallayout> |
| 142 | /csound/event/instr "fff..." [float data] |
| 143 | </literallayout> |
| 144 | <para> |
| 145 | Instantiates an instrument with p-fields as defined in the message |
| 146 | type and data. Only float types are accepted. |
| 147 | </para> |
| 148 | |
| 149 | <literallayout> |
| 150 | /csound/channel/a[/b/c...] "..." [float data] |
| 151 | </literallayout> |
| 152 | <para> |
| 153 | Sets software bus channels a[, b, c, ...] according to the message |
| 154 | type and data. Floats and strings are allowed, one datum per channel. |
| 155 | </para> |
| 156 | |
| 157 | <literallayout> |
| 158 | /csound/event/end |
| 159 | /csound/exit |
| 160 | /csound/close |
| 161 | /csound/stop |
| 162 | </literallayout> |
| 163 | <para> |
| 164 | Stops the performance and UDP server |
| 165 | </para> |
| 166 | |
| 167 | <para> User-defined: these are defined by any other addresses chosen |
| 168 | by the user. They are stored in a list and can be retrieved using the |
| 169 | OSClisten opcode. |
| 170 | </para> |
| 171 | |
| 172 | <para> |
| 173 | Code example: |
| 174 | </para> |
| 175 | |
| 176 | <programlisting> |
| 177 | <emphasis role="nt"><CsoundSynthesizer></emphasis> |
| 178 | <emphasis role="nt"><CsOptions></emphasis> |
| 179 | -odac --port=7000 <emphasis role="c1">;;;RT audio out and |
| 180 | server port</emphasis> |
| 181 | <emphasis role="nt"></CsOptions></emphasis> |
| 182 | <emphasis role="nt"><CsInstruments></emphasis> |
| 183 | |
| 184 | 0dbfs = 1 |
| 185 | |
| 186 | instr 1 |
| 187 | freq:k chnget "freq" |
| 188 | amp:k chnget "amp" |
| 189 | out oscili(0dbfs*amp, p4*freq) |
| 190 | status:k, f:k, mess:S, n:k OSClisten "/in", "fsi" |
| 191 | puts mess, status |
| 192 | printk2 n |
| 193 | printk2 f |
| 194 | status, nums:k[] OSClisten "/ina", "fi" |
| 195 | printk2 nums[0] |
| 196 | printk2 nums[1] |
| 197 | endin |
| 198 | |
| 199 | instr 2 |
| 200 | OSCsend 0, "localhost", 7000, "/csound/event/instr", "ffff", 1, 0, 1, 300 |
| 201 | OSCsend 1, "localhost", 7000, "/csound/channel/freq/amp", "ff", p4, p5 |
| 202 | OSCsend 2, "localhost", 7000, "/in", "fsi", p5, "hello", p4 |
| 203 | OSCsend 3, "localhost", 7000, "/ina", "fi", p5, p4 |
| 204 | OSCsend 4, "localhost", 7000, "/csound/event", "s", "i3 4 1" |
| 205 | OSCsend 5, "localhost", 7000, "/csound/compile", "s", "schedule 1,2,2,500" |
| 206 | endin |
| 207 | |
| 208 | instr 3 |
| 209 | OSCsend 0, "localhost", 7000, "/csound/stop" |
| 210 | endin |
| 211 | |
| 212 | <emphasis role="nt"></CsInstruments></emphasis> |
| 213 | <emphasis role="nt"><CsScore></emphasis> |
| 214 | i2 0 2 1 0.2 |
| 215 | <emphasis role="nt"></CsScore></emphasis> |
| 216 | <emphasis role="nt"></CsoundSynthesizer></emphasis> |
| 217 | </programlisting> |
| 218 | </section> |