File: midiout.c

package info (click to toggle)
csound 1%3A6.18.1%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 63,220 kB
  • sloc: ansic: 192,643; cpp: 14,149; javascript: 9,654; objc: 9,181; python: 3,376; java: 3,337; sh: 1,840; yacc: 1,255; xml: 985; perl: 635; lisp: 411; tcl: 341; lex: 217; makefile: 128
file content (566 lines) | stat: -rw-r--r-- 17,759 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
/*
    midiout.c:

    Copyright (C) 1997 Gabriel Maldonado, John ffitch

    This file is part of Csound.

    The Csound Library is free software; you can redistribute it
    and/or modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    Csound is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with Csound; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
    02110-1301 USA
*/

/****************************************/
/** midiout UGs by Gabriel Maldonado   **/
/****************************************/

/* Some modifications by JPff for general use */

#include <math.h>
#include "csoundCore.h"
#include "midiout.h"

#define MGLOB(x) (((CSOUND*)csound)->midiGlobals->x)

#define NUMCHN          (16)
#define EXTRA_TIME      (1)

extern void openMIDIout(CSOUND *);
/* static MYFLT   invkr; */
void note_on(CSOUND *, int32_t chan, int32_t num, int32_t vel);
void note_off(CSOUND *, int32_t chan, int32_t num, int32_t vel);
void control_change(CSOUND *, int32_t chan, int32_t num, int32_t value);
void after_touch(CSOUND *, int32_t chan, int32_t value);
void program_change(CSOUND *, int32_t chan, int32_t num);
void pitch_bend(CSOUND *, int32_t chan, int32_t lsb, int32_t msb);
void poly_after_touch(CSOUND *, int32_t chan, int32_t note_num, int32_t value);
void send_midi_message(CSOUND *, int32_t status, int32_t data1, int32_t data2);

int32_t release_set(CSOUND *csound, REL *p)
{
    IGN(csound);
    if (p->h.insdshead->xtratim < EXTRA_TIME)
      /* if not initialised by another opcode */
      p->h.insdshead->xtratim = EXTRA_TIME;
    return OK;
}

int32_t release(CSOUND *csound, REL *p)
{
    IGN(csound);
    if (p->h.insdshead->relesing)
      *p->r = FL(1.0); /* TRUE */
    else
      *p->r = FL(0.0); /* FALSE */
    return OK;
}

int32_t xtratim(CSOUND *csound, XTRADUR *p)
{
    IGN(csound);
    int32_t *xtra = &(p->h.insdshead->xtratim);
    int32_t tim = (int32_t)(*p->extradur * p->h.insdshead->ekr);
    if (*xtra < tim)  /* gab-a5 revised */
      *xtra = tim;
    return OK;
}

int32_t mclock_set(CSOUND *csound, MCLOCK *p)
{
    IGN(csound);
    p->period= CS_EKR / *p->freq;
    p->clock_tics = p->period;
    p->beginning_flag = TRUE;
    return OK;
}

int32_t mclock(CSOUND *csound, MCLOCK *p)
{
    if (UNLIKELY(p->beginning_flag)) {    /* first time */
      send_midi_message(csound, 0xF8, 0, 0);    /* clock message */
      p->beginning_flag=FALSE;
      return OK;
    }
    else if ((MYFLT) CS_KCNT > p->clock_tics) {
      send_midi_message(csound, 0xF8, 0, 0);    /* clock message */
      p->clock_tics += p->period;
    }
    return OK;
}

int32_t mrtmsg(CSOUND *csound, MRT *p)
{
    switch ((int32_t)*p->message) {
    case 0:
      send_midi_message(csound, 0xFC, 0, 0); /* stop */
      break;
    case 1:
      send_midi_message(csound, 0xFA, 0, 0); /* start */
      break;
    case 2:
      send_midi_message(csound, 0xFB, 0, 0); /* continue */
      break;
    case -1:
      send_midi_message(csound, 0xFF, 0, 0); /* system_reset */
      break;
    case -2:
      send_midi_message(csound, 0xFE, 0, 0); /* active_sensing */
      break;
    default:
      return csound->InitError(csound, Str("illegal mrtmsg argument"));
    }
    return OK;
}

int32_t iout_on(CSOUND *csound, OUT_ON *p)
{
    note_on(csound, (int32_t)*p->ichn-1,(int32_t)*p->inum,(int32_t)*p->ivel);
    return OK;
}

