File: conv.c

package info (click to toggle)
midish 1.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,344 kB
  • sloc: ansic: 22,502; sh: 268; makefile: 119
file content (404 lines) | stat: -rw-r--r-- 9,931 bytes parent folder | download
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
/*
 * Copyright (c) 2003-2010 Alexandre Ratchov <alex@caoua.org>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

/*
 * This module provides to midish events that are self-contained, and
 * that do not depend on any context. Standard MIDI events/messages
 * aren't context free: for instance the meaning of a "data entry"
 * controller depends of the last NRPN/RPN controller; dealing with
 * contexts would overcomplicate most midish code (filters, tracks),
 * that's why we define those context-free events: XCTL, NRPN, RPN,
 * and XPC.
 *
 * This module make convertions between context-free events (XPC,
 * XCTL, RPN, NRPN) and basic events (PC, CTL). For instatance a bank
 * controller followed by a prog change will be converted to a
 * "extended" prog change (XPC) that contains also the bank number.
 * In order to generate XPCs whose context is the current bank number,
 * we keep the bank number into a statelist. Similarly the current
 * NRPN and RPN numbers are kept.
 *
 * To keep the state, we use statelist_xxx() functions. However since
 * we store only controllers, we roll simplified lookup() and update()
 * functions.
 */

#include "utils.h"
#include "state.h"
#include "conv.h"

#define CHAN_MATCH(e1, e2) \
	((e1)->ch == (e2)->ch && (e1)->dev == (e2)->dev)

#define CTL_MATCH(e1, e2) \
	((e1)->ctl_num == (e2)->ctl_num && CHAN_MATCH((e1), (e2)))

/*
 * create a state for the given controller event. If there is already
 * one, then update it.
 */
void
conv_setctl(struct statelist *slist, struct ev *ev)
{
	struct state *i;

	for (i = slist->first; i != NULL; i = i->next) {
		if (CTL_MATCH(&i->ev, ev)) {
			i->ev.ctl_val = ev->ctl_val;
			return;
		}
	}
	i = state_new();
	statelist_add(slist, i);
	i->ev = *ev;
}

/*
 * return the state (ie the value) of the given controller number with
 * the same channel/device as the given event. If there is no state
 * recorded, then return EV_UNDEF
 */
unsigned
conv_getctl(struct statelist *slist, struct ev *ev, unsigned num)
{
	struct state *i;

	for (i = slist->first; i != NULL; i = i->next) {
		if (i->ev.ctl_num == num && CHAN_MATCH(&i->ev, ev)) {
			return i->ev.ctl_val;
		}
	}
	return EV_UNDEF;
}

/*
 * delete the state of the given controller number with the
 * same channel/device as the given event.
 */
void
conv_rmctl(struct statelist *slist, struct ev *ev, unsigned num)
{
	struct state *i;

	for (i = slist->first; i != NULL; i = i->next) {
		if (i->ev.ctl_num == num && CHAN_MATCH(&i->ev, ev)) {
			statelist_rm(slist, i);
			state_del(i);
			break;
		}
	}
}

/*
 * return the 14bit value of a pair of (high, low) controllers with
 * the same device/channel as the given event. If the state of of one
 * of the high or low controllers is missing, then EV_UNDEF is
 * returned.
 */
unsigned
conv_getctx(struct statelist *slist, struct ev *ev, unsigned hi, unsigned lo)
{
	unsigned vhi, vlo;

	vlo = conv_getctl(slist, ev, lo);
	if (vlo == EV_UNDEF) {
		return EV_UNDEF;
	}
	vhi = conv_getctl(slist, ev, hi);
	if (vhi == EV_UNDEF) {
		return EV_UNDEF;
	}
	return vlo + (vhi << 7);
}

/*
 * convert an old-style event (ie CTL, PC) to a context-free event (ie
 * XCTL, NRPN, RPN, XPC). If an event is available, 'rev' parameter is
 * filled and 1 is returned.
 */
unsigned
conv_packev(struct statelist *l, unsigned xctlset, unsigned flags,
	    struct ev *ev, struct ev *rev)
{
	unsigned num, val;

