Debian Codegraph @

prboom2/src/MUSIC/oplplayer.c

raw
1 // Emacs style mode select -*- C -*-
2 //-----------------------------------------------------------------------------
3 //
4 // Copyright(C) 1993-1996 Id Software, Inc.
5 // Copyright(C) 2005 Simon Howard
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License
9 // as published by the Free Software Foundation; either version 2
10 // of the License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20 // 02111-1307, USA.
21 //
22 // DESCRIPTION:
23 // System interface for music.
24 //
25 //-----------------------------------------------------------------------------
26
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30
31 #include "doomdef.h"
32 #include "doomstat.h"
33 #include "memio.h"
34 #include "mus2mid.h"
35
36 #include "m_misc.h"
37 #include "s_sound.h"
38 #include "w_wad.h"
39 #include "z_zone.h"
40
41 #include "opl.h"
42 #include "midifile.h"
43
44 #include "musicplayer.h"
45
46 #include "lprintf.h"
47
48 #include "dsda/configuration.h"
49
50 static int opl_opl3mode;
51
52 // #define OPL_MIDI_DEBUG
53
54 #define MAXMIDLENGTH (96 * 1024)
55 #define GENMIDI_NUM_INSTRS 128
56 #define GENMIDI_NUM_PERCUSSION 47
57
58 #define GENMIDI_HEADER "#OPL_II#"
59 #define GENMIDI_FLAG_FIXED 0x0001 /* fixed pitch */
60 #define GENMIDI_FLAG_2VOICE 0x0004 /* double voice (OPL3) */
61
62 typedef struct
63 {
64 byte tremolo;
65 byte attack;
66 byte sustain;
67 byte waveform;
68 byte scale;
69 byte level;
70 } PACKEDATTR genmidi_op_t;
71
72 typedef struct
73 {
74 genmidi_op_t modulator;
75 byte feedback;
76 genmidi_op_t carrier;
77 byte unused;
78 short base_note_offset;
79 } PACKEDATTR genmidi_voice_t;
80
81 typedef struct
82 {
83 unsigned short flags;
84 byte fine_tuning;
85 byte fixed_note;
86
87 genmidi_voice_t voices[2];
88 } PACKEDATTR genmidi_instr_t;
89
90 typedef struct
91 {
92 // The instrument currently used for this track.
93
94 const genmidi_instr_t *instrument;
95
96 // Volume level
97
98 int volume;
99 int volume_base;
100
101 // Pan
102
103 int pan;
104
105 // Pitch bend value:
106
107 int bend;
108
109 } opl_channel_data_t;
110
111 // Data associated with a track that is currently playing.
112
113 typedef struct
114 {
115 // Data for each channel.
116
117 opl_channel_data_t channels[MIDI_CHANNELS_PER_TRACK];
118
119 // Track iterator used to read new events.
120
121 midi_track_iter_t *iter;
122 } opl_track_data_t;
123
124 typedef struct opl_voice_s opl_voice_t;
125
126 struct opl_voice_s
127 {
128 // Index of this voice:
129 int index;
130
131 // The operators used by this voice:
132 int op1, op2;
133
134 // Array used by voice:
135 int array;
136
137 // Currently-loaded instrument data
138 const genmidi_instr_t *current_instr;
139
140 // The voice number in the instrument to use.
141 // This is normally set to zero; if this is a double voice
142 // instrument, it may be one.
143 unsigned int current_instr_voice;
144
145 // The channel currently using this voice.
146 opl_channel_data_t *channel;
147
148 // The midi key that this voice is playing.
149 unsigned int key;
150
151 // The note being played. This is normally the same as
152 // the key, but if the instrument is a fixed pitch
153 // instrument, it is different.
154 unsigned int note;
155
156 // The frequency value being used.
157 unsigned int freq;
158
159 // The volume of the note being played on this channel.
160 unsigned int note_volume;
161
162 // The current volume (register value) that has been set for this channel.
163 unsigned int car_volume;
164 unsigned int mod_volume;
165
166 // Pan.
167 unsigned int reg_pan;
168
169 // Priority.
170 unsigned int priority;
171 };
172
173 // Operators used by the different voices.
174
175 static const int voice_operators[2][OPL_NUM_VOICES] = {
176 { 0x00, 0x01, 0x02, 0x08, 0x09, 0x0a, 0x10, 0x11, 0x12 },
177 { 0x03, 0x04, 0x05, 0x0b, 0x0c, 0x0d, 0x13, 0x14, 0x15 }
178 };
179
180 // Frequency values to use for each note.
181
182 static const unsigned short frequency_curve[] = {
183
184 0x133, 0x133, 0x134, 0x134, 0x135, 0x136, 0x136, 0x137, // -1
185 0x137, 0x138, 0x138, 0x139, 0x139, 0x13a, 0x13b, 0x13b,
186 0x13c, 0x13c, 0x13d, 0x13d, 0x13e, 0x13f, 0x13f, 0x140,
187 0x140, 0x141, 0x142, 0x142, 0x143, 0x143, 0x144, 0x144,
188
189 0x145, 0x146, 0x146, 0x147, 0x147, 0x148, 0x149, 0x149, // -2
190 0x14a, 0x14a, 0x14b, 0x14c, 0x14c, 0x14d, 0x14d, 0x14e,
191 0x14f, 0x14f, 0x150, 0x150, 0x151, 0x152, 0x152, 0x153,
192 0x153, 0x154, 0x155, 0x155, 0x156, 0x157, 0x157, 0x158,
193
194 // These are used for the first seven MIDI note values:
195
196 0x158, 0x159, 0x15a, 0x15a, 0x15b, 0x15b, 0x15c, 0x15d, // 0
197 0x15d, 0x15e, 0x15f, 0x15f, 0x160, 0x161, 0x161, 0x162,
198 0x162, 0x163, 0x164, 0x164, 0x165, 0x166, 0x166, 0x167,
199 0x168, 0x168, 0x169, 0x16a, 0x16a, 0x16b, 0x16c, 0x16c,
200
201 0x16d, 0x16e, 0x16e, 0x16f, 0x170, 0x170, 0x171, 0x172, // 1
202 0x172, 0x173, 0x174, 0x174, 0x175, 0x176, 0x176, 0x177,
203 0x178, 0x178, 0x179, 0x17a, 0x17a, 0x17b, 0x17c, 0x17c,
204 0x17d, 0x17e, 0x17e, 0x17f, 0x180, 0x181, 0x181, 0x182,
205
206 0x183, 0x183, 0x184, 0x185, 0x185, 0x186, 0x187, 0x188, // 2
207 0x188, 0x189, 0x18a, 0x18a, 0x18b, 0x18c, 0x18d, 0x18d,
208 0x18e, 0x18f, 0x18f, 0x190, 0x191, 0x192, 0x192, 0x193,
209 0x194, 0x194, 0x195, 0x196, 0x197, 0x197, 0x198, 0x199,
210
211 0x19a, 0x19a, 0x19b, 0x19c, 0x19d, 0x19d, 0x19e, 0x19f, // 3
212 0x1a0, 0x1a0, 0x1a1, 0x1a2, 0x1a3, 0x1a3, 0x1a4, 0x1a5,
213 0x1a6, 0x1a6, 0x1a7, 0x1a8, 0x1a9, 0x1a9, 0x1aa, 0x1ab,
214 0x1ac, 0x1ad, 0x1ad, 0x1ae, 0x1af, 0x1b0, 0x1b0, 0x1b1,
215
216 0x1b2, 0x1b3, 0x1b4, 0x1b4, 0x1b5, 0x1b6, 0x1b7, 0x1b8, // 4
217 0x1b8, 0x1b9, 0x1ba, 0x1bb, 0x1bc, 0x1bc, 0x1bd, 0x1be,
218 0x1bf, 0x1c0, 0x1c0, 0x1c1, 0x1c2, 0x1c3, 0x1c4, 0x1c4,
219 0x1c5, 0x1c6, 0x1c7, 0x1c8, 0x1c9, 0x1c9, 0x1ca, 0x1cb,
220
221 0x1cc, 0x1cd, 0x1ce, 0x1ce, 0x1cf, 0x1d0, 0x1d1, 0x1d2, // 5
222 0x1d3, 0x1d3, 0x1d4, 0x1d5, 0x1d6, 0x1d7, 0x1d8, 0x1d8,
223 0x1d9, 0x1da, 0x1db, 0x1dc, 0x1dd, 0x1de, 0x1de, 0x1df,
224 0x1e0, 0x1e1, 0x1e2, 0x1e3, 0x1e4, 0x1e5, 0x1e5, 0x1e6,
225
226 0x1e7, 0x1e8, 0x1e9, 0x1ea, 0x1eb, 0x1ec, 0x1ed, 0x1ed, // 6
227 0x1ee, 0x1ef, 0x1f0, 0x1f1, 0x1f2, 0x1f3, 0x1f4, 0x1f5,
228 0x1f6, 0x1f6, 0x1f7, 0x1f8, 0x1f9, 0x1fa, 0x1fb, 0x1fc,
229 0x1fd, 0x1fe, 0x1ff, 0x200, 0x201, 0x201, 0x202, 0x203,
230
231 // First note of looped range used for all octaves:
232
233 0x204, 0x205, 0x206, 0x207, 0x208, 0x209, 0x20a, 0x20b, // 7
234 0x20c, 0x20d, 0x20e, 0x20f, 0x210, 0x210, 0x211, 0x212,
235 0x213, 0x214, 0x215, 0x216, 0x217, 0x218, 0x219, 0x21a,
236 0x21b, 0x21c, 0x21d, 0x21e, 0x21f, 0x220, 0x221, 0x222,
237
238 0x223, 0x224, 0x225, 0x226, 0x227, 0x228, 0x229, 0x22a, // 8
239 0x22b, 0x22c, 0x22d, 0x22e, 0x22f, 0x230, 0x231, 0x232,
240 0x233, 0x234, 0x235, 0x236, 0x237, 0x238, 0x239, 0x23a,
241 0x23b, 0x23c, 0x23d, 0x23e, 0x23f, 0x240, 0x241, 0x242,
242
243 0x244, 0x245, 0x246, 0x247, 0x248, 0x249, 0x24a, 0x24b, // 9
244 0x24c, 0x24d, 0x24e, 0x24f, 0x250, 0x251, 0x252, 0x253,
245 0x254, 0x256, 0x257, 0x258, 0x259, 0x25a, 0x25b, 0x25c,
246 0x25d, 0x25e, 0x25f, 0x260, 0x262, 0x263, 0x264, 0x265,
247
248 0x266, 0x267, 0x268, 0x269, 0x26a, 0x26c, 0x26d, 0x26e, // 10
249 0x26f, 0x270, 0x271, 0x272, 0x273, 0x275, 0x276, 0x277,
250 0x278, 0x279, 0x27a, 0x27b, 0x27d, 0x27e, 0x27f, 0x280,
251 0x281, 0x282, 0x284, 0x285, 0x286, 0x287, 0x288, 0x289,
252
253 0x28b, 0x28c, 0x28d, 0x28e, 0x28f, 0x290, 0x292, 0x293, // 11
254 0x294, 0x295, 0x296, 0x298, 0x299, 0x29a, 0x29b, 0x29c,
255 0x29e, 0x29f, 0x2a0, 0x2a1, 0x2a2, 0x2a4, 0x2a5, 0x2a6,
256 0x2a7, 0x2a9, 0x2aa, 0x2ab, 0x2ac, 0x2ae, 0x2af, 0x2b0,
257
258 0x2b1, 0x2b2, 0x2b4, 0x2b5, 0x2b6, 0x2b7, 0x2b9, 0x2ba, // 12
259 0x2bb, 0x2bd, 0x2be, 0x2bf, 0x2c0, 0x2c2, 0x2c3, 0x2c4,
260 0x2c5, 0x2c7, 0x2c8, 0x2c9, 0x2cb, 0x2cc, 0x2cd, 0x2ce,
261 0x2d0, 0x2d1, 0x2d2, 0x2d4, 0x2d5, 0x2d6, 0x2d8, 0x2d9,
262
263 0x2da, 0x2dc, 0x2dd, 0x2de, 0x2e0, 0x2e1, 0x2e2, 0x2e4, // 13
264 0x2e5, 0x2e6, 0x2e8, 0x2e9, 0x2ea, 0x2ec, 0x2ed, 0x2ee,
265 0x2f0, 0x2f1, 0x2f2, 0x2f4, 0x2f5, 0x2f6, 0x2f8, 0x2f9,
266 0x2fb, 0x2fc, 0x2fd, 0x2ff, 0x300, 0x302, 0x303, 0x304,
267
268 0x306, 0x307, 0x309, 0x30a, 0x30b, 0x30d, 0x30e, 0x310, // 14
269 0x311, 0x312, 0x314, 0x315, 0x317, 0x318, 0x31a, 0x31b,
270 0x31c, 0x31e, 0x31f, 0x321, 0x322, 0x324, 0x325, 0x327,
271 0x328, 0x329, 0x32b, 0x32c, 0x32e, 0x32f, 0x331, 0x332,
272
273 0x334, 0x335, 0x337, 0x338, 0x33a, 0x33b, 0x33d, 0x33e, // 15
274 0x340, 0x341, 0x343, 0x344, 0x346, 0x347, 0x349, 0x34a,
275 0x34c, 0x34d, 0x34f, 0x350, 0x352, 0x353, 0x355, 0x357,
276 0x358, 0x35a, 0x35b, 0x35d, 0x35e, 0x360, 0x361, 0x363,
277
278 0x365, 0x366, 0x368, 0x369, 0x36b, 0x36c, 0x36e, 0x370, // 16
279 0x371, 0x373, 0x374, 0x376, 0x378, 0x379, 0x37b, 0x37c,
280 0x37e, 0x380, 0x381, 0x383, 0x384, 0x386, 0x388, 0x389,
281 0x38b, 0x38d, 0x38e, 0x390, 0x392, 0x393, 0x395, 0x397,
282
283 0x398, 0x39a, 0x39c, 0x39d, 0x39f, 0x3a1, 0x3a2, 0x3a4, // 17
284 0x3a6, 0x3a7, 0x3a9, 0x3ab, 0x3ac, 0x3ae, 0x3b0, 0x3b1,
285 0x3b3, 0x3b5, 0x3b7, 0x3b8, 0x3ba, 0x3bc, 0x3bd, 0x3bf,
286 0x3c1, 0x3c3, 0x3c4, 0x3c6, 0x3c8, 0x3ca, 0x3cb, 0x3cd,
287
288 // The last note has an incomplete range, and loops round back to
289 // the start. Note that the last value is actually a buffer overrun
290 // and does not fit with the other values.
291
292 0x3cf, 0x3d1, 0x3d2, 0x3d4, 0x3d6, 0x3d8, 0x3da, 0x3db, // 18
293 0x3dd, 0x3df, 0x3e1, 0x3e3, 0x3e4, 0x3e6, 0x3e8, 0x3ea,
294 0x3ec, 0x3ed, 0x3ef, 0x3f1, 0x3f3, 0x3f5, 0x3f6, 0x3f8,
295 0x3fa, 0x3fc, 0x3fe, 0x36c,
296 };
297
298 // Mapping from MIDI volume level to OPL level value.
299
300 static const unsigned int volume_mapping_table[] = {
301 0, 1, 3, 5, 6, 8, 10, 11,
302 13, 14, 16, 17, 19, 20, 22, 23,
303 25, 26, 27, 29, 30, 32, 33, 34,
304 36, 37, 39, 41, 43, 45, 47, 49,
305 50, 52, 54, 55, 57, 59, 60, 61,
306 63, 64, 66, 67, 68, 69, 71, 72,
307 73, 74, 75, 76, 77, 79, 80, 81,
308 82, 83, 84, 84, 85, 86, 87, 88,
309 89, 90, 91, 92, 92, 93, 94, 95,
310 96, 96, 97, 98, 99, 99, 100, 101,
311 101, 102, 103, 103, 104, 105, 105, 106,
312 107, 107, 108, 109, 109, 110, 110, 111,
313 112, 112, 113, 113, 114, 114, 115, 115,
314 116, 117, 117, 118, 118, 119, 119, 120,
315 120, 121, 121, 122, 122, 123, 123, 123,
316 124, 124, 125, 125, 126, 126, 127, 127
317 };
318
319 // DMX version to emulate for OPL emulation:
320 typedef enum {
321 opl_doom1_1_666, // Doom 1 v1.666
322 opl_doom2_1_666, // Doom 2 v1.666, Hexen, Heretic
323 opl_doom_1_9 // Doom v1.9, Strife
324 } opl_driver_ver_t;
325
326 static opl_driver_ver_t opl_drv_ver = opl_doom_1_9;
327 static dboolean music_initialized = false;
328
329 //static dboolean musicpaused = false;
330 static int start_music_volume;
331 static int current_music_volume;
332
333 // GENMIDI lump instrument data:
334
335 static const genmidi_instr_t *main_instrs;
336 static const genmidi_instr_t *percussion_instrs;
337 static char (*main_instr_names)[32];
338 static char (*percussion_names)[32];
339
340 // Voices:
341
342 static opl_voice_t voices[OPL_NUM_VOICES * 2];
343 static opl_voice_t *voice_free_list[OPL_NUM_VOICES * 2];
344 static opl_voice_t *voice_alloced_list[OPL_NUM_VOICES * 2];
345 static int voice_free_num;
346 static int voice_alloced_num;
347 static int opl_opl3mode;
348 static int num_opl_voices;
349
350 // Data for each channel.
351
352 static opl_channel_data_t channels[MIDI_CHANNELS_PER_TRACK];
353
354 // Track data for playing tracks:
355
356 static opl_track_data_t *tracks;
357 static unsigned int num_tracks = 0;
358 static unsigned int running_tracks = 0;
359 static dboolean song_looping;
360
361 // Tempo control variables
362
363 static unsigned int ticks_per_beat;
364 static unsigned int us_per_beat;
365
366 // If true, OPL sound channels are reversed to their correct arrangement
367 // (as intended by the MIDI standard) rather than the backwards one
368 // used by DMX due to a bug.
369
370 static dboolean opl_stereo_correct = false;
371
372 // Load instrument table from GENMIDI lump:
373
374 static dboolean LoadInstrumentTable(void)
375 {
376 const byte *lump;
377
378 lump = (const byte*)W_LumpByName("GENMIDI");
379
380 // Check header
381
382 if (strncmp((const char *) lump, GENMIDI_HEADER, strlen(GENMIDI_HEADER)) != 0)
383 {
384 return false;
385 }
386
387 main_instrs = (const genmidi_instr_t *) (lump + strlen(GENMIDI_HEADER));
388 percussion_instrs = main_instrs + GENMIDI_NUM_INSTRS;
389
390 // There's no way for c to define a const array
391 #pragma GCC diagnostic push
392 #pragma GCC diagnostic ignored "-Wcast-qual"
393 main_instr_names =
394 (char (*)[32]) (percussion_instrs + GENMIDI_NUM_PERCUSSION);
395 percussion_names = main_instr_names + GENMIDI_NUM_INSTRS;
396 #pragma GCC diagnostic pop
397
398 return true;
399 }
400
401 // Get the next available voice from the freelist.
402
403 static opl_voice_t *GetFreeVoice(void)
404 {
405 opl_voice_t *result;
406 int i;
407
408 // None available?
409
410 if (voice_free_num == 0)
411 {
412 return NULL;
413 }
414
415 // Remove from free list
416
417 result = voice_free_list[0];
418
419 voice_free_num--;
420
421 for (i = 0; i < voice_free_num; i++)
422 {
423 voice_free_list[i] = voice_free_list[i + 1];
424 }
425
426 // Add to allocated list
427
428 voice_alloced_list[voice_alloced_num++] = result;
429
430 return result;
431 }
432
433 // Release a voice back to the freelist.
434
435 static void VoiceKeyOff(opl_voice_t *voice);
436
437 static void ReleaseVoice(int index)
438 {
439 opl_voice_t *voice;
440 dboolean double_voice;
441 int i;
442
443 // Doom 2 1.666 OPL crash emulation.
444 if (index >= voice_alloced_num)
445 {
446
447 voice_alloced_num = 0;
448 voice_free_num = 0;
449 return;
450 }
451
452 voice = voice_alloced_list[index];
453
454 VoiceKeyOff(voice);
455
456 voice->channel = NULL;
457 voice->note = 0;
458
459 double_voice = voice->current_instr_voice != 0;
460
461 // Remove from alloced list.
462
463 voice_alloced_num--;
464
465 for (i = index; i < voice_alloced_num; i++)
466 {
467 voice_alloced_list[i] = voice_alloced_list[i + 1];
468 }
469
470 // Search to the end of the freelist (This is how Doom behaves!)
471
472 voice_free_list[voice_free_num++] = voice;
473
474 if (double_voice && opl_drv_ver < opl_doom_1_9)
475 {
476 ReleaseVoice(index);
477 }
478 }
479
480 // Load data to the specified Operator
481
482 static void LoadOperatorData(int Operator,
483 const genmidi_op_t *data,
484 dboolean max_level,
485 unsigned int *volume)
486 {
487 int level;
488
489 // The scale and level fields must be combined for the level register.
490 // For the carrier wave we always set the maximum level.
491
492 level = data->scale;
493
494 if (max_level)
495 {
496 level |= 0x3f;
497 }
498 else
499 {
500 level |= data->level;
501 }
502
503 *volume = level;
504
505 OPL_WriteRegister(OPL_REGS_LEVEL + Operator, level);
506 OPL_WriteRegister(OPL_REGS_TREMOLO + Operator, data->tremolo);
507 OPL_WriteRegister(OPL_REGS_ATTACK + Operator, data->attack);
508 OPL_WriteRegister(OPL_REGS_SUSTAIN + Operator, data->sustain);
509 OPL_WriteRegister(OPL_REGS_WAVEFORM + Operator, data->waveform);
510 }
511
512 // Set the instrument for a particular voice.
513
514 static void SetVoiceInstrument(opl_voice_t *voice,
515 const genmidi_instr_t *instr,
516 unsigned int instr_voice)
517 {
518 const genmidi_voice_t *data;
519 unsigned int modulating;
520
521 // Instrument already set for this channel?
522
523 if (voice->current_instr == instr
524 && voice->current_instr_voice == instr_voice)
525 {
526 return;
527 }
528
529 voice->current_instr = instr;
530 voice->current_instr_voice = instr_voice;
531
532 data = &instr->voices[instr_voice];
533
534 // Are we usind modulated feedback mode?
535
536 modulating = (data->feedback & 0x01) == 0;
537
538 // Doom loads the second operator first, then the first.
539 // The carrier is set to minimum volume until the voice volume
540 // is set in SetVoiceVolume (below). If we are not using
541 // modulating mode, we must set both to minimum volume.
542
543 LoadOperatorData(voice->op2 | voice->array, &data->carrier, true,
544 &voice->car_volume);
545 LoadOperatorData(voice->op1 | voice->array, &data->modulator, !modulating,
546 &voice->mod_volume);
547
548 // Set feedback register that control the connection between the
549 // two operators. Turn on bits in the upper nybble; I think this
550 // is for OPL3, where it turns on channel A/B.
551
552 OPL_WriteRegister((OPL_REGS_FEEDBACK + voice->index) | voice->array,
553 data->feedback | voice->reg_pan);
554
555 // Calculate voice priority.
556
557 voice->priority = 0x0f - (data->carrier.attack >> 4)
558 + 0x0f - (data->carrier.sustain & 0x0f);
559 }
560
561 static void SetVoiceVolume(opl_voice_t *voice, unsigned int volume)
562 {
563 const genmidi_voice_t *opl_voice;
564 unsigned int midi_volume;
565 unsigned int full_volume;
566 unsigned int car_volume;
567 unsigned int mod_volume;
568
569 voice->note_volume = volume;
570
571 opl_voice = &voice->current_instr->voices[voice->current_instr_voice];
572
573 // Multiply note volume and channel volume to get the actual volume.
574
575 midi_volume = 2 * (volume_mapping_table[voice->channel->volume] + 1);
576
577 full_volume = (volume_mapping_table[voice->note_volume] * midi_volume)
578 >> 9;
579
580 // The volume value to use in the register:
581 car_volume = 0x3f - full_volume;
582
583 // Update the volume register(s) if necessary.
584
585 if (car_volume != (voice->car_volume & 0x3f))
586 {
587 voice->car_volume = car_volume | (voice->car_volume & 0xc0);
588
589 OPL_WriteRegister((OPL_REGS_LEVEL + voice->op2) | voice->array,
590 voice->car_volume);
591
592 // If we are using non-modulated feedback mode, we must set the
593 // volume for both voices.
594
595 if ((opl_voice->feedback & 0x01) != 0
596 && opl_voice->modulator.level != 0x3f)
597 {
598 mod_volume = opl_voice->modulator.level;
599 if (mod_volume < car_volume)
600 {
601 mod_volume = car_volume;
602 }
603
604 mod_volume |= voice->mod_volume & 0xc0;
605
606 if(mod_volume != voice->mod_volume)
607 {
608 voice->mod_volume = mod_volume;
609 OPL_WriteRegister((OPL_REGS_LEVEL + voice->op1) | voice->array,
610 mod_volume |
611 (opl_voice->modulator.scale & 0xc0));
612 }
613 }
614 }
615 }
616
617 static void SetVoicePan(opl_voice_t *voice, unsigned int pan)
618 {
619 const genmidi_voice_t *opl_voice;
620
621 voice->reg_pan = pan;
622 opl_voice = &voice->current_instr->voices[voice->current_instr_voice];;
623
624 OPL_WriteRegister((OPL_REGS_FEEDBACK + voice->index) | voice->array,
625 opl_voice->feedback | pan);
626 }
627
628 // Initialize the voice table and freelist
629
630 static void InitVoices(void)
631 {
632 int i;
633
634 // Start with an empty free list.
635
636 voice_free_num = num_opl_voices;
637 voice_alloced_num = 0;
638
639 // Initialize each voice.
640
641 for (i = 0; i < num_opl_voices; ++i)
642 {
643 voices[i].index = i % OPL_NUM_VOICES;
644 voices[i].op1 = voice_operators[0][i % OPL_NUM_VOICES];
645 voices[i].op2 = voice_operators[1][i % OPL_NUM_VOICES];
646 voices[i].array = (i / OPL_NUM_VOICES) << 8;
647 voices[i].current_instr = NULL;
648
649 // Add this voice to the freelist.
650
651 voice_free_list[i] = &voices[i];
652 }
653 }
654
655 static void SetChannelVolume(opl_channel_data_t *channel, unsigned int volume,
656 dboolean clip_start);
657
658 // Set music volume (0 - 127)
659
660 static void I_OPL_SetMusicVolume(int volume)
661 {
662 int opl_vol = volume * 127 / 15; // OPL synth internally uses 0-127
663
664 unsigned int i;
665
666 // Internal state variable.
667
668 current_music_volume = opl_vol;
669
670 // Update the volume of all voices.
671
672 for (i = 0; i < MIDI_CHANNELS_PER_TRACK; ++i)
673 {
674 if (i == 15)
675 {
676 SetChannelVolume(&channels[i], opl_vol, false);
677 }
678 else
679 {
680 SetChannelVolume(&channels[i], channels[i].volume_base, false);
681 }
682 }
683 }
684
685 static void VoiceKeyOff(opl_voice_t *voice)
686 {
687 OPL_WriteRegister((OPL_REGS_FREQ_2 + voice->index) | voice->array,
688 voice->freq >> 8);
689 }
690
691 static opl_channel_data_t *TrackChannelForEvent(opl_track_data_t *track,
692 midi_event_t *event)
693 {
694 unsigned int channel_num = event->data.channel.channel;
695
696 // MIDI uses track #9 for percussion, but for MUS it's track #15
697 // instead. Because DMX works on MUS data internally, we need to
698 // swap back to the MUS version of the channel number.
699 if (channel_num == 9)
700 {
701 channel_num = 15;
702 }
703 else if (channel_num == 15)
704 {
705 channel_num = 9;
706 }
707
708 return &channels[channel_num];
709 }
710
711 // Get the frequency that we should be using for a voice.
712
713 static void KeyOffEvent(opl_track_data_t *track, midi_event_t *event)
714 {
715 opl_channel_data_t *channel;
716 unsigned int key;
717 unsigned int i;
718
719 /*
720 printf("note off: channel %i, %i, %i\n",
721 event->data.channel.channel,
722 event->data.channel.param1,
723 event->data.channel.param2);
724 */
725
726 channel = TrackChannelForEvent(track, event);
727 key = event->data.channel.param1;
728
729 // Turn off voices being used to play this key.
730 // If it is a double voice instrument there will be two.
731
732 for (i = 0; i < voice_alloced_num; i++)
733 {
734 if (voice_alloced_list[i]->channel == channel
735 && voice_alloced_list[i]->key == key)
736 {
737 // Finished with this voice now.
738
739 ReleaseVoice(i);
740
741 i--;
742 }
743 }
744 }
745
746 // When all voices are in use, we must discard an existing voice to
747 // play a new note. Find and free an existing voice. The channel
748 // passed to the function is the channel for the new note to be
749 // played.
750
751 static void ReplaceExistingVoice(void)
752 {
753 int i;
754 int result;
755
756 // Check the allocated voices, if we find an instrument that is
757 // of a lower priority to the new instrument, discard it.
758 // If a voice is being used to play the second voice of an instrument,
759 // use that, as second voices are non-essential.
760 // Lower numbered MIDI channels implicitly have a higher priority
761 // than higher-numbered channels, eg. MIDI channel 1 is never
762 // discarded for MIDI channel 2.
763
764 result = 0;
765
766 for (i = 0; i < voice_alloced_num; i++)
767 {
768 if (voice_alloced_list[i]->current_instr_voice != 0
769 || voice_alloced_list[i]->channel
770 >= voice_alloced_list[result]->channel)
771 {
772 result = i;
773 }
774 }
775
776 ReleaseVoice(result);
777 }
778
779 // Alternate versions of ReplaceExistingVoice() used when emulating old
780 // versions of the DMX library used in Doom 1.666, Heretic and Hexen.
781
782 static void ReplaceExistingVoiceDoom1(void)
783 {
784 int i;
785 int result;
786
787 result = 0;
788
789 for (i = 0; i < voice_alloced_num; i++)
790 {
791 if (voice_alloced_list[i]->channel
792 > voice_alloced_list[result]->channel)
793 {
794 result = i;
795 }
796 }
797
798 ReleaseVoice(result);
799 }
800
801 static void ReplaceExistingVoiceDoom2(opl_channel_data_t *channel)
802 {
803 int i;
804 int result;
805 int priority;
806
807 result = 0;
808
809 priority = 0x8000;
810
811 for (i = 0; i < voice_alloced_num - 3; i++)
812 {
813 if (voice_alloced_list[i]->priority < priority
814 && voice_alloced_list[i]->channel >= channel)
815 {
816 priority = voice_alloced_list[i]->priority;
817 result = i;
818 }
819 }
820
821 ReleaseVoice(result);
822 }
823
824
825 static unsigned int FrequencyForVoice(opl_voice_t *voice)
826 {
827 const genmidi_voice_t *gm_voice;
828 signed int freq_index;
829 unsigned int octave;
830 unsigned int sub_index;
831 signed int note;
832
833 note = voice->note;
834
835 // Apply note offset.
836 // Don't apply offset if the instrument is a fixed note instrument.
837
838 gm_voice = &voice->current_instr->voices[voice->current_instr_voice];
839
840 if ((LittleShort(voice->current_instr->flags) & GENMIDI_FLAG_FIXED) == 0)
841 {
842 note += (signed short) LittleShort(gm_voice->base_note_offset);
843 }
844
845 // Avoid possible overflow due to base note offset:
846
847 while (note < 0)
848 {
849 note += 12;
850 }
851
852 while (note > 95)
853 {
854 note -= 12;
855 }
856
857 freq_index = 64 + 32 * note + voice->channel->bend;
858
859 // If this is the second voice of a double voice instrument, the
860 // frequency index can be adjusted by the fine tuning field.
861
862 if (voice->current_instr_voice != 0)
863 {
864 freq_index += (voice->current_instr->fine_tuning / 2) - 64;
865 }
866
867 if (freq_index < 0)
868 {
869 freq_index = 0;
870 }
871
872 // The first 7 notes use the start of the table, while
873 // consecutive notes loop around the latter part.
874
875 if (freq_index < 284)
876 {
877 return frequency_curve[freq_index];
878 }
879
880 sub_index = (freq_index - 284) % (12 * 32);
881 octave = (freq_index - 284) / (12 * 32);
882
883 // Once the seventh octave is reached, things break down.
884 // We can only go up to octave 7 as a maximum anyway (the OPL
885 // register only has three bits for octave number), but for the
886 // notes in octave 7, the first five bits have octave=7, the
887 // following notes have octave=6. This 7/6 pattern repeats in
888 // following octaves (which are technically impossible to
889 // represent anyway).
890
891 if (octave >= 7)
892 {
893 octave = 7;
894 }
895
896 // Calculate the resulting register value to use for the frequency.
897
898 return frequency_curve[sub_index + 284] | (octave << 10);
899 }
900
901 // Update the frequency that a voice is programmed to use.
902
903 static void UpdateVoiceFrequency(opl_voice_t *voice)
904 {
905 unsigned int freq;
906
907 // Calculate the frequency to use for this voice and update it
908 // if neccessary.
909
910 freq = FrequencyForVoice(voice);
911
912 if (voice->freq != freq)
913 {
914 OPL_WriteRegister((OPL_REGS_FREQ_1 + voice->index) | voice->array,
915 freq & 0xff);
916 OPL_WriteRegister((OPL_REGS_FREQ_2 + voice->index) | voice->array,
917 (freq >> 8) | 0x20);
918
919 voice->freq = freq;
920 }
921 }
922
923 // Program a single voice for an instrument. For a double voice
924 // instrument (GENMIDI_FLAG_2VOICE), this is called twice for each
925 // key on event.
926
927 static void VoiceKeyOn(opl_channel_data_t *channel,
928 const genmidi_instr_t *instrument,
929 unsigned int instrument_voice,
930 unsigned int note,
931 unsigned int key,
932 unsigned int volume)
933 {
934 opl_voice_t *voice;
935
936 if (!opl_opl3mode && opl_drv_ver == opl_doom1_1_666)
937 {
938 instrument_voice = 0;
939 }
940
941 // Find a voice to use for this new note.
942
943 voice = GetFreeVoice();
944
945 if (voice == NULL)
946 {
947 return;
948 }
949
950 voice->channel = channel;
951 voice->key = key;
952
953 // Work out the note to use. This is normally the same as
954 // the key, unless it is a fixed pitch instrument.
955
956 if ((LittleShort(instrument->flags) & GENMIDI_FLAG_FIXED) != 0)
957 {
958 voice->note = instrument->fixed_note;
959 }
960 else
961 {
962 voice->note = note;
963 }
964
965 voice->reg_pan = channel->pan;
966
967 // Program the voice with the instrument data:
968
969 SetVoiceInstrument(voice, instrument, instrument_voice);
970
971 // Set the volume level.
972
973 SetVoiceVolume(voice, volume);
974
975 // Write the frequency value to turn the note on.
976
977 voice->freq = 0;
978 UpdateVoiceFrequency(voice);
979 }
980
981 static void KeyOnEvent(opl_track_data_t *track, midi_event_t *event)
982 {
983 const genmidi_instr_t *instrument;
984 opl_channel_data_t *channel;
985 unsigned int note, key, volume, voicenum;
986 dboolean double_voice;
987
988 /*
989 printf("note on: channel %i, %i, %i\n",
990 event->data.channel.channel,
991 event->data.channel.param1,
992 event->data.channel.param2);
993 */
994
995 note = event->data.channel.param1;
996 key = event->data.channel.param1;
997 volume = event->data.channel.param2;
998
999 // A volume of zero means key off. Some MIDI tracks, eg. the ones
1000 // in AV.wad, use a second key on with a volume of zero to mean
1001 // key off.
1002 if (volume <= 0)
1003 {
1004 KeyOffEvent(track, event);
1005 return;
1006 }
1007
1008 // The channel.
1009 channel = TrackChannelForEvent(track, event);
1010
1011 // Percussion channel is treated differently.
1012 if (event->data.channel.channel == 9)
1013 {
1014 if (key < 35 || key > 81)
1015 {
1016 return;
1017 }
1018
1019 instrument = &percussion_instrs[key - 35];
1020
1021 note = 60;
1022 }
1023 else
1024 {
1025 instrument = channel->instrument;
1026 }
1027
1028 double_voice = (LittleShort(instrument->flags) & GENMIDI_FLAG_2VOICE) != 0;
1029
1030 switch (opl_drv_ver)
1031 {
1032 case opl_doom1_1_666:
1033 voicenum = double_voice + 1;
1034 if (!opl_opl3mode)
1035 {
1036 voicenum = 1;
1037 }
1038 while (voice_alloced_num > num_opl_voices - voicenum)
1039 {
1040 ReplaceExistingVoiceDoom1();
1041 }
1042
1043 // Find and program a voice for this instrument. If this
1044 // is a double voice instrument, we must do this twice.
1045
1046 if (double_voice)
1047 {
1048 VoiceKeyOn(channel, instrument, 1, note, key, volume);
1049 }
1050
1051 VoiceKeyOn(channel, instrument, 0, note, key, volume);
1052 break;
1053 case opl_doom2_1_666:
1054 if (voice_alloced_num == num_opl_voices)
1055 {
1056 ReplaceExistingVoiceDoom2(channel);
1057 }
1058 if (voice_alloced_num == num_opl_voices - 1 && double_voice)
1059 {
1060 ReplaceExistingVoiceDoom2(channel);
1061 }
1062
1063 // Find and program a voice for this instrument. If this
1064 // is a double voice instrument, we must do this twice.
1065
1066 if (double_voice)
1067 {
1068 VoiceKeyOn(channel, instrument, 1, note, key, volume);
1069 }
1070
1071 VoiceKeyOn(channel, instrument, 0, note, key, volume);
1072 break;
1073 default:
1074 case opl_doom_1_9:
1075 if (voice_free_num == 0)
1076 {
1077 ReplaceExistingVoice();
1078 }
1079
1080 // Find and program a voice for this instrument. If this
1081 // is a double voice instrument, we must do this twice.
1082
1083 VoiceKeyOn(channel, instrument, 0, note, key, volume);
1084
1085 if (double_voice)
1086 {
1087 VoiceKeyOn(channel, instrument, 1, note, key, volume);
1088 }
1089 break;
1090 }
1091 }
1092
1093 static void ProgramChangeEvent(opl_track_data_t *track, midi_event_t *event)
1094 {
1095 opl_channel_data_t *channel;
1096 int instrument;
1097
1098 // Set the instrument used on this channel.
1099
1100 channel = TrackChannelForEvent(track, event);
1101 instrument = event->data.channel.param1;
1102 channel->instrument = &main_instrs[instrument];
1103
1104 // TODO: Look through existing voices that are turned on on this
1105 // channel, and change the instrument.
1106 }
1107
1108 static void SetChannelVolume(opl_channel_data_t *channel, unsigned int volume,
1109 dboolean clip_start)
1110 {
1111 unsigned int i;
1112
1113 channel->volume_base = volume;
1114
1115 if (volume > current_music_volume)
1116 {
1117 volume = current_music_volume;
1118 }
1119
1120 if (clip_start && volume > start_music_volume)
1121 {
1122 volume = start_music_volume;
1123 }
1124
1125 channel->volume = volume;
1126
1127 // Update all voices that this channel is using.
1128
1129 for (i = 0; i < num_opl_voices; ++i)
1130 {
1131 if (voices[i].channel == channel)
1132 {
1133 SetVoiceVolume(&voices[i], voices[i].note_volume);
1134 }
1135 }
1136 }
1137
1138 static void SetChannelPan(opl_channel_data_t *channel, unsigned int pan)
1139 {
1140 unsigned int reg_pan;
1141 unsigned int i;
1142
1143 // The DMX library has the stereo channels backwards, maybe because
1144 // Paul Radek had a Soundblaster card with the channels reversed, or
1145 // perhaps it was just a bug in the OPL3 support that was never
1146 // finished. By default we preserve this bug, but we also provide a
1147 // secret DMXOPTION to fix it.
1148 if (opl_stereo_correct)
1149 {
1150 pan = 144 - pan;
1151 }
1152
1153 if (opl_opl3mode)
1154 {
1155 if (pan >= 96)
1156 {
1157 reg_pan = 0x10;
1158 }
1159 else if (pan <= 48)
1160 {
1161 reg_pan = 0x20;
1162 }
1163 else
1164 {
1165 reg_pan = 0x30;
1166 }
1167 if (channel->pan != reg_pan)
1168 {
1169 channel->pan = reg_pan;
1170 for (i = 0; i < num_opl_voices; i++)
1171 {
1172 if (voices[i].channel == channel)
1173 {
1174 SetVoicePan(&voices[i], reg_pan);
1175 }
1176 }
1177 }
1178 }
1179 }
1180
1181 // Handler for the MIDI_CONTROLLER_ALL_NOTES_OFF channel event.
1182 static void AllNotesOff(opl_channel_data_t *channel, unsigned int param)
1183 {
1184 int i;
1185
1186 for (i = 0; i < voice_alloced_num; i++)
1187 {
1188 if (voice_alloced_list[i]->channel == channel)
1189 {
1190 // Finished with this voice now.
1191
1192 ReleaseVoice(i);
1193
1194 i--;
1195 }
1196 }
1197 }
1198
1199 static void ControllerEvent(opl_track_data_t *track, midi_event_t *event)
1200 {
1201 opl_channel_data_t *channel;
1202 unsigned int controller;
1203 unsigned int param;
1204
1205 /*
1206 printf("change controller: channel %i, %i, %i\n",
1207 event->data.channel.channel,
1208 event->data.channel.param1,
1209 event->data.channel.param2);
1210 */
1211
1212 channel = TrackChannelForEvent(track, event);
1213 controller = event->data.channel.param1;
1214 param = event->data.channel.param2;
1215
1216 switch (controller)
1217 {
1218 case MIDI_CONTROLLER_MAIN_VOLUME:
1219 SetChannelVolume(channel, param, true);
1220 break;
1221
1222 case MIDI_CONTROLLER_PAN:
1223 SetChannelPan(channel, param);
1224 break;
1225
1226 case MIDI_CONTROLLER_ALL_NOTES_OFF:
1227 AllNotesOff(channel, param);
1228 break;
1229
1230 default:
1231 #ifdef OPL_MIDI_DEBUG
1232 lprintf (LO_WARN, "Unknown MIDI controller type: %i\n", controller);
1233 #endif
1234 break;
1235 }
1236 }
1237
1238 // Process a pitch bend event.
1239
1240 static void PitchBendEvent(opl_track_data_t *track, midi_event_t *event)
1241 {
1242 opl_channel_data_t *channel;
1243 unsigned int i;
1244 opl_voice_t *voice_updated_list[OPL_NUM_VOICES * 2];
1245 unsigned int voice_updated_num = 0;
1246 opl_voice_t *voice_not_updated_list[OPL_NUM_VOICES * 2];
1247 unsigned int voice_not_updated_num = 0;
1248
1249 // Update the channel bend value. Only the MSB of the pitch bend
1250 // value is considered: this is what Doom does.
1251
1252 channel = TrackChannelForEvent(track, event);
1253 channel->bend = event->data.channel.param2 - 64;
1254
1255 // Update all voices for this channel.
1256
1257 for (i = 0; i < voice_alloced_num; ++i)
1258 {
1259 if (voice_alloced_list[i]->channel == channel)
1260 {
1261 UpdateVoiceFrequency(voice_alloced_list[i]);
1262 voice_updated_list[voice_updated_num++] = voice_alloced_list[i];
1263 }
1264 else
1265 {
1266 voice_not_updated_list[voice_not_updated_num++] =
1267 voice_alloced_list[i];
1268 }
1269 }
1270
1271 for (i = 0; i < voice_not_updated_num; i++)
1272 {
1273 voice_alloced_list[i] = voice_not_updated_list[i];
1274 }
1275
1276 for (i = 0; i < voice_updated_num; i++)
1277 {
1278 voice_alloced_list[i + voice_not_updated_num] = voice_updated_list[i];
1279 }
1280 }
1281
1282 static void MetaSetTempo(unsigned int tempo)
1283 {
1284 OPL_AdjustCallbacks((float) us_per_beat / tempo);
1285 us_per_beat = tempo;
1286 }
1287
1288 // Process a meta event.
1289
1290 static void MetaEvent(opl_track_data_t *track, midi_event_t *event)
1291 {
1292 byte *data = event->data.meta.data;
1293 unsigned int data_len = event->data.meta.length;
1294
1295 switch (event->data.meta.type)
1296 {
1297 // Things we can just ignore.
1298
1299 case MIDI_META_SEQUENCE_NUMBER:
1300 case MIDI_META_TEXT:
1301 case MIDI_META_COPYRIGHT:
1302 case MIDI_META_TRACK_NAME:
1303 case MIDI_META_INSTR_NAME:
1304 case MIDI_META_LYRICS:
1305 case MIDI_META_MARKER:
1306 case MIDI_META_CUE_POINT:
1307 case MIDI_META_SEQUENCER_SPECIFIC:
1308 break;
1309
1310 case MIDI_META_SET_TEMPO:
1311 if (data_len == 3)
1312 {
1313 MetaSetTempo((data[0] << 16) | (data[1] << 8) | data[2]);
1314 }
1315 break;
1316
1317 // End of track - actually handled when we run out of events
1318 // in the track, see below.
1319
1320 case MIDI_META_END_OF_TRACK:
1321 break;
1322
1323 default:
1324 #ifdef OPL_MIDI_DEBUG
1325 lprintf (LO_WARN, "Unknown MIDI meta event type: %i\n",
1326 event->data.meta.type);
1327 #endif
1328 break;
1329 }
1330 }
1331
1332 // Process a MIDI event from a track.
1333
1334 static void ProcessEvent(opl_track_data_t *track, midi_event_t *event)
1335 {
1336 switch (event->event_type)
1337 {
1338 case MIDI_EVENT_NOTE_OFF:
1339 KeyOffEvent(track, event);
1340 break;
1341
1342 case MIDI_EVENT_NOTE_ON:
1343 KeyOnEvent(track, event);
1344 break;
1345
1346 case MIDI_EVENT_CONTROLLER:
1347 ControllerEvent(track, event);
1348 break;
1349
1350 case MIDI_EVENT_PROGRAM_CHANGE:
1351 ProgramChangeEvent(track, event);
1352 break;
1353
1354 case MIDI_EVENT_PITCH_BEND:
1355 PitchBendEvent(track, event);
1356 break;
1357
1358 case MIDI_EVENT_META:
1359 MetaEvent(track, event);
1360 break;
1361
1362 // SysEx events can be ignored.
1363
1364 case MIDI_EVENT_SYSEX:
1365 case MIDI_EVENT_SYSEX_SPLIT:
1366 break;
1367
1368 default:
1369 #ifdef OPL_MIDI_DEBUG
1370 lprintf (LO_WARN, "Unknown MIDI event type %i\n", event->event_type);
1371 #endif
1372 break;
1373 }
1374 }
1375
1376 static void ScheduleTrack(opl_track_data_t *track);
1377 static void InitChannel(opl_channel_data_t *channel);
1378
1379 // Restart a song from the beginning.
1380
1381 static void RestartSong(void)
1382 {
1383 unsigned int i;
1384
1385 running_tracks = num_tracks;
1386
1387 start_music_volume = current_music_volume;
1388
1389 // fix buggy songs that forget to terminate notes held over loop point
1390 // sdl_mixer does this as well
1391 for (i=0; i<OPL_NUM_VOICES; ++i)
1392 {
1393 if (voices[i].channel != NULL
1394 && voices[i].current_instr < percussion_instrs)
1395 {
1396 VoiceKeyOff(&voices[i]);
1397 }
1398 }
1399
1400
1401 for (i=0; i<num_tracks; ++i)
1402 {
1403 MIDI_RestartIterator(tracks[i].iter);
1404 ScheduleTrack(&tracks[i]);
1405 }
1406
1407 for (i = 0; i < MIDI_CHANNELS_PER_TRACK; ++i)
1408 {
1409 InitChannel(&channels[i]);
1410 }
1411 }
1412
1413 // Callback function invoked when another event needs to be read from
1414 // a track.
1415
1416 static void TrackTimerCallback(void *arg)
1417 {
1418 opl_track_data_t *track = (opl_track_data_t*)arg;
1419 midi_event_t *event;
1420
1421 // Get the next event and process it.
1422
1423 if (!MIDI_GetNextEvent(track->iter, &event))
1424 {
1425 return;
1426 }
1427
1428 ProcessEvent(track, event);
1429
1430 // End of track?
1431
1432 if (event->event_type == MIDI_EVENT_META
1433 && event->data.meta.type == MIDI_META_END_OF_TRACK)
1434 {
1435 --running_tracks;
1436
1437 // When all tracks have finished, restart the song.
1438
1439 if (running_tracks <= 0 && song_looping)
1440 {
1441 RestartSong();
1442 }
1443
1444 return;
1445 }
1446
1447 // Reschedule the callback for the next event in the track.
1448
1449 ScheduleTrack(track);
1450 }
1451
1452 static void ScheduleTrack(opl_track_data_t *track)
1453 {
1454 unsigned int nticks;
1455 uint64_t us;
1456
1457 // Get the number of milliseconds until the next event.
1458
1459 nticks = MIDI_GetDeltaTime(track->iter);
1460 us = ((uint64_t) nticks * us_per_beat) / ticks_per_beat;
1461
1462 // Set a timer to be invoked when the next event is
1463 // ready to play.
1464
1465 OPL_SetCallback(us, TrackTimerCallback, track);
1466 }
1467
1468 // Initialize a channel.
1469
1470 static void InitChannel(opl_channel_data_t *channel)
1471 {
1472 // TODO: Work out sensible defaults?
1473
1474 channel->instrument = &main_instrs[0];
1475 channel->volume = current_music_volume;
1476 channel->volume_base = 100;
1477 if (channel->volume > channel->volume_base)
1478 {
1479 channel->volume = channel->volume_base;
1480 }
1481 channel->pan = 0x30;
1482 channel->bend = 0;
1483 }
1484
1485 // Start a MIDI track playing:
1486
1487 static void StartTrack(const midi_file_t *file, unsigned int track_num)
1488 {
1489 opl_track_data_t *track;
1490
1491 track = &tracks[track_num];
1492 track->iter = MIDI_IterateTrack(file, track_num);
1493
1494 // Schedule the first event.
1495
1496 ScheduleTrack(track);
1497 }
1498
1499 // Start playing a mid
1500
1501 static void I_OPL_PlaySong(const void *handle, int looping)
1502 {
1503 const midi_file_t *file;
1504 unsigned int i;
1505
1506 if (!music_initialized || handle == NULL)
1507 {
1508 return;
1509 }
1510
1511 // This is required to interface with OPL (I think)
1512 #pragma GCC diagnostic push
1513 #pragma GCC diagnostic ignored "-Wcast-qual"
1514 file = (midi_file_t*)handle;
1515 #pragma GCC diagnostic pop
1516
1517 // Allocate track data.
1518
1519 tracks = (opl_track_data_t*)Z_Malloc(MIDI_NumTracks(file) * sizeof(opl_track_data_t));
1520
1521 num_tracks = MIDI_NumTracks(file);
1522 running_tracks = num_tracks;
1523 song_looping = looping;
1524
1525 ticks_per_beat = MIDI_GetFileTimeDivision(file);
1526
1527 // Default is 120 bpm.
1528 // TODO: this is wrong
1529
1530 us_per_beat = 500 * 1000;
1531
1532 start_music_volume = current_music_volume = snd_MusicVolume * 127 / 15;
1533
1534 for (i = 0; i < num_tracks; ++i)
1535 {
1536 StartTrack(file, i);
1537 }
1538
1539 for (i = 0; i < MIDI_CHANNELS_PER_TRACK; ++i)
1540 {
1541 InitChannel(&channels[i]);
1542 }
1543 }
1544
1545 static void I_OPL_PauseSong(void)
1546 {
1547 unsigned int i;
1548
1549 if (!music_initialized)
1550 {
1551 return;
1552 }
1553
1554 // Pause OPL callbacks.
1555
1556 OPL_SetPaused(1);
1557
1558 // Turn off all main instrument voices (not percussion).
1559 // This is what Vanilla does.
1560
1561 for (i = 0; i < num_opl_voices; ++i)
1562 {
1563 if (voices[i].channel != NULL
1564 && voices[i].current_instr < percussion_instrs)
1565 {
1566 VoiceKeyOff(&voices[i]);
1567 }
1568 }
1569 }
1570
1571 static void I_OPL_ResumeSong(void)
1572 {
1573 if (!music_initialized)
1574 {
1575 return;
1576 }
1577
1578 OPL_SetPaused(0);
1579 }
1580
1581 static void I_OPL_StopSong(void)
1582 {
1583 unsigned int i;
1584
1585 if (!music_initialized)
1586 {
1587 return;
1588 }
1589
1590
1591 // Stop all playback.
1592
1593 OPL_ClearCallbacks();
1594
1595 // Free all voices.
1596
1597 for (i = 0; i < MIDI_CHANNELS_PER_TRACK; ++i)
1598 {
1599 AllNotesOff(&channels[i], 0);
1600 }
1601
1602 // Free all track data.
1603
1604 for (i=0; i<num_tracks; ++i)
1605 {
1606 MIDI_FreeIterator(tracks[i].iter);
1607 }
1608
1609 Z_Free(tracks);
1610
1611 tracks = NULL;
1612 num_tracks = 0;
1613
1614 }
1615
1616 static void I_OPL_UnRegisterSong(const void *handle)
1617 {
1618 if (!music_initialized)
1619 {
1620 return;
1621 }
1622
1623 if (handle != NULL)
1624 {
1625 // This is required to interface with OPL (I think)
1626 #pragma GCC diagnostic push
1627 #pragma GCC diagnostic ignored "-Wcast-qual"
1628 MIDI_FreeFile((midi_file_t *) handle);
1629 #pragma GCC diagnostic pop
1630 }
1631 }
1632
1633 // Determine whether memory block is a .mid file
1634
1635 static dboolean IsMid(byte *mem, int len)
1636 {
1637 return len > 4 && !memcmp(mem, "MThd", 4);
1638 }
1639
1640 // now only takes files in MIDI format
1641 static const void *I_OPL_RegisterSong(const void *data, unsigned len)
1642 {
1643 midi_file_t *result;
1644 midimem_t mf;
1645
1646 if (!music_initialized)
1647 {
1648 return NULL;
1649 }
1650 mf.len = len;
1651 mf.pos = 0;
1652 mf.data = data;
1653
1654 // NSM: if a file has a miniscule timecode we have to not load it.
1655 // if it's 0, we'll hang in scheduling and never finish. if it's
1656 // very small but close to 0, there's probably something wrong with it
1657
1658 // this check of course isn't very accurate, but to actually get the
1659 // time numbers we have to traverse the tracks and everything
1660 if (mf.len < 100)
1661 {
1662 lprintf (LO_WARN, "I_OPL_RegisterSong: Very short MIDI (%llu bytes)\n", (unsigned long long)mf.len);
1663 return NULL;
1664 }
1665
1666 result = MIDI_LoadFile(&mf);
1667
1668 if (result == NULL)
1669 {
1670 lprintf (LO_WARN, "I_OPL_RegisterSong: Failed to load MID.\n");
1671 }
1672
1673
1674 return result;
1675 }
1676
1677
1678 static void I_OPL_ShutdownMusic(void)
1679 {
1680 if (music_initialized)
1681 {
1682 // Stop currently-playing track, if there is one:
1683
1684 I_OPL_StopSong();
1685
1686 OPL_Shutdown();
1687
1688 music_initialized = false;
1689 }
1690 }
1691
1692 // Initialize music subsystem
1693
1694 int I_OPL_InitMusic(int samplerate)
1695 {
1696 opl_opl3mode = dsda_IntConfig(dsda_config_mus_opl_opl3mode);
1697
1698 if (!OPL_Init(samplerate))
1699 {
1700 //printf("Dude. The Adlib isn't responding.\n");
1701 return 0;
1702 }
1703
1704 if (opl_opl3mode)
1705 {
1706 num_opl_voices = OPL_NUM_VOICES * 2;
1707 }
1708 else
1709 {
1710 num_opl_voices = OPL_NUM_VOICES;
1711 }
1712
1713 // Load instruments from GENMIDI lump:
1714
1715 if (!LoadInstrumentTable())
1716 {
1717 OPL_Shutdown();
1718 return false;
1719 }
1720
1721 InitVoices();
1722
1723 tracks = NULL;
1724 num_tracks = 0;
1725 music_initialized = true;
1726
1727 return 1;
1728 }
1729
1730 const char *I_OPL_SynthName (void)
1731 {
1732 return "opl synth player";
1733 }
1734
1735 void I_OPL_RenderSamples (void *dest, unsigned nsamp)
1736 {
1737 OPL_Render_Samples (dest, nsamp);
1738 }
1739
1740 const music_player_t opl_synth_player =
1741 {
1742 I_OPL_SynthName,
1743 I_OPL_InitMusic,
1744 I_OPL_ShutdownMusic,
1745 I_OPL_SetMusicVolume,
1746 I_OPL_PauseSong,
1747 I_OPL_ResumeSong,
1748 I_OPL_RegisterSong,
1749 I_OPL_UnRegisterSong,
1750 I_OPL_PlaySong,
1751 I_OPL_StopSong,
1752 I_OPL_RenderSamples
1753 };