int32_t iout_off(CSOUND *csound, OUT_ON *p)
{
    note_off(csound, (int32_t)*p->ichn-1,(int32_t)*p->inum,(int32_t)*p->ivel);
    return OK;
}

int32_t iout_on_dur_set(CSOUND *csound, OUT_ON_DUR *p)
{
    int32_t temp;
    if (p->h.insdshead->xtratim < EXTRA_TIME)
      /* if not initialised by another opcode */
      p->h.insdshead->xtratim = EXTRA_TIME;

    p->chn = (temp = abs((int32_t)*p->ichn-1)) < NUMCHN ? temp : NUMCHN-1;
    p->num = (temp = abs((int32_t)*p->inum)) < 128 ? temp : 127;
    p->vel = (temp = abs((int32_t)*p->ivel)) < 128 ? temp : 127;

    note_on(csound, p->chn,p->num, p->vel);
    p->istart_time = (MYFLT)CS_KCNT * CS_ONEDKR;
    p->fl_expired = FALSE;
    p->fl_extra_dur = FALSE;
    return OK;
}

int32_t iout_on_dur(CSOUND *csound, OUT_ON_DUR *p)
{
    if (!(p->fl_expired)) {
      MYFLT actual_dur = (MYFLT) CS_KCNT * CS_ONEDKR
                         - p->istart_time;
      MYFLT dur = *p->idur;
      if (dur < actual_dur) {
        p->fl_expired = TRUE;
        note_off(csound, p->chn, p->num, p->vel);
      }
      else if (p->h.insdshead->relesing) {
        p->fl_expired = TRUE;
        note_off(csound, p->chn, p->num, p->vel);
      }
    }
    return OK;
}

int32_t iout_on_dur2(CSOUND *csound, OUT_ON_DUR *p)
{
    if (!(p->fl_expired)) {
      MYFLT actual_dur = (MYFLT)CS_KCNT * CS_ONEDKR
                         - p->istart_time;
      MYFLT dur = *p->idur;
      if (dur < actual_dur) {
        p->fl_expired = TRUE;
        note_off(csound, p->chn, p->num, p->vel);
      }
      else if (p->h.insdshead->relesing || p->fl_extra_dur) {

        if (!p->fl_extra_dur && dur > actual_dur) {

          p->h.insdshead->offtim +=  dur - actual_dur+ FL(1.0);
          p->h.insdshead->relesing =0;
          p->fl_extra_dur=TRUE;
        }
        else if (dur <= actual_dur) {
          note_off(csound, p->chn, p->num, p->vel);
        }
      }
    }
    return OK;
}

int32_t moscil_set(CSOUND *csound, MOSCIL *p)
{
    IGN(csound);
    if (p->h.insdshead->xtratim < EXTRA_TIME)
      /* if not initialised by another opcode */
      p->h.insdshead->xtratim = EXTRA_TIME;
    p->istart_time = (MYFLT)CS_KCNT * CS_ONEDKR;
    p->fl_first_note   = TRUE;
    p->fl_note_expired = TRUE;
    p->fl_end_note     = FALSE;
    return OK;
}

int32_t moscil(CSOUND *csound, MOSCIL *p)
{
    if (p->fl_first_note) {
      p->fl_first_note = FALSE;
      goto first_note;
    }
    if (!(p->fl_note_expired)) {
      if (p->h.insdshead->relesing) {
        p->fl_note_expired = TRUE;
        p->fl_end_note     = TRUE;
        note_off(csound, p->last_chn, p->last_num, p->last_vel);
      }
      else if (p->last_dur < (MYFLT)CS_KCNT * CS_ONEDKR
                             - p->istart_time) {
        p->fl_note_expired = TRUE;
        note_off(csound, p->last_chn, p->last_num, p->last_vel);
      }
    }
    else {
      if (!p->fl_end_note
          && p->last_pause + p->last_dur <
             (MYFLT)CS_KCNT * CS_ONEDKR - p->istart_time
          && !(p->h.insdshead->relesing)) {
        MYFLT ftemp;
        p->istart_time = p->istart_time + p->last_pause + p->last_dur;
        p->last_dur   =         /* dur must be at least 1/kr */
          (ftemp = *p->kdur) > 0 ? ftemp : CS_ONEDKR;
        p->last_pause = (ftemp = *p->kpause) > 0 ? ftemp : CS_ONEDKR;
 first_note:
        {
          int32_t temp;
          p->last_chn = (temp = abs((int32_t)*p->kchn-1)) < NUMCHN ? temp :
                                                                  NUMCHN-1;
          p->last_num = (temp = abs((int32_t)*p->knum)) < 128    ? temp : 127;
          p->last_vel = (temp = abs((int32_t)*p->kvel)) < 128    ? temp : 127;
        }
        p->fl_note_expired = FALSE;
        note_on(csound, p->last_chn, p->last_num, p->last_vel);
      }
    }
    return OK;
}

