Debian Codegraph @

prboom2/src/mus2mid.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 // Copyright(C) 2006 Ben Ryves 2006
7 //
8 // This program is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU General Public License
10 // as published by the Free Software Foundation; either version 2
11 // of the License, or (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
21 // 02111-1307, USA.
22 //
23 // mus2mid.c - Ben Ryves 2006 - http://benryves.com - [email protected]
24 // Use to convert a MUS file into a single track, type 0 MIDI file.
25
26 // e6y
27 // All tabs are replaced with spaces.
28 // Fixed eol style of files.
29
30 #include <stdio.h>
31
32 #include "doomtype.h"
33 #include "m_swap.h"
34
35 #include "memio.h"
36 #include "mus2mid.h"
37
38 #define NUM_CHANNELS 16
39
40 #define MIDI_PERCUSSION_CHAN 9
41 #define MUS_PERCUSSION_CHAN 15
42
43 // MUS event codes
44 typedef enum
45 {
46 mus_releasekey = 0x00,
47 mus_presskey = 0x10,
48 mus_pitchwheel = 0x20,
49 mus_systemevent = 0x30,
50 mus_changecontroller = 0x40,
51 mus_scoreend = 0x60
52 } musevent;
53
54 // MIDI event codes
55 typedef enum
56 {
57 midi_releasekey = 0x80,
58 midi_presskey = 0x90,
59 midi_aftertouchkey = 0xA0,
60 midi_changecontroller = 0xB0,
61 midi_changepatch = 0xC0,
62 midi_aftertouchchannel = 0xD0,
63 midi_pitchwheel = 0xE0
64 } midievent;
65
66 // Standard MIDI type 0 header + track header
67 static const byte midiheader[] =
68 {
69 'M', 'T', 'h', 'd', // Main header
70 0x00, 0x00, 0x00, 0x06, // Header size
71 0x00, 0x00, // MIDI type (0)
72 0x00, 0x01, // Number of tracks
73 0x00, 0x46, // Resolution
74 'M', 'T', 'r', 'k', // Start of track
75 0x00, 0x00, 0x00, 0x00 // Placeholder for track length
76 };
77
78 // Cached channel velocities
79 static byte channelvelocities[] =
80 {
81 127, 127, 127, 127, 127, 127, 127, 127,
82 127, 127, 127, 127, 127, 127, 127, 127
83 };
84
85 // Timestamps between sequences of MUS events
86
87 static unsigned int queuedtime = 0;
88
89 // Counter for the length of the track
90
91 static unsigned int tracksize;
92
93 static const byte controller_map[] =
94 {
95 0x00, 0x20, 0x01, 0x07, 0x0A, 0x0B, 0x5B, 0x5D,
96 0x40, 0x43, 0x78, 0x7B, 0x7E, 0x7F, 0x79
97 };
98
99 static int channel_map[NUM_CHANNELS];
100
101 // Write timestamp to a MIDI file.
102
103 static dboolean WriteTime(unsigned int time, MEMFILE *midioutput)
104 {
105 unsigned int buffer = time & 0x7F;
106 byte writeval;
107
108 while ((time >>= 7) != 0)
109 {
110 buffer <<= 8;
111 buffer |= ((time & 0x7F) | 0x80);
112 }
113
114 for (;;)
115 {
116 writeval = (byte)(buffer & 0xFF);
117
118 if (mem_fwrite(&writeval, 1, 1, midioutput) != 1)
119 {
120 return true;
121 }
122
123 ++tracksize;
124
125 if ((buffer & 0x80) != 0)
126 {
127 buffer >>= 8;
128 }
129 else
130 {
131 queuedtime = 0;
132 return false;
133 }
134 }
135 }
136
137
138 // Write the end of track marker
139 static dboolean WriteEndTrack(MEMFILE *midioutput)
140 {
141 byte endtrack[] = {0xFF, 0x2F, 0x00};
142
143 if (WriteTime(queuedtime, midioutput))
144 {
145 return true;
146 }
147
148 if (mem_fwrite(endtrack, 1, 3, midioutput) != 3)
149 {
150 return true;
151 }
152
153 tracksize += 3;
154 return false;
155 }
156
157 // Write a key press event
158 static dboolean WritePressKey(byte channel, byte key,
159 byte velocity, MEMFILE *midioutput)
160 {
161 byte working = midi_presskey | channel;
162
163 if (WriteTime(queuedtime, midioutput))
164 {
165 return true;
166 }
167
168 if (mem_fwrite(&working, 1, 1, midioutput) != 1)
169 {
170 return true;
171 }
172
173 working = key & 0x7F;
174
175 if (mem_fwrite(&working, 1, 1, midioutput) != 1)
176 {
177 return true;
178 }
179
180 working = velocity & 0x7F;
181
182 if (mem_fwrite(&working, 1, 1, midioutput) != 1)
183 {
184 return true;
185 }
186
187 tracksize += 3;
188
189 return false;
190 }
191
192 // Write a key release event
193 static dboolean WriteReleaseKey(byte channel, byte key,
194 MEMFILE *midioutput)
195 {
196 byte working = midi_releasekey | channel;
197
198 if (WriteTime(queuedtime, midioutput))
199 {
200 return true;
201 }
202
203 if (mem_fwrite(&working, 1, 1, midioutput) != 1)
204 {
205 return true;
206 }
207
208 working = key & 0x7F;
209
210 if (mem_fwrite(&working, 1, 1, midioutput) != 1)
211 {
212 return true;
213 }
214
215 working = 0;
216
217 if (mem_fwrite(&working, 1, 1, midioutput) != 1)
218 {
219 return true;
220 }
221
222 tracksize += 3;
223
224 return false;
225 }
226
227 // Write a pitch wheel/bend event
228 static dboolean WritePitchWheel(byte channel, short wheel,
229 MEMFILE *midioutput)
230 {
231 byte working = midi_pitchwheel | channel;
232
233 if (WriteTime(queuedtime, midioutput))
234 {
235 return true;
236 }
237
238 if (mem_fwrite(&working, 1, 1, midioutput) != 1)
239 {
240 return true;
241 }
242
243 working = wheel & 0x7F;
244
245 if (mem_fwrite(&working, 1, 1, midioutput) != 1)
246 {
247 return true;
248 }
249
250 working = (wheel >> 7) & 0x7F;
251
252 if (mem_fwrite(&working, 1, 1, midioutput) != 1)
253 {
254 return true;
255 }
256
257 tracksize += 3;
258 return false;
259 }
260
261 // Write a patch change event
262 static dboolean WriteChangePatch(byte channel, byte patch,
263 MEMFILE *midioutput)
264 {
265 byte working = midi_changepatch | channel;
266
267 if (WriteTime(queuedtime, midioutput))
268 {
269 return true;
270 }
271
272 if (mem_fwrite(&working, 1, 1, midioutput) != 1)
273 {
274 return true;
275 }
276
277 working = patch & 0x7F;
278
279 if (mem_fwrite(&working, 1, 1, midioutput) != 1)
280 {
281 return true;
282 }
283
284 tracksize += 2;
285
286 return false;
287 }
288
289 // Write a valued controller change event
290
291 static dboolean WriteChangeController_Valued(byte channel,
292 byte control,
293 byte value,
294 MEMFILE *midioutput)
295 {
296 byte working = midi_changecontroller | channel;
297
298 if (WriteTime(queuedtime, midioutput))
299 {
300 return true;
301 }
302
303 if (mem_fwrite(&working, 1, 1, midioutput) != 1)
304 {
305 return true;
306 }
307
308 working = control & 0x7F;
309
310 if (mem_fwrite(&working, 1, 1, midioutput) != 1)
311 {
312 return true;
313 }
314
315 // Quirk in vanilla DOOM? MUS controller values should be
316 // 7-bit, not 8-bit.
317
318 working = value;// & 0x7F;
319
320 // Fix on said quirk to stop MIDI players from complaining that
321 // the value is out of range:
322
323 if (working & 0x80)
324 {
325 working = 0x7F;
326 }
327
328 if (mem_fwrite(&working, 1, 1, midioutput) != 1)
329 {
330 return true;
331 }
332
333 tracksize += 3;
334
335 return false;
336 }
337
338 // Write a valueless controller change event
339 static dboolean WriteChangeController_Valueless(byte channel,
340 byte control,
341 MEMFILE *midioutput)
342 {
343 return WriteChangeController_Valued(channel, control, 0,
344 midioutput);
345 }
346
347 // Allocate a free MIDI channel.
348
349 static int AllocateMIDIChannel(void)
350 {
351 int result;
352 int max;
353 int i;
354
355 // Find the current highest-allocated channel.
356
357 max = -1;
358
359 for (i=0; i<NUM_CHANNELS; ++i)
360 {
361 if (channel_map[i] > max)
362 {
363 max = channel_map[i];
364 }
365 }
366
367 // max is now equal to the highest-allocated MIDI channel. We can
368 // now allocate the next available channel. This also works if
369 // no channels are currently allocated (max=-1)
370
371 result = max + 1;
372
373 // Don't allocate the MIDI percussion channel!
374
375 if (result == MIDI_PERCUSSION_CHAN)
376 {
377 ++result;
378 }
379
380 return result;
381 }
382
383 // Given a MUS channel number, get the MIDI channel number to use
384 // in the outputted file.
385
386 static int GetMIDIChannel(int mus_channel, MEMFILE *midioutput)
387 {
388 // Find the MIDI channel to use for this MUS channel.
389 // MUS channel 15 is the percusssion channel.
390
391 if (mus_channel == MUS_PERCUSSION_CHAN)
392 {
393 return MIDI_PERCUSSION_CHAN;
394 }
395 else
396 {
397 // If a MIDI channel hasn't been allocated for this MUS channel
398 // yet, allocate the next free MIDI channel.
399
400 if (channel_map[mus_channel] == -1)
401 {
402 channel_map[mus_channel] = AllocateMIDIChannel();
403
404 // First time using the channel, send an "all notes off"
405 // event. This fixes "The D_DDTBLU disease" described here:
406 // https://www.doomworld.com/vb/source-ports/66802-the
407 WriteChangeController_Valueless(channel_map[mus_channel], 0x7b,
408 midioutput);
409 }
410
411 return channel_map[mus_channel];
412 }
413 }
414
415 static dboolean ReadMusHeader(MEMFILE *file, musheader *header)
416 {
417 dboolean result;
418
419 result = mem_fread(&header->id, sizeof(byte), 4, file) == 4
420 && mem_fread(&header->scorelength, sizeof(short), 1, file) == 1
421 && mem_fread(&header->scorestart, sizeof(short), 1, file) == 1
422 && mem_fread(&header->primarychannels, sizeof(short), 1, file) == 1
423 && mem_fread(&header->secondarychannels, sizeof(short), 1, file) == 1
424 && mem_fread(&header->instrumentcount, sizeof(short), 1, file) == 1;
425
426 if (result)
427 {
428 header->scorelength = LittleShort(header->scorelength);
429 header->scorestart = LittleShort(header->scorestart);
430 header->primarychannels = LittleShort(header->primarychannels);
431 header->secondarychannels = LittleShort(header->secondarychannels);
432 header->instrumentcount = LittleShort(header->instrumentcount);
433 }
434
435 return result;
436 }
437
438
439 // Read a MUS file from a stream (musinput) and output a MIDI file to
440 // a stream (midioutput).
441 //
442 // Returns 0 on success or 1 on failure.
443
444 dboolean mus2mid(MEMFILE *musinput, MEMFILE *midioutput)
445 {
446 // Header for the MUS file
447 musheader musfileheader;
448
449 // Descriptor for the current MUS event
450 byte eventdescriptor;
451 int channel; // Channel number
452 musevent event;
453
454
455 // Bunch of vars read from MUS lump
456 byte key;
457 byte controllernumber;
458 byte controllervalue;
459
460 // Buffer used for MIDI track size record
461 byte tracksizebuffer[4];
462
463 // Flag for when the score end marker is hit.
464 int hitscoreend = 0;
465
466 // Temp working byte
467 byte working;
468 // Used in building up time delays
469 unsigned int timedelay;
470
471 // Initialise channel map to mark all channels as unused.
472
473 for (channel=0; channel<NUM_CHANNELS; ++channel)
474 {
475 channel_map[channel] = -1;
476 }
477
478 // Grab the header
479
480 if (!ReadMusHeader(musinput, &musfileheader))
481 {
482 return true;
483 }
484
485 // Check MUS header
486 if (musfileheader.id[0] != 'M'
487 || musfileheader.id[1] != 'U'
488 || musfileheader.id[2] != 'S'
489 || musfileheader.id[3] != 0x1A)
490 {
491 return true;
492 }
493
494 // Seek to where the data is held
495 if (mem_fseek(musinput, (long)musfileheader.scorestart,
496 MEM_SEEK_SET) != 0)
497 {
498 return true;
499 }
500
501 // So, we can assume the MUS file is faintly legit. Let's start
502 // writing MIDI data...
503
504 mem_fwrite(midiheader, 1, sizeof(midiheader), midioutput);
505 tracksize = 0;
506
507 // Now, process the MUS file:
508 while (!hitscoreend)
509 {
510 // Handle a block of events:
511
512 while (!hitscoreend)
513 {
514 // Fetch channel number and event code:
515
516 if (mem_fread(&eventdescriptor, 1, 1, musinput) != 1)
517 {
518 return true;
519 }
520
521 channel = GetMIDIChannel(eventdescriptor & 0x0F, midioutput);
522 event = eventdescriptor & 0x70;
523
524 switch (event)
525 {
526 case mus_releasekey:
527 if (mem_fread(&key, 1, 1, musinput) != 1)
528 {
529 return true;
530 }
531
532 if (WriteReleaseKey(channel, key, midioutput))
533 {
534 return true;
535 }
536
537 break;
538
539 case mus_presskey:
540 if (mem_fread(&key, 1, 1, musinput) != 1)
541 {
542 return true;
543 }
544
545 if (key & 0x80)
546 {
547 if (mem_fread(&channelvelocities[channel], 1, 1, musinput) != 1)
548 {
549 return true;
550 }
551
552 channelvelocities[channel] &= 0x7F;
553 }
554
555 if (WritePressKey(channel, key,
556 channelvelocities[channel], midioutput))
557 {
558 return true;
559 }
560
561 break;
562
563 case mus_pitchwheel:
564 if (mem_fread(&key, 1, 1, musinput) != 1)
565 {
566 break;
567 }
568 if (WritePitchWheel(channel, (short)(key * 64), midioutput))
569 {
570 return true;
571 }
572
573 break;
574
575 case mus_systemevent:
576 if (mem_fread(&controllernumber, 1, 1, musinput) != 1)
577 {
578 return true;
579 }
580 if (controllernumber < 10 || controllernumber > 14)
581 {
582 return true;
583 }
584
585 if (WriteChangeController_Valueless(channel,
586 controller_map[controllernumber],
587 midioutput))
588 {
589 return true;
590 }
591
592 break;
593
594 case mus_changecontroller:
595 if (mem_fread(&controllernumber, 1, 1, musinput) != 1)
596 {
597 return true;
598 }
599
600 if (mem_fread(&controllervalue, 1, 1, musinput) != 1)
601 {
602 return true;
603 }
604
605 if (controllernumber == 0)
606 {
607 if (WriteChangePatch(channel, controllervalue,
608 midioutput))
609 {
610 return true;
611 }
612 }
613 else
614 {
615 if (controllernumber < 1 || controllernumber > 9)
616 {
617 return true;
618 }
619
620 if (WriteChangeController_Valued(channel,
621 controller_map[controllernumber],
622 controllervalue,
623 midioutput))
624 {
625 return true;
626 }
627 }
628
629 break;
630
631 case mus_scoreend:
632 hitscoreend = 1;
633 break;
634
635 default:
636 return true;
637 break;
638 }
639
640 if (eventdescriptor & 0x80)
641 {
642 break;
643 }
644 }
645 // Now we need to read the time code:
646 if (!hitscoreend)
647 {
648 timedelay = 0;
649 for (;;)
650 {
651 if (mem_fread(&working, 1, 1, musinput) != 1)
652 {
653 return true;
654 }
655
656 timedelay = timedelay * 128 + (working & 0x7F);
657 if ((working & 0x80) == 0)
658 {
659 break;
660 }
661 }
662 queuedtime += timedelay;
663 }
664 }
665
666 // End of track
667 if (WriteEndTrack(midioutput))
668 {
669 return true;
670 }
671
672 // Write the track size into the stream
673 if (mem_fseek(midioutput, 18, MEM_SEEK_SET))
674 {
675 return true;
676 }
677
678 tracksizebuffer[0] = (tracksize >> 24) & 0xff;
679 tracksizebuffer[1] = (tracksize >> 16) & 0xff;
680 tracksizebuffer[2] = (tracksize >> 8) & 0xff;
681 tracksizebuffer[3] = tracksize & 0xff;
682
683 if (mem_fwrite(tracksizebuffer, 1, 4, midioutput) != 4)
684 {
685 return true;
686 }
687
688 return false;
689 }