summaryrefslogtreecommitdiffstats
path: root/sys/dev/sound/midi/midisynth.c
blob: 0c6dd8a3a81772bfa8cad8c7c0d6391327632fc2 (plain)
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
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
/*
 * Copyright by Hannu Savolainen 1993
 *
 * Redistribution and use in source and binary forms, with or
 * without modification, are permitted provided that the following
 * conditions are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above
 *    copyright notice, this list of conditions and the following
 *    disclaimer in the documentation and/or other materials provided
 *    with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS
 * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
 * AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * $FreeBSD$
 *
 */

/*
 * This is the interface for a sequencer to interact a midi driver.
 * This interface translates the sequencer operations to the corresponding
 * midi messages, and vice versa.
 */

#include <dev/sound/midi/midi.h>

#define TYPEDRANGE(type, x, lower, upper) \
{ \
	type tl, tu; \
	tl = (lower); \
	tu = (upper); \
	if (x < tl) { \
		x = tl; \
	} else if(x > tu) { \
		x = tu; \
	} \
}

/*
 * These functions goes into midisynthdev_op_desc.
 */
static mdsy_killnote_t synth_killnote;
static mdsy_setinstr_t synth_setinstr;
static mdsy_startnote_t synth_startnote;
static mdsy_reset_t synth_reset;
static mdsy_hwcontrol_t synth_hwcontrol;
static mdsy_loadpatch_t synth_loadpatch;
static mdsy_panning_t synth_panning;
static mdsy_aftertouch_t synth_aftertouch;
static mdsy_controller_t synth_controller;
static mdsy_patchmgr_t synth_patchmgr;
static mdsy_bender_t synth_bender;
static mdsy_allocvoice_t synth_allocvoice;
static mdsy_setupvoice_t synth_setupvoice;
static mdsy_sendsysex_t synth_sendsysex;
static mdsy_prefixcmd_t synth_prefixcmd;
static mdsy_volumemethod_t synth_volumemethod;
static mdsy_readraw_t synth_readraw;
static mdsy_writeraw_t synth_writeraw;

/*
 * This is the synthdev_info for a midi interface device.
 * You may have to replace a few of functions for an internal
 * synthesizer.
 */
synthdev_info midisynth_op_desc = {
	synth_killnote,
	synth_setinstr,
	synth_startnote,
	synth_reset,
	synth_hwcontrol,
	synth_loadpatch,
	synth_panning,
	synth_aftertouch,
	synth_controller,
	synth_patchmgr,
	synth_bender,
	synth_allocvoice,
	synth_setupvoice,
	synth_sendsysex,
	synth_prefixcmd,
	synth_volumemethod,
	synth_readraw,
	synth_writeraw,
};

/* The following functions are local. */
static int synth_leavesysex(mididev_info *md);

/*
 * Here are the main functions to interact to the midi sequencer.
 * These are called from the sequencer functions in sys/i386/isa/snd/sequencer.c.
 */

static int
synth_killnote(mididev_info *md, int chn, int note, int vel)
{
	int unit, msg, chp;
	synthdev_info *sd;
	u_char c[3];

	unit = md->unit;
	sd = &md->synth;

	if (note < 0 || note > 127 || chn < 0 || chn > 15)
		return (EINVAL);
	TYPEDRANGE(int, vel, 0, 127);
	if (synth_leavesysex(md) == EAGAIN)
		return (EAGAIN);

	msg = sd->prev_out_status & 0xf0;
	chp = sd->prev_out_status & 0x0f;

	if (chp == chn && ((msg == 0x90 && vel == 64) || msg == 0x80)) {
		/* Use running status. */
		c[0] = (u_char)note;
		if (msg == 0x90)
			/* The note was on. */
			c[1] = 0;
		else
			c[1] = (u_char)vel;

		if (synth_prefixcmd(md, c[0]))
			return (0);
		if (md->synth.writeraw(md, c, 2, 1) == EAGAIN)
			return (EAGAIN);
	} else {
		if (vel == 64) {
			c[0] = 0x90 | (chn & 0x0f); /* Note on. */
			c[1] = (u_char)note;
			c[2] = 0;
		} else {
			c[0] = 0x80 | (chn & 0x0f); /* Note off. */
			c[1] = (u_char)note;
			c[2] = (u_char)vel;
		}

		if (synth_prefixcmd(md, c[0]))
			return (0);
		if (md->synth.writeraw(md, c, 3, 1) == EAGAIN)
			return EAGAIN;
		/* Update the status. */
		sd->prev_out_status = c[0];
	}

	return (0);
}