int32_t kvar_out_on_set(CSOUND *csound, KOUT_ON *p)
{
    IGN(csound);
    if (p->h.insdshead->xtratim < EXTRA_TIME)
      /* if not initialised by another opcode */
      p->h.insdshead->xtratim = EXTRA_TIME;
    p->fl_first_note = TRUE;
    return OK;
}

int32_t kvar_out_on(CSOUND *csound, KOUT_ON *p)
{
    if (p->fl_first_note) {
      int32_t temp;

      p->last_chn = (temp = abs((int32_t)*p->kchn-1)) < NUMCHN  ? temp : NUMCHN-1;
      p->last_num = (temp = abs((int32_t)*p->knum)) < 128     ? temp : 127;
      p->last_vel = (temp = abs((int32_t)*p->kvel)) < 128     ? temp : 127;
      p->fl_first_note   = FALSE;
      p->fl_note_expired = FALSE;

      note_on(csound, p->last_chn, p->last_num, p->last_vel);
    }
    else if (p->fl_note_expired) return OK;
    else {
      if (p->h.insdshead->relesing) {
        note_off(csound, p->last_chn, p->last_num, p->last_vel);
        p->fl_note_expired = TRUE;
      }
      else {
        int32_t tt;
        int32_t curr_chn = (tt = abs((int32_t)*p->kchn-1)) < NUMCHN ? tt : NUMCHN-1;
        int32_t curr_num = (tt = abs((int32_t)*p->knum)) < 128    ? tt : 127;
        int32_t curr_vel = (tt = abs((int32_t)*p->kvel)) < 128    ? tt : 127;

        if (  p->last_chn != curr_chn
               || p->last_num != curr_num
               || p->last_vel != curr_vel
              ) {
          note_off(csound, p->last_chn, p->last_num, p->last_vel);

          p->last_chn = curr_chn;
          p->last_num = curr_num;
          p->last_vel = curr_vel;

          note_on(csound, curr_chn, curr_num, curr_vel);
        }
      }
    }
    return OK;
}

int32_t out_controller (CSOUND *csound, OUT_CONTR *p)
{
    /* if (!(p->h.insdshead->prvinstance)) JPff/VL */ {
      /* if prev instance already allocated in the same MIDI chan */
      int32_t value;
      MYFLT min = *p->min;
      value =  (int32_t)((*p->value - min) * FL(127.0) / (*p->max - min));
      value = (value < 128) ?  value : 127;
      value = (value > -1) ?  value : 0;
      if (value != p->last_value ||
          *p->chn != p->lastchn ||
          *p->num != p->lastctrl) {
        /* csound->Message(csound, "out contr value: %d\n", value); */
        control_change(csound, (int32_t)*p->chn-1,(int32_t)*p->num ,value);
        p->last_value = value;
        p->lastchn = *p->chn;
        p->lastctrl = *p->num;
        }
    }
    return OK;
}

int32_t out_aftertouch (CSOUND *csound, OUT_ATOUCH *p)
{
    /* if (!(p->h.insdshead->prvinstance)) JPff/VL */ {
      /* if prev instance already allocated in the same MIDI chan */
      int32_t value;
      MYFLT min = *p->min;
      value =  (int32_t)((*p->value - min) * FL(127.0) / (*p->max - min));
      value = value < 128 ?  value : 127;
      value = value > -1  ?  value : 0;
      if (value != p->last_value || *p->chn != p->lastchn) {
        after_touch(csound, (int32_t)*p->chn-1, value);
        p->last_value = value;
        p->lastchn = *p->chn;
       }
    }
    return OK;
}

