forked from RobertBeckebans/RBDOOM-3-BFG
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmus2midi.cpp
357 lines (304 loc) · 9.5 KB
/
mus2midi.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/*
===========================================================================
Doom 3 BFG Edition GPL Source Code
Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company.
This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").
Doom 3 BFG Edition Source Code is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Doom 3 BFG Edition Source Code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Doom 3 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>.
In addition, the Doom 3 BFG Edition Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 BFG Edition Source Code. If not, please request a copy in writing from id Software at the address below.
If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
===========================================================================
*/
#include "Precompiled.h"
#include "globaldata.h"
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
// mus header
// reads a variable length integer
unsigned long ReadVarLen( char* buffer ) {
unsigned long value;
byte c;
if ((value = *buffer++) & 0x80) {
value &= 0x7f;
do {
value = (value << 7) + ((c = *buffer++) & 0x7f);
} while (c & 0x80);
}
return value;
}
// Writes a variable length integer to a buffer, and returns bytes written
int WriteVarLen( long value, byte* out )
{
long buffer, count = 0;
buffer = value & 0x7f;
while ((value >>= 7) > 0) {
buffer <<= 8;
buffer += 0x80;
buffer += (value & 0x7f);
}
while (1) {
++count;
*out = (byte)buffer;
++out;
if (buffer & 0x80)
buffer >>= 8;
else
break;
}
return count;
}
// writes a byte, and returns the buffer
unsigned char* WriteByte(void* buf, byte b)
{
unsigned char* buffer = (unsigned char*)buf;
*buffer++ = b;
return buffer;
}
unsigned char* WriteShort(void* b, unsigned short s)
{
unsigned char* buffer = (unsigned char*)b;
*buffer++ = (s >> 8);
*buffer++ = (s & 0x00FF);
return buffer;
}
unsigned char* WriteInt(void* b, unsigned int i)
{
unsigned char* buffer = (unsigned char*)b;
*buffer++ = (i & 0xff000000) >> 24;
*buffer++ = (i & 0x00ff0000) >> 16;
*buffer++ = (i & 0x0000ff00) >> 8;
*buffer++ = (i & 0x000000ff);
return buffer;
}
// Format - 0(1 track only), 1(1 or more tracks, each play same time), 2(1 or more, each play seperatly)
void Midi_CreateHeader(MidiHeaderChunk_t* header, short format, short track_count, short division)
{
WriteInt(header->name, 'MThd');
WriteInt(&header->length, 6);
WriteShort(&header->format, format);
WriteShort(&header->ntracks, track_count);
WriteShort(&header->division, division);
}
unsigned char* Midi_WriteTempo(unsigned char* buffer, int tempo)
{
buffer = WriteByte(buffer, 0x00); // delta time
buffer = WriteByte(buffer, 0xff); // sys command
buffer = WriteShort(buffer, 0x5103); // command - set tempo
buffer = WriteByte(buffer, tempo & 0x000000ff);
buffer = WriteByte(buffer, (tempo & 0x0000ff00) >> 8);
buffer = WriteByte(buffer, (tempo & 0x00ff0000) >> 16);
return buffer;
}
int Midi_UpdateBytesWritten(int* bytes_written, int to_add, int max)
{
*bytes_written += to_add;
if (max && *bytes_written > max)
{
assert(0);
return 0;
}
return 1;
}
unsigned char MidiMap[] =
{
0, // prog change
0, // bank sel
1, //2 // mod pot
0x07, //3 // volume
0x0A, //4 // pan pot
0x0B, //5 // expression pot
0x5B, //6 // reverb depth
0x5D, //7 // chorus depth
0x40, //8 // sustain pedal
0x43, //9 // soft pedal
0x78, //10 // all sounds off
0x7B, //11 // all notes off
0x7E, //12 // mono(use numchannels + 1)
0x7F, //13 // poly
0x79, //14 // reset all controllers
};
// The MUS data is stored in little-endian.
namespace {
unsigned short LittleToNative( const unsigned short value ) {
return value;
}
}
int Mus2Midi(unsigned char* bytes, unsigned char* out, int* len)
{
// mus header and instruments
MUSheader_t header;
// current position in read buffer
unsigned char* cur = bytes,* end;
// Midi header(format 0)
MidiHeaderChunk_t midiHeader;
// Midi track header, only 1 needed(format 0)
MidiTrackChunk_t midiTrackHeader;
// Stores the position of the midi track header(to change the size)
byte* midiTrackHeaderOut;
// Delta time for midi event
int delta_time = 0;
int temp;
int channel_volume[MIDI_MAXCHANNELS] = {0};
int bytes_written = 0;
int channelMap[MIDI_MAXCHANNELS], currentChannel = 0;
byte last_status = 0;
// read the mus header
memcpy(&header, cur, sizeof(header));
cur += sizeof(header);
header.scoreLen = LittleToNative( header.scoreLen );
header.scoreStart = LittleToNative( header.scoreStart );
header.channels = LittleToNative( header.channels );
header.sec_channels = LittleToNative( header.sec_channels );
header.instrCnt = LittleToNative( header.instrCnt );
header.dummy = LittleToNative( header.dummy );
// only 15 supported
if (header.channels > MIDI_MAXCHANNELS - 1)
return 0;
// Map channel 15 to 9(percussions)
for (temp = 0; temp < MIDI_MAXCHANNELS; ++temp) {
channelMap[temp] = -1;
channel_volume[temp] = 0x40;
}
channelMap[15] = 9;
// Get current position, and end of position
cur = bytes + header.scoreStart;
end = cur + header.scoreLen;
// Write out midi header
Midi_CreateHeader(&midiHeader, 0, 1, 0x0059);
Midi_UpdateBytesWritten(&bytes_written, MIDIHEADERSIZE, *len);
memcpy(out, &midiHeader, MIDIHEADERSIZE); // cannot use sizeof(packs it to 16 bytes)
out += MIDIHEADERSIZE;
// Store this position, for later filling in the midiTrackHeader
Midi_UpdateBytesWritten(&bytes_written, sizeof(midiTrackHeader), *len);
midiTrackHeaderOut = out;
out += sizeof(midiTrackHeader);
// microseconds per quarter note(yikes)
Midi_UpdateBytesWritten(&bytes_written, 7, *len);
out = Midi_WriteTempo(out, 0x001aa309);
// Percussions channel starts out at full volume
Midi_UpdateBytesWritten(&bytes_written, 4, *len);
out = WriteByte(out, 0x00);
out = WriteByte(out, 0xB9);
out = WriteByte(out, 0x07);
out = WriteByte(out, 127);
// Main Loop
while (cur < end) {
byte channel;
byte event;
byte temp_buffer[32]; // temp buffer for current iterator
byte *out_local = temp_buffer;
byte status, bit1, bit2, bitc = 2;
// Read in current bit
event = *cur++;
channel = (event & 15); // current channel
// Write variable length delta time
out_local += WriteVarLen(delta_time, out_local);
if (channelMap[channel] < 0) {
// Set all channels to 127 volume
out_local = WriteByte(out_local, 0xB0 + currentChannel);
out_local = WriteByte(out_local, 0x07);
out_local = WriteByte(out_local, 127);
out_local = WriteByte(out_local, 0x00);
channelMap[channel] = currentChannel++;
if (currentChannel == 9)
++currentChannel;
}
status = channelMap[channel];
// Handle ::g->events
switch ((event & 122) >> 4)
{
default:
assert(0);
break;
case MUSEVENT_KEYOFF:
status |= 0x80;
bit1 = *cur++;
bit2 = 0x40;
break;
case MUSEVENT_KEYON:
status |= 0x90;
bit1 = *cur & 127;
if (*cur++ & 128) // volume bit?
channel_volume[channelMap[channel]] = *cur++;
bit2 = channel_volume[channelMap[channel]];
break;
case MUSEVENT_PITCHWHEEL:
status |= 0xE0;
bit1 = (*cur & 1) >> 6;
bit2 = (*cur++ >> 1) & 127;
break;
case MUSEVENT_CHANNELMODE:
status |= 0xB0;
assert(*cur < sizeof(MidiMap) / sizeof(MidiMap[0]));
bit1 = MidiMap[*cur++];
bit2 = (*cur++ == 12) ? header.channels + 1 : 0x00;
break;
case MUSEVENT_CONTROLLERCHANGE:
if (*cur == 0) {
cur++;
status |= 0xC0;
bit1 = *cur++;
bitc = 1;
} else {
status |= 0xB0;
assert(*cur < sizeof(MidiMap) / sizeof(MidiMap[0]));
bit1 = MidiMap[*cur++];
bit2 = *cur++;
}
break;
case 5: // Unknown
assert(0);
break;
case MUSEVENT_END: // End
status = 0xff;
bit1 = 0x2f;
bit2 = 0x00;
assert(cur == end);
break;
case 7: // Unknown
assert(0);
break;
}
// Write it out
out_local = WriteByte(out_local, status);
out_local = WriteByte(out_local, bit1);
if (bitc == 2)
out_local = WriteByte(out_local, bit2);
// Write out temp stuff
if (out_local != temp_buffer)
{
Midi_UpdateBytesWritten(&bytes_written, out_local - temp_buffer, *len);
memcpy(out, temp_buffer, out_local - temp_buffer);
out += out_local - temp_buffer;
}
if (event & 128) {
delta_time = 0;
do {
delta_time = delta_time * 128 + (*cur & 127);
} while ((*cur++ & 128));
} else {
delta_time = 0;
}
}
// Write out track header
WriteInt(midiTrackHeader.name, 'MTrk');
WriteInt(&midiTrackHeader.length, out - midiTrackHeaderOut - sizeof(midiTrackHeader));
memcpy(midiTrackHeaderOut, &midiTrackHeader, sizeof(midiTrackHeader));
// Store length written
*len = bytes_written;
/*{
FILE* file = f o pen("d:\\test.midi", "wb");
fwrite(midiTrackHeaderOut - sizeof(MidiHeaderChunk_t), bytes_written, 1, file);
fclose(file);
}*/
return 1;
}