static int
synth_setinstr(mididev_info *md, int chn, int instr)
{
	int unit;
	synthdev_info *sd;
	u_char c[2];

	unit = md->unit;
	sd = &md->synth;

	if (instr < 0 || instr > 127 || chn < 0 || chn > 15)
		return (EINVAL);

	if (synth_leavesysex(md) == EAGAIN)
		return (EAGAIN);

	c[0] = 0xc0 | (chn & 0x0f); /* Progamme change. */
	c[1] = (u_char)instr;
	if (md->synth.writeraw(md, c, 3, 1) == EAGAIN)
		return (EAGAIN);
	/* Update the status. */
	sd->prev_out_status = c[0];

	return (0);
}

static int
synth_startnote(mididev_info *md, int chn, int note, int vel)
{
	int unit, msg, chp;
	synthdev_info *sd;
	u_char c[3];

	unit = md->unit;
	sd = &md->synth;

	if (note < 0 || note > 127 || chn < 0 || chn > 15)
		return (EINVAL);
	TYPEDRANGE(int, vel, 0, 127);
	if (synth_leavesysex(md) == EAGAIN)
		return (EAGAIN);

	msg = sd->prev_out_status & 0xf0;
	chp = sd->prev_out_status & 0x0f;

	if (chp == chn && msg == 0x90) {
		/* Use running status. */
		c[0] = (u_char)note;
		c[1] = (u_char)vel;
		if (synth_prefixcmd(md, c[0]))
			return (0);
		if (md->synth.writeraw(md, c, 2, 1) == EAGAIN)
			return (EAGAIN);
	} else {
		c[0] = 0x90 | (chn & 0x0f); /* Note on. */
		c[1] = (u_char)note;
		c[2] = (u_char)vel;
		if (synth_prefixcmd(md, c[0]))
			return (0);
		if (md->synth.writeraw(md, c, 3, 1) == EAGAIN)
			return (EAGAIN);
		/* Update the status. */
		sd->prev_out_status = c[0];
	}

	return (0);
}

static int
synth_reset(mididev_info *md)
{
	synth_leavesysex(md);
	return (0);
}

static int
synth_hwcontrol(mididev_info *md, u_char *event)
{
	/* NOP. */
	return (0);
}

static int
synth_loadpatch(mididev_info *md, int format, struct uio *buf, int offs, int count, int pmgr_flag)
{
	struct sysex_info sysex;
	synthdev_info *sd;
	int unit, i, eox_seen, first_byte, left, src_offs, hdr_size;
	u_char c[count];

	unit = md->unit;
	sd = &md->synth;

	eox_seen = 0;
	first_byte = 1;
	hdr_size = offsetof(struct sysex_info, data);

	if (synth_leavesysex(md) == EAGAIN)
		return (EAGAIN);

	if (synth_prefixcmd(md, 0xf0))
		return (0);
	if (format != SYSEX_PATCH) {
		printf("synth_loadpatch: patch format 0x%x is invalid.\n", format);
		return (EINVAL);
	}
	if (count < hdr_size) {
		printf("synth_loadpatch: patch header is too short.\n");
		return (EINVAL);
	}
	count -= hdr_size;

	/* Copy the patch data. */
	if (uiomove((caddr_t)&((char *)&sysex)[offs], hdr_size - offs, buf))
		printf("synth_loadpatch: memory mangled?\n");

	if (count < sysex.len) {
		sysex.len = (long)count;
		printf("synth_loadpatch: sysex record of %d bytes is too long, adjusted to %d bytes.\n", (int)sysex.len, count);
	}
	left = sysex.len;
	src_offs = 0;

	for (i = 0 ; i < left ; i++) {
		uiomove((caddr_t)&c[i], 1, buf);
		eox_seen = i > 0 && (c[i] & 0x80) != 0;
		if (eox_seen && c[i] != 0xf7)
			c[i] = 0xf7;
		if (i == 0 && c[i] != 0x80) {
			printf("synth_loadpatch: sysex does not begin with the status.\n");
			return (EINVAL);
		}
		if (!first_byte && (c[i] & 0x80) != 0) {
			md->synth.writeraw(md, c, i + 1, 0);
			/* Update the status. */
			sd->prev_out_status = c[i];
			return (0);
		}
		first_byte = 0;
	}

	if (!eox_seen) {
		c[0] = 0xf7;
		md->synth.writeraw(md, c, 1, 0);
		sd->prev_out_status = c[0];
	}

	return (0);
}

