Skip to content

Latest commit

 

History

History
379 lines (357 loc) · 9 KB

timidity.c

File metadata and controls

379 lines (357 loc) · 9 KB
 
Oct 21, 1999
Oct 21, 1999
1
2
3
4
/*
TiMidity -- Experimental MIDI to WAVE converter
Copyright (C) 1995 Tuukka Toivonen <toivonen@clinet.fi>
Dec 31, 2011
Dec 31, 2011
5
6
7
This program is free software; you can redistribute it and/or modify
it under the terms of the Perl Artistic License, available in COPYING.
*/
Oct 21, 1999
Oct 21, 1999
8
9
10
11
12
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Dec 21, 1999
Dec 21, 1999
13
#include "SDL.h"
Oct 21, 1999
Oct 21, 1999
14
15
16
17
18
19
#include "config.h"
#include "common.h"
#include "instrum.h"
#include "playmidi.h"
#include "readmidi.h"
#include "output.h"
May 14, 2006
May 14, 2006
20
#include "ctrlmode.h"
Oct 21, 1999
Oct 21, 1999
21
22
23
24
25
26
27
28
29
#include "timidity.h"
#include "tables.h"
void (*s32tobuf)(void *dp, int32 *lp, int32 c);
int free_instruments_afterwards=0;
static char def_instr_name[256]="";
int AUDIO_BUFFER_SIZE;
Jul 4, 2005
Jul 4, 2005
30
31
resample_t *resample_buffer=NULL;
int32 *common_buffer=NULL;
Aug 21, 2004
Aug 21, 2004
32
int num_ochannels;
Oct 21, 1999
Oct 21, 1999
33
34
35
#define MAXWORDS 10
Oct 4, 2009
Oct 4, 2009
36
static int read_config_file(const char *name)
Oct 21, 1999
Oct 21, 1999
37
38
{
FILE *fp;
Oct 12, 2009
Oct 12, 2009
39
char tmp[PATH_MAX], *w[MAXWORDS], *cp;
Oct 21, 1999
Oct 21, 1999
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
ToneBank *bank=0;
int i, j, k, line=0, words;
static int rcf_count=0;
if (rcf_count>50)
{
ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
"Probable source loop in configuration files");
return (-1);
}
if (!(fp=open_file(name, 1, OF_VERBOSE)))
return -1;
while (fgets(tmp, sizeof(tmp), fp))
Aug 21, 2004
Aug 21, 2004
55
56
{
line++;
Oct 21, 1999
Oct 21, 1999
57
58
w[words=0]=strtok(tmp, " \t\r\n\240");
if (!w[0] || (*w[0]=='#')) continue;
Oct 6, 2018
Oct 6, 2018
59
while (w[words] && (words < (MAXWORDS-1)))
Aug 21, 2004
Aug 21, 2004
60
61
62
63
64
65
66
67
68
{
w[++words]=strtok(0," \t\r\n\240");
if (w[words] && w[words][0]=='#') break;
}
if (!strcmp(w[0], "map")) continue;
if (!strcmp(w[0], "dir"))
{
if (words < 2)
{
Oct 21, 1999
Oct 21, 1999
69
70
ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
"%s: line %d: No directory given\n", name, line);
Oct 6, 2018
Oct 6, 2018
71
close_file(fp);
Oct 21, 1999
Oct 21, 1999
72
return -2;
Aug 21, 2004
Aug 21, 2004
73
74
75
76
77
}
for (i=1; i<words; i++)
add_to_pathlist(w[i]);
}
else if (!strcmp(w[0], "source"))
Oct 21, 1999
Oct 21, 1999
78
79
80
81
82
{
if (words < 2)
{
ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
"%s: line %d: No file name given\n", name, line);
Oct 6, 2018
Oct 6, 2018
83
close_file(fp);
Oct 21, 1999
Oct 21, 1999
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
return -2;
}
for (i=1; i<words; i++)
{
rcf_count++;
read_config_file(w[i]);
rcf_count--;
}
}
else if (!strcmp(w[0], "default"))
{
if (words != 2)
{
ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
"%s: line %d: Must specify exactly one patch name\n",
name, line);
Oct 6, 2018
Oct 6, 2018
100
close_file(fp);
Oct 21, 1999
Oct 21, 1999
101
102
103
104
105
106
107
108
109
110
111
112
return -2;
}
strncpy(def_instr_name, w[1], 255);
def_instr_name[255]='\0';
}
else if (!strcmp(w[0], "drumset"))
{
if (words < 2)
{
ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
"%s: line %d: No drum set number given\n",
name, line);
Oct 6, 2018
Oct 6, 2018
113
close_file(fp);
Oct 21, 1999
Oct 21, 1999
114
115
116
117
118
119
120
121
return -2;
}
i=atoi(w[1]);
if (i<0 || i>127)
{
ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
"%s: line %d: Drum set must be between 0 and 127\n",
name, line);
Oct 6, 2018
Oct 6, 2018
122
close_file(fp);
Oct 21, 1999
Oct 21, 1999
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
return -2;
}
if (!drumset[i])
{
drumset[i]=safe_malloc(sizeof(ToneBank));
memset(drumset[i], 0, sizeof(ToneBank));
}
bank=drumset[i];
}
else if (!strcmp(w[0], "bank"))
{
if (words < 2)
{
ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
"%s: line %d: No bank number given\n",
name, line);
Oct 6, 2018
Oct 6, 2018
139
close_file(fp);
Oct 21, 1999
Oct 21, 1999
140
141
142
143
144
145
146
147
return -2;
}
i=atoi(w[1]);
if (i<0 || i>127)
{
ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
"%s: line %d: Tone bank must be between 0 and 127\n",
name, line);
Oct 6, 2018
Oct 6, 2018
148
close_file(fp);
Oct 21, 1999
Oct 21, 1999
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
return -2;
}
if (!tonebank[i])
{
tonebank[i]=safe_malloc(sizeof(ToneBank));
memset(tonebank[i], 0, sizeof(ToneBank));
}
bank=tonebank[i];
}
else {
if ((words < 2) || (*w[0] < '0' || *w[0] > '9'))
{
ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
"%s: line %d: syntax error\n", name, line);
return -2;
}
i=atoi(w[0]);
if (i<0 || i>127)
{
ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
"%s: line %d: Program must be between 0 and 127\n",
name, line);
Oct 6, 2018
Oct 6, 2018
171
close_file(fp);
Oct 21, 1999
Oct 21, 1999
172
173
174
175
176
177
178
179
return -2;
}
if (!bank)
{
ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
"%s: line %d: Must specify tone bank or drum set "
"before assignment\n",
name, line);
Oct 6, 2018
Oct 6, 2018
180
close_file(fp);
Oct 21, 1999
Oct 21, 1999
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
return -2;
}
if (bank->tone[i].name)
free(bank->tone[i].name);
strcpy((bank->tone[i].name=safe_malloc(strlen(w[1])+1)),w[1]);
bank->tone[i].note=bank->tone[i].amp=bank->tone[i].pan=
bank->tone[i].strip_loop=bank->tone[i].strip_envelope=
bank->tone[i].strip_tail=-1;
for (j=2; j<words; j++)
{
if (!(cp=strchr(w[j], '=')))
{
ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "%s: line %d: bad patch option %s\n",
name, line, w[j]);
Oct 6, 2018
Oct 6, 2018
196
close_file(fp);
Oct 21, 1999
Oct 21, 1999
197
198
199
200
201
202
203
204
205
206
207
return -2;
}
*cp++=0;
if (!strcmp(w[j], "amp"))
{
k=atoi(cp);
if ((k<0 || k>MAX_AMPLIFICATION) || (*cp < '0' || *cp > '9'))
{
ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
"%s: line %d: amplification must be between "
"0 and %d\n", name, line, MAX_AMPLIFICATION);
Oct 6, 2018
Oct 6, 2018
208
close_file(fp);
Oct 21, 1999
Oct 21, 1999
209
210
211
212
213
214
215
216
217
218
219
220
return -2;
}
bank->tone[i].amp=k;
}
else if (!strcmp(w[j], "note"))
{
k=atoi(cp);
if ((k<0 || k>127) || (*cp < '0' || *cp > '9'))
{
ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
"%s: line %d: note must be between 0 and 127\n",
name, line);
Oct 6, 2018
Oct 6, 2018
221
close_file(fp);
Oct 21, 1999
Oct 21, 1999
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
return -2;
}
bank->tone[i].note=k;
}
else if (!strcmp(w[j], "pan"))
{
if (!strcmp(cp, "center"))
k=64;
else if (!strcmp(cp, "left"))
k=0;
else if (!strcmp(cp, "right"))
k=127;
else
k=((atoi(cp)+100) * 100) / 157;
if ((k<0 || k>127) ||
(k==0 && *cp!='-' && (*cp < '0' || *cp > '9')))
{
ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
"%s: line %d: panning must be left, right, "
"center, or between -100 and 100\n",
name, line);
Oct 6, 2018
Oct 6, 2018
243
close_file(fp);
Oct 21, 1999
Oct 21, 1999
244
245
246
247
248
249
250
251
252
253
254
255
256
257
return -2;
}
bank->tone[i].pan=k;
}
else if (!strcmp(w[j], "keep"))
{
if (!strcmp(cp, "env"))
bank->tone[i].strip_envelope=0;
else if (!strcmp(cp, "loop"))
bank->tone[i].strip_loop=0;
else
{
ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
"%s: line %d: keep must be env or loop\n", name, line);
Oct 6, 2018
Oct 6, 2018
258
close_file(fp);
Oct 21, 1999
Oct 21, 1999
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
return -2;
}
}
else if (!strcmp(w[j], "strip"))
{
if (!strcmp(cp, "env"))
bank->tone[i].strip_envelope=1;
else if (!strcmp(cp, "loop"))
bank->tone[i].strip_loop=1;
else if (!strcmp(cp, "tail"))
bank->tone[i].strip_tail=1;
else
{
ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
"%s: line %d: strip must be env, loop, or tail\n",
name, line);
Oct 6, 2018
Oct 6, 2018
275
close_file(fp);
Oct 21, 1999
Oct 21, 1999
276
277
278
279
280
281
282
return -2;
}
}
else
{
ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "%s: line %d: bad patch option %s\n",
name, line, w[j]);
Oct 6, 2018
Oct 6, 2018
283
close_file(fp);
Oct 21, 1999
Oct 21, 1999
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
return -2;
}
}
}
}
if (ferror(fp))
{
ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "Can't read from %s\n", name);
close_file(fp);
return -2;
}
close_file(fp);
return 0;
}
int Timidity_Init(int rate, int format, int channels, int samples)
{
Oct 3, 2009
Oct 3, 2009
301
302
303
304
const char *env = getenv("TIMIDITY_CFG");
if (!env || read_config_file(env)<0) {
if (read_config_file(CONFIG_FILE)<0) {
if (read_config_file(CONFIG_FILE_ETC)<0) {
Oct 10, 2009
Oct 10, 2009
305
return(-1);
Jul 13, 2005
Jul 13, 2005
306
}
Jul 4, 2005
Jul 4, 2005
307
}
Oct 21, 1999
Oct 21, 1999
308
309
}
Aug 21, 2004
Aug 21, 2004
310
311
312
313
if (channels < 1 || channels == 3 || channels == 5 || channels > 6) return(-1);
num_ochannels = channels;
Oct 21, 1999
Oct 21, 1999
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
/* Set play mode parameters */
play_mode->rate = rate;
play_mode->encoding = 0;
if ( (format&0xFF) == 16 ) {
play_mode->encoding |= PE_16BIT;
}
if ( (format&0x8000) ) {
play_mode->encoding |= PE_SIGNED;
}
if ( channels == 1 ) {
play_mode->encoding |= PE_MONO;
}
switch (format) {
case AUDIO_S8:
s32tobuf = s32tos8;
break;
case AUDIO_U8:
s32tobuf = s32tou8;
break;
case AUDIO_S16LSB:
s32tobuf = s32tos16l;
break;
case AUDIO_S16MSB:
s32tobuf = s32tos16b;
break;
case AUDIO_U16LSB:
s32tobuf = s32tou16l;
break;
case AUDIO_U16MSB:
s32tobuf = s32tou16b;
break;
default:
ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "Unsupported audio format");
return(-1);
}
AUDIO_BUFFER_SIZE = samples;
/* Allocate memory for mixing (WARNING: Memory leak!) */
Aug 21, 2004
Aug 21, 2004
352
353
resample_buffer = safe_malloc(AUDIO_BUFFER_SIZE*sizeof(resample_t)+100);
common_buffer = safe_malloc(AUDIO_BUFFER_SIZE*num_ochannels*sizeof(int32));
Oct 21, 1999
Oct 21, 1999
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
init_tables();
if (ctl->open(0, 0)) {
ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "Couldn't open %s\n", ctl->id_name);
return(-1);
}
if (!control_ratio) {
control_ratio = play_mode->rate / CONTROLS_PER_SECOND;
if(control_ratio<1)
control_ratio=1;
else if (control_ratio > MAX_CONTROL_RATIO)
control_ratio=MAX_CONTROL_RATIO;
}
if (*def_instr_name)
set_default_instrument(def_instr_name);
return(0);
}
Oct 12, 2009
Oct 12, 2009
374
char timidity_error[TIMIDITY_ERROR_SIZE] = "";
Oct 4, 2009
Oct 4, 2009
375
const char *Timidity_Error(void)
Oct 21, 1999
Oct 21, 1999
376
377
378
{
return(timidity_error);
}