File: setfx.c

package info (click to toggle)
awesfx 0.5.2-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 772 kB
  • sloc: ansic: 6,532; sh: 56; makefile: 47
file content (447 lines) | stat: -rw-r--r-- 11,597 bytes parent folder | download | duplicates (4)
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
/*================================================================
 * setfx -- load user defined chorus / reverb mode effects
 *
 * Copyright (C) 1996-1999 Takashi Iwai
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *================================================================*/

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#ifdef __FreeBSD__
#  include <machine/soundcard.h>
#elif defined(linux)
#  include <linux/soundcard.h>
#endif
#include <awe_voice.h>
#include "util.h"
#include "seq.h"
#include "aweseq.h"
#include "awe_version.h"

/*----------------------------------------------------------------*/

static void usage(void);
static int my_getline(FILE *fp);
static int nextline(FILE *fp);
static char *gettok(FILE *fp);
static char *divtok(char *src, char *divs, int only_one);
static int htoi(char *p);

static void read_chorus(int mode, char *name, int incl, FILE *fp);
static void read_reverb(int mode, char *name, int incl, FILE *fp);

/*----------------------------------------------------------------*/

static char chorus_defined[AWE_CHORUS_NUMBERS] = {1,1,1,1,1,1,1,1,};
static char reverb_defined[AWE_CHORUS_NUMBERS] = {1,1,1,1,1,1,1,1,};

char *progname;
int verbose = 0;

/* file buffer */
static int curline;
static char line[1024];
static int connected;

/*----------------------------------------------------------------*/

static void usage(void)
{
	fprintf(stderr, "setfx -- load user defined chorus / reverb mode effects\n");
	fprintf(stderr, VERSION_NOTE);
	fprintf(stderr, "usage:	setfx config-file\n");
#ifdef DEFAULT_SF_PATH
	fprintf(stderr, "   system default path is %s\n", DEFAULT_SF_PATH);
#endif
	exit(1);
}

int main(int argc, char **argv)
{
	char *default_sf_path;
	char sfname[500];
	FILE *fp;
	int c;
	char *seq_devname = NULL;
	int seq_devidx = -1;

	/* set program name */
	progname = strrchr(argv[0], '/');
	if (progname == NULL)
		progname = argv[0];
	else
		progname++;

	while ((c = getopt(argc, argv, "F:D:")) != -1) {
		switch (c) {
		case 'F':
			seq_devname = optarg;
			break;
		case 'D':
			seq_devidx = atoi(optarg);
			break;
		default:
			usage();
			exit(1);
		}
	}

	if (optind >= argc) {
		usage();
		return 1;
	}

	/* search the config file */
	default_sf_path = getenv("SFBANKDIR");
#ifdef DEFAULT_SF_PATH
	if (default_sf_path == NULL || *default_sf_path == 0)
		default_sf_path = safe_strdup(DEFAULT_SF_PATH);
#endif

	if (! awe_search_file_name(sfname, sizeof(sfname), argv[optind], default_sf_path, NULL)) {
		fprintf(stderr, "%s: can't find such a file %s\n",
			progname, argv[optind]);
		return 1;
	}

	if ((fp = fopen(sfname, "r")) == NULL) {
		fprintf(stderr, "%s: can't open %s\n", progname, sfname);
		return 1;
	}

	curline = 0;
	if (!my_getline(fp))
		return 0;

	seq_init(seq_devname, seq_devidx);

	do {
		int chorus, mode, incl;
		char *tok, *name;

		/* chorus/reverb header */
		tok = divtok(line, ":", TRUE);
		if (*tok == 'c' || *tok == 'C')
			chorus = 1;
		else if (*tok == 'r' || *tok == 'R')
			chorus = 0;
		else
			continue;

		/* mode index */
		tok = divtok(NULL, ":", TRUE);
		if (tok == NULL || *tok == 0) {
			fprintf(stderr, "%s: illegal line %d\n",
				progname, curline);
			continue;
		}
		mode = atoi(tok);
		if (mode < 8 || mode >= 32) {
			fprintf(stderr, "%s: illegal mode %d in line %d\n",
				progname, mode, curline);
			continue;
		}

		/* name of the mode */
		name = divtok(NULL, ":", TRUE);
		if (name == NULL || *name == 0) {
			fprintf(stderr, "%s: illegal line %d\n",
				progname, curline);
			continue;
		}

		/* include mode index */
		tok = divtok(NULL, ":", TRUE);
		if (tok == NULL) {
			fprintf(stderr, "%s: illegal line %d\n",
				progname, curline);
			continue;
		}
		if (*tok) {
			incl = atoi(tok);
			if (incl < 0 || incl >= 31 ||
			    (chorus && !chorus_defined[incl]) ||
			    (!chorus && !reverb_defined[incl])) {
				fprintf(stderr, "%s: illegal include %d in line %d\n",
					progname, incl, curline);
				continue;
			}
		} else
			incl = -1;

		/* parse parameters */
		if (chorus)
			read_chorus(mode, name, incl, fp);
		else
			read_reverb(mode, name, incl, fp);

	} while (nextline(fp));

	fclose(fp);
	seq_end();
	return 0;
}


