Skip to content

Latest commit

 

History

History
604 lines (528 loc) · 15.2 KB

instrum.c

File metadata and controls

604 lines (528 loc) · 15.2 KB
 
Oct 21, 1999
Oct 21, 1999
1
/*
Oct 18, 2017
Oct 18, 2017
2
Oct 21, 1999
Oct 21, 1999
3
4
5
6
TiMidity -- Experimental MIDI to WAVE converter
Copyright (C) 1995 Tuukka Toivonen <toivonen@clinet.fi>
This program is free software; you can redistribute it and/or modify
Dec 31, 2011
Dec 31, 2011
7
it under the terms of the Perl Artistic License, available in COPYING.
Oct 18, 2017
Oct 18, 2017
8
Dec 16, 2019
Dec 16, 2019
9
10
instrum.c
Oct 18, 2017
Oct 18, 2017
11
12
13
14
15
16
17
Code to load and unload GUS-compatible instrument patches.
*/
#if HAVE_CONFIG_H
# include <config.h>
#endif
Oct 21, 1999
Oct 21, 1999
18
19
20
21
22
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
Oct 18, 2017
Oct 18, 2017
23
24
25
26
#include "SDL.h"
#include "timidity.h"
#include "options.h"
Oct 21, 1999
Oct 21, 1999
27
28
29
30
#include "common.h"
#include "instrum.h"
#include "resample.h"
#include "tables.h"
Aug 21, 2004
Aug 21, 2004
31
Oct 21, 1999
Oct 21, 1999
32
33
34
35
36
37
38
39
static void free_instrument(Instrument *ip)
{
Sample *sp;
int i;
if (!ip) return;
for (i=0; i<ip->samples; i++)
{
sp=&(ip->sample[i]);
Oct 18, 2017
Oct 18, 2017
40
free(sp->data);
Oct 21, 1999
Oct 21, 1999
41
42
43
44
45
}
free(ip->sample);
free(ip);
}
Oct 18, 2017
Oct 18, 2017
46
static void free_bank(MidiSong *song, int dr, int b)
Oct 21, 1999
Oct 21, 1999
47
48
{
int i;
Oct 18, 2017
Oct 18, 2017
49
50
51
ToneBank *bank=((dr) ? song->drumset[b] : song->tonebank[b]);
for (i=0; i<MAXBANK; i++)
if (bank->instrument[i])
Aug 21, 2004
Aug 21, 2004
52
{
Oct 18, 2017
Oct 18, 2017
53
54
55
56
/* Not that this could ever happen, of course */
if (bank->instrument[i] != MAGIC_LOAD_INSTRUMENT)
free_instrument(bank->instrument[i]);
bank->instrument[i]=0;
Aug 21, 2004
Aug 21, 2004
57
58
59
}
}
Oct 18, 2017
Oct 18, 2017
60
static Sint32 convert_envelope_rate(MidiSong *song, Uint8 rate)
Aug 21, 2004
Aug 21, 2004
61
{
Oct 18, 2017
Oct 18, 2017
62
Sint32 r;
Dec 16, 2019
Dec 16, 2019
63
Oct 18, 2017
Oct 18, 2017
64
65
66
r = 3 - ((rate >> 6) & 0x3);
r *= 3;
r = (Sint32) (rate & 0x3f) << r; /* 6.9 fixed point */
Aug 21, 2004
Aug 21, 2004
67
68
/* 15.15 fixed point. */
Oct 18, 2017
Oct 18, 2017
69
r = ((r * 44100) / song->rate) * song->control_ratio;
Aug 21, 2004
Aug 21, 2004
70
Oct 18, 2017
Oct 18, 2017
71
72
73
74
75
#ifdef FAST_DECAY
return r << 10;
#else
return r << 9;
#endif
Oct 21, 1999
Oct 21, 1999
76
77
}
Oct 18, 2017
Oct 18, 2017
78
static Sint32 convert_envelope_offset(Uint8 offset)
Oct 21, 1999
Oct 21, 1999
79
80
81
82
83
84
85
86
{
/* This is not too good... Can anyone tell me what these values mean?
Are they GUS-style "exponential" volumes? And what does that mean? */
/* 15.15 fixed point */
return offset << (7+15);
}
Oct 18, 2017
Oct 18, 2017
87
static Sint32 convert_tremolo_sweep(MidiSong *song, Uint8 sweep)
Oct 21, 1999
Oct 21, 1999
88
89
90
91
92
{
if (!sweep)
return 0;
return
Oct 18, 2017
Oct 18, 2017
93
94
((song->control_ratio * SWEEP_TUNING) << SWEEP_SHIFT) /
(song->rate * sweep);
Oct 21, 1999
Oct 21, 1999
95
96
}
Oct 18, 2017
Oct 18, 2017
97
98
static Sint32 convert_vibrato_sweep(MidiSong *song, Uint8 sweep,
Sint32 vib_control_ratio)
Oct 21, 1999
Oct 21, 1999
99
100
101
102
103
{
if (!sweep)
return 0;
return
Oct 18, 2017
Oct 18, 2017
104
105
(Sint32) (FSCALE((double) (vib_control_ratio) * SWEEP_TUNING, SWEEP_SHIFT)
/ (double)(song->rate * sweep));
Oct 21, 1999
Oct 21, 1999
106
107
108
109
/* this was overflowing with seashore.pat
((vib_control_ratio * SWEEP_TUNING) << SWEEP_SHIFT) /
Oct 18, 2017
Oct 18, 2017
110
(song->rate * sweep); */
Oct 21, 1999
Oct 21, 1999
111
112
}
Oct 18, 2017
Oct 18, 2017
113
static Sint32 convert_tremolo_rate(MidiSong *song, Uint8 rate)
Oct 21, 1999
Oct 21, 1999
114
115
{
return
Oct 18, 2017
Oct 18, 2017
116
117
((SINE_CYCLE_LENGTH * song->control_ratio * rate) << RATE_SHIFT) /
(TREMOLO_RATE_TUNING * song->rate);
Oct 21, 1999
Oct 21, 1999
118
119
}
Oct 18, 2017
Oct 18, 2017
120
static Sint32 convert_vibrato_rate(MidiSong *song, Uint8 rate)
Oct 21, 1999
Oct 21, 1999
121
122
123
{
/* Return a suitable vibrato_control_ratio value */
return
Oct 18, 2017
Oct 18, 2017
124
(VIBRATO_RATE_TUNING * song->rate) /
Oct 21, 1999
Oct 21, 1999
125
126
127
(rate * 2 * VIBRATO_SAMPLE_INCREMENTS);
}
Oct 18, 2017
Oct 18, 2017
128
static void reverse_data(Sint16 *sp, Sint32 ls, Sint32 le)
Oct 21, 1999
Oct 21, 1999
129
{
Oct 18, 2017
Oct 18, 2017
130
Sint16 s, *ep=sp+le;
Oct 21, 1999
Oct 21, 1999
131
132
133
134
135
136
137
138
139
140
141
sp+=ls;
le-=ls;
le/=2;
while (le--)
{
s=*sp;
*sp++=*ep;
*ep--=s;
}
}
Dec 16, 2019
Dec 16, 2019
142
/*
Oct 21, 1999
Oct 21, 1999
143
144
145
146
If panning or note_to_use != -1, it will be used for all samples,
instead of the sample-specific values in the instrument file.
For note_to_use, any value <0 or >127 will be forced to 0.
Dec 16, 2019
Dec 16, 2019
147
Oct 21, 1999
Oct 21, 1999
148
149
150
151
For other parameters, 1 means yes, 0 means no, other values are
undefined.
TODO: do reverse loops right */
Oct 18, 2017
Oct 18, 2017
152
153
static Instrument *load_instrument(MidiSong *song, char *name, int percussion,
int panning, int amp, int note_to_use,
Oct 21, 1999
Oct 21, 1999
154
int strip_loop, int strip_envelope,
Oct 18, 2017
Oct 18, 2017
155
int strip_tail)
Oct 21, 1999
Oct 21, 1999
156
157
{
Instrument *ip;
Oct 18, 2017
Oct 18, 2017
158
159
160
Sample *sp;
SDL_RWops *rw;
char tmp[1024];
Dec 16, 2019
Dec 16, 2019
161
int i,j;
Oct 21, 1999
Oct 21, 1999
162
163
static char *patch_ext[] = PATCH_EXT_LIST;
Dec 16, 2019
Dec 16, 2019
164
(void)percussion; /* unused */
Oct 21, 1999
Oct 21, 1999
165
if (!name) return 0;
Dec 16, 2019
Dec 16, 2019
166
Oct 21, 1999
Oct 21, 1999
167
/* Open patch file */
Oct 18, 2017
Oct 18, 2017
168
if ((rw=open_file(name)) == NULL)
Oct 21, 1999
Oct 21, 1999
169
170
171
172
{
/* Try with various extensions */
for (i=0; patch_ext[i]; i++)
{
Oct 18, 2017
Oct 18, 2017
173
if (strlen(name)+strlen(patch_ext[i])<1024)
Oct 21, 1999
Oct 21, 1999
174
{
Oct 18, 2017
Oct 18, 2017
175
176
177
strcpy(tmp, name);
strcat(tmp, patch_ext[i]);
if ((rw=open_file(tmp)) != NULL)
Oct 21, 1999
Oct 21, 1999
178
179
180
181
break;
}
}
}
Dec 16, 2019
Dec 16, 2019
182
183
if (rw == NULL)
Oct 21, 1999
Oct 21, 1999
184
{
Oct 18, 2017
Oct 18, 2017
185
SNDDBG(("Instrument `%s' can't be found.\n", name));
Oct 21, 1999
Oct 21, 1999
186
187
return 0;
}
Dec 16, 2019
Dec 16, 2019
188
Oct 18, 2017
Oct 18, 2017
189
SNDDBG(("Loading instrument %s\n", tmp));
Dec 16, 2019
Dec 16, 2019
190
Oct 21, 1999
Oct 21, 1999
191
192
193
/* Read some headers and do cursory sanity checks. There are loads
of magic offsets. This could be rewritten... */
Oct 18, 2017
Oct 18, 2017
194
if ((239 != SDL_RWread(rw, tmp, 1, 239)) ||
Oct 21, 1999
Oct 21, 1999
195
196
197
198
(memcmp(tmp, "GF1PATCH110\0ID#000002", 22) &&
memcmp(tmp, "GF1PATCH100\0ID#000002", 22))) /* don't know what the
differences are */
{
Oct 18, 2017
Oct 18, 2017
199
SNDDBG(("%s: not an instrument\n", name));
Oct 21, 2017
Oct 21, 2017
200
SDL_RWclose(rw);
Oct 21, 1999
Oct 21, 1999
201
202
return 0;
}
Dec 16, 2019
Dec 16, 2019
203
204
if (tmp[82] != 1 && tmp[82] != 0) /* instruments. To some patch makers,
Oct 21, 1999
Oct 21, 1999
205
206
0 means 1 */
{
Oct 18, 2017
Oct 18, 2017
207
SNDDBG(("Can't handle patches with %d instruments\n", tmp[82]));
Oct 21, 2017
Oct 21, 2017
208
SDL_RWclose(rw);
Oct 21, 1999
Oct 21, 1999
209
210
211
212
213
return 0;
}
if (tmp[151] != 1 && tmp[151] != 0) /* layers. What's a layer? */
{
Oct 18, 2017
Oct 18, 2017
214
SNDDBG(("Can't handle instruments with %d layers\n", tmp[151]));
Oct 21, 2017
Oct 21, 2017
215
SDL_RWclose(rw);
Oct 21, 1999
Oct 21, 1999
216
217
return 0;
}
Dec 16, 2019
Dec 16, 2019
218
Oct 18, 2017
Oct 18, 2017
219
220
221
222
ip=safe_malloc(sizeof(Instrument));
ip->samples = tmp[198];
ip->sample = safe_malloc(sizeof(Sample) * ip->samples);
for (i=0; i<ip->samples; i++)
Oct 21, 1999
Oct 21, 1999
223
{
Oct 18, 2017
Oct 18, 2017
224
225
226
227
Uint8 fractions;
Sint32 tmplong;
Uint16 tmpshort;
Uint8 tmpchar;
Oct 21, 1999
Oct 21, 1999
228
229
#define READ_CHAR(thing) \
Oct 18, 2017
Oct 18, 2017
230
if (1 != SDL_RWread(rw, &tmpchar, 1, 1)) goto fail; \
Oct 21, 1999
Oct 21, 1999
231
232
thing = tmpchar;
#define READ_SHORT(thing) \
Oct 18, 2017
Oct 18, 2017
233
234
if (1 != SDL_RWread(rw, &tmpshort, 2, 1)) goto fail; \
thing = SDL_SwapLE16(tmpshort);
Oct 21, 1999
Oct 21, 1999
235
#define READ_LONG(thing) \
Oct 18, 2017
Oct 18, 2017
236
if (1 != SDL_RWread(rw, &tmplong, 4, 1)) goto fail; \
Dec 16, 2019
Dec 16, 2019
237
thing = (Sint32)SDL_SwapLE32((Uint32)tmplong);
Oct 21, 1999
Oct 21, 1999
238
Oct 18, 2017
Oct 18, 2017
239
240
241
SDL_RWseek(rw, 7, RW_SEEK_CUR); /* Skip the wave name */
if (1 != SDL_RWread(rw, &fractions, 1, 1))
Oct 21, 1999
Oct 21, 1999
242
243
{
fail:
Oct 18, 2017
Oct 18, 2017
244
SNDDBG(("Error reading sample %d\n", i));
Oct 21, 1999
Oct 21, 1999
245
for (j=0; j<i; j++)
Oct 18, 2017
Oct 18, 2017
246
247
free(ip->sample[j].data);
free(ip->sample);
Oct 21, 1999
Oct 21, 1999
248
free(ip);
Oct 21, 2017
Oct 21, 2017
249
SDL_RWclose(rw);
Oct 21, 1999
Oct 21, 1999
250
251
252
return 0;
}
Oct 18, 2017
Oct 18, 2017
253
sp=&(ip->sample[i]);
Dec 16, 2019
Dec 16, 2019
254
Oct 21, 1999
Oct 21, 1999
255
256
257
258
259
260
261
READ_LONG(sp->data_length);
READ_LONG(sp->loop_start);
READ_LONG(sp->loop_end);
READ_SHORT(sp->sample_rate);
READ_LONG(sp->low_freq);
READ_LONG(sp->high_freq);
READ_LONG(sp->root_freq);
Oct 18, 2017
Oct 18, 2017
262
263
SDL_RWseek(rw, 2, RW_SEEK_CUR); /* Why have a "root frequency" and then
* "tuning"?? */
Dec 16, 2019
Dec 16, 2019
264
Oct 21, 1999
Oct 21, 1999
265
266
267
268
269
READ_CHAR(tmp[0]);
if (panning==-1)
sp->panning = (tmp[0] * 8 + 4) & 0x7f;
else
Oct 18, 2017
Oct 18, 2017
270
sp->panning=(Uint8)(panning & 0x7F);
Aug 21, 2004
Aug 21, 2004
271
Oct 21, 1999
Oct 21, 1999
272
/* envelope, tremolo, and vibrato */
Oct 18, 2017
Oct 18, 2017
273
if (18 != SDL_RWread(rw, tmp, 1, 18)) goto fail;
Oct 21, 1999
Oct 21, 1999
274
275
276
277
278
if (!tmp[13] || !tmp[14])
{
sp->tremolo_sweep_increment=
sp->tremolo_phase_increment=sp->tremolo_depth=0;
Oct 18, 2017
Oct 18, 2017
279
SNDDBG((" * no tremolo\n"));
Oct 21, 1999
Oct 21, 1999
280
281
282
}
else
{
Oct 18, 2017
Oct 18, 2017
283
284
sp->tremolo_sweep_increment=convert_tremolo_sweep(song, tmp[12]);
sp->tremolo_phase_increment=convert_tremolo_rate(song, tmp[13]);
Oct 21, 1999
Oct 21, 1999
285
sp->tremolo_depth=tmp[14];
Oct 18, 2017
Oct 18, 2017
286
SNDDBG((" * tremolo: sweep %d, phase %d, depth %d\n",
Oct 21, 1999
Oct 21, 1999
287
sp->tremolo_sweep_increment, sp->tremolo_phase_increment,
Oct 18, 2017
Oct 18, 2017
288
sp->tremolo_depth));
Oct 21, 1999
Oct 21, 1999
289
290
291
292
293
294
}
if (!tmp[16] || !tmp[17])
{
sp->vibrato_sweep_increment=
sp->vibrato_control_ratio=sp->vibrato_depth=0;
Oct 18, 2017
Oct 18, 2017
295
SNDDBG((" * no vibrato\n"));
Oct 21, 1999
Oct 21, 1999
296
297
298
}
else
{
Oct 18, 2017
Oct 18, 2017
299
sp->vibrato_control_ratio=convert_vibrato_rate(song, tmp[16]);
Oct 21, 1999
Oct 21, 1999
300
sp->vibrato_sweep_increment=
Oct 18, 2017
Oct 18, 2017
301
convert_vibrato_sweep(song, tmp[15], sp->vibrato_control_ratio);
Oct 21, 1999
Oct 21, 1999
302
sp->vibrato_depth=tmp[17];
Oct 18, 2017
Oct 18, 2017
303
SNDDBG((" * vibrato: sweep %d, ctl %d, depth %d\n",
Oct 21, 1999
Oct 21, 1999
304
sp->vibrato_sweep_increment, sp->vibrato_control_ratio,
Oct 18, 2017
Oct 18, 2017
305
sp->vibrato_depth));
Oct 21, 1999
Oct 21, 1999
306
307
308
309
}
READ_CHAR(sp->modes);
Oct 18, 2017
Oct 18, 2017
310
311
SDL_RWseek(rw, 40, RW_SEEK_CUR); /* skip the useless scale frequency, scale
Dec 16, 2019
Dec 16, 2019
312
313
factor (what's it mean?), and reserved
space */
Oct 21, 1999
Oct 21, 1999
314
315
316
/* Mark this as a fixed-pitch instrument if such a deed is desired. */
if (note_to_use!=-1)
Oct 18, 2017
Oct 18, 2017
317
sp->note_to_use=(Uint8)(note_to_use);
Oct 21, 1999
Oct 21, 1999
318
319
else
sp->note_to_use=0;
Dec 16, 2019
Dec 16, 2019
320
Oct 21, 1999
Oct 21, 1999
321
322
323
324
325
326
327
328
329
330
331
332
/* seashore.pat in the Midia patch set has no Sustain. I don't
understand why, and fixing it by adding the Sustain flag to
all looped patches probably breaks something else. We do it
anyway. */
if (sp->modes & MODES_LOOPING)
sp->modes |= MODES_SUSTAIN;
/* Strip any loops and envelopes we're permitted to */
if ((strip_loop==1) &&
(sp->modes & (MODES_SUSTAIN | MODES_LOOPING |
MODES_PINGPONG | MODES_REVERSE)))
{
Oct 18, 2017
Oct 18, 2017
333
SNDDBG((" - Removing loop and/or sustain\n"));
Oct 21, 1999
Oct 21, 1999
334
335
336
337
338
339
sp->modes &=~(MODES_SUSTAIN | MODES_LOOPING |
MODES_PINGPONG | MODES_REVERSE);
}
if (strip_envelope==1)
{
Oct 21, 2017
Oct 21, 2017
340
if (sp->modes & MODES_ENVELOPE) {
Oct 18, 2017
Oct 18, 2017
341
SNDDBG((" - Removing envelope\n"));
Oct 21, 2017
Oct 21, 2017
342
}
Oct 21, 1999
Oct 21, 1999
343
344
345
346
347
348
349
350
351
352
sp->modes &= ~MODES_ENVELOPE;
}
else if (strip_envelope != 0)
{
/* Have to make a guess. */
if (!(sp->modes & (MODES_LOOPING | MODES_PINGPONG | MODES_REVERSE)))
{
/* No loop? Then what's there to sustain? No envelope needed
either... */
sp->modes &= ~(MODES_SUSTAIN|MODES_ENVELOPE);
Oct 18, 2017
Oct 18, 2017
353
SNDDBG((" - No loop, removing sustain and envelope\n"));
Oct 21, 1999
Oct 21, 1999
354
355
356
357
358
359
}
else if (!memcmp(tmp, "??????", 6) || tmp[11] >= 100)
{
/* Envelope rates all maxed out? Envelope end at a high "offset"?
That's a weird envelope. Take it out. */
sp->modes &= ~MODES_ENVELOPE;
Oct 18, 2017
Oct 18, 2017
360
SNDDBG((" - Weirdness, removing envelope\n"));
Oct 21, 1999
Oct 21, 1999
361
362
363
364
365
366
367
368
}
else if (!(sp->modes & MODES_SUSTAIN))
{
/* No sustain? Then no envelope. I don't know if this is
justified, but patches without sustain usually don't need the
envelope either... at least the Gravis ones. They're mostly
drums. I think. */
sp->modes &= ~MODES_ENVELOPE;
Oct 18, 2017
Oct 18, 2017
369
SNDDBG((" - No sustain, removing envelope\n"));
Oct 21, 1999
Oct 21, 1999
370
371
372
}
}
Oct 18, 2017
Oct 18, 2017
373
for (j=0; j<6; j++)
Oct 21, 1999
Oct 21, 1999
374
375
{
sp->envelope_rate[j]=
Oct 18, 2017
Oct 18, 2017
376
convert_envelope_rate(song, tmp[j]);
Oct 21, 1999
Oct 21, 1999
377
378
379
380
381
sp->envelope_offset[j]=
convert_envelope_offset(tmp[6+j]);
}
/* Then read the sample data */
Oct 21, 2017
Oct 21, 2017
382
sp->data = (sample_t *) safe_malloc(sp->data_length+4);
Oct 18, 2017
Oct 18, 2017
383
if (1 != SDL_RWread(rw, sp->data, sp->data_length, 1))
Oct 21, 1999
Oct 21, 1999
384
goto fail;
Dec 16, 2019
Dec 16, 2019
385
Oct 21, 1999
Oct 21, 1999
386
387
if (!(sp->modes & MODES_16BIT)) /* convert to 16-bit data */
{
Oct 21, 2017
Oct 21, 2017
388
Sint32 k=sp->data_length;
Oct 18, 2017
Oct 18, 2017
389
Uint8 *cp=(Uint8 *)(sp->data);
Oct 21, 2017
Oct 21, 2017
390
Uint16 *tmp16,*new16;
Oct 21, 1999
Oct 21, 1999
391
392
393
sp->data_length *= 2;
sp->loop_start *= 2;
sp->loop_end *= 2;
Oct 21, 2017
Oct 21, 2017
394
395
396
397
398
tmp16 = new16 = (Uint16 *) safe_malloc(sp->data_length+4);
while (k--)
*tmp16++ = (Uint16)(*cp++) << 8;
free(sp->data);
sp->data = (sample_t *)new16;
Oct 21, 1999
Oct 21, 1999
399
}
Apr 30, 2006
Apr 30, 2006
400
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
Oct 21, 1999
Oct 21, 1999
401
402
403
else
/* convert to machine byte order */
{
Oct 21, 2017
Oct 21, 2017
404
405
406
407
408
409
Sint32 k=sp->data_length/2;
Sint16 *tmp16=(Sint16 *)sp->data,s;
while (k--)
{
s=SDL_SwapLE16(*tmp16);
*tmp16++=s;
Oct 21, 1999
Oct 21, 1999
410
411
412
}
}
#endif
Dec 16, 2019
Dec 16, 2019
413
Oct 21, 1999
Oct 21, 1999
414
415
if (sp->modes & MODES_UNSIGNED) /* convert to signed data */
{
Oct 21, 2017
Oct 21, 2017
416
417
418
419
Sint32 k=sp->data_length/2;
Sint16 *tmp16=(Sint16 *)sp->data;
while (k--)
*tmp16++ ^= 0x8000;
Oct 21, 1999
Oct 21, 1999
420
421
422
423
424
}
/* Reverse reverse loops and pass them off as normal loops */
if (sp->modes & MODES_REVERSE)
{
Oct 18, 2017
Oct 18, 2017
425
Sint32 t;
Oct 21, 1999
Oct 21, 1999
426
427
428
/* The GUS apparently plays reverse loops by reversing the
whole sample. We do the same because the GUS does not SUCK. */
Oct 18, 2017
Oct 18, 2017
429
430
SNDDBG(("Reverse loop in %s\n", name));
reverse_data((Sint16 *)sp->data, 0, sp->data_length/2);
Oct 21, 1999
Oct 21, 1999
431
432
433
434
435
436
437
438
439
440
441
t=sp->loop_start;
sp->loop_start=sp->data_length - sp->loop_end;
sp->loop_end=sp->data_length - t;
sp->modes &= ~MODES_REVERSE;
sp->modes |= MODES_LOOPING; /* just in case */
}
#ifdef ADJUST_SAMPLE_VOLUMES
if (amp!=-1)
Oct 18, 2017
Oct 18, 2017
442
sp->volume=(float)((amp) / 100.0);
Oct 21, 1999
Oct 21, 1999
443
444
445
446
447
else
{
/* Try to determine a volume scaling factor for the sample.
This is a very crude adjustment, but things sound more
balanced with it. Still, this should be a runtime option. */
Oct 21, 2017
Oct 21, 2017
448
Sint32 k=sp->data_length/2;
Oct 18, 2017
Oct 18, 2017
449
Sint16 maxamp=0,a;
Oct 21, 2017
Oct 21, 2017
450
451
Sint16 *tmp16=(Sint16 *)sp->data;
while (k--)
Oct 21, 1999
Oct 21, 1999
452
{
Oct 21, 2017
Oct 21, 2017
453
a=*tmp16++;
Oct 21, 1999
Oct 21, 1999
454
455
456
457
if (a<0) a=-a;
if (a>maxamp)
maxamp=a;
}
Oct 18, 2017
Oct 18, 2017
458
459
sp->volume=(float)(32768.0 / maxamp);
SNDDBG((" * volume comp: %f\n", sp->volume));
Oct 21, 1999
Oct 21, 1999
460
461
462
463
464
465
466
467
468
469
470
471
}
#else
if (amp!=-1)
sp->volume=(double)(amp) / 100.0;
else
sp->volume=1.0;
#endif
sp->data_length /= 2; /* These are in bytes. Convert into samples. */
sp->loop_start /= 2;
sp->loop_end /= 2;
Oct 21, 2017
Oct 21, 2017
472
473
474
475
/* initialize the added extra sample space (see the +4 bytes in
allocation) using the last actual sample: */
sp->data[sp->data_length] = sp->data[sp->data_length+1] = 0;
Oct 21, 1999
Oct 21, 1999
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
/* Then fractional samples */
sp->data_length <<= FRACTION_BITS;
sp->loop_start <<= FRACTION_BITS;
sp->loop_end <<= FRACTION_BITS;
/* Adjust for fractional loop points. This is a guess. Does anyone
know what "fractions" really stands for? */
sp->loop_start |=
(fractions & 0x0F) << (FRACTION_BITS-4);
sp->loop_end |=
((fractions>>4) & 0x0F) << (FRACTION_BITS-4);
/* If this instrument will always be played on the same note,
and it's not looped, we can resample it now. */
if (sp->note_to_use && !(sp->modes & MODES_LOOPING))
Oct 18, 2017
Oct 18, 2017
491
pre_resample(song, sp);
Oct 21, 1999
Oct 21, 1999
492
493
494
495
if (strip_tail==1)
{
/* Let's not really, just say we did. */
Oct 18, 2017
Oct 18, 2017
496
SNDDBG((" - Stripping tail\n"));
Oct 21, 1999
Oct 21, 1999
497
498
sp->data_length = sp->loop_end;
}
Oct 18, 2017
Oct 18, 2017
499
}
Oct 21, 1999
Oct 21, 1999
500
Oct 18, 2017
Oct 18, 2017
501
502
SDL_RWclose(rw);
return ip;
Oct 21, 1999
Oct 21, 1999
503
504
}
Oct 18, 2017
Oct 18, 2017
505
static int fill_bank(MidiSong *song, int dr, int b)
Oct 21, 1999
Oct 21, 1999
506
507
{
int i, errors=0;
Oct 18, 2017
Oct 18, 2017
508
ToneBank *bank=((dr) ? song->drumset[b] : song->tonebank[b]);
Oct 21, 1999
Oct 21, 1999
509
510
if (!bank)
{
Oct 18, 2017
Oct 18, 2017
511
512
SNDDBG(("Huh. Tried to load instruments in non-existent %s %d\n",
(dr) ? "drumset" : "tone bank", b));
Oct 21, 1999
Oct 21, 1999
513
514
return 0;
}
Oct 18, 2017
Oct 18, 2017
515
for (i=0; i<MAXBANK; i++)
Oct 21, 1999
Oct 21, 1999
516
{
Oct 18, 2017
Oct 18, 2017
517
if (bank->instrument[i]==MAGIC_LOAD_INSTRUMENT)
Oct 21, 1999
Oct 21, 1999
518
519
520
{
if (!(bank->tone[i].name))
{
Oct 18, 2017
Oct 18, 2017
521
SNDDBG(("No instrument mapped to %s %d, program %d%s\n",
Oct 21, 1999
Oct 21, 1999
522
(dr)? "drum set" : "tone bank", b, i,
Oct 18, 2017
Oct 18, 2017
523
(b!=0) ? "" : " - this instrument will not be heard"));
Oct 21, 1999
Oct 21, 1999
524
525
526
527
528
529
if (b!=0)
{
/* Mark the corresponding instrument in the default
bank / drumset for loading (if it isn't already) */
if (!dr)
{
Oct 18, 2017
Oct 18, 2017
530
531
if (!(song->tonebank[0]->instrument[i]))
song->tonebank[0]->instrument[i] =
Oct 21, 1999
Oct 21, 1999
532
533
534
535
MAGIC_LOAD_INSTRUMENT;
}
else
{
Oct 18, 2017
Oct 18, 2017
536
537
if (!(song->drumset[0]->instrument[i]))
song->drumset[0]->instrument[i] =
Oct 21, 1999
Oct 21, 1999
538
539
540
MAGIC_LOAD_INSTRUMENT;
}
}
Oct 18, 2017
Oct 18, 2017
541
bank->instrument[i] = 0;
Oct 21, 1999
Oct 21, 1999
542
543
errors++;
}
Oct 18, 2017
Oct 18, 2017
544
545
546
else if (!(bank->instrument[i] =
load_instrument(song,
bank->tone[i].name,
Oct 21, 1999
Oct 21, 1999
547
548
549
550
(dr) ? 1 : 0,
bank->tone[i].pan,
bank->tone[i].amp,
(bank->tone[i].note!=-1) ?
Oct 18, 2017
Oct 18, 2017
551
552
bank->tone[i].note :
((dr) ? i : -1),
Oct 21, 1999
Oct 21, 1999
553
554
555
556
557
558
(bank->tone[i].strip_loop!=-1) ?
bank->tone[i].strip_loop :
((dr) ? 1 : -1),
(bank->tone[i].strip_envelope != -1) ?
bank->tone[i].strip_envelope :
((dr) ? 1 : -1),
Oct 18, 2017
Oct 18, 2017
559
bank->tone[i].strip_tail )))
Oct 21, 1999
Oct 21, 1999
560
{
Oct 18, 2017
Oct 18, 2017
561
SNDDBG(("Couldn't load instrument %s (%s %d, program %d)\n",
Oct 21, 1999
Oct 21, 1999
562
bank->tone[i].name,
Oct 18, 2017
Oct 18, 2017
563
(dr)? "drum set" : "tone bank", b, i));
Oct 21, 1999
Oct 21, 1999
564
565
566
567
568
569
570
errors++;
}
}
}
return errors;
}
Oct 18, 2017
Oct 18, 2017
571
int load_missing_instruments(MidiSong *song)
Oct 21, 1999
Oct 21, 1999
572
{
Aug 21, 2004
Aug 21, 2004
573
int i=MAXBANK,errors=0;
Oct 21, 1999
Oct 21, 1999
574
575
while (i--)
{
Oct 18, 2017
Oct 18, 2017
576
577
578
579
if (song->tonebank[i])
errors+=fill_bank(song,0,i);
if (song->drumset[i])
errors+=fill_bank(song,1,i);
Oct 21, 1999
Oct 21, 1999
580
581
582
583
}
return errors;
}
Oct 18, 2017
Oct 18, 2017
584
void free_instruments(MidiSong *song)
Oct 21, 1999
Oct 21, 1999
585
{
Oct 18, 2017
Oct 18, 2017
586
int i=MAXBANK;
Oct 21, 1999
Oct 21, 1999
587
588
while(i--)
{
Oct 18, 2017
Oct 18, 2017
589
590
591
592
if (song->tonebank[i])
free_bank(song, 0, i);
if (song->drumset[i])
free_bank(song, 1, i);
Oct 21, 1999
Oct 21, 1999
593
594
595
}
}
Oct 18, 2017
Oct 18, 2017
596
int set_default_instrument(MidiSong *song, char *name)
Oct 21, 1999
Oct 21, 1999
597
{
Oct 18, 2017
Oct 18, 2017
598
599
Instrument *ip;
if (!(ip=load_instrument(song, name, 0, -1, -1, -1, 0, 0, 0)))
Oct 21, 1999
Oct 21, 1999
600
return -1;
Oct 18, 2017
Oct 18, 2017
601
602
song->default_instrument = ip;
song->default_program = SPECIAL_PROGRAM;
Oct 21, 1999
Oct 21, 1999
603
604
return 0;
}