| 1 | /* |
| 2 | * File: seqmidi.m - convert to the sequencer events |
| 3 | * |
| 4 | * Copyright (C) 1999 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 <string.h> |
| 23 | #include "seqlib.h" |
| 24 | #include "seqpriv.h" |
| 25 | |
| 26 | /* |
| 27 | * Initialize a midi event from the context. The source and |
| 28 | * destination addresses will be set from the information |
| 29 | * stored in the context, which depends on the previous |
| 30 | * connection calls. In addition the time and channel are set. |
| 31 | * This should be called first before any of the following |
| 32 | * functions. |
| 33 | * |
| 34 | * Arguments: |
| 35 | * ctxp - Client application |
| 36 | * ep - Event to init |
| 37 | * time - Midi time |
| 38 | * devchan - Midi channel |
| 39 | */ |
| 40 | void |
| 41 | seq_midi_event_init(seq_context_t *ctxp, snd_seq_event_t *ep, |
| 42 | unsigned long time, int devchan) |
| 43 | { |
| 44 | int dev; |
| 45 | |
| 46 | dev = devchan >> 4; |
| 47 | |
| 48 | /* |
| 49 | * If insufficient output devices have been registered, then we |
| 50 | * just scale the device back to fit in the correct range. This |
| 51 | * is not necessarily what you want. |
| 52 | */ |
| 53 | if (dev >= ctxp->ctxndest) |
| 54 | dev = dev % ctxp->ctxndest; |
| 55 | |
| 56 | snd_seq_ev_clear(ep); |
| 57 | snd_seq_ev_schedule_tick(ep, ctxp->queue, 0, time); |
| 58 | ep->source = ctxp->source; |
| 59 | if (ctxp->ctxndest > 0) |
| 60 | ep->dest = g_array_index(ctxp->destlist, snd_seq_addr_t, dev); |
| 61 | } |
| 62 | |
| 63 | /* |
| 64 | * Send a note event. |
| 65 | * Arguments: |
| 66 | * ctxp - Client context |
| 67 | * ep - Event template |
| 68 | * note - Pitch of note |
| 69 | * vel - Velocity of note |
| 70 | * length - Length of note |
| 71 | */ |
| 72 | void |
| 73 | seq_midi_note(seq_context_t *ctxp, snd_seq_event_t *ep, int devchan, int note, int vel, |
| 74 | int length) |
| 75 | { |
| 76 | ep->type = SND_SEQ_EVENT_NOTE; |
| 77 | |
| 78 | ep->data.note.channel = devchan & 0xf; |
| 79 | ep->data.note.note = note; |
| 80 | ep->data.note.velocity = vel; |
| 81 | ep->data.note.duration = length; |
| 82 | |
| 83 | seq_write(ctxp, ep); |
| 84 | } |
| 85 | |
| 86 | /* |
| 87 | * Send a note on event. |
| 88 | * Arguments: |
| 89 | * ctxp - Client context |
| 90 | * ep - Event template |
| 91 | * note - Pitch of note |
| 92 | * vel - Velocity of note |
| 93 | * length - Length of note |
| 94 | */ |
| 95 | void |
| 96 | seq_midi_note_on(seq_context_t *ctxp, snd_seq_event_t *ep, int devchan, int note, int vel, |
| 97 | int length) |
| 98 | { |
| 99 | ep->type = SND_SEQ_EVENT_NOTEON; |
| 100 | |
| 101 | ep->data.note.channel = devchan & 0xf; |
| 102 | ep->data.note.note = note; |
| 103 | ep->data.note.velocity = vel; |
| 104 | ep->data.note.duration = length; |
| 105 | |
| 106 | seq_write(ctxp, ep); |
| 107 | } |
| 108 | |
| 109 | /* |
| 110 | * Send a note off event. |
| 111 | * Arguments: |
| 112 | * ctxp - Client context |
| 113 | * ep - Event template |
| 114 | * note - Pitch of note |
| 115 | * vel - Velocity of note |
| 116 | * length - Length of note |
| 117 | */ |
| 118 | void |
| 119 | seq_midi_note_off(seq_context_t *ctxp, snd_seq_event_t *ep, int devchan, int note, int vel, |
| 120 | int length) |
| 121 | { |
| 122 | ep->type = SND_SEQ_EVENT_NOTEOFF; |
| 123 | |
| 124 | ep->data.note.channel = devchan & 0xf; |
| 125 | ep->data.note.note = note; |
| 126 | ep->data.note.velocity = vel; |
| 127 | ep->data.note.duration = length; |
| 128 | |
| 129 | seq_write(ctxp, ep); |
| 130 | } |
| 131 | |
| 132 | /* |
| 133 | * Send a key pressure event. |
| 134 | * Arguments: |
| 135 | * ctxp - Application context |
| 136 | * ep - Event template |
| 137 | * note - Note to be altered |
| 138 | * value - Pressure value |
| 139 | */ |
| 140 | void |
| 141 | seq_midi_keypress(seq_context_t *ctxp, snd_seq_event_t *ep, int devchan, int note, |
| 142 | int value) |
| 143 | { |
| 144 | ep->type = SND_SEQ_EVENT_KEYPRESS; |
| 145 | |
| 146 | ep->data.control.channel = devchan & 0xf; |
| 147 | ep->data.control.param = note; |
| 148 | ep->data.control.value = value; |
| 149 | seq_write(ctxp, ep); |
| 150 | } |
| 151 | |
| 152 | /* |
| 153 | * Send a control event. |
| 154 | * Arguments: |
| 155 | * ctxp - Application context |
| 156 | * ep - Event template |
| 157 | * control - Controller to change |
| 158 | * value - Value to set it to |
| 159 | */ |
| 160 | void |
| 161 | seq_midi_control(seq_context_t *ctxp, snd_seq_event_t *ep, int devchan, int control, |
| 162 | int value) |
| 163 | { |
| 164 | ep->type = SND_SEQ_EVENT_CONTROLLER; |
| 165 | |
| 166 | ep->data.control.channel = devchan & 0xf; |
| 167 | ep->data.control.param = control; |
| 168 | ep->data.control.value = value; |
| 169 | seq_write(ctxp, ep); |
| 170 | } |
| 171 | |
| 172 | /* |
| 173 | * Send a program change event. |
| 174 | * Arguments: |
| 175 | * ctxp - Application context |
| 176 | * ep - Event template |
| 177 | * program - Program to set |
| 178 | */ |
| 179 | void |
| 180 | seq_midi_program(seq_context_t *ctxp, snd_seq_event_t *ep, int devchan, int program) |
| 181 | { |
| 182 | ep->type = SND_SEQ_EVENT_PGMCHANGE; |
| 183 | |
| 184 | ep->data.control.channel = devchan & 0xf; |
| 185 | ep->data.control.value = program; |
| 186 | seq_write(ctxp, ep); |
| 187 | } |
| 188 | |
| 189 | /* |
| 190 | * Send a channel pressure event. |
| 191 | * Arguments: |
| 192 | * ctxp - Application context |
| 193 | * ep - Event template |
| 194 | * pressure - Pressure value |
| 195 | */ |
| 196 | void |
| 197 | seq_midi_chanpress(seq_context_t *ctxp, snd_seq_event_t *ep, int devchan, int pressure) |
| 198 | { |
| 199 | ep->type = SND_SEQ_EVENT_CHANPRESS; |
| 200 | |
| 201 | ep->data.control.channel = devchan & 0xf; |
| 202 | ep->data.control.value = pressure; |
| 203 | seq_write(ctxp, ep); |
| 204 | } |
| 205 | |
| 206 | /* |
| 207 | * Send a pitchbend message. The bend parameter is centered on |
| 208 | * zero, negative values mean a lower pitch. |
| 209 | * Arguments: |
| 210 | * ctxp - Application context |
| 211 | * ep - Event template |
| 212 | * bend - Bend value, centered on zero. |
| 213 | */ |
| 214 | void |
| 215 | seq_midi_pitchbend(seq_context_t *ctxp, snd_seq_event_t *ep, int devchan, int bend) |
| 216 | { |
| 217 | ep->type = SND_SEQ_EVENT_PITCHBEND; |
| 218 | |
| 219 | ep->data.control.channel = devchan & 0xf; |
| 220 | ep->data.control.value = bend; |
| 221 | seq_write(ctxp, ep); |
| 222 | } |
| 223 | |
| 224 | /* |
| 225 | * Send a tempo event. The tempo parameter is in microseconds |
| 226 | * per beat. |
| 227 | * Arguments: |
| 228 | * ctxp - Application context |
| 229 | * ep - Event template |
| 230 | * tempo - New tempo in usec per beat |
| 231 | */ |
| 232 | void |
| 233 | seq_midi_tempo(seq_context_t *ctxp, snd_seq_event_t *ep, int tempo) |
| 234 | { |
| 235 | ep->type = SND_SEQ_EVENT_TEMPO; |
| 236 | |
| 237 | ep->data.queue.queue = ctxp->queue; |
| 238 | ep->data.queue.param.value = tempo; |
| 239 | ep->dest.client = SND_SEQ_CLIENT_SYSTEM; |
| 240 | ep->dest.port = SND_SEQ_PORT_SYSTEM_TIMER; |
| 241 | seq_write(ctxp, ep); |
| 242 | } |
| 243 | |
| 244 | /* |
| 245 | * Send a sysex event. The status byte is to distiguish |
| 246 | * continuation sysex messages. |
| 247 | * Arguments: |
| 248 | * ctxp - Application context |
| 249 | * ep - Event template |
| 250 | * status - Status byte for sysex |
| 251 | * data - Data to send |
| 252 | * length - Length of data |
| 253 | */ |
| 254 | void |
| 255 | seq_midi_sysex(seq_context_t *ctxp, snd_seq_event_t *ep, int status, |
| 256 | unsigned char *data, int length) |
| 257 | { |
| 258 | unsigned char *ndata; |
| 259 | int nlen; |
| 260 | |
| 261 | ep->type = SND_SEQ_EVENT_SYSEX; |
| 262 | |
| 263 | ndata = g_malloc(length + 1); |
| 264 | nlen = length +1; |
| 265 | |
| 266 | ndata[0] = status; |
| 267 | memcpy(ndata+1, data, length); |
| 268 | |
| 269 | snd_seq_ev_set_variable(ep, nlen, ndata); |
| 270 | |
| 271 | seq_write(ctxp, ep); |
| 272 | |
| 273 | g_free(ndata); |
| 274 | } |
| 275 | |
| 276 | /* |
| 277 | * Send an echo event back to the source client at the specified |
| 278 | * time. |
| 279 | * |
| 280 | * Arguments: |
| 281 | * ctxp - Application context |
| 282 | * time - Time of event |
| 283 | */ |
| 284 | void |
| 285 | seq_midi_echo(seq_context_t *ctxp, unsigned long time) |
| 286 | { |
| 287 | snd_seq_event_t ev; |
| 288 | |
| 289 | seq_midi_event_init(ctxp, &ev, time, 0); |
| 290 | /* Loop back */ |
| 291 | ev.dest = ctxp->source; |
| 292 | seq_write(ctxp, &ev); |
| 293 | } |
| 294 |