/*----------------------------------------------------------------
 * pre-defined chorus and reverb modes
 *----------------------------------------------------------------*/

static awe_chorus_fx_rec chorus_parm[AWE_CHORUS_NUMBERS] = {
	{0xE600, 0x03F6, 0xBC2C ,0x00000000, 0x0000006D}, /* chorus 1 */
	{0xE608, 0x031A, 0xBC6E, 0x00000000, 0x0000017C}, /* chorus 2 */
	{0xE610, 0x031A, 0xBC84, 0x00000000, 0x00000083}, /* chorus 3 */
	{0xE620, 0x0269, 0xBC6E, 0x00000000, 0x0000017C}, /* chorus 4 */
	{0xE680, 0x04D3, 0xBCA6, 0x00000000, 0x0000005B}, /* feedback */
	{0xE6E0, 0x044E, 0xBC37, 0x00000000, 0x00000026}, /* flanger */
	{0xE600, 0x0B06, 0xBC00, 0x0000E000, 0x00000083}, /* short delay */
	{0xE6C0, 0x0B06, 0xBC00, 0x0000E000, 0x00000083}, /* short delay + feedback */
};

static awe_reverb_fx_rec reverb_parm[AWE_REVERB_NUMBERS] = {
{{  /* room 1 */
	0xB488, 0xA450, 0x9550, 0x84B5, 0x383A, 0x3EB5, 0x72F4,
	0x72A4, 0x7254, 0x7204, 0x7204, 0x7204, 0x4416, 0x4516,
	0xA490, 0xA590, 0x842A, 0x852A, 0x842A, 0x852A, 0x8429,
	0x8529, 0x8429, 0x8529, 0x8428, 0x8528, 0x8428, 0x8528,
}},
{{  /* room 2 */
	0xB488, 0xA458, 0x9558, 0x84B5, 0x383A, 0x3EB5, 0x7284,
	0x7254, 0x7224, 0x7224, 0x7254, 0x7284, 0x4448, 0x4548,
	0xA440, 0xA540, 0x842A, 0x852A, 0x842A, 0x852A, 0x8429,
	0x8529, 0x8429, 0x8529, 0x8428, 0x8528, 0x8428, 0x8528,
}},
{{  /* room 3 */
	0xB488, 0xA460, 0x9560, 0x84B5, 0x383A, 0x3EB5, 0x7284,
	0x7254, 0x7224, 0x7224, 0x7254, 0x7284, 0x4416, 0x4516,
	0xA490, 0xA590, 0x842C, 0x852C, 0x842C, 0x852C, 0x842B,
	0x852B, 0x842B, 0x852B, 0x842A, 0x852A, 0x842A, 0x852A,
}},
{{  /* hall 1 */
	0xB488, 0xA470, 0x9570, 0x84B5, 0x383A, 0x3EB5, 0x7284,
	0x7254, 0x7224, 0x7224, 0x7254, 0x7284, 0x4448, 0x4548,
	0xA440, 0xA540, 0x842B, 0x852B, 0x842B, 0x852B, 0x842A,
	0x852A, 0x842A, 0x852A, 0x8429, 0x8529, 0x8429, 0x8529,
}},
{{  /* hall 2 */
	0xB488, 0xA470, 0x9570, 0x84B5, 0x383A, 0x3EB5, 0x7254,
	0x7234, 0x7224, 0x7254, 0x7264, 0x7294, 0x44C3, 0x45C3,
	0xA404, 0xA504, 0x842A, 0x852A, 0x842A, 0x852A, 0x8429,
	0x8529, 0x8429, 0x8529, 0x8428, 0x8528, 0x8428, 0x8528,
}},
{{  /* plate */
	0xB4FF, 0xA470, 0x9570, 0x84B5, 0x383A, 0x3EB5, 0x7234,
	0x7234, 0x7234, 0x7234, 0x7234, 0x7234, 0x4448, 0x4548,
	0xA440, 0xA540, 0x842A, 0x852A, 0x842A, 0x852A, 0x8429,
	0x8529, 0x8429, 0x8529, 0x8428, 0x8528, 0x8428, 0x8528,
}},
{{  /* delay */
	0xB4FF, 0xA470, 0x9500, 0x84B5, 0x333A, 0x39B5, 0x7204,
	0x7204, 0x7204, 0x7204, 0x7204, 0x72F4, 0x4400, 0x4500,
	0xA4FF, 0xA5FF, 0x8420, 0x8520, 0x8420, 0x8520, 0x8420,
	0x8520, 0x8420, 0x8520, 0x8420, 0x8520, 0x8420, 0x8520,
}},
{{  /* panning delay */
	0xB4FF, 0xA490, 0x9590, 0x8474, 0x333A, 0x39B5, 0x7204,
	0x7204, 0x7204, 0x7204, 0x7204, 0x72F4, 0x4400, 0x4500,
	0xA4FF, 0xA5FF, 0x8420, 0x8520, 0x8420, 0x8520, 0x8420,
	0x8520, 0x8420, 0x8520, 0x8420, 0x8520, 0x8420, 0x8520,
}},
};