	if (ev->cmd == EV_PC) {
		rev->cmd = EV_XPC;
		rev->dev = ev->dev;
		rev->ch = ev->ch;
		rev->pc_prog = ev->v0;
		rev->pc_bank = (flags & CONV_XPC) ?
		    conv_getctx(l, ev, BANK_HI, BANK_LO) : 0;
		return 1;
	} else if (ev->cmd == EV_CTL) {
		switch (ev->ctl_num) {
		case BANK_HI:
			if (!(flags & CONV_XPC))
				break;
			conv_rmctl(l, ev, BANK_LO);
			conv_setctl(l, ev);
			return 0;
		case RPN_HI:
			if (!(flags & CONV_XPC))
				break;
			conv_rmctl(l, ev, NRPN_LO);
			conv_rmctl(l, ev, RPN_LO);
			conv_setctl(l, ev);
			return 0;
		case NRPN_HI:
			if (!(flags & CONV_NRPN))
				break;
			conv_rmctl(l, ev, RPN_LO);
			conv_rmctl(l, ev, NRPN_LO);
			conv_setctl(l, ev);
			return 0;
		case DATAENT_HI:
			if (!(flags & (CONV_RPN | CONV_NRPN)))
				break;
			conv_rmctl(l, ev, DATAENT_LO);
			conv_setctl(l, ev);
			return 0;
		case BANK_LO:
			if (!(flags & CONV_XPC))
				break;
			conv_setctl(l, ev);
			return 0;
		case NRPN_LO:
			if (!(flags & CONV_NRPN))
				break;
			conv_rmctl(l, ev, RPN_LO);
			conv_setctl(l, ev);
			return 0;
		case RPN_LO:
			if (!(flags & CONV_RPN))
				break;
			conv_rmctl(l, ev, NRPN_LO);
			conv_setctl(l, ev);
			return 0;
		case DATAENT_LO:
			if (!(flags & (CONV_RPN | CONV_NRPN)))
				break;
			num = conv_getctx(l, ev, NRPN_HI, NRPN_LO);
			if (num != EV_UNDEF) {
				rev->cmd = EV_NRPN;
			} else {
				num = conv_getctx(l, ev,
				    RPN_HI, NRPN_LO);
				if (num == EV_UNDEF)
					return 0;
				rev->cmd = EV_RPN;
			}
			val = conv_getctl(l, ev, DATAENT_HI);
			if (val == EV_UNDEF)
				return 0;
			rev->dev = ev->dev;
			rev->ch = ev->ch;
			rev->ctl_num = num;
			rev->ctl_val = ev->ctl_val + (val << 7);
			return 1;
		}
		if (ev->ctl_num < 32) {
			if (EVCTL_ISFINE(xctlset, ev->ctl_num)) {
				conv_setctl(l, ev);
				return 0;
			}
		} else if (ev->ctl_num < 64) {
			num = ev->ctl_num - 32;
			if (EVCTL_ISFINE(xctlset, num)) {
				val = conv_getctl(l, ev, num);
				if (val == EV_UNDEF)
					return 0;
				rev->ctl_num = num;
				rev->ctl_val = ev->ctl_val + (val << 7);
				goto done;
			}
		}
		rev->ctl_num = ev->ctl_num;
		rev->ctl_val = ev->ctl_val << 7;
	done:
		rev->cmd = EV_XCTL;
		rev->dev = ev->dev;
		rev->ch = ev->ch;
		return 1;
	} else {
		*rev = *ev;
		return 1;
	}
}

/*
 * convert a context-free event (XCTL, RPN, NRPN, XPC) to an array of
 * old-style events (CTL, PC). Renturn the number of events filled in
 * the array.
 */
unsigned
conv_unpackev(struct statelist *slist, unsigned xctlset, unsigned flags,
	      struct ev *ev, struct ev *rev)
{
	unsigned val, hi;
	unsigned nev = 0;

