| 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" |
| 18 | #include "elements.h" |
| 19 | #include "except.h" |
| 20 | #include "intl.h" |
| 21 | |
| 22 | #include <stdio.h> |
| 23 | #include <errno.h> |
| 24 | #include <string.h> |
| 25 | #include "seqlib.h" |
| 26 | #include "seqpriv.h" |
| 27 | |
| 28 | static void set_channel(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_t * |
| 35 | seq_create_context() |
| 36 | { |
| 37 | seq_context_t *ctxp; |
| 38 | int q; |
| 39 | |
| 40 | ctxp = g_new(seq_context_t, 1); |
| 41 | ctxp->main = ctxp; /* This is the main context */ |
| 42 | |
| 43 | if (snd_seq_open(&ctxp->handle, "hw", SND_SEQ_OPEN_DUPLEX, 0) < 0) |
| 44 | except(ioError, "Could not open sequencer: %s", snd_strerror(errno)); |
| 45 | |
| 46 | q = snd_seq_alloc_queue(ctxp->handle); |
| 47 | ctxp->client = snd_seq_client_id(ctxp->handle); |
| 48 | ctxp->queue = q; |
| 49 | |
| 50 | ctxp->destlist = g_array_new(0, 0, sizeof(snd_seq_addr_t)); |
| 51 | |
| 52 | ctxp->queue = q; |
| 53 | ctxp->source.client = ctxp->client; |
| 54 | ctxp->source.port = 0; |
| 55 | |
| 56 | seq_new_port(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_t * |
| 69 | seq_new_client(seq_context_t *ctxp) |
| 70 | { |
| 71 | |
| 72 | return NULL;/*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_context(seq_context_t *ctxp) |
| 83 | { |
| 84 | |
| 85 | if (ctxp->main == ctxp) { |
| 86 | /* This is the main context */ |
| 87 | snd_seq_event_t ev; |
| 88 | unsigned long t; |
| 89 | |
| 90 | snd_seq_drop_output(ctxp->handle); |
| 91 | |
| 92 | t = 0; |
| 93 | seq_midi_event_init(ctxp, &ev, t, 0); |
| 94 | seq_midi_control(ctxp, &ev, 0, MIDI_CTL_ALL_SOUNDS_OFF, 0); |
| 95 | seq_send_to_all(ctxp, &ev); |
| 96 | snd_seq_drain_output(ctxp->handle); |
| 97 | |
| 98 | snd_seq_free_queue(ctxp->handle, ctxp->queue); |
| 99 | snd_seq_close(ctxp->handle); |
| 100 | } |
| 101 | |
| 102 | g_free(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_port(seq_context_t *ctxp) |
| 113 | { |
| 114 | int ret; |
| 115 | |
| 116 | ret = snd_seq_create_simple_port(ctxp->handle, NULL, |
| 117 | SND_SEQ_PORT_CAP_WRITE | |
| 118 | SND_SEQ_PORT_CAP_SUBS_WRITE | |
| 119 | SND_SEQ_PORT_CAP_READ, |
| 120 | SND_SEQ_PORT_TYPE_MIDI_GENERIC); |
| 121 | if (ret < 0) { |
| 122 | printf("Error creating port %s\n", snd_strerror(errno)); |
| 123 | return -1; |
| 124 | } |
| 125 | ctxp->port_count++; |
| 126 | ctxp->source.port = ret; |
| 127 | |
| 128 | return ret;/*XXX*/ |
| 129 | } |
| 130 | |
| 131 | void |
| 132 | seq_destroy_port(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_add(seq_context_t *ctxp, int client, int port) |
| 147 | { |
| 148 | snd_seq_addr_t addr; |
| 149 | int ret; |
| 150 | |
| 151 | memset(&addr, 0, sizeof(addr)); |
| 152 | addr.client = client; |
| 153 | addr.port = port; |
| 154 | |
| 155 | g_array_append_val(ctxp->destlist, addr); |
| 156 | |
| 157 | ret = snd_seq_connect_to(ctxp->handle, ctxp->source.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_tempo(seq_context_t *ctxp, int resolution, int tempo, int realtime) |
| 178 | { |
| 179 | snd_seq_queue_tempo_t *qtempo; |
| 180 | int ret; |
| 181 | |
| 182 | snd_seq_queue_tempo_alloca(&qtempo); |
| 183 | memset(qtempo, 0, snd_seq_queue_tempo_sizeof()); |
| 184 | snd_seq_queue_tempo_set_ppq(qtempo, resolution); |
| 185 | snd_seq_queue_tempo_set_tempo(qtempo, 60*1000000/tempo); |
| 186 | |
| 187 | ret = snd_seq_set_queue_tempo(ctxp->handle, ctxp->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_queue(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_sendto(seq_context_t *ctxp, snd_seq_event_t *ev, int client, int port) |
| 216 | { |
| 217 | ev->source = ctxp->source; |
| 218 | ev->queue = ctxp->queue; |
| 219 | ev->dest.client = client; |
| 220 | ev->dest.port = port; |
| 221 | seq_write(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_all(seq_context_t *ctxp, snd_seq_event_t *ep) |
| 240 | { |
| 241 | int dev; |
| 242 | int chan; |
| 243 | snd_seq_addr_t *addr; |
| 244 | |
| 245 | for (dev = 0; ; dev++) { |
| 246 | addr = seq_dev_addr(ctxp, dev); |
| 247 | if (addr == NULL) |
| 248 | break; /* No more */ |
| 249 | |
| 250 | ep->queue = ctxp->queue; |
| 251 | ep->dest = *addr; |
| 252 | for (chan = 0; chan < 16; chan++) { |
| 253 | set_channel(ep, chan); |
| 254 | (void) seq_write(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_t * |
| 271 | seq_dev_addr(seq_context_t *ctxp, int dev) |
| 272 | { |
| 273 | GArray *list; |
| 274 | snd_seq_addr_t *addr; |
| 275 | |
| 276 | list = ctxp->destlist; |
| 277 | |
| 278 | if (dev >= (int)list->len) |
| 279 | return NULL; |
| 280 | |
| 281 | addr = &g_array_index(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_timer(seq_context_t *ctxp) |
| 292 | { |
| 293 | seq_control_timer(ctxp, 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_timer(seq_context_t *ctxp) |
| 302 | { |
| 303 | seq_control_timer(ctxp, SND_SEQ_EVENT_STOP); |
| 304 | } |
| 305 | |
| 306 | void |
| 307 | seq_control_timer(seq_context_t *ctxp, int onoff) |
| 308 | { |
| 309 | |
| 310 | if (onoff == SND_SEQ_EVENT_START) |
| 311 | snd_seq_start_queue(ctxp->handle, ctxp->queue, 0); |
| 312 | else |
| 313 | snd_seq_stop_queue(ctxp->handle, ctxp->queue, 0); |
| 314 | |
| 315 | #ifdef USE_DRAIN |
| 316 | snd_seq_drain_output(ctxp->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_write(seq_context_t *ctxp, snd_seq_event_t *ep) |
| 332 | { |
| 333 | int err = 0; |
| 334 | |
| 335 | err = snd_seq_event_output(ctxp->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_handle(seq_context_t *ctxp) |
| 349 | { |
| 350 | return ctxp->handle; |
| 351 | } |
| 352 | |
| 353 | static void |
| 354 | set_channel(snd_seq_event_t *ep, int chan) |
| 355 | { |
| 356 | |
| 357 | switch (ep->type) { |
| 358 | case SND_SEQ_EVENT_NOTE: |
| 359 | case SND_SEQ_EVENT_NOTEON: |
| 360 | case SND_SEQ_EVENT_NOTEOFF: |
| 361 | ep->data.note.channel = chan; |
| 362 | break; |
| 363 | case SND_SEQ_EVENT_KEYPRESS: |
| 364 | case SND_SEQ_EVENT_PGMCHANGE: |
| 365 | case SND_SEQ_EVENT_CHANPRESS: |
| 366 | case SND_SEQ_EVENT_PITCHBEND: |
| 367 | case SND_SEQ_EVENT_CONTROL14: |
| 368 | case SND_SEQ_EVENT_NONREGPARAM: |
| 369 | case SND_SEQ_EVENT_REGPARAM: |
| 370 | case SND_SEQ_EVENT_CONTROLLER: |
| 371 | ep->data.control.channel = chan; |
| 372 | break; |
| 373 | default: |
| 374 | if (snd_seq_ev_is_channel_type(ep)) |
| 375 | g_warning("Missed a case in set_channel"); |
| 376 | break; |
| 377 | } |
| 378 | } |