/*----------------------------------------------------------------
 * read reverb definition
 *----------------------------------------------------------------*/

static void read_reverb(int mode, char *name, int incl, FILE *fp)
{
	char *p;
	int i, val;
	struct reverb_pat {
		awe_patch_info patch;
		awe_reverb_fx_rec v;
	} r;

	if (incl >= 0) {
		/* include from pre-defined mode */
		reverb_parm[mode] = reverb_parm[incl];
		while ((p = gettok(fp)) != NULL) {
			int src;
			char *q;
			for (q = p; *q; q++) {
				if (*q == '=') {
					*q++ = 0;
					break;
				}
			}
			if (!*q) {
				fprintf(stderr, "%s: illegal reverb definition: %s\n",
					progname, name);
				return;
			}
			src = atoi(p);
			val = htoi(q);
			if (src < 0 || src >= 28) {
				fprintf(stderr, "%s: illegal reverb parm: %s\n",
					progname, name);
				return;
			}
			reverb_parm[mode].parms[src] = val;
		}
	} else {
		/* define all 28 parameters */
		for (i = 0; i < 28; i++) {
			if ((p = gettok(fp)) == NULL) {
				fprintf(stderr, "%s: too short reverb definition: %s\n",
					progname, name);
				return;
			}
			val = htoi(p);
			reverb_parm[mode].parms[i] = val;
		}
	}
	r.patch.optarg = mode;
	r.patch.len = sizeof(awe_reverb_fx_rec);
	r.patch.type = AWE_LOAD_REVERB_FX;
	r.v = reverb_parm[mode];
	seq_load_patch(&r, sizeof(r));

	reverb_defined[mode] = 1;
}

/*----------------------------------------------------------------
 * read chorus definition
 *----------------------------------------------------------------*/

static void read_chorus(int mode, char *name, int incl, FILE *fp)
{
	char *p;
	int i, val;
	struct chorus_pat {
		awe_patch_info patch;
		awe_chorus_fx_rec v;
	} c;

	/* define all five parameters */
	for (i = 0; i < 5; i++) {
		if ((p = gettok(fp)) == NULL) {
			fprintf(stderr, "%s: illegal chorus definition: %s\n",
				progname, name);
			return;
		}
		val = htoi(p);
		switch (i) {
		case 0: chorus_parm[mode].feedback = val; break;
		case 1: chorus_parm[mode].delay_offset = val; break;
		case 2: chorus_parm[mode].lfo_depth = val; break;
		case 3: chorus_parm[mode].delay = val; break;
		case 4: chorus_parm[mode].lfo_freq = val; break;
		}
	}

	c.patch.optarg = mode;
	c.patch.len = sizeof(awe_chorus_fx_rec);
	c.patch.type = AWE_LOAD_CHORUS_FX;
	c.v = chorus_parm[mode];
	seq_load_patch(&c, sizeof(c));

	chorus_defined[mode] = 1;
}


/*----------------------------------------------------------------
 * read a line and parse tokens
 *----------------------------------------------------------------*/

static int my_getline(FILE *fp)
{
	char *p;
	curline++;
	connected = FALSE;
	if (fgets(line, sizeof(line), fp) == NULL)
		return FALSE;
	/* strip the linefeed and backslash at the tail */
	for (p = line; *p && *p != '\n'; p++) {
		if (*p == '\\' && p[1] == '\n') {
			*p = 0;
			connected = TRUE;
			break;
		}
	}
	*p = 0;
	return TRUE;
}

static int nextline(FILE *fp)
{
	if (connected) {
		do {
			if (! my_getline(fp))
				return FALSE;
		} while (connected);
		return TRUE;
	} else {
		return my_getline(fp);
	}
}

/* hex to integer */
static int htoi(char *p)
{
	return strtol(p, NULL, 16);
}

/* get a token separated by spaces */
static char *gettok(FILE *fp)
{
	char *tok;
	tok = divtok(NULL, " \t\r\n", FALSE);
	while (tok == NULL || *tok == 0) {
		if (! connected) return NULL;
		if (! my_getline(fp)) return NULL;
		tok = divtok(line, " \t\r\n", FALSE);
	}
	return tok;
}

/* divide a token with specified terminators;
 * this behaves just like strtok.  if only_one is TRUE, divtok checks only
 * one letter as a divider. */
static char *divtok(char *src, char *divs, int only_one)
{
	static char *lastp = NULL;
	char *tok;
	if (src)
		lastp = src;
	if (!only_one) {
		for (; *lastp && strchr(divs, *lastp); lastp++)
			;
	}
	tok = lastp;
	for (; *lastp && !strchr(divs, *lastp); lastp++)
		;
	
	if (lastp)
		*lastp++ = 0;
	return tok;
}