	if (ev->cmd == EV_XCTL) {
		switch (ev->ctl_num) {
		case BANK_HI:
		case BANK_LO:
			if (flags & CONV_XPC)
				return 0;
			break;
		case NRPN_HI:
		case NRPN_LO:
			if (flags & CONV_NRPN)
				return 0;
			break;
		case RPN_HI:
		case RPN_LO:
			if (flags & CONV_RPN)
				return 0;
			break;
		case DATAENT_HI:
		case DATAENT_LO:
			if (flags & (CONV_NRPN | CONV_RPN))
				return 0;
			break;
		}
		if (ev->ctl_num < 32 && EVCTL_ISFINE(xctlset, ev->ctl_num)) {
			hi = ev->ctl_val >> 7;
			val = conv_getctl(slist, ev, ev->ctl_num);
			if (val != hi || val == EV_UNDEF) {
				rev->cmd = EV_CTL;
				rev->dev = ev->dev;
				rev->ch = ev->ch;
				rev->ctl_num = ev->ctl_num;
				rev->ctl_val = hi;
				conv_setctl(slist, rev);
				rev++;
				nev++;
			}
			rev->cmd = EV_CTL;
			rev->dev = ev->dev;
			rev->ch = ev->ch;
			rev->ctl_num = ev->ctl_num + 32;
			rev->ctl_val = ev->ctl_val & 0x7f;
			rev++;
			nev++;
			return nev;
		} else {
			rev->cmd = EV_CTL;
			rev->dev = ev->dev;
			rev->ch = ev->ch;
			rev->ctl_num = ev->ctl_num;
			rev->ctl_val = ev->ctl_val >> 7;
			return 1;
		}
	} else if (ev->cmd == EV_XPC) {
		if (flags & CONV_XPC) {
			val = conv_getctx(slist, ev, BANK_HI, BANK_LO);
			if (val != ev->pc_bank && ev->pc_bank != EV_UNDEF) {
				rev->cmd = EV_CTL;
				rev->dev = ev->dev;
				rev->ch = ev->ch;
				rev->ctl_num = BANK_HI;
				rev->ctl_val = ev->pc_bank >> 7;
				conv_setctl(slist, rev);
				rev++;
				nev++;
				rev->cmd = EV_CTL;
				rev->dev = ev->dev;
				rev->ch = ev->ch;
				rev->ctl_num = BANK_LO;
				rev->ctl_val = ev->pc_bank & 0x7f;
				conv_setctl(slist, rev);
				rev++;
				nev++;
			}
		}
		rev->cmd = EV_PC;
		rev->dev = ev->dev;
		rev->ch = ev->ch;
		rev->v0 = ev->pc_prog;
		rev++;
		nev++;
		return nev;
	} else if (ev->cmd == EV_NRPN) {
		if (!(flags & CONV_NRPN))
			return 0;
		val = conv_getctx(slist, ev, NRPN_HI, NRPN_LO);
		if (val != ev->rpn_num) {
			conv_rmctl(slist, ev, RPN_HI);
			conv_rmctl(slist, ev, RPN_LO);
			rev->cmd = EV_CTL;
			rev->dev = ev->dev;
			rev->ch = ev->ch;
			rev->ctl_num = NRPN_HI;
			rev->ctl_val = ev->rpn_num >> 7;
			conv_setctl(slist, rev);
			rev++;
			nev++;
			rev->cmd = EV_CTL;
			rev->dev = ev->dev;
			rev->ch = ev->ch;
			rev->ctl_num = NRPN_LO;
			rev->ctl_val = ev->rpn_num & 0x7f;
			conv_setctl(slist, rev);
			rev++;
			nev++;
		}
	dataentry:
		rev->cmd = EV_CTL;
		rev->dev = ev->dev;
		rev->ch = ev->ch;
		rev->ctl_num = DATAENT_HI;
		rev->ctl_val = ev->rpn_val >> 7;
		rev++;
		nev++;
		rev->cmd = EV_CTL;
		rev->dev = ev->dev;
		rev->ch = ev->ch;
		rev->ctl_num = DATAENT_LO;
		rev->ctl_val = ev->rpn_val & 0x7f;
		rev++;
		nev++;
		return nev;
	} else if (ev->cmd == EV_RPN) {
		if (!(flags & CONV_RPN))
			return 0;
		val = conv_getctx(slist, ev, RPN_HI, RPN_LO);
		if (val != ev->rpn_num) {
			conv_rmctl(slist, ev, NRPN_HI);
			conv_rmctl(slist, ev, NRPN_LO);
			rev->cmd = EV_CTL;
			rev->dev = ev->dev;
			rev->ch = ev->ch;
			rev->ctl_num = RPN_HI;
			rev->ctl_val = ev->rpn_num >> 7;
			conv_setctl(slist, rev);
			rev++;
			nev++;
			rev->cmd = EV_CTL;
			rev->dev = ev->dev;
			rev->ch = ev->ch;
			rev->ctl_num = RPN_LO;
			rev->ctl_val = ev->rpn_num & 0x7f;
			conv_setctl(slist, rev);
			rev++;
			nev++;
		}
		goto dataentry;
	} else {
		*rev = *ev;
		return 1;
	}
}