int32_t out_poly_aftertouch (CSOUND *csound, OUT_POLYATOUCH *p)
{
    int32_t value;
    MYFLT min = *p->min;
    value =  (int32_t)((*p->value - min) * FL(127.0) / (*p->max - min));
    value = value < 128 ?  value : 127;
    value = value > -1  ?  value : 0;
    if (value != p->last_value ||
        *p->chn != p->lastchn  ||
        *p->num != p->lastctrl) {
      poly_after_touch(csound, (int32_t)*p->chn-1, (int32_t)*p->num, value);
      p->last_value = value;
      p->lastchn = *p->chn;
      p->lastctrl = *p->num;
}

    return OK;
}

int32_t out_progchange (CSOUND *csound, OUT_PCHG *p)
{
    /* if (!(p->h.insdshead->prvinstance)) JPff/VL */ {
      /* if prev instance already allocated in the same MIDI chan */
      int32_t prog_num;
      MYFLT min = *p->min;
      prog_num =  (int32_t)((*p->prog_num - min) * FL(127.0) / (*p->max - min));
      prog_num = prog_num < 128 ?  prog_num : 127;
      prog_num = prog_num > -1  ?  prog_num : 0;
      if (prog_num != p->last_prog_num || *p->chn != p->lastchn) {
        program_change(csound, (int32_t)*p->chn-1, prog_num);
        p->last_prog_num = prog_num;
        p->lastchn = *p->chn;
        }
    }
    return OK;
}

int32_t out_controller14 (CSOUND *csound, OUT_CONTR14 *p)
{
    /* if (!(p->h.insdshead->prvinstance)) JPff/VL */ {
      /* if prev instance already allocated in the same MIDI chan */
      int32_t value;
      MYFLT min = *p->min;

      value =  (int32_t)((*p->value - min) * FL(16383.0) / (*p->max - min));
      value = (value < 16384) ?  value : 16383;
      value = (value > -1) ?  value : 0;

      if (value != p->last_value  ||
          *p->chn != p->lastchn   ||
          *p->msb_num != p->lastctrl) {
        uint32_t msb = value >> 7;
        uint32_t lsb = value & 0x7F;
        csound->Warning(csound, Str("out contr14 msb:%x lsb:%x\n"), msb, lsb);
        control_change(csound, (int32_t)*p->chn-1, (int32_t)*p->msb_num, msb);
        control_change(csound, (int32_t)*p->chn-1, (int32_t)*p->lsb_num, lsb);
        p->last_value = value;
        p->lastchn = *p->chn;
        p->lastctrl = *p->msb_num;
         }
    }
    return OK;
}

int32_t out_pitch_bend(CSOUND *csound, OUT_PB *p)
{
    /* if (p->h.insdshead->prvinstance) { */
    /*   /\* if prev instance already allocated in the same MIDI chan *\/ */
    /*   return OK; */
    /* } */
    /* JPff/VL else */ {
      int32_t   value;
      MYFLT min = *p->min;

      value = (int32_t)((*p->value - min) * FL(16383.0) / (*p->max - min));
      value = (value < 16384  ?  value : 16383);
      value = (value > -1     ?  value : 0);
      if (value != p->last_value || *p->chn != p->lastchn )  {
        uint32_t msb = value >> 7;
        uint32_t lsb = value & 0x7F;
        pitch_bend(csound, (int32_t)*p->chn - 1, lsb, msb);
        p->last_value = value;
        p->lastchn = *p->chn;
        }
    }
    return OK;
}

int32_t kon2_set(CSOUND *csound, KON2 *p)
{
   IGN(csound);
    /* if not initialised by another opcode */
    if (p->h.insdshead->xtratim < EXTRA_TIME)
      p->h.insdshead->xtratim = EXTRA_TIME;
    /*p->fl_first_note = TRUE;*/
    p->fl_note_expired = FALSE;

    return OK;
}