static int
synth_panning(mididev_info *md, int chn, int pan)
{
	/* NOP. */
	return (0);
}

static int
synth_aftertouch(mididev_info *md, int chn, int press)
{
	int unit, msg, chp;
	synthdev_info *sd;
	u_char c[2];

	unit = md->unit;
	sd = &md->synth;

	if (press < 0 || press > 127 || chn < 0 || chn > 15)
		return (EINVAL);
	if (synth_leavesysex(md) == EAGAIN)
		return (EAGAIN);

	msg = sd->prev_out_status & 0xf0;
	chp = sd->prev_out_status & 0x0f;

	if (chp == chn && msg == 0xd0) {
		/* Use running status. */
		c[0] = (u_char)press;
		if (synth_prefixcmd(md, c[0]))
			return (0);
		if (md->synth.writeraw(md, c, 1, 1) == EAGAIN)
			return (EAGAIN);
	} else {
		c[0] = 0xd0 | (chn & 0x0f); /* Channel Pressure. */
		c[1] = (u_char)press;
		if (synth_prefixcmd(md, c[0]))
			return (0);
		if (md->synth.writeraw(md, c, 2, 1) == EAGAIN)
			return (EAGAIN);
		/* Update the status. */
		sd->prev_out_status = c[0];
	}

	return (0);
}

static int
synth_controller(mididev_info *md, int chn, int ctrlnum, int val)
{
	int unit, msg, chp;
	synthdev_info *sd;
	u_char c[3];

	unit = md->unit;
	sd = &md->synth;

	if (ctrlnum < 1 || ctrlnum > 127 || chn < 0 || chn > 15)
		return (EINVAL);
	if (synth_leavesysex(md) == EAGAIN)
		return (EAGAIN);

	msg = sd->prev_out_status & 0xf0;
	chp = sd->prev_out_status & 0x0f;

	if (chp == chn && msg == 0xb0) {
		/* Use running status. */
		c[0] = (u_char)ctrlnum;
		c[1] = (u_char)val & 0x7f;
		if (synth_prefixcmd(md, c[0]))
			return (0);
		if (md->synth.writeraw(md, c, 2, 1) == EAGAIN)
			return (EAGAIN);
	} else {
		c[0] = 0xb0 | (chn & 0x0f); /* Control Message. */
		c[1] = (u_char)ctrlnum;
		if (synth_prefixcmd(md, c[0]))
			return (0);
		if (md->synth.writeraw(md, c, 3, 1) == EAGAIN)
			return (EAGAIN);
		/* Update the status. */
		sd->prev_out_status = c[0];
	}

	return (0);
}

static int
synth_patchmgr(mididev_info *md, struct patmgr_info *rec)
{
	return (EINVAL);
}

static int
synth_bender(mididev_info *md, int chn, int val)
{
	int unit, msg, chp;
	synthdev_info *sd;
	u_char c[3];

	unit = md->unit;
	sd = &md->synth;

	if (val < 0 || val > 16383 || chn < 0 || chn > 15)
		return (EINVAL);
	if (synth_leavesysex(md) == EAGAIN)
		return (EAGAIN);

	msg = sd->prev_out_status & 0xf0;
	chp = sd->prev_out_status & 0x0f;

	if (chp == chn && msg == 0xe0) {
		/* Use running status. */
		c[0] = (u_char)val & 0x7f;
		c[1] = (u_char)(val >> 7) & 0x7f;
		if (synth_prefixcmd(md, c[0]))
			return (0);
		if (md->synth.writeraw(md, c, 2, 1) == EAGAIN)
			return (EAGAIN);
	} else {
		c[0] = 0xe0 | (chn & 0x0f); /* Pitch bend. */
		c[1] = (u_char)val & 0x7f;
		c[2] = (u_char)(val >> 7) & 0x7f;
		if (synth_prefixcmd(md, c[0]))
			return (0);
		if (md->synth.writeraw(md, c, 3, 1) == EAGAIN)
			return (EAGAIN);
		/* Update the status. */
		sd->prev_out_status = c[0];
	}

	return (0);
}

