| 1 | /* |
| 2 | * File: seqlib.m - Interface to the alsa sequencer library. |
| 3 | * |
| 4 | * Copyright (C) 1999-2003 Steve Ratcliffe |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License version 2 as |
| 8 | * published by the Free Software Foundation. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | */ |
| 15 | |
| 16 | |
| 17 | #include "glib.h"cxx . . $ `<file>/src/glib.h`/ |
| 18 | #include "elements.h"cxx . . $ `<file>/src/elements.h`/ |
| 19 | #include "except.h"cxx . . $ `<file>/src/except.h`/ |
| 20 | #include "intl.h"cxx . . $ `<file>/src/intl.h`/ |
| 21 | |
| 22 | #include <stdio.h> |
| 23 | #include <errno.h> |
| 24 | #include <string.h> |
| 25 | #include "seqlib.h"cxx . . $ `<file>/src/seqlib.h`/ |
| 26 | #include "seqpriv.h"cxx . . $ `<file>/src/seqpriv.h`/ |
| 27 | |
| 28 | static void set_channelcxx . . $ set_channel(5c24c07e69e3137f).(snd_seq_event_tcxx . . $ snd_seq_event_t# *ep, int chan); |
| 29 | |
| 30 | /* |
| 31 | * Create a main client for an application. A queue is |
| 32 | * allocated and a client is created. |
| 33 | */ |
| 34 | seq_context_tcxx . . $ seq_context_t# * |
| 35 | seq_create_contextcxx . . $ seq_create_context(41f2338c700f0527).() |
| 36 | { |
| 37 | seq_context_tcxx . . $ seq_context_t# *ctxp; |
| 38 | int q; |
| 39 | |
| 40 | ctxp = g_newcxx . . $ `src/glib.h:310:11`!(seq_context_t, 1); |
| 41 | ctxp->maincxx . . $ seq_context#main. = ctxp; /* This is the main context */ |
| 42 | |
| 43 | if (snd_seq_opencxx . . $ snd_seq_open(b4c7df1da4c88f65).(&ctxp->handlecxx . . $ seq_context#handle., "hw", SND_SEQ_OPEN_DUPLEXcxx . . $ `<external>/4963296836554771614/seq.h:60:9`!, 0) < 0) |
| 44 | exceptcxx . . $ except(af5967826b8ba103).(ioErrorcxx . . $ ioError., "Could not open sequencer: %s", snd_strerrorcxx . . $ snd_strerror(f7ae7e56280f18ae).(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!)); |
| 45 | |
| 46 | q = snd_seq_alloc_queuecxx . . $ snd_seq_alloc_queue(13c0955b497a8b28).(ctxp->handlecxx . . $ seq_context#handle.); |
| 47 | ctxp->clientcxx . . $ seq_context#client. = snd_seq_client_idcxx . . $ snd_seq_client_id(13c0955b497a8b28).(ctxp->handlecxx . . $ seq_context#handle.); |
| 48 | ctxp->queuecxx . . $ seq_context#queue. = q; |
| 49 | |
| 50 | ctxp->destlistcxx . . $ seq_context#destlist. = g_array_newcxx . . $ g_array_new(fe898421933afc00).(0, 0, sizeof(snd_seq_addr_tcxx . . $ snd_seq_addr_t#)); |
| 51 | |
| 52 | ctxp->queuecxx . . $ seq_context#queue. = q; |
| 53 | ctxp->sourcecxx . . $ seq_context#source..clientcxx . . $ snd_seq_addr#client. = ctxp->clientcxx . . $ seq_context#client.; |
| 54 | ctxp->sourcecxx . . $ seq_context#source..portcxx . . $ snd_seq_addr#port. = 0; |
| 55 | |
| 56 | seq_new_portcxx . . $ seq_new_port(da9fcbd0febe7307).(ctxp); |
| 57 | |
| 58 | return ctxp; |
| 59 | } |
| 60 | |
| 61 | /* |
| 62 | * Create another client context based on the specified |
| 63 | * context. The same queue will be used for both clients. A new |
| 64 | * client will be created. |
| 65 | * Arguments: |
| 66 | * ctxp - |
| 67 | */ |
| 68 | seq_context_tcxx . . $ seq_context_t# * |
| 69 | seq_new_clientcxx . . $ seq_new_client(38c39f32ea8fa0e3).(seq_context_tcxx . . $ seq_context_t# *ctxp) |
| 70 | { |
| 71 | |
| 72 | return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;/*XXX*/ |
| 73 | |
| 74 | } |
| 75 | |
| 76 | /* |
| 77 | * Free a context and any associated data and resources. If the |
| 78 | * main context is freed then the sequencer is closed. TODO: |
| 79 | * link all together so can all be closed at once. |
| 80 | */ |
| 81 | void |
| 82 | seq_free_contextcxx . . $ seq_free_context(e410d57486f0cd63).(seq_context_tcxx . . $ seq_context_t# *ctxp) |
| 83 | { |
| 84 | |
| 85 | if (ctxp->maincxx . . $ seq_context#main. == ctxp) { |
| 86 | /* This is the main context */ |
| 87 | snd_seq_event_tcxx . . $ snd_seq_event_t# ev; |
| 88 | unsigned long t; |
| 89 | |
| 90 | snd_seq_drop_outputcxx . . $ snd_seq_drop_output(13c0955b497a8b28).(ctxp->handlecxx . . $ seq_context#handle.); |
| 91 | |
| 92 | t = 0; |
| 93 | seq_midi_event_initcxx . . $ seq_midi_event_init(1825f7fe94d84599).(ctxp, &ev, t, 0); |
| 94 | seq_midi_controlcxx . . $ seq_midi_control(d8303e21ad8a41d2).(ctxp, &ev, 0, MIDI_CTL_ALL_SOUNDS_OFFcxx . . $ `<external>/10705128940795816901/asoundef.h:337:9`!, 0); |
| 95 | seq_send_to_allcxx . . $ seq_send_to_all(f9711ae91b75e353).(ctxp, &ev); |
| 96 | snd_seq_drain_outputcxx . . $ snd_seq_drain_output(13c0955b497a8b28).(ctxp->handlecxx . . $ seq_context#handle.); |
| 97 | |
| 98 | snd_seq_free_queuecxx . . $ snd_seq_free_queue(cbeb0d688e62acc4).(ctxp->handlecxx . . $ seq_context#handle., ctxp->queuecxx . . $ seq_context#queue.); |
| 99 | snd_seq_closecxx . . $ snd_seq_close(13c0955b497a8b28).(ctxp->handlecxx . . $ seq_context#handle.); |
| 100 | } |
| 101 | |
| 102 | g_freecxx . . $ `src/glib.h:1302:9`!(ctxp); |
| 103 | } |
| 104 | |
| 105 | /* |
| 106 | * Creates a new port on the specified context and returns the |
| 107 | * port number. |
| 108 | * Arguments: |
| 109 | * ctxp - Context to create the port for |
| 110 | */ |
| 111 | int |
| 112 | seq_new_portcxx . . $ seq_new_port(da9fcbd0febe7307).(seq_context_tcxx . . $ seq_context_t# *ctxp) |
| 113 | { |
| 114 | int ret; |
| 115 | |
| 116 | ret = snd_seq_create_simple_portcxx . . $ snd_seq_create_simple_port(cc66b188ddddfbfa).(ctxp->handlecxx . . $ seq_context#handle., NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, |
| 117 | SND_SEQ_PORT_CAP_WRITEcxx . . $ `<external>/4963296836554771614/seq.h:244:9`! | |
| 118 | SND_SEQ_PORT_CAP_SUBS_WRITEcxx . . $ `<external>/4963296836554771614/seq.h:252:9`! | |
| 119 | SND_SEQ_PORT_CAP_READcxx . . $ `<external>/4963296836554771614/seq.h:243:9`!, |
| 120 | SND_SEQ_PORT_TYPE_MIDI_GENERICcxx . . $ `<external>/4963296836554771614/seq.h:267:9`!); |
| 121 | if (ret < 0) { |
| 122 | printfcxx . . $ printf(581fddd72a8db8fc).("Error creating port %s\n", snd_strerrorcxx . . $ snd_strerror(f7ae7e56280f18ae).(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!)); |
| 123 | return -1; |
| 124 | } |
| 125 | ctxp->port_countcxx . . $ seq_context#port_count.++; |
| 126 | ctxp->sourcecxx . . $ seq_context#source..portcxx . . $ snd_seq_addr#port. = ret; |
| 127 | |
| 128 | return ret;/*XXX*/ |
| 129 | } |
| 130 | |
| 131 | void |
| 132 | seq_destroy_portcxx . . $ seq_destroy_port(cbf6e4b539b61ec5).(seq_context_tcxx . . $ seq_context_t# *ctxp, int port) |
| 133 | { |
| 134 | } |
| 135 | |
| 136 | /* |
| 137 | * Set up the context so that all subsequent events will be sent |
| 138 | * to the specified client and port combination. A |
| 139 | * subscription is made to the client/port combination. |
| 140 | * Arguments: |
| 141 | * ctxp - Context to modify |
| 142 | * client - Client to send subsequent events to |
| 143 | * port - Port on the client to send events to |
| 144 | */ |
| 145 | int |
| 146 | seq_connect_addcxx . . $ seq_connect_add(68dd04cef335395).(seq_context_tcxx . . $ seq_context_t# *ctxp, int client, int port) |
| 147 | { |
| 148 | snd_seq_addr_tcxx . . $ snd_seq_addr_t# addr; |
| 149 | int ret; |
| 150 | |
| 151 | memsetcxx . . $ memset(d1de1e4fb215f1c6).(&addr, 0, sizeof(addr)); |
| 152 | addr.clientcxx . . $ snd_seq_addr#client. = client; |
| 153 | addr.portcxx . . $ snd_seq_addr#port. = port; |
| 154 | |
| 155 | g_array_append_valcxx . . $ `src/glib.h:1529:9`!(ctxp->destlist, addr); |
| 156 | |
| 157 | ret = snd_seq_connect_tocxx . . $ snd_seq_connect_to(3da77b32daeaa232).(ctxp->handlecxx . . $ seq_context#handle., ctxp->sourcecxx . . $ seq_context#source..portcxx . . $ snd_seq_addr#port., client, port); |
| 158 | |
| 159 | return ret; |
| 160 | } |
| 161 | |
| 162 | /* |
| 163 | * Set the initial time base and tempo. This should only be used |
| 164 | * for initialisation when there is nothing playing. To |
| 165 | * change the tempo during a song tempo change events are used. |
| 166 | * If realtime is false the resolution is in ticks per quarter |
| 167 | * note. If true, the the resolution is microseconds. There is |
| 168 | * a macro XXX to convert from SMPTE codes. |
| 169 | * |
| 170 | * Arguments: |
| 171 | * ctxp - Application context |
| 172 | * resolution - Ticks per quarter note or realtime resolution |
| 173 | * tempo - Beats per minute |
| 174 | * realtime - True if absolute time base |
| 175 | */ |
| 176 | int |
| 177 | seq_init_tempocxx . . $ seq_init_tempo(601f68a32ffec70f).(seq_context_tcxx . . $ seq_context_t# *ctxp, int resolution, int tempo, int realtime) |
| 178 | { |
| 179 | snd_seq_queue_tempo_tcxx . . $ snd_seq_queue_tempo_t# *qtempo; |
| 180 | int ret; |
| 181 | |
| 182 | snd_seq_queue_tempo_allocacxx . . $ `<external>/4963296836554771614/seq.h:506:9`!(&qtempo); |
| 183 | memsetcxx . . $ memset(d1de1e4fb215f1c6).(qtempo, 0, snd_seq_queue_tempo_sizeofcxx . . $ snd_seq_queue_tempo_sizeof(f708dce9a8856c70).()); |
| 184 | snd_seq_queue_tempo_set_ppqcxx . . $ snd_seq_queue_tempo_set_ppq(f6ed54f98ea54ef9).(qtempo, resolution); |
| 185 | snd_seq_queue_tempo_set_tempocxx . . $ snd_seq_queue_tempo_set_tempo(baa6dbc0312da988).(qtempo, 60*1000000/tempo); |
| 186 | |
| 187 | ret = snd_seq_set_queue_tempocxx . . $ snd_seq_set_queue_tempo(214a411c51cc9e8e).(ctxp->handlecxx . . $ seq_context#handle., ctxp->queuecxx . . $ seq_context#queue., qtempo); |
| 188 | |
| 189 | return ret; |
| 190 | } |
| 191 | |
| 192 | /* |
| 193 | * Set the context to use the specified queue. All events sent |
| 194 | * on this context will now use this queue. |
| 195 | * |
| 196 | * Arguments: |
| 197 | * ctxp - Context to modify |
| 198 | * q - The queue number |
| 199 | */ |
| 200 | void |
| 201 | seq_set_queuecxx . . $ seq_set_queue(cbf6e4b539b61ec5).(seq_context_tcxx . . $ seq_context_t# *ctxp, int q) |
| 202 | { |
| 203 | } |
| 204 | |
| 205 | /* |
| 206 | * Send the event to the specified client and port. |
| 207 | * |
| 208 | * Arguments: |
| 209 | * ctxp - Client context |
| 210 | * ev - Event to send |
| 211 | * client - Client to send the event to |
| 212 | * port - Port to send the event to |
| 213 | */ |
| 214 | int |
| 215 | seq_sendtocxx . . $ seq_sendto(77c49dfd684db7ae).(seq_context_tcxx . . $ seq_context_t# *ctxp, snd_seq_event_tcxx . . $ snd_seq_event_t# *ev, int client, int port) |
| 216 | { |
| 217 | ev->sourcecxx . . $ snd_seq_event#source. = ctxp->sourcecxx . . $ seq_context#source.; |
| 218 | ev->queuecxx . . $ snd_seq_event#queue. = ctxp->queuecxx . . $ seq_context#queue.; |
| 219 | ev->destcxx . . $ snd_seq_event#dest..clientcxx . . $ snd_seq_addr#client. = client; |
| 220 | ev->destcxx . . $ snd_seq_event#dest..portcxx . . $ snd_seq_addr#port. = port; |
| 221 | seq_writecxx . . $ seq_write(f9711ae91b75e353).(ctxp, ev); |
| 222 | |
| 223 | return 0; |
| 224 | } |
| 225 | |
| 226 | /* |
| 227 | * seq_send_to_all: |
| 228 | * Send the event to all the connected devices across all |
| 229 | * possible channels. The messages are sent to the blocking |
| 230 | * control port and so should not have timestamps in the |
| 231 | * future. This function is intended for all-sounds-off type |
| 232 | * controls etc. |
| 233 | * |
| 234 | * Arguments: |
| 235 | * ctxp: Client context |
| 236 | * ep: Event prototype to be sent |
| 237 | */ |
| 238 | int |
| 239 | seq_send_to_allcxx . . $ seq_send_to_all(f9711ae91b75e353).(seq_context_tcxx . . $ seq_context_t# *ctxp, snd_seq_event_tcxx . . $ snd_seq_event_t# *ep) |
| 240 | { |
| 241 | int dev; |
| 242 | int chan; |
| 243 | snd_seq_addr_tcxx . . $ snd_seq_addr_t# *addr; |
| 244 | |
| 245 | for (dev = 0; ; dev++) { |
| 246 | addr = seq_dev_addrcxx . . $ seq_dev_addr(91c8a0532865b134).(ctxp, dev); |
| 247 | if (addr == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) |
| 248 | break; /* No more */ |
| 249 | |
| 250 | ep->queuecxx . . $ snd_seq_event#queue. = ctxp->queuecxx . . $ seq_context#queue.; |
| 251 | ep->destcxx . . $ snd_seq_event#dest. = *addr; |
| 252 | for (chan = 0; chan < 16; chan++) { |
| 253 | set_channelcxx . . $ set_channel(5c24c07e69e3137f).(ep, chan); |
| 254 | (void) seq_writecxx . . $ seq_write(f9711ae91b75e353).(ctxp, ep); |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | return 0; |
| 259 | } |
| 260 | |
| 261 | /* |
| 262 | * seq_dev_addr: |
| 263 | * Return the address corresponding to the specified logical |
| 264 | * device. |
| 265 | * |
| 266 | * Arguments: |
| 267 | * dev: Device to get the address of. |
| 268 | * inout: Input or output device |
| 269 | */ |
| 270 | snd_seq_addr_tcxx . . $ snd_seq_addr_t# * |
| 271 | seq_dev_addrcxx . . $ seq_dev_addr(91c8a0532865b134).(seq_context_tcxx . . $ seq_context_t# *ctxp, int dev) |
| 272 | { |
| 273 | GArraycxx . . $ GArray# *list; |
| 274 | snd_seq_addr_tcxx . . $ snd_seq_addr_t# *addr; |
| 275 | |
| 276 | list = ctxp->destlistcxx . . $ seq_context#destlist.; |
| 277 | |
| 278 | if (dev >= (int)list->lencxx . . $ _GArray#len.) |
| 279 | return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 280 | |
| 281 | addr = &g_array_indexcxx . . $ `src/glib.h:1532:9`!(list, snd_seq_addr_t, dev); |
| 282 | |
| 283 | return addr; |
| 284 | } |
| 285 | |
| 286 | /* |
| 287 | * Start the timer. (What about timers other than the system |
| 288 | * one?) |
| 289 | */ |
| 290 | void |
| 291 | seq_start_timercxx . . $ seq_start_timer(e410d57486f0cd63).(seq_context_tcxx . . $ seq_context_t# *ctxp) |
| 292 | { |
| 293 | seq_control_timercxx . . $ seq_control_timer(cbf6e4b539b61ec5).(ctxp, SND_SEQ_EVENT_STARTcxx . . $ SND_SEQ_EVENT_START.); |
| 294 | } |
| 295 | |
| 296 | /* |
| 297 | * Stop the timer. (What about timers other than the system |
| 298 | * one?) |
| 299 | */ |
| 300 | void |
| 301 | seq_stop_timercxx . . $ seq_stop_timer(e410d57486f0cd63).(seq_context_tcxx . . $ seq_context_t# *ctxp) |
| 302 | { |
| 303 | seq_control_timercxx . . $ seq_control_timer(cbf6e4b539b61ec5).(ctxp, SND_SEQ_EVENT_STOPcxx . . $ SND_SEQ_EVENT_STOP.); |
| 304 | } |
| 305 | |
| 306 | void |
| 307 | seq_control_timercxx . . $ seq_control_timer(cbf6e4b539b61ec5).(seq_context_tcxx . . $ seq_context_t# *ctxp, int onoff) |
| 308 | { |
| 309 | |
| 310 | if (onoff == SND_SEQ_EVENT_STARTcxx . . $ SND_SEQ_EVENT_START.) |
| 311 | snd_seq_start_queuecxx . . $ `<external>/4135540427972067373/seqmid.h:340:9`!(ctxp->handle, ctxp->queue, 0); |
| 312 | else |
| 313 | snd_seq_stop_queuecxx . . $ `<external>/4135540427972067373/seqmid.h:349:9`!(ctxp->handle, ctxp->queue, 0); |
| 314 | |
| 315 | #ifdef USE_DRAINcxx . . $ `<built-in>:421:9`! |
| 316 | snd_seq_drain_outputcxx . . $ snd_seq_drain_output(13c0955b497a8b28).(ctxp->handlecxx . . $ seq_context#handle.); |
| 317 | #else |
| 318 | snd_seq_flush_output(ctxp->handle); |
| 319 | #endif |
| 320 | } |
| 321 | |
| 322 | /* |
| 323 | * Write out the event. This routine blocks until |
| 324 | * successfully written. |
| 325 | * |
| 326 | * Arguments: |
| 327 | * ctxp - Application context |
| 328 | * ep - Event |
| 329 | */ |
| 330 | int |
| 331 | seq_writecxx . . $ seq_write(f9711ae91b75e353).(seq_context_tcxx . . $ seq_context_t# *ctxp, snd_seq_event_tcxx . . $ snd_seq_event_t# *ep) |
| 332 | { |
| 333 | int err = 0; |
| 334 | |
| 335 | err = snd_seq_event_outputcxx . . $ snd_seq_event_output(efd054e2b1273355).(ctxp->handlecxx . . $ seq_context#handle., ep); |
| 336 | if (err < 0) |
| 337 | return err; |
| 338 | |
| 339 | return err; |
| 340 | } |
| 341 | |
| 342 | /* |
| 343 | * Return the handle to the underlying snd_seq stream. |
| 344 | * Arguments: |
| 345 | * ctxp - Application context |
| 346 | */ |
| 347 | void * |
| 348 | seq_handlecxx . . $ seq_handle(43ef867995de2152).(seq_context_tcxx . . $ seq_context_t# *ctxp) |
| 349 | { |
| 350 | return ctxp->handlecxx . . $ seq_context#handle.; |
| 351 | } |
| 352 | |
| 353 | static void |
| 354 | set_channelcxx . . $ set_channel(5c24c07e69e3137f).(snd_seq_event_tcxx . . $ snd_seq_event_t# *ep, int chan) |
| 355 | { |
| 356 | |
| 357 | switch (ep->typecxx . . $ snd_seq_event#type.) { |
| 358 | case SND_SEQ_EVENT_NOTEcxx . . $ SND_SEQ_EVENT_NOTE.: |
| 359 | case SND_SEQ_EVENT_NOTEONcxx . . $ SND_SEQ_EVENT_NOTEON.: |
| 360 | case SND_SEQ_EVENT_NOTEOFFcxx . . $ SND_SEQ_EVENT_NOTEOFF.: |
| 361 | ep->datacxx . . $ snd_seq_event#data..notecxx . . $ snd_seq_event_data#note..channelcxx . . $ snd_seq_ev_note#channel. = chan; |
| 362 | break; |
| 363 | case SND_SEQ_EVENT_KEYPRESScxx . . $ SND_SEQ_EVENT_KEYPRESS.: |
| 364 | case SND_SEQ_EVENT_PGMCHANGEcxx . . $ SND_SEQ_EVENT_PGMCHANGE.: |
| 365 | case SND_SEQ_EVENT_CHANPRESScxx . . $ SND_SEQ_EVENT_CHANPRESS.: |
| 366 | case SND_SEQ_EVENT_PITCHBENDcxx . . $ SND_SEQ_EVENT_PITCHBEND.: |
| 367 | case SND_SEQ_EVENT_CONTROL14cxx . . $ SND_SEQ_EVENT_CONTROL14.: |
| 368 | case SND_SEQ_EVENT_NONREGPARAMcxx . . $ SND_SEQ_EVENT_NONREGPARAM.: |
| 369 | case SND_SEQ_EVENT_REGPARAMcxx . . $ SND_SEQ_EVENT_REGPARAM.: |
| 370 | case SND_SEQ_EVENT_CONTROLLERcxx . . $ SND_SEQ_EVENT_CONTROLLER.: |
| 371 | ep->datacxx . . $ snd_seq_event#data..controlcxx . . $ snd_seq_event_data#control..channelcxx . . $ snd_seq_ev_ctrl#channel. = chan; |
| 372 | break; |
| 373 | default: |
| 374 | if (snd_seq_ev_is_channel_typecxx . . $ `<external>/4963296836554771614/seq.h:708:9`!(ep)) |
| 375 | g_warningcxx . . $ `src/glib.h:1293:9`!("Missed a case in set_channel"); |
| 376 | break; |
| 377 | } |
| 378 | } |