int32_t kon2(CSOUND *csound, KON2 *p)
{
    /*
        if (p->fl_first_note) {
          register int32_t tt;

          p->last_chn = (tt = abs((int32_t)*p->kchn)) < NUMCHN  ? tt : NUMCHN-1;
          p->last_num = (tt = abs((int32_t)*p->knum)) < 128     ? tt : 127;
          p->last_vel = (tt = abs((int32_t)*p->kvel)) < 128     ? tt : 127;
          p->fl_first_note   = FALSE;
          p->fl_note_expired = FALSE;

          note_on(csound, p->last_chn, p->last_num, p->last_vel);
        }

    */
    /*else */
    if (p->fl_note_expired) return OK;
    else {
      if (p->h.insdshead->relesing) {
        note_off(csound, p->last_chn,p->last_num,p->last_vel);
        p->fl_note_expired = TRUE;
      }
      else {
        int32_t tt;

        int32_t curr_chn = (tt = abs((int32_t)*p->kchn-1)) <= NUMCHN ? tt : NUMCHN;
        int32_t curr_num = (tt = abs((int32_t)*p->knum)) < 128    ? tt : 127;
        int32_t curr_vel = (tt = abs((int32_t)*p->kvel)) < 128    ? tt : 127;

        if ((int32_t)(*p->ktrig +FL(0.5)) != 0  )/* i.e. equal to 1  */
                                /*   p->last_chn != curr_chn
                                || p->last_num != curr_num
                                || p->last_vel != curr_vel
                                ) */
          {
            note_off(csound, p->last_chn, p->last_num, p->last_vel);
            p->last_chn = curr_chn;
            p->last_num = curr_num;
            p->last_vel = curr_vel;
            note_on(csound, curr_chn, curr_num, curr_vel);
          }
      }
    }
    return OK;
}

int32_t midiout(CSOUND *csound, MIDIOUT *p)         /*gab-A6 fixed*/
{
    int32_t st, ch, d1, d2;

    st = (int32_t)(*p->in_type + FL(0.5));
    if (!st)
      return OK;
    st = (st & 0x70) | 0x80;
    ch = (int32_t)(*p->in_chan - FL(0.5)) & 0x0F;
    d1 = (int32_t)(*p->in_dat1 + FL(0.5)) & 0x7F;
    d2 = (int32_t)(*p->in_dat2 + FL(0.5)) & 0x7F;
    send_midi_message(csound, st | ch, d1, d2);
    return OK;
}

int32_t nrpn(CSOUND *csound, NRPN *p)
{
    int32_t chan = (int32_t)*p->chan-1, parm = (int32_t)*p->parm_num;
    int32_t value = (int32_t)*p->parm_value;
    if (chan != p->old_chan || parm != p->old_parm || value != p->old_value) {
      int32_t status = 176 | chan;
      int32_t parm_msb =  parm >> 7;
      int32_t parm_lsb =  parm  & 0x7f;

      int32_t value_msb = (value + 8192) >> 7;
      int32_t value_lsb = (value + 8192) % 128;

      send_midi_message(csound, status, 99, parm_msb);
      send_midi_message(csound, status, 98, parm_lsb);
      send_midi_message(csound, status,  6, value_msb);
      send_midi_message(csound, status, 38, value_lsb);
      p->old_chan = chan;
      p->old_parm = parm;
      p->old_value = value;
    }
    return OK;
}

int32_t mdelay_set(CSOUND *csound, MDELAY *p)
{
   IGN(csound);
    p->read_index = 0;
    p->write_index = 0;
    memset(p->status, 0, DELTAB_LENGTH);
    return OK;
}

int32_t mdelay(CSOUND *csound, MDELAY *p)                   /*gab-A6 fixed*/
{
    int32_t read_index = p->read_index % DELTAB_LENGTH;
    int32_t write_index = p->write_index % DELTAB_LENGTH;
    MYFLT present_time =  CS_KCNT * CS_ONEDKR;

    if (((int32_t)*p->in_status == 0x90 || (int32_t)*p->in_status == 0x80)) {
      p->status[write_index] = (int32_t)*p->in_status;
      p->chan[write_index] = (int32_t)*p->in_chan-1;
      p->dat1[write_index] = (int32_t)*p->in_dat1;
      p->dat2[write_index] = (int32_t)*p->in_dat2;
      p->time[write_index] = present_time;
      (p->write_index)++;
    }
    if (p->status[read_index] &&
        p->time[read_index] + *p->kdelay <= present_time) {
      int32_t number = p->dat1[read_index];
      int32_t velocity = p->dat2[read_index];
      send_midi_message(csound,  p->status[read_index] | p->chan[read_index],
                                 ((number   > 127) ? 127 : number),
                                 ((velocity > 127) ? 127 : velocity));
      (p->read_index)++;
    }
    return OK;
}