static int
synth_allocvoice(mididev_info *md, int chn, int note, struct voice_alloc_info *alloc)
{
	/* NOP. */
	return (0);
}

static int
synth_setupvoice(mididev_info *md, int voice, int chn)
{
	/* NOP. */
	return (0);
}

static int
synth_sendsysex(mididev_info *md, u_char *sysex, int len)
{
	int unit, i, j;
	synthdev_info *sd;
	u_char c[len];

	unit = md->unit;
	sd = &md->synth;

	for (i = 0 ; i < len ; i++) {
		switch (sysex[i]) {
		case 0xf0:
			/* Sysex begins. */
			if (synth_prefixcmd(md, 0xf0))
				return (0);
			sd->sysex_state = 1;
			break;
		case 0xf7:
			/* Sysex ends. */
			if (!sd->sysex_state)
				return (0);
			sd->sysex_state = 0;
			break;
		default:
			if (!sd->sysex_state)
				return (0);
			if ((sysex[i] & 0x80) != 0) {
				/* A status in a sysex? */
				sysex[i] = 0xf7;
				sd->sysex_state = 0;
			}
			break;
		}
		c[i] = sysex[i];
		if (!sd->sysex_state)
			break;
	}
	if (md->synth.writeraw(md, c, i, 1) == EAGAIN)
		return (EAGAIN);

	/* Update the status. */
	for (j = i - 1 ; j >= 0 ; j--)
		if ((c[j] & 0x80) != 0) {
			sd->prev_out_status = c[j];
			break;
		}

	return (0);
}

static int
synth_prefixcmd(mididev_info *md, int status)
{
	/* NOP. */
	return (0);
}

static int
synth_volumemethod(mididev_info *md, int mode)
{
	/* NOP. */
	return (0);
}

static int
synth_readraw(mididev_info *md, u_char *buf, int len, int nonblock)
{
	int unit, ret, s;

	if (md == NULL)
		return (ENXIO);

	unit = md->unit;
	if (unit >= nmidi + nsynth) {
		DEB(printf("synth_readraw: unit %d does not exist.\n", unit));
		return (ENXIO);
	}
	if ((md->fflags & FREAD) == 0) {
		DEB(printf("mpu_readraw: unit %d is not for reading.\n", unit));
		return (EIO);
	}

	s = splmidi();

	/* Begin recording. */
	md->callback(md, MIDI_CB_START | MIDI_CB_RD);

	if (nonblock) {
		/* Have we got enough data to read? */
		if (md->midi_dbuf_in.rl < len)
			return (EAGAIN);
	}

	ret = midibuf_seqread(&md->midi_dbuf_in, buf, len);

	splx(s);

	if (ret < 0)
		ret = -ret;
	else
		ret = 0;

	return (ret);
}

static int
synth_writeraw(mididev_info *md, u_char *buf, int len, int nonblock)
{
	int unit, ret, s;

	if (md == NULL)
		return (ENXIO);

	unit = md->unit;

	if (unit >= nmidi + nsynth) {
		DEB(printf("synth_writeraw: unit %d does not exist.\n", unit));
		return (ENXIO);
	}
	if ((md->fflags & FWRITE) == 0) {
		DEB(printf("synth_writeraw: unit %d is not for writing.\n", unit));
		return (EIO);
	}

	/* For nonblocking, have we got enough space to write? */
	if (nonblock && md->midi_dbuf_out.fl < len)
		return (EAGAIN);

	s = splmidi();

	ret = midibuf_seqwrite(&md->midi_dbuf_out, buf, len);
	if (ret < 0)
		ret = -ret;
	else
		ret = 0;

	/* Begin playing. */
	md->callback(md, MIDI_CB_START | MIDI_CB_WR);

	splx(s);

	return (ret);
}

/*
 * The functions below here are the libraries for the above ones.
 */

static int
synth_leavesysex(mididev_info *md)
{
	int unit;
	synthdev_info *sd;
	u_char c;

	unit = md->unit;
	sd = &md->synth;

	if (!sd->sysex_state)
		return (0);

	sd->sysex_state = 0;
	c = 0xf7;
	if (md->synth.writeraw(md, &c, sizeof(c), 1) == EAGAIN)
		return (EAGAIN);
	sd->sysex_state = 0;
	/* Update the status. */
	sd->prev_out_status = c;

	return (0);
}
OpenPOWER on IntegriCloud