Skip to content

Latest commit

 

History

History
800 lines (689 loc) · 21 KB

playmidi.c

File metadata and controls

800 lines (689 loc) · 21 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
9
10
11
12
13
14
15
playmidi.c -- random stuff in need of rearrangement
*/
#if HAVE_CONFIG_H
# include <config.h>
#endif
Oct 21, 1999
Oct 21, 1999
16
17
18
19
20
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Oct 18, 2017
Oct 18, 2017
21
#include "SDL.h"
Jul 4, 2005
Jul 4, 2005
22
Oct 18, 2017
Oct 18, 2017
23
24
#include "timidity.h"
#include "options.h"
Oct 21, 1999
Oct 21, 1999
25
26
27
28
29
30
#include "instrum.h"
#include "playmidi.h"
#include "output.h"
#include "mix.h"
#include "tables.h"
Oct 18, 2017
Oct 18, 2017
31
static void adjust_amplification(MidiSong *song)
Oct 21, 1999
Oct 21, 1999
32
{
Oct 18, 2017
Oct 18, 2017
33
song->master_volume = (float)(song->amplification) / (float)100.0;
Aug 21, 2004
Aug 21, 2004
34
35
}
Oct 18, 2017
Oct 18, 2017
36
static void reset_voices(MidiSong *song)
Oct 21, 1999
Oct 21, 1999
37
38
39
{
int i;
for (i=0; i<MAX_VOICES; i++)
Oct 18, 2017
Oct 18, 2017
40
song->voice[i].status=VOICE_FREE;
Oct 21, 1999
Oct 21, 1999
41
42
43
}
/* Process the Reset All Controllers event */
Oct 18, 2017
Oct 18, 2017
44
static void reset_controllers(MidiSong *song, int c)
Oct 21, 1999
Oct 21, 1999
45
{
Oct 18, 2017
Oct 18, 2017
46
47
48
49
50
song->channel[c].volume=90; /* Some standard says, although the SCC docs say 0. */
song->channel[c].expression=127; /* SCC-1 does this. */
song->channel[c].sustain=0;
song->channel[c].pitchbend=0x2000;
song->channel[c].pitchfactor=0; /* to be computed */
Oct 21, 1999
Oct 21, 1999
51
52
}
Oct 18, 2017
Oct 18, 2017
53
static void reset_midi(MidiSong *song)
Oct 21, 1999
Oct 21, 1999
54
55
{
int i;
Aug 21, 2004
Aug 21, 2004
56
for (i=0; i<MAXCHAN; i++)
Oct 21, 1999
Oct 21, 1999
57
{
Oct 18, 2017
Oct 18, 2017
58
reset_controllers(song, i);
Oct 21, 1999
Oct 21, 1999
59
/* The rest of these are unaffected by the Reset All Controllers event */
Oct 18, 2017
Oct 18, 2017
60
61
62
63
song->channel[i].program=song->default_program;
song->channel[i].panning=NO_PANNING;
song->channel[i].pitchsens=2;
song->channel[i].bank=0; /* tone bank or drum set */
Oct 21, 1999
Oct 21, 1999
64
}
Oct 18, 2017
Oct 18, 2017
65
reset_voices(song);
Oct 21, 1999
Oct 21, 1999
66
67
}
Oct 18, 2017
Oct 18, 2017
68
static void select_sample(MidiSong *song, int v, Instrument *ip, int vel)
Oct 21, 1999
Oct 21, 1999
69
{
Oct 18, 2017
Oct 18, 2017
70
Sint32 f, cdiff, diff;
Oct 21, 1999
Oct 21, 1999
71
72
73
74
75
76
77
78
int s,i;
Sample *sp, *closest;
s=ip->samples;
sp=ip->sample;
if (s==1)
{
Oct 18, 2017
Oct 18, 2017
79
song->voice[v].sample=sp;
Oct 21, 1999
Oct 21, 1999
80
81
82
return;
}
Oct 18, 2017
Oct 18, 2017
83
84
85
f=song->voice[v].orig_frequency;
for (i=0; i<s; i++)
{
Oct 21, 2017
Oct 21, 2017
86
if (sp->low_freq <= f && sp->high_freq >= f)
Oct 18, 2017
Oct 18, 2017
87
88
89
90
91
92
93
{
song->voice[v].sample=sp;
return;
}
sp++;
}
Oct 21, 1999
Oct 21, 1999
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
No suitable sample found! We'll select the sample whose root
frequency is closest to the one we want. (Actually we should
probably convert the low, high, and root frequencies to MIDI note
values and compare those.) */
cdiff=0x7FFFFFFF;
closest=sp=ip->sample;
for(i=0; i<s; i++)
{
diff=sp->root_freq - f;
if (diff<0) diff=-diff;
if (diff<cdiff)
{
cdiff=diff;
closest=sp;
}
sp++;
}
Oct 18, 2017
Oct 18, 2017
113
song->voice[v].sample=closest;
Oct 21, 1999
Oct 21, 1999
114
115
116
return;
}
Oct 18, 2017
Oct 18, 2017
117
static void recompute_freq(MidiSong *song, int v)
Oct 21, 1999
Oct 21, 1999
118
119
{
int
Oct 18, 2017
Oct 18, 2017
120
121
sign=(song->voice[v].sample_increment < 0), /* for bidirectional loops */
pb=song->channel[song->voice[v].channel].pitchbend;
Oct 21, 1999
Oct 21, 1999
122
123
double a;
Oct 18, 2017
Oct 18, 2017
124
if (!song->voice[v].sample->sample_rate)
Oct 21, 1999
Oct 21, 1999
125
126
return;
Oct 18, 2017
Oct 18, 2017
127
if (song->voice[v].vibrato_control_ratio)
Oct 21, 1999
Oct 21, 1999
128
129
130
131
132
133
{
/* This instrument has vibrato. Invalidate any precomputed
sample_increments. */
int i=VIBRATO_SAMPLE_INCREMENTS;
while (i--)
Oct 18, 2017
Oct 18, 2017
134
song->voice[v].vibrato_sample_increment[i]=0;
Oct 21, 1999
Oct 21, 1999
135
136
137
}
if (pb==0x2000 || pb<0 || pb>0x3FFF)
Oct 18, 2017
Oct 18, 2017
138
song->voice[v].frequency = song->voice[v].orig_frequency;
Oct 21, 1999
Oct 21, 1999
139
140
141
else
{
pb-=0x2000;
Oct 18, 2017
Oct 18, 2017
142
if (!(song->channel[song->voice[v].channel].pitchfactor))
Oct 21, 1999
Oct 21, 1999
143
144
{
/* Damn. Somebody bent the pitch. */
Oct 18, 2017
Oct 18, 2017
145
Sint32 i=pb*song->channel[song->voice[v].channel].pitchsens;
Oct 21, 1999
Oct 21, 1999
146
147
if (pb<0)
i=-i;
Oct 18, 2017
Oct 18, 2017
148
149
song->channel[song->voice[v].channel].pitchfactor=
(float)(bend_fine[(i>>5) & 0xFF] * bend_coarse[i>>13]);
Oct 21, 1999
Oct 21, 1999
150
151
}
if (pb>0)
Oct 18, 2017
Oct 18, 2017
152
153
154
song->voice[v].frequency=
(Sint32)(song->channel[song->voice[v].channel].pitchfactor *
(double)(song->voice[v].orig_frequency));
Oct 21, 1999
Oct 21, 1999
155
else
Oct 18, 2017
Oct 18, 2017
156
157
158
song->voice[v].frequency=
(Sint32)((double)(song->voice[v].orig_frequency) /
song->channel[song->voice[v].channel].pitchfactor);
Oct 21, 1999
Oct 21, 1999
159
160
}
Oct 18, 2017
Oct 18, 2017
161
162
163
164
a = FSCALE(((double)(song->voice[v].sample->sample_rate) *
(double)(song->voice[v].frequency)) /
((double)(song->voice[v].sample->root_freq) *
(double)(song->rate)),
Oct 21, 1999
Oct 21, 1999
165
166
167
168
169
FRACTION_BITS);
if (sign)
a = -a; /* need to preserve the loop direction */
Oct 18, 2017
Oct 18, 2017
170
song->voice[v].sample_increment = (Sint32)(a);
Oct 21, 1999
Oct 21, 1999
171
172
}
Oct 18, 2017
Oct 18, 2017
173
static void recompute_amp(MidiSong *song, int v)
Aug 21, 2004
Aug 21, 2004
174
{
Oct 18, 2017
Oct 18, 2017
175
Sint32 tempamp;
Oct 21, 1999
Oct 21, 1999
176
177
178
/* TODO: use fscale */
Oct 18, 2017
Oct 18, 2017
179
180
181
182
183
tempamp= (song->voice[v].velocity *
song->channel[song->voice[v].channel].volume *
song->channel[song->voice[v].channel].expression); /* 21 bits */
if (!(song->encoding & PE_MONO))
Oct 21, 1999
Oct 21, 1999
184
{
Oct 18, 2017
Oct 18, 2017
185
if (song->voice[v].panning > 60 && song->voice[v].panning < 68)
Oct 21, 1999
Oct 21, 1999
186
{
Oct 18, 2017
Oct 18, 2017
187
188
189
190
191
song->voice[v].panned=PANNED_CENTER;
song->voice[v].left_amp=
FSCALENEG((double)(tempamp) * song->voice[v].sample->volume * song->master_volume,
21);
Oct 21, 1999
Oct 21, 1999
192
}
Oct 18, 2017
Oct 18, 2017
193
else if (song->voice[v].panning<5)
Oct 21, 1999
Oct 21, 1999
194
{
Oct 18, 2017
Oct 18, 2017
195
song->voice[v].panned = PANNED_LEFT;
Oct 21, 1999
Oct 21, 1999
196
Oct 18, 2017
Oct 18, 2017
197
198
song->voice[v].left_amp=
FSCALENEG((double)(tempamp) * song->voice[v].sample->volume * song->master_volume,
Oct 21, 1999
Oct 21, 1999
199
200
20);
}
Oct 18, 2017
Oct 18, 2017
201
else if (song->voice[v].panning>123)
Oct 21, 1999
Oct 21, 1999
202
{
Oct 18, 2017
Oct 18, 2017
203
song->voice[v].panned = PANNED_RIGHT;
Oct 21, 1999
Oct 21, 1999
204
Oct 18, 2017
Oct 18, 2017
205
206
song->voice[v].left_amp= /* left_amp will be used */
FSCALENEG((double)(tempamp) * song->voice[v].sample->volume * song->master_volume,
Oct 21, 1999
Oct 21, 1999
207
208
209
210
20);
}
else
{
Oct 18, 2017
Oct 18, 2017
211
song->voice[v].panned = PANNED_MYSTERY;
Oct 21, 1999
Oct 21, 1999
212
Oct 18, 2017
Oct 18, 2017
213
214
215
216
217
song->voice[v].left_amp=
FSCALENEG((double)(tempamp) * song->voice[v].sample->volume * song->master_volume,
27);
song->voice[v].right_amp = song->voice[v].left_amp * (song->voice[v].panning);
song->voice[v].left_amp *= (float)(127 - song->voice[v].panning);
Oct 21, 1999
Oct 21, 1999
218
219
220
221
}
}
else
{
Oct 18, 2017
Oct 18, 2017
222
song->voice[v].panned = PANNED_CENTER;
Oct 21, 1999
Oct 21, 1999
223
Oct 18, 2017
Oct 18, 2017
224
225
song->voice[v].left_amp=
FSCALENEG((double)(tempamp) * song->voice[v].sample->volume * song->master_volume,
Oct 21, 1999
Oct 21, 1999
226
227
228
229
21);
}
}
Oct 18, 2017
Oct 18, 2017
230
static void start_note(MidiSong *song, MidiEvent *e, int i)
Oct 21, 1999
Oct 21, 1999
231
232
{
Instrument *ip;
Oct 18, 2017
Oct 18, 2017
233
234
235
int j;
if (ISDRUMCHANNEL(song, e->channel))
Oct 21, 1999
Oct 21, 1999
236
{
Oct 18, 2017
Oct 18, 2017
237
if (!(ip=song->drumset[song->channel[e->channel].bank]->instrument[e->a]))
Oct 21, 1999
Oct 21, 1999
238
{
Oct 18, 2017
Oct 18, 2017
239
if (!(ip=song->drumset[0]->instrument[e->a]))
Oct 21, 1999
Oct 21, 1999
240
241
return; /* No instrument? Then we can't play. */
}
Oct 18, 2017
Oct 18, 2017
242
if (ip->samples != 1)
Oct 21, 1999
Oct 21, 1999
243
{
Oct 18, 2017
Oct 18, 2017
244
245
SNDDBG(("Strange: percussion instrument with %d samples!",
ip->samples));
Oct 21, 1999
Oct 21, 1999
246
247
248
}
if (ip->sample->note_to_use) /* Do we have a fixed pitch? */
Oct 18, 2017
Oct 18, 2017
249
song->voice[i].orig_frequency = freq_table[(int)(ip->sample->note_to_use)];
Oct 21, 1999
Oct 21, 1999
250
else
Oct 18, 2017
Oct 18, 2017
251
252
253
254
song->voice[i].orig_frequency = freq_table[e->a & 0x7F];
/* drums are supposed to have only one sample */
song->voice[i].sample = ip->sample;
Oct 21, 1999
Oct 21, 1999
255
256
257
}
else
{
Oct 18, 2017
Oct 18, 2017
258
259
260
261
if (song->channel[e->channel].program == SPECIAL_PROGRAM)
ip=song->default_instrument;
else if (!(ip=song->tonebank[song->channel[e->channel].bank]->
instrument[song->channel[e->channel].program]))
Oct 21, 1999
Oct 21, 1999
262
{
Oct 18, 2017
Oct 18, 2017
263
if (!(ip=song->tonebank[0]->instrument[song->channel[e->channel].program]))
Oct 21, 1999
Oct 21, 1999
264
265
return; /* No instrument? Then we can't play. */
}
Oct 18, 2017
Oct 18, 2017
266
Oct 21, 1999
Oct 21, 1999
267
if (ip->sample->note_to_use) /* Fixed-pitch instrument? */
Oct 18, 2017
Oct 18, 2017
268
song->voice[i].orig_frequency = freq_table[(int)(ip->sample->note_to_use)];
Oct 21, 1999
Oct 21, 1999
269
else
Oct 18, 2017
Oct 18, 2017
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
song->voice[i].orig_frequency = freq_table[e->a & 0x7F];
select_sample(song, i, ip, e->b);
}
song->voice[i].status = VOICE_ON;
song->voice[i].channel = e->channel;
song->voice[i].note = e->a;
song->voice[i].velocity = e->b;
song->voice[i].sample_offset = 0;
song->voice[i].sample_increment = 0; /* make sure it isn't negative */
song->voice[i].tremolo_phase = 0;
song->voice[i].tremolo_phase_increment = song->voice[i].sample->tremolo_phase_increment;
song->voice[i].tremolo_sweep = song->voice[i].sample->tremolo_sweep_increment;
song->voice[i].tremolo_sweep_position = 0;
song->voice[i].vibrato_sweep = song->voice[i].sample->vibrato_sweep_increment;
song->voice[i].vibrato_sweep_position = 0;
song->voice[i].vibrato_control_ratio = song->voice[i].sample->vibrato_control_ratio;
song->voice[i].vibrato_control_counter = song->voice[i].vibrato_phase = 0;
Oct 21, 1999
Oct 21, 1999
290
for (j=0; j<VIBRATO_SAMPLE_INCREMENTS; j++)
Oct 18, 2017
Oct 18, 2017
291
song->voice[i].vibrato_sample_increment[j] = 0;
Aug 21, 2004
Aug 21, 2004
292
Oct 18, 2017
Oct 18, 2017
293
294
if (song->channel[e->channel].panning != NO_PANNING)
song->voice[i].panning = song->channel[e->channel].panning;
Oct 21, 1999
Oct 21, 1999
295
else
Oct 18, 2017
Oct 18, 2017
296
song->voice[i].panning = song->voice[i].sample->panning;
Oct 21, 1999
Oct 21, 1999
297
Oct 18, 2017
Oct 18, 2017
298
299
300
recompute_freq(song, i);
recompute_amp(song, i);
if (song->voice[i].sample->modes & MODES_ENVELOPE)
Oct 21, 1999
Oct 21, 1999
301
302
{
/* Ramp up from 0 */
Oct 18, 2017
Oct 18, 2017
303
304
305
306
307
song->voice[i].envelope_stage = 0;
song->voice[i].envelope_volume = 0;
song->voice[i].control_counter = 0;
recompute_envelope(song, i);
apply_envelope_to_amp(song, i);
Oct 21, 1999
Oct 21, 1999
308
309
310
}
else
{
Oct 18, 2017
Oct 18, 2017
311
312
song->voice[i].envelope_increment = 0;
apply_envelope_to_amp(song, i);
Oct 21, 1999
Oct 21, 1999
313
314
315
}
}
Oct 18, 2017
Oct 18, 2017
316
static void kill_note(MidiSong *song, int i)
Oct 21, 1999
Oct 21, 1999
317
{
Oct 18, 2017
Oct 18, 2017
318
song->voice[i].status = VOICE_DIE;
Oct 21, 1999
Oct 21, 1999
319
320
321
}
/* Only one instance of a note can be playing on a single channel. */
Oct 18, 2017
Oct 18, 2017
322
static void note_on(MidiSong *song)
Oct 21, 1999
Oct 21, 1999
323
{
Oct 18, 2017
Oct 18, 2017
324
325
326
int i = song->voices, lowest=-1;
Sint32 lv=0x7FFFFFFF, v;
MidiEvent *e = song->current_event;
Oct 21, 1999
Oct 21, 1999
327
328
329
while (i--)
{
Oct 18, 2017
Oct 18, 2017
330
if (song->voice[i].status == VOICE_FREE)
Oct 21, 1999
Oct 21, 1999
331
lowest=i; /* Can't get a lower volume than silence */
Oct 18, 2017
Oct 18, 2017
332
333
334
else if (song->voice[i].channel==e->channel &&
(song->voice[i].note==e->a || song->channel[song->voice[i].channel].mono))
kill_note(song, i);
Oct 21, 1999
Oct 21, 1999
335
336
337
338
339
}
if (lowest != -1)
{
/* Found a free voice. */
Oct 18, 2017
Oct 18, 2017
340
start_note(song,e,lowest);
Oct 21, 1999
Oct 21, 1999
341
342
343
344
return;
}
/* Look for the decaying note with the lowest volume */
Oct 18, 2017
Oct 18, 2017
345
i = song->voices;
Oct 21, 1999
Oct 21, 1999
346
347
while (i--)
{
Oct 18, 2017
Oct 18, 2017
348
349
if ((song->voice[i].status != VOICE_ON) &&
(song->voice[i].status != VOICE_DIE))
Oct 21, 1999
Oct 21, 1999
350
{
Oct 18, 2017
Oct 18, 2017
351
352
353
354
v = song->voice[i].left_mix;
if ((song->voice[i].panned == PANNED_MYSTERY)
&& (song->voice[i].right_mix > v))
v = song->voice[i].right_mix;
Oct 21, 1999
Oct 21, 1999
355
356
357
358
359
360
361
362
363
364
365
366
367
368
if (v<lv)
{
lv=v;
lowest=i;
}
}
}
if (lowest != -1)
{
/* This can still cause a click, but if we had a free voice to
spare for ramping down this note, we wouldn't need to kill it
in the first place... Still, this needs to be fixed. Perhaps
we could use a reserve of voices to play dying notes only. */
Oct 18, 2017
Oct 18, 2017
369
370
371
372
song->cut_notes++;
song->voice[lowest].status=VOICE_FREE;
start_note(song,e,lowest);
Oct 21, 1999
Oct 21, 1999
373
374
}
else
Oct 18, 2017
Oct 18, 2017
375
song->lost_notes++;
Oct 21, 1999
Oct 21, 1999
376
377
}
Oct 18, 2017
Oct 18, 2017
378
static void finish_note(MidiSong *song, int i)
Oct 21, 1999
Oct 21, 1999
379
{
Oct 18, 2017
Oct 18, 2017
380
if (song->voice[i].sample->modes & MODES_ENVELOPE)
Oct 21, 1999
Oct 21, 1999
381
382
{
/* We need to get the envelope out of Sustain stage */
Oct 18, 2017
Oct 18, 2017
383
384
385
386
song->voice[i].envelope_stage = 3;
song->voice[i].status = VOICE_OFF;
recompute_envelope(song, i);
apply_envelope_to_amp(song, i);
Oct 21, 1999
Oct 21, 1999
387
388
389
390
391
392
}
else
{
/* Set status to OFF so resample_voice() will let this voice out
of its loop, if any. In any case, this voice dies when it
hits the end of its data (ofs>=data_length). */
Oct 18, 2017
Oct 18, 2017
393
song->voice[i].status = VOICE_OFF;
Oct 21, 1999
Oct 21, 1999
394
395
396
}
}
Oct 18, 2017
Oct 18, 2017
397
static void note_off(MidiSong *song)
Oct 21, 1999
Oct 21, 1999
398
{
Oct 18, 2017
Oct 18, 2017
399
400
401
int i = song->voices;
MidiEvent *e = song->current_event;
Oct 21, 1999
Oct 21, 1999
402
while (i--)
Oct 18, 2017
Oct 18, 2017
403
404
405
if (song->voice[i].status == VOICE_ON &&
song->voice[i].channel == e->channel &&
song->voice[i].note == e->a)
Oct 21, 1999
Oct 21, 1999
406
{
Oct 18, 2017
Oct 18, 2017
407
if (song->channel[e->channel].sustain)
Oct 21, 1999
Oct 21, 1999
408
{
Oct 18, 2017
Oct 18, 2017
409
song->voice[i].status = VOICE_SUSTAINED;
Oct 21, 1999
Oct 21, 1999
410
411
}
else
Oct 18, 2017
Oct 18, 2017
412
finish_note(song, i);
Oct 21, 1999
Oct 21, 1999
413
414
415
416
417
return;
}
}
/* Process the All Notes Off event */
Oct 18, 2017
Oct 18, 2017
418
static void all_notes_off(MidiSong *song)
Oct 21, 1999
Oct 21, 1999
419
{
Oct 18, 2017
Oct 18, 2017
420
421
422
423
int i = song->voices;
int c = song->current_event->channel;
SNDDBG(("All notes off on channel %d", c));
Oct 21, 1999
Oct 21, 1999
424
while (i--)
Oct 18, 2017
Oct 18, 2017
425
426
if (song->voice[i].status == VOICE_ON &&
song->voice[i].channel == c)
Oct 21, 1999
Oct 21, 1999
427
{
Oct 18, 2017
Oct 18, 2017
428
429
if (song->channel[c].sustain)
song->voice[i].status = VOICE_SUSTAINED;
Oct 21, 1999
Oct 21, 1999
430
else
Oct 18, 2017
Oct 18, 2017
431
finish_note(song, i);
Oct 21, 1999
Oct 21, 1999
432
433
434
435
}
}
/* Process the All Sounds Off event */
Oct 18, 2017
Oct 18, 2017
436
static void all_sounds_off(MidiSong *song)
Oct 21, 1999
Oct 21, 1999
437
{
Oct 18, 2017
Oct 18, 2017
438
439
440
int i = song->voices;
int c = song->current_event->channel;
Oct 21, 1999
Oct 21, 1999
441
while (i--)
Oct 18, 2017
Oct 18, 2017
442
443
444
if (song->voice[i].channel == c &&
song->voice[i].status != VOICE_FREE &&
song->voice[i].status != VOICE_DIE)
Oct 21, 1999
Oct 21, 1999
445
{
Oct 18, 2017
Oct 18, 2017
446
kill_note(song, i);
Oct 21, 1999
Oct 21, 1999
447
448
449
}
}
Oct 18, 2017
Oct 18, 2017
450
static void adjust_pressure(MidiSong *song)
Oct 21, 1999
Oct 21, 1999
451
{
Oct 18, 2017
Oct 18, 2017
452
453
454
MidiEvent *e = song->current_event;
int i = song->voices;
Oct 21, 1999
Oct 21, 1999
455
while (i--)
Oct 18, 2017
Oct 18, 2017
456
457
458
if (song->voice[i].status == VOICE_ON &&
song->voice[i].channel == e->channel &&
song->voice[i].note == e->a)
Oct 21, 1999
Oct 21, 1999
459
{
Oct 18, 2017
Oct 18, 2017
460
461
462
song->voice[i].velocity = e->b;
recompute_amp(song, i);
apply_envelope_to_amp(song, i);
Oct 21, 1999
Oct 21, 1999
463
464
465
466
return;
}
}
Oct 18, 2017
Oct 18, 2017
467
static void drop_sustain(MidiSong *song)
Oct 21, 1999
Oct 21, 1999
468
{
Oct 18, 2017
Oct 18, 2017
469
470
int i = song->voices;
int c = song->current_event->channel;
Oct 21, 1999
Oct 21, 1999
471
472
while (i--)
Oct 18, 2017
Oct 18, 2017
473
474
if (song->voice[i].status == VOICE_SUSTAINED && song->voice[i].channel == c)
finish_note(song, i);
Oct 21, 1999
Oct 21, 1999
475
476
}
Oct 18, 2017
Oct 18, 2017
477
static void adjust_pitchbend(MidiSong *song)
Oct 21, 1999
Oct 21, 1999
478
{
Oct 18, 2017
Oct 18, 2017
479
480
481
int c = song->current_event->channel;
int i = song->voices;
Oct 21, 1999
Oct 21, 1999
482
while (i--)
Oct 18, 2017
Oct 18, 2017
483
if (song->voice[i].status != VOICE_FREE && song->voice[i].channel == c)
Oct 21, 1999
Oct 21, 1999
484
{
Oct 18, 2017
Oct 18, 2017
485
recompute_freq(song, i);
Oct 21, 1999
Oct 21, 1999
486
487
488
}
}
Oct 18, 2017
Oct 18, 2017
489
static void adjust_volume(MidiSong *song)
Oct 21, 1999
Oct 21, 1999
490
{
Oct 18, 2017
Oct 18, 2017
491
492
493
int c = song->current_event->channel;
int i = song->voices;
Oct 21, 1999
Oct 21, 1999
494
while (i--)
Oct 18, 2017
Oct 18, 2017
495
496
if (song->voice[i].channel == c &&
(song->voice[i].status==VOICE_ON || song->voice[i].status==VOICE_SUSTAINED))
Oct 21, 1999
Oct 21, 1999
497
{
Oct 18, 2017
Oct 18, 2017
498
499
recompute_amp(song, i);
apply_envelope_to_amp(song, i);
Oct 21, 1999
Oct 21, 1999
500
501
502
}
}
Oct 18, 2017
Oct 18, 2017
503
static void seek_forward(MidiSong *song, Sint32 until_time)
Oct 21, 1999
Oct 21, 1999
504
{
Oct 18, 2017
Oct 18, 2017
505
506
reset_voices(song);
while (song->current_event->time < until_time)
Oct 21, 1999
Oct 21, 1999
507
{
Oct 18, 2017
Oct 18, 2017
508
switch(song->current_event->type)
Oct 21, 1999
Oct 21, 1999
509
510
511
512
{
/* All notes stay off. Just handle the parameter changes. */
case ME_PITCH_SENS:
Oct 18, 2017
Oct 18, 2017
513
514
515
song->channel[song->current_event->channel].pitchsens =
song->current_event->a;
song->channel[song->current_event->channel].pitchfactor = 0;
Oct 21, 1999
Oct 21, 1999
516
517
518
break;
case ME_PITCHWHEEL:
Oct 18, 2017
Oct 18, 2017
519
520
521
song->channel[song->current_event->channel].pitchbend =
song->current_event->a + song->current_event->b * 128;
song->channel[song->current_event->channel].pitchfactor = 0;
Oct 21, 1999
Oct 21, 1999
522
523
524
break;
case ME_MAINVOLUME:
Oct 18, 2017
Oct 18, 2017
525
526
song->channel[song->current_event->channel].volume =
song->current_event->a;
Aug 21, 2004
Aug 21, 2004
527
528
break;
Oct 21, 1999
Oct 21, 1999
529
case ME_PAN:
Oct 18, 2017
Oct 18, 2017
530
531
song->channel[song->current_event->channel].panning =
song->current_event->a;
Oct 21, 1999
Oct 21, 1999
532
533
534
break;
case ME_EXPRESSION:
Oct 18, 2017
Oct 18, 2017
535
536
song->channel[song->current_event->channel].expression =
song->current_event->a;
Oct 21, 1999
Oct 21, 1999
537
538
539
break;
case ME_PROGRAM:
Oct 18, 2017
Oct 18, 2017
540
if (ISDRUMCHANNEL(song, song->current_event->channel))
Oct 21, 1999
Oct 21, 1999
541
/* Change drum set */
Oct 18, 2017
Oct 18, 2017
542
543
song->channel[song->current_event->channel].bank =
song->current_event->a;
Oct 21, 1999
Oct 21, 1999
544
else
Oct 18, 2017
Oct 18, 2017
545
546
song->channel[song->current_event->channel].program =
song->current_event->a;
Oct 21, 1999
Oct 21, 1999
547
548
549
break;
case ME_SUSTAIN:
Oct 18, 2017
Oct 18, 2017
550
551
song->channel[song->current_event->channel].sustain =
song->current_event->a;
Aug 21, 2004
Aug 21, 2004
552
553
break;
Oct 21, 1999
Oct 21, 1999
554
case ME_RESET_CONTROLLERS:
Oct 18, 2017
Oct 18, 2017
555
reset_controllers(song, song->current_event->channel);
Oct 21, 1999
Oct 21, 1999
556
557
558
break;
case ME_TONE_BANK:
Oct 18, 2017
Oct 18, 2017
559
560
song->channel[song->current_event->channel].bank =
song->current_event->a;
Oct 21, 1999
Oct 21, 1999
561
562
563
break;
case ME_EOT:
Oct 18, 2017
Oct 18, 2017
564
song->current_sample = song->current_event->time;
Oct 21, 1999
Oct 21, 1999
565
566
return;
}
Oct 18, 2017
Oct 18, 2017
567
song->current_event++;
Oct 21, 1999
Oct 21, 1999
568
}
Oct 18, 2017
Oct 18, 2017
569
570
571
572
/*song->current_sample=song->current_event->time;*/
if (song->current_event != song->events)
song->current_event--;
song->current_sample=until_time;
Oct 21, 1999
Oct 21, 1999
573
574
}
Oct 18, 2017
Oct 18, 2017
575
static void skip_to(MidiSong *song, Sint32 until_time)
Oct 21, 1999
Oct 21, 1999
576
{
Oct 18, 2017
Oct 18, 2017
577
578
if (song->current_sample > until_time)
song->current_sample = 0;
Oct 21, 1999
Oct 21, 1999
579
Oct 18, 2017
Oct 18, 2017
580
581
582
583
reset_midi(song);
song->buffered_count = 0;
song->buffer_pointer = song->common_buffer;
song->current_event = song->events;
Oct 21, 1999
Oct 21, 1999
584
585
if (until_time)
Oct 18, 2017
Oct 18, 2017
586
seek_forward(song, until_time);
Oct 21, 1999
Oct 21, 1999
587
588
}
Oct 18, 2017
Oct 18, 2017
589
static void do_compute_data(MidiSong *song, Sint32 count)
Oct 21, 1999
Oct 21, 1999
590
591
{
int i;
Oct 18, 2017
Oct 18, 2017
592
593
594
memset(song->buffer_pointer, 0,
(song->encoding & PE_MONO) ? (count * 4) : (count * 8));
for (i = 0; i < song->voices; i++)
Oct 21, 1999
Oct 21, 1999
595
{
Oct 18, 2017
Oct 18, 2017
596
597
if(song->voice[i].status != VOICE_FREE)
mix_voice(song, song->buffer_pointer, i, count);
Oct 21, 1999
Oct 21, 1999
598
}
Oct 18, 2017
Oct 18, 2017
599
song->current_sample += count;
Oct 21, 1999
Oct 21, 1999
600
601
602
603
}
/* count=0 means flush remaining buffered data to output device, then
flush the device itself */
Oct 18, 2017
Oct 18, 2017
604
static void compute_data(MidiSong *song, void *stream, Sint32 count)
Oct 21, 1999
Oct 21, 1999
605
{
Oct 18, 2017
Oct 18, 2017
606
int channels;
Oct 21, 1999
Oct 21, 1999
607
Oct 18, 2017
Oct 18, 2017
608
if ( song->encoding & PE_MONO )
Oct 21, 1999
Oct 21, 1999
609
610
channels = 1;
else
Oct 18, 2017
Oct 18, 2017
611
channels = 2;
Oct 21, 1999
Oct 21, 1999
612
613
614
if (!count)
{
Oct 18, 2017
Oct 18, 2017
615
616
617
618
619
if (song->buffered_count)
song->write(stream, song->common_buffer, channels * song->buffered_count);
song->buffer_pointer = song->common_buffer;
song->buffered_count = 0;
return;
Oct 21, 1999
Oct 21, 1999
620
621
}
Oct 18, 2017
Oct 18, 2017
622
while ((count + song->buffered_count) >= song->buffer_size)
Oct 21, 1999
Oct 21, 1999
623
{
Oct 18, 2017
Oct 18, 2017
624
625
626
627
628
do_compute_data(song, song->buffer_size - song->buffered_count);
count -= song->buffer_size - song->buffered_count;
song->write(stream, song->common_buffer, channels * song->buffer_size);
song->buffer_pointer = song->common_buffer;
song->buffered_count = 0;
Oct 21, 1999
Oct 21, 1999
629
630
631
}
if (count>0)
{
Oct 18, 2017
Oct 18, 2017
632
633
634
do_compute_data(song, count);
song->buffered_count += count;
song->buffer_pointer += (song->encoding & PE_MONO) ? count : count*2;
Oct 21, 1999
Oct 21, 1999
635
636
637
}
}
Oct 18, 2017
Oct 18, 2017
638
639
640
641
642
643
644
645
void Timidity_Start(MidiSong *song)
{
song->playing = 1;
adjust_amplification(song);
skip_to(song, 0);
}
void Timidity_Seek(MidiSong *song, Uint32 ms)
Oct 21, 1999
Oct 21, 1999
646
{
Oct 18, 2017
Oct 18, 2017
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
skip_to(song, (ms * song->rate) / 1000);
}
Uint32 Timidity_GetSongLength(MidiSong *song)
{
MidiEvent *last_event = &song->events[song->groomed_event_count - 1];
/* We want last_event->time * 1000 / song->rate */
Uint32 retvalue = (last_event->time / song->rate) * 1000;
retvalue += (last_event->time % song->rate) * 1000 / song->rate;
return retvalue;
}
int Timidity_PlaySome(MidiSong *song, void *stream, Sint32 len)
{
Sint32 start_sample, end_sample, samples;
int bytes_per_sample;
if (!song->playing)
return 0;
Oct 21, 1999
Oct 21, 1999
666
Oct 21, 2017
Oct 21, 2017
667
668
669
bytes_per_sample = 1;
bytes_per_sample *= ((song->encoding & PE_32BIT) ? 4 : ((song->encoding & PE_16BIT) ? 2 : 1));
bytes_per_sample *= ((song->encoding & PE_MONO) ? 1 : 2);
Oct 18, 2017
Oct 18, 2017
670
671
672
673
674
samples = len / bytes_per_sample;
start_sample = song->current_sample;
end_sample = song->current_sample+samples;
while ( song->current_sample < end_sample ) {
Oct 21, 1999
Oct 21, 1999
675
/* Handle all events that should happen at this time */
Oct 18, 2017
Oct 18, 2017
676
677
while (song->current_event->time <= song->current_sample) {
switch(song->current_event->type) {
Oct 21, 1999
Oct 21, 1999
678
679
680
681
/* Effects affecting a single note */
case ME_NOTEON:
Oct 18, 2017
Oct 18, 2017
682
683
if (!(song->current_event->b)) /* Velocity 0? */
note_off(song);
Oct 21, 1999
Oct 21, 1999
684
else
Oct 18, 2017
Oct 18, 2017
685
note_on(song);
Oct 21, 1999
Oct 21, 1999
686
687
688
break;
case ME_NOTEOFF:
Oct 18, 2017
Oct 18, 2017
689
note_off(song);
Oct 21, 1999
Oct 21, 1999
690
691
692
break;
case ME_KEYPRESSURE:
Oct 18, 2017
Oct 18, 2017
693
adjust_pressure(song);
Oct 21, 1999
Oct 21, 1999
694
695
696
697
698
break;
/* Effects affecting a single channel */
case ME_PITCH_SENS:
Oct 18, 2017
Oct 18, 2017
699
700
701
song->channel[song->current_event->channel].pitchsens =
song->current_event->a;
song->channel[song->current_event->channel].pitchfactor = 0;
Oct 21, 1999
Oct 21, 1999
702
703
704
break;
case ME_PITCHWHEEL:
Oct 18, 2017
Oct 18, 2017
705
706
707
song->channel[song->current_event->channel].pitchbend =
song->current_event->a + song->current_event->b * 128;
song->channel[song->current_event->channel].pitchfactor = 0;
Oct 21, 1999
Oct 21, 1999
708
/* Adjust pitch for notes already playing */
Oct 18, 2017
Oct 18, 2017
709
adjust_pitchbend(song);
Oct 21, 1999
Oct 21, 1999
710
711
712
break;
case ME_MAINVOLUME:
Oct 18, 2017
Oct 18, 2017
713
714
715
song->channel[song->current_event->channel].volume =
song->current_event->a;
adjust_volume(song);
Oct 21, 1999
Oct 21, 1999
716
break;
Oct 18, 2017
Oct 18, 2017
717
Oct 21, 1999
Oct 21, 1999
718
case ME_PAN:
Oct 18, 2017
Oct 18, 2017
719
720
song->channel[song->current_event->channel].panning =
song->current_event->a;
Oct 21, 1999
Oct 21, 1999
721
722
723
break;
case ME_EXPRESSION:
Oct 18, 2017
Oct 18, 2017
724
725
726
song->channel[song->current_event->channel].expression =
song->current_event->a;
adjust_volume(song);
Oct 21, 1999
Oct 21, 1999
727
728
729
break;
case ME_PROGRAM:
Oct 18, 2017
Oct 18, 2017
730
if (ISDRUMCHANNEL(song, song->current_event->channel)) {
Oct 21, 1999
Oct 21, 1999
731
/* Change drum set */
Oct 18, 2017
Oct 18, 2017
732
733
song->channel[song->current_event->channel].bank =
song->current_event->a;
Oct 21, 1999
Oct 21, 1999
734
735
}
else
Oct 18, 2017
Oct 18, 2017
736
737
song->channel[song->current_event->channel].program =
song->current_event->a;
Oct 21, 1999
Oct 21, 1999
738
739
740
break;
case ME_SUSTAIN:
Oct 18, 2017
Oct 18, 2017
741
742
743
744
song->channel[song->current_event->channel].sustain =
song->current_event->a;
if (!song->current_event->a)
drop_sustain(song);
Oct 21, 1999
Oct 21, 1999
745
746
747
break;
case ME_RESET_CONTROLLERS:
Oct 18, 2017
Oct 18, 2017
748
reset_controllers(song, song->current_event->channel);
Oct 21, 1999
Oct 21, 1999
749
750
751
break;
case ME_ALL_NOTES_OFF:
Oct 18, 2017
Oct 18, 2017
752
all_notes_off(song);
Oct 21, 1999
Oct 21, 1999
753
754
755
break;
case ME_ALL_SOUNDS_OFF:
Oct 18, 2017
Oct 18, 2017
756
all_sounds_off(song);
Oct 21, 1999
Oct 21, 1999
757
break;
Oct 18, 2017
Oct 18, 2017
758
Oct 21, 1999
Oct 21, 1999
759
case ME_TONE_BANK:
Oct 18, 2017
Oct 18, 2017
760
761
song->channel[song->current_event->channel].bank =
song->current_event->a;
Oct 21, 1999
Oct 21, 1999
762
break;
Oct 18, 2017
Oct 18, 2017
763
Oct 21, 1999
Oct 21, 1999
764
765
case ME_EOT:
/* Give the last notes a couple of seconds to decay */
Oct 18, 2017
Oct 18, 2017
766
767
768
769
770
771
SNDDBG(("Playing time: ~%d seconds\n",
song->current_sample/song->rate+2));
SNDDBG(("Notes cut: %d\n", song->cut_notes));
SNDDBG(("Notes lost totally: %d\n", song->lost_notes));
song->playing = 0;
return (song->current_sample - start_sample) * bytes_per_sample;
Oct 21, 1999
Oct 21, 1999
772
}
Oct 18, 2017
Oct 18, 2017
773
song->current_event++;
Oct 21, 1999
Oct 21, 1999
774
}
Oct 18, 2017
Oct 18, 2017
775
776
if (song->current_event->time > end_sample)
compute_data(song, stream, end_sample-song->current_sample);
Oct 21, 1999
Oct 21, 1999
777
else
Oct 18, 2017
Oct 18, 2017
778
compute_data(song, stream, song->current_event->time-song->current_sample);
Oct 21, 1999
Oct 21, 1999
779
}
Oct 18, 2017
Oct 18, 2017
780
return samples * bytes_per_sample;
Oct 21, 1999
Oct 21, 1999
781
782
}
Oct 18, 2017
Oct 18, 2017
783
void Timidity_SetVolume(MidiSong *song, int volume)
Oct 21, 1999
Oct 21, 1999
784
785
786
{
int i;
if (volume > MAX_AMPLIFICATION)
Oct 18, 2017
Oct 18, 2017
787
song->amplification = MAX_AMPLIFICATION;
Oct 21, 1999
Oct 21, 1999
788
789
else
if (volume < 0)
Oct 18, 2017
Oct 18, 2017
790
song->amplification = 0;
Oct 21, 1999
Oct 21, 1999
791
else
Oct 18, 2017
Oct 18, 2017
792
793
794
795
song->amplification = volume;
adjust_amplification(song);
for (i = 0; i < song->voices; i++)
if (song->voice[i].status != VOICE_FREE)
Oct 21, 1999
Oct 21, 1999
796
{
Oct 18, 2017
Oct 18, 2017
797
798
recompute_amp(song, i);
apply_envelope_to_amp(song, i);
Oct 21, 1999
Oct 21, 1999
799
800
}
}