Skip to content

Latest commit

 

History

History
801 lines (690 loc) · 21 KB

playmidi.c

File metadata and controls

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