| 1 |
// Emacs style mode select -*- C -*- |
| 2 |
//----------------------------------------------------------------------------- |
| 3 |
// |
| 4 |
// Copyright(C) 2009 Simon Howard |
| 5 |
// |
| 6 |
// This program is free software; you can redistribute it and/or |
| 7 |
// modify it under the terms of the GNU General Public License |
| 8 |
// as published by the Free Software Foundation; either version 2 |
| 9 |
// of the License, or (at your option) any later version. |
| 10 |
// |
| 11 |
// This program is distributed in the hope that it will be useful, |
| 12 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
// GNU General Public License for more details. |
| 15 |
// |
| 16 |
// You should have received a copy of the GNU General Public License |
| 17 |
// along with this program; if not, write to the Free Software |
| 18 |
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
| 19 |
// 02111-1307, USA. |
| 20 |
// |
| 21 |
// DESCRIPTION: |
| 22 |
// Reading of MIDI files. |
| 23 |
// |
| 24 |
//----------------------------------------------------------------------------- |
| 25 |
|
| 26 |
#include <stdio.h> |
| 27 |
#include <stdlib.h> |
| 28 |
#include <string.h> |
| 29 |
#include <assert.h> |
| 30 |
|
| 31 |
#ifndef TEST |
| 32 |
#include "doomdef.h" |
| 33 |
#include "doomtype.h" |
| 34 |
#else |
| 35 |
typedef enum {false, true} dboolean; |
| 36 |
typedef unsigned char byte; |
| 37 |
#define PACKEDATTR __attribute__((packed)) |
| 38 |
#endif |
| 39 |
#include "lprintf.h" |
| 40 |
#include "midifile.h" |
| 41 |
|
| 42 |
#define HEADER_CHUNK_ID "MThd" |
| 43 |
#define TRACK_CHUNK_ID "MTrk" |
| 44 |
#define MAX_BUFFER_SIZE 0x10000 |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
#ifdef HAVE_CONFIG_H |
| 51 |
#include "config.h" |
| 52 |
#endif |
| 53 |
|
| 54 |
#ifndef ntohl |
| 55 |
#ifdef WORDS_BIGENDIAN |
| 56 |
#define ntohl |
| 57 |
#define ntohs |
| 58 |
#else // WORDS_BIGENDIAN |
| 59 |
|
| 60 |
#include <stdint.h> |
| 61 |
|
| 62 |
#define ntohl(x) \ |
| 63 |
((((uint32_t)(x) & 0x000000ffU) << 24) | \ |
| 64 |
(((uint32_t)(x) & 0x0000ff00U) << 8) | \ |
| 65 |
(((uint32_t)(x) & 0x00ff0000U) >> 8) | \ |
| 66 |
(((uint32_t)(x) & 0xff000000U) >> 24)) |
| 67 |
|
| 68 |
#define ntohs(x) \ |
| 69 |
((((uint16_t)(x) & 0x00ff) << 8) | \ |
| 70 |
(((uint16_t)(x) & 0xff00) >> 8)) |
| 71 |
#endif // WORDS_BIGENDIAN |
| 72 |
#endif // ntohl |
| 73 |
|
| 74 |
|
| 75 |
#ifdef _MSC_VER |
| 76 |
#pragma pack(push, 1) |
| 77 |
#endif |
| 78 |
|
| 79 |
typedef struct |
| 80 |
{ |
| 81 |
byte chunk_id[4]; |
| 82 |
unsigned int chunk_size; |
| 83 |
} PACKEDATTR chunk_header_t; |
| 84 |
|
| 85 |
typedef struct |
| 86 |
{ |
| 87 |
chunk_header_t chunk_header; |
| 88 |
unsigned short format_type; |
| 89 |
unsigned short num_tracks; |
| 90 |
unsigned short time_division; |
| 91 |
} PACKEDATTR midi_header_t; |
| 92 |
|
| 93 |
#ifdef _MSC_VER |
| 94 |
#pragma pack(pop) |
| 95 |
#endif |
| 96 |
|
| 97 |
typedef struct |
| 98 |
{ |
| 99 |
// Length in bytes: |
| 100 |
|
| 101 |
unsigned int data_len; |
| 102 |
|
| 103 |
// Events in this track: |
| 104 |
|
| 105 |
midi_event_t *events; |
| 106 |
unsigned int num_events; |
| 107 |
unsigned int num_event_mem; // NSM track size of structure |
| 108 |
} midi_track_t; |
| 109 |
|
| 110 |
struct midi_track_iter_s |
| 111 |
{ |
| 112 |
midi_track_t *track; |
| 113 |
unsigned int position; |
| 114 |
}; |
| 115 |
|
| 116 |
struct midi_file_s |
| 117 |
{ |
| 118 |
midi_header_t header; |
| 119 |
|
| 120 |
// All tracks in this file: |
| 121 |
midi_track_t *tracks; |
| 122 |
unsigned int num_tracks; |
| 123 |
|
| 124 |
// Data buffer used to store data read for SysEx or meta events: |
| 125 |
byte *buffer; |
| 126 |
unsigned int buffer_size; |
| 127 |
}; |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
// Check the header of a chunk: |
| 132 |
|
| 133 |
static dboolean CheckChunkHeader(chunk_header_t *chunk, |
| 134 |
const char *expected_id) |
| 135 |
{ |
| 136 |
dboolean result; |
| 137 |
|
| 138 |
result = (memcmp((char *) chunk->chunk_id, expected_id, 4) == 0); |
| 139 |
|
| 140 |
if (!result) |
| 141 |
{ |
| 142 |
lprintf (LO_WARN, "CheckChunkHeader: Expected '%s' chunk header, " |
| 143 |
"got '%c%c%c%c'\n", |
| 144 |
expected_id, |
| 145 |
chunk->chunk_id[0], chunk->chunk_id[1], |
| 146 |
chunk->chunk_id[2], chunk->chunk_id[3]); |
| 147 |
} |
| 148 |
|
| 149 |
return result; |
| 150 |
} |
| 151 |
|
| 152 |
// Read a single byte. Returns false on error. |
| 153 |
|
| 154 |
static dboolean ReadByte(byte *result, midimem_t *mf) |
| 155 |
{ |
| 156 |
if (mf->pos >= mf->len) |
| 157 |
{ |
| 158 |
lprintf (LO_WARN, "ReadByte: Unexpected end of file\n"); |
| 159 |
return false; |
| 160 |
} |
| 161 |
|
| 162 |
*result = mf->data[mf->pos++]; |
| 163 |
return true; |
| 164 |
} |
| 165 |
|
| 166 |
static dboolean ReadMultipleBytes (void *dest, size_t len, midimem_t *mf) |
| 167 |
{ |
| 168 |
byte *cdest = (byte *) dest; |
| 169 |
unsigned i; |
| 170 |
for (i = 0; i < len; i++) |
| 171 |
{ |
| 172 |
if (!ReadByte (cdest + i, mf)) |
| 173 |
{ |
| 174 |
lprintf (LO_WARN, "ReadMultipleBytes: Unexpected end of file\n"); |
| 175 |
return false; |
| 176 |
} |
| 177 |
} |
| 178 |
return true; |
| 179 |
} |
| 180 |
|
| 181 |
// Read a variable-length value. |
| 182 |
|
| 183 |
static dboolean ReadVariableLength(unsigned int *result, midimem_t *mf) |
| 184 |
{ |
| 185 |
int i; |
| 186 |
byte b; |
| 187 |
|
| 188 |
*result = 0; |
| 189 |
|
| 190 |
for (i=0; i<4; ++i) |
| 191 |
{ |
| 192 |
if (!ReadByte(&b, mf)) |
| 193 |
{ |
| 194 |
lprintf (LO_WARN, "ReadVariableLength: Error while reading " |
| 195 |
"variable-length value\n"); |
| 196 |
return false; |
| 197 |
} |
| 198 |
|
| 199 |
// Insert the bottom seven bits from this byte. |
| 200 |
|
| 201 |
*result <<= 7; |
| 202 |
*result |= b & 0x7f; |
| 203 |
|
| 204 |
// If the top bit is not set, this is the end. |
| 205 |
|
| 206 |
if ((b & 0x80) == 0) |
| 207 |
{ |
| 208 |
return true; |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
lprintf (LO_WARN, "ReadVariableLength: Variable-length value too " |
| 213 |
"long: maximum of four bytes\n"); |
| 214 |
return false; |
| 215 |
} |
| 216 |
|
| 217 |
// Read a byte sequence into the data buffer. |
| 218 |
|
| 219 |
static void *ReadByteSequence(unsigned int num_bytes, midimem_t *mf) |
| 220 |
{ |
| 221 |
unsigned int i; |
| 222 |
byte *result; |
| 223 |
|
| 224 |
// events can be length 0. malloc(0) is not portable (can return NULL) |
| 225 |
if (!num_bytes) |
| 226 |
return Z_Malloc (4); |
| 227 |
|
| 228 |
// Allocate a buffer: |
| 229 |
|
| 230 |
result = (byte*)Z_Malloc(num_bytes); |
| 231 |
|
| 232 |
if (result == NULL) |
| 233 |
{ |
| 234 |
lprintf (LO_WARN, "ReadByteSequence: Failed to allocate buffer %u bytes\n", num_bytes); |
| 235 |
return NULL; |
| 236 |
} |
| 237 |
|
| 238 |
// Read the data: |
| 239 |
|
| 240 |
for (i=0; i<num_bytes; ++i) |
| 241 |
{ |
| 242 |
if (!ReadByte(&result[i], mf)) |
| 243 |
{ |
| 244 |
lprintf (LO_WARN, "ReadByteSequence: Error while reading byte %u\n", i); |
| 245 |
Z_Free(result); |
| 246 |
return NULL; |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
return result; |
| 251 |
} |
| 252 |
|
| 253 |
// Read a MIDI channel event. |
| 254 |
// two_param indicates that the event type takes two parameters |
| 255 |
// (three byte) otherwise it is single parameter (two byte) |
| 256 |
|
| 257 |
static dboolean ReadChannelEvent(midi_event_t *event, |
| 258 |
byte event_type, dboolean two_param, |
| 259 |
midimem_t *mf) |
| 260 |
{ |
| 261 |
byte b; |
| 262 |
|
| 263 |
// Set basics: |
| 264 |
|
| 265 |
event->event_type = (midi_event_type_t)(event_type & 0xf0); |
| 266 |
event->data.channel.channel = event_type & 0x0f; |
| 267 |
|
| 268 |
// Read parameters: |
| 269 |
|
| 270 |
if (!ReadByte(&b, mf)) |
| 271 |
{ |
| 272 |
lprintf (LO_WARN, "ReadChannelEvent: Error while reading channel " |
| 273 |
"event parameters\n"); |
| 274 |
return false; |
| 275 |
} |
| 276 |
|
| 277 |
event->data.channel.param1 = b; |
| 278 |
|
| 279 |
// Second parameter: |
| 280 |
|
| 281 |
if (two_param) |
| 282 |
{ |
| 283 |
if (!ReadByte(&b, mf)) |
| 284 |
{ |
| 285 |
lprintf (LO_WARN, "ReadChannelEvent: Error while reading channel " |
| 286 |
"event parameters\n"); |
| 287 |
return false; |
| 288 |
} |
| 289 |
|
| 290 |
event->data.channel.param2 = b; |
| 291 |
} |
| 292 |
|
| 293 |
return true; |
| 294 |
} |
| 295 |
|
| 296 |
// Read sysex event: |
| 297 |
|
| 298 |
static dboolean ReadSysExEvent(midi_event_t *event, int event_type, |
| 299 |
midimem_t *mf) |
| 300 |
{ |
| 301 |
event->event_type = (midi_event_type_t)event_type; |
| 302 |
|
| 303 |
if (!ReadVariableLength(&event->data.sysex.length, mf)) |
| 304 |
{ |
| 305 |
lprintf (LO_WARN, "ReadSysExEvent: Failed to read length of " |
| 306 |
"SysEx block\n"); |
| 307 |
return false; |
| 308 |
} |
| 309 |
|
| 310 |
// Read the byte sequence: |
| 311 |
|
| 312 |
event->data.sysex.data = (byte*)ReadByteSequence(event->data.sysex.length, mf); |
| 313 |
|
| 314 |
if (event->data.sysex.data == NULL) |
| 315 |
{ |
| 316 |
lprintf (LO_WARN, "ReadSysExEvent: Failed while reading SysEx event\n"); |
| 317 |
return false; |
| 318 |
} |
| 319 |
|
| 320 |
return true; |
| 321 |
} |
| 322 |
|
| 323 |
// Read meta event: |
| 324 |
|
| 325 |
static dboolean ReadMetaEvent(midi_event_t *event, midimem_t *mf) |
| 326 |
{ |
| 327 |
byte b; |
| 328 |
|
| 329 |
event->event_type = MIDI_EVENT_META; |
| 330 |
|
| 331 |
// Read meta event type: |
| 332 |
|
| 333 |
if (!ReadByte(&b, mf)) |
| 334 |
{ |
| 335 |
lprintf (LO_WARN, "ReadMetaEvent: Failed to read meta event type\n"); |
| 336 |
return false; |
| 337 |
} |
| 338 |
|
| 339 |
event->data.meta.type = b; |
| 340 |
|
| 341 |
// Read length of meta event data: |
| 342 |
|
| 343 |
if (!ReadVariableLength(&event->data.meta.length, mf)) |
| 344 |
{ |
| 345 |
lprintf (LO_WARN, "ReadMetaEvent: Failed to read length of " |
| 346 |
"MetaEvent block\n"); |
| 347 |
return false; |
| 348 |
} |
| 349 |
|
| 350 |
// Read the byte sequence: |
| 351 |
|
| 352 |
event->data.meta.data = (byte*)ReadByteSequence(event->data.meta.length, mf); |
| 353 |
|
| 354 |
if (event->data.meta.data == NULL) |
| 355 |
{ |
| 356 |
lprintf (LO_WARN, "ReadMetaEvent: Failed while reading MetaEvent\n"); |
| 357 |
return false; |
| 358 |
} |
| 359 |
|
| 360 |
return true; |
| 361 |
} |
| 362 |
|
| 363 |
static dboolean ReadEvent(midi_event_t *event, unsigned int *last_event_type, |
| 364 |
midimem_t *mf) |
| 365 |
{ |
| 366 |
byte event_type; |
| 367 |
|
| 368 |
if (!ReadVariableLength(&event->delta_time, mf)) |
| 369 |
{ |
| 370 |
lprintf (LO_WARN, "ReadEvent: Failed to read event timestamp\n"); |
| 371 |
return false; |
| 372 |
} |
| 373 |
|
| 374 |
if (!ReadByte(&event_type, mf)) |
| 375 |
{ |
| 376 |
lprintf (LO_WARN, "ReadEvent: Failed to read event type\n"); |
| 377 |
return false; |
| 378 |
} |
| 379 |
|
| 380 |
// All event types have their top bit set. Therefore, if |
| 381 |
// the top bit is not set, it is because we are using the "same |
| 382 |
// as previous event type" shortcut to save a byte. Skip back |
| 383 |
// a byte so that we read this byte again. |
| 384 |
|
| 385 |
if ((event_type & 0x80) == 0) |
| 386 |
{ |
| 387 |
event_type = *last_event_type; |
| 388 |
mf->pos--; |
| 389 |
} |
| 390 |
else |
| 391 |
{ |
| 392 |
*last_event_type = event_type; |
| 393 |
} |
| 394 |
|
| 395 |
// Check event type: |
| 396 |
|
| 397 |
switch (event_type & 0xf0) |
| 398 |
{ |
| 399 |
// Two parameter channel events: |
| 400 |
|
| 401 |
case MIDI_EVENT_NOTE_OFF: |
| 402 |
case MIDI_EVENT_NOTE_ON: |
| 403 |
case MIDI_EVENT_AFTERTOUCH: |
| 404 |
case MIDI_EVENT_CONTROLLER: |
| 405 |
case MIDI_EVENT_PITCH_BEND: |
| 406 |
return ReadChannelEvent(event, event_type, true, mf); |
| 407 |
|
| 408 |
// Single parameter channel events: |
| 409 |
|
| 410 |
case MIDI_EVENT_PROGRAM_CHANGE: |
| 411 |
case MIDI_EVENT_CHAN_AFTERTOUCH: |
| 412 |
return ReadChannelEvent(event, event_type, false, mf); |
| 413 |
|
| 414 |
default: |
| 415 |
break; |
| 416 |
} |
| 417 |
|
| 418 |
// Specific value? |
| 419 |
|
| 420 |
switch (event_type) |
| 421 |
{ |
| 422 |
case MIDI_EVENT_SYSEX: |
| 423 |
case MIDI_EVENT_SYSEX_SPLIT: |
| 424 |
return ReadSysExEvent(event, event_type, mf); |
| 425 |
|
| 426 |
case MIDI_EVENT_META: |
| 427 |
return ReadMetaEvent(event, mf); |
| 428 |
|
| 429 |
default: |
| 430 |
break; |
| 431 |
} |
| 432 |
|
| 433 |
lprintf (LO_WARN, "ReadEvent: Unknown MIDI event type: 0x%x\n", event_type); |
| 434 |
return false; |
| 435 |
} |
| 436 |
|
| 437 |
// Free an event: |
| 438 |
|
| 439 |
static void FreeEvent(midi_event_t *event) |
| 440 |
{ |
| 441 |
// Some event types have dynamically allocated buffers assigned |
| 442 |
// to them that must be freed. |
| 443 |
|
| 444 |
switch (event->event_type) |
| 445 |
{ |
| 446 |
case MIDI_EVENT_SYSEX: |
| 447 |
case MIDI_EVENT_SYSEX_SPLIT: |
| 448 |
Z_Free(event->data.sysex.data); |
| 449 |
break; |
| 450 |
|
| 451 |
case MIDI_EVENT_META: |
| 452 |
Z_Free(event->data.meta.data); |
| 453 |
break; |
| 454 |
|
| 455 |
default: |
| 456 |
// Nothing to do. |
| 457 |
break; |
| 458 |
} |
| 459 |
} |
| 460 |
|
| 461 |
// Read and check the track chunk header |
| 462 |
|
| 463 |
static dboolean ReadTrackHeader(midi_track_t *track, midimem_t *mf) |
| 464 |
{ |
| 465 |
size_t records_read; |
| 466 |
chunk_header_t chunk_header; |
| 467 |
|
| 468 |
records_read = ReadMultipleBytes(&chunk_header, sizeof(chunk_header_t), mf); |
| 469 |
|
| 470 |
if (records_read < 1) |
| 471 |
{ |
| 472 |
return false; |
| 473 |
} |
| 474 |
|
| 475 |
if (!CheckChunkHeader(&chunk_header, TRACK_CHUNK_ID)) |
| 476 |
{ |
| 477 |
return false; |
| 478 |
} |
| 479 |
|
| 480 |
track->data_len = ntohl(chunk_header.chunk_size); |
| 481 |
|
| 482 |
return true; |
| 483 |
} |
| 484 |
|
| 485 |
static dboolean ReadTrack(midi_track_t *track, midimem_t *mf) |
| 486 |
{ |
| 487 |
midi_event_t *new_events = NULL; |
| 488 |
midi_event_t *event; |
| 489 |
unsigned int last_event_type; |
| 490 |
|
| 491 |
track->num_events = 0; |
| 492 |
track->events = NULL; |
| 493 |
track->num_event_mem = 0; // NSM |
| 494 |
|
| 495 |
// Read the header: |
| 496 |
|
| 497 |
if (!ReadTrackHeader(track, mf)) |
| 498 |
{ |
| 499 |
return false; |
| 500 |
} |
| 501 |
|
| 502 |
// Then the events: |
| 503 |
|
| 504 |
last_event_type = 0; |
| 505 |
|
| 506 |
for (;;) |
| 507 |
{ |
| 508 |
// Resize the track slightly larger to hold another event: |
| 509 |
/* |
| 510 |
new_events = Z_Realloc(track->events, |
| 511 |
sizeof(midi_event_t) * (track->num_events + 1)); |
| 512 |
*/ |
| 513 |
if (track->num_events == track->num_event_mem) |
| 514 |
{ // depending on the state of the heap and the malloc implementation, realloc() |
| 515 |
// one more event at a time can be VERY slow. 10sec+ in MSVC |
| 516 |
track->num_event_mem += 100; |
| 517 |
new_events = (midi_event_t*)Z_Realloc (track->events, sizeof (midi_event_t) * track->num_event_mem); |
| 518 |
} |
| 519 |
|
| 520 |
if (new_events == NULL) |
| 521 |
{ |
| 522 |
return false; |
| 523 |
} |
| 524 |
|
| 525 |
track->events = new_events; |
| 526 |
|
| 527 |
// Read the next event: |
| 528 |
|
| 529 |
event = &track->events[track->num_events]; |
| 530 |
if (!ReadEvent(event, &last_event_type, mf)) |
| 531 |
{ |
| 532 |
return false; |
| 533 |
} |
| 534 |
|
| 535 |
++track->num_events; |
| 536 |
|
| 537 |
// End of track? |
| 538 |
|
| 539 |
if (event->event_type == MIDI_EVENT_META |
| 540 |
&& event->data.meta.type == MIDI_META_END_OF_TRACK) |
| 541 |
{ |
| 542 |
break; |
| 543 |
} |
| 544 |
} |
| 545 |
|
| 546 |
return true; |
| 547 |
} |
| 548 |
|
| 549 |
// Free a track: |
| 550 |
|
| 551 |
static void FreeTrack(midi_track_t *track) |
| 552 |
{ |
| 553 |
unsigned i; |
| 554 |
|
| 555 |
for (i=0; i<track->num_events; ++i) |
| 556 |
{ |
| 557 |
FreeEvent(&track->events[i]); |
| 558 |
} |
| 559 |
|
| 560 |
Z_Free(track->events); |
| 561 |
} |
| 562 |
|
| 563 |
static dboolean ReadAllTracks(midi_file_t *file, midimem_t *mf) |
| 564 |
{ |
| 565 |
unsigned int i; |
| 566 |
|
| 567 |
// Allocate list of tracks and read each track: |
| 568 |
|
| 569 |
file->tracks = (midi_track_t*)Z_Malloc(sizeof(midi_track_t) * file->num_tracks); |
| 570 |
|
| 571 |
if (file->tracks == NULL) |
| 572 |
{ |
| 573 |
return false; |
| 574 |
} |
| 575 |
|
| 576 |
memset(file->tracks, 0, sizeof(midi_track_t) * file->num_tracks); |
| 577 |
|
| 578 |
// Read each track: |
| 579 |
|
| 580 |
for (i=0; i<file->num_tracks; ++i) |
| 581 |
{ |
| 582 |
if (!ReadTrack(&file->tracks[i], mf)) |
| 583 |
{ |
| 584 |
return false; |
| 585 |
} |
| 586 |
} |
| 587 |
|
| 588 |
return true; |
| 589 |
} |
| 590 |
|
| 591 |
// Read and check the header chunk. |
| 592 |
|
| 593 |
static dboolean ReadFileHeader(midi_file_t *file, midimem_t *mf) |
| 594 |
{ |
| 595 |
size_t records_read; |
| 596 |
unsigned int format_type; |
| 597 |
|
| 598 |
records_read = ReadMultipleBytes (&file->header, sizeof(midi_header_t), mf); |
| 599 |
|
| 600 |
if (records_read < 1) |
| 601 |
{ |
| 602 |
return false; |
| 603 |
} |
| 604 |
|
| 605 |
if (!CheckChunkHeader(&file->header.chunk_header, HEADER_CHUNK_ID) |
| 606 |
|| ntohl(file->header.chunk_header.chunk_size) != 6) |
| 607 |
{ |
| 608 |
lprintf (LO_WARN, "ReadFileHeader: Invalid MIDI chunk header! " |
| 609 |
"chunk_size=%" PRIu32 "\n", |
| 610 |
ntohl(file->header.chunk_header.chunk_size)); |
| 611 |
return false; |
| 612 |
} |
| 613 |
|
| 614 |
format_type = ntohs(file->header.format_type); |
| 615 |
file->num_tracks = ntohs(file->header.num_tracks); |
| 616 |
|
| 617 |
if ((format_type != 0 && format_type != 1) |
| 618 |
|| file->num_tracks < 1) |
| 619 |
{ |
| 620 |
lprintf (LO_WARN, "ReadFileHeader: Only type 0/1 " |
| 621 |
"MIDI files supported!\n"); |
| 622 |
return false; |
| 623 |
} |
| 624 |
// NSM |
| 625 |
file->header.time_division = ntohs (file->header.time_division); |
| 626 |
|
| 627 |
|
| 628 |
return true; |
| 629 |
} |
| 630 |
|
| 631 |
void MIDI_FreeFile(midi_file_t *file) |
| 632 |
{ |
| 633 |
unsigned i; |
| 634 |
|
| 635 |
if (file->tracks != NULL) |
| 636 |
{ |
| 637 |
for (i=0; i<file->num_tracks; ++i) |
| 638 |
{ |
| 639 |
FreeTrack(&file->tracks[i]); |
| 640 |
} |
| 641 |
|
| 642 |
Z_Free(file->tracks); |
| 643 |
} |
| 644 |
|
| 645 |
Z_Free(file); |
| 646 |
} |
| 647 |
|
| 648 |
midi_file_t *MIDI_LoadFile (midimem_t *mf) |
| 649 |
{ |
| 650 |
midi_file_t *file; |
| 651 |
|
| 652 |
file = (midi_file_t*)Z_Malloc(sizeof(midi_file_t)); |
| 653 |
|
| 654 |
if (file == NULL) |
| 655 |
{ |
| 656 |
return NULL; |
| 657 |
} |
| 658 |
|
| 659 |
file->tracks = NULL; |
| 660 |
file->num_tracks = 0; |
| 661 |
file->buffer = NULL; |
| 662 |
file->buffer_size = 0; |
| 663 |
|
| 664 |
// Read MIDI file header |
| 665 |
|
| 666 |
if (!ReadFileHeader(file, mf)) |
| 667 |
{ |
| 668 |
MIDI_FreeFile(file); |
| 669 |
return NULL; |
| 670 |
} |
| 671 |
|
| 672 |
// Read all tracks: |
| 673 |
|
| 674 |
if (!ReadAllTracks(file, mf)) |
| 675 |
{ |
| 676 |
MIDI_FreeFile(file); |
| 677 |
return NULL; |
| 678 |
} |
| 679 |
|
| 680 |
return file; |
| 681 |
} |
| 682 |
|
| 683 |
// Get the number of tracks in a MIDI file. |
| 684 |
|
| 685 |
unsigned int MIDI_NumTracks(const midi_file_t *file) |
| 686 |
{ |
| 687 |
return file->num_tracks; |
| 688 |
} |
| 689 |
|
| 690 |
// Start iterating over the events in a track. |
| 691 |
|
| 692 |
midi_track_iter_t *MIDI_IterateTrack(const midi_file_t *file, unsigned int track) |
| 693 |
{ |
| 694 |
midi_track_iter_t *iter; |
| 695 |
|
| 696 |
assert(track < file->num_tracks); |
| 697 |
|
| 698 |
iter = (midi_track_iter_t*)Z_Malloc(sizeof(*iter)); |
| 699 |
iter->track = &file->tracks[track]; |
| 700 |
iter->position = 0; |
| 701 |
|
| 702 |
return iter; |
| 703 |
} |
| 704 |
|
| 705 |
void MIDI_FreeIterator(midi_track_iter_t *iter) |
| 706 |
{ |
| 707 |
Z_Free(iter); |
| 708 |
} |
| 709 |
|
| 710 |
// Get the time until the next MIDI event in a track. |
| 711 |
|
| 712 |
unsigned int MIDI_GetDeltaTime(midi_track_iter_t *iter) |
| 713 |
{ |
| 714 |
if (iter->position < iter->track->num_events) |
| 715 |
{ |
| 716 |
midi_event_t *next_event; |
| 717 |
|
| 718 |
next_event = &iter->track->events[iter->position]; |
| 719 |
|
| 720 |
return next_event->delta_time; |
| 721 |
} |
| 722 |
else |
| 723 |
{ |
| 724 |
return 0; |
| 725 |
} |
| 726 |
} |
| 727 |
|
| 728 |
// Get a pointer to the next MIDI event. |
| 729 |
|
| 730 |
int MIDI_GetNextEvent(midi_track_iter_t *iter, midi_event_t **event) |
| 731 |
{ |
| 732 |
if (iter->position < iter->track->num_events) |
| 733 |
{ |
| 734 |
*event = &iter->track->events[iter->position]; |
| 735 |
++iter->position; |
| 736 |
|
| 737 |
return 1; |
| 738 |
} |
| 739 |
else |
| 740 |
{ |
| 741 |
return 0; |
| 742 |
} |
| 743 |
} |
| 744 |
|
| 745 |
unsigned int MIDI_GetFileTimeDivision(const midi_file_t *file) |
| 746 |
{ |
| 747 |
return file->header.time_division; |
| 748 |
} |
| 749 |
|
| 750 |
void MIDI_RestartIterator(midi_track_iter_t *iter) |
| 751 |
{ |
| 752 |
iter->position = 0; |
| 753 |
} |
| 754 |
|
| 755 |
|
| 756 |
|
| 757 |
static void MIDI_PrintFlatListDBG (const midi_event_t **evs) |
| 758 |
{ |
| 759 |
const midi_event_t *event; |
| 760 |
|
| 761 |
while (1) |
| 762 |
{ |
| 763 |
event = *evs++; |
| 764 |
|
| 765 |
if (event->delta_time > 0) |
| 766 |
printf("Delay: %i ticks\n", event->delta_time); |
| 767 |
|
| 768 |
|
| 769 |
switch (event->event_type) |
| 770 |
{ |
| 771 |
case MIDI_EVENT_NOTE_OFF: |
| 772 |
printf ("MIDI_EVENT_NOTE_OFF\n");break; |
| 773 |
case MIDI_EVENT_NOTE_ON: |
| 774 |
printf ("MIDI_EVENT_NOTE_ON\n");break; |
| 775 |
case MIDI_EVENT_AFTERTOUCH: |
| 776 |
printf ("MIDI_EVENT_AFTERTOUCH\n");break; |
| 777 |
case MIDI_EVENT_CONTROLLER: |
| 778 |
printf ("MIDI_EVENT_CONTROLLER\n");break; |
| 779 |
case MIDI_EVENT_PROGRAM_CHANGE: |
| 780 |
printf ("MIDI_EVENT_PROGRAM_CHANGE\n");break; |
| 781 |
case MIDI_EVENT_CHAN_AFTERTOUCH: |
| 782 |
printf ("MIDI_EVENT_CHAN_AFTERTOUCH\n");break; |
| 783 |
case MIDI_EVENT_PITCH_BEND: |
| 784 |
printf ("MIDI_EVENT_PITCH_BEND\n");break; |
| 785 |
case MIDI_EVENT_SYSEX: |
| 786 |
printf ("MIDI_EVENT_SYSEX\n");break; |
| 787 |
case MIDI_EVENT_SYSEX_SPLIT: |
| 788 |
printf ("MIDI_EVENT_SYSEX_SPLIT\n");break; |
| 789 |
case MIDI_EVENT_META: |
| 790 |
printf ("MIDI_EVENT_META\n");break; |
| 791 |
|
| 792 |
default: |
| 793 |
printf ("(unknown)\n");break; |
| 794 |
} |
| 795 |
switch(event->event_type) |
| 796 |
{ |
| 797 |
case MIDI_EVENT_NOTE_OFF: |
| 798 |
case MIDI_EVENT_NOTE_ON: |
| 799 |
case MIDI_EVENT_AFTERTOUCH: |
| 800 |
case MIDI_EVENT_CONTROLLER: |
| 801 |
case MIDI_EVENT_PROGRAM_CHANGE: |
| 802 |
case MIDI_EVENT_CHAN_AFTERTOUCH: |
| 803 |
case MIDI_EVENT_PITCH_BEND: |
| 804 |
printf("\tChannel: %i\n", event->data.channel.channel); |
| 805 |
printf("\tParameter 1: %i\n", event->data.channel.param1); |
| 806 |
printf("\tParameter 2: %i\n", event->data.channel.param2); |
| 807 |
break; |
| 808 |
|
| 809 |
case MIDI_EVENT_SYSEX: |
| 810 |
case MIDI_EVENT_SYSEX_SPLIT: |
| 811 |
printf("\tLength: %i\n", event->data.sysex.length); |
| 812 |
break; |
| 813 |
|
| 814 |
case MIDI_EVENT_META: |
| 815 |
printf("\tMeta type: %i\n", event->data.meta.type); |
| 816 |
printf("\tLength: %i\n", event->data.meta.length); |
| 817 |
break; |
| 818 |
} |
| 819 |
if (event->event_type == MIDI_EVENT_META && |
| 820 |
event->data.meta.type == MIDI_META_END_OF_TRACK) |
| 821 |
{ |
| 822 |
printf ("gotta go!\n"); |
| 823 |
return; |
| 824 |
} |
| 825 |
} |
| 826 |
} |
| 827 |
|
| 828 |
|
| 829 |
|
| 830 |
|
| 831 |
|
| 832 |
|
| 833 |
// NSM: an alternate iterator tool. |
| 834 |
|
| 835 |
midi_event_t **MIDI_GenerateFlatList (midi_file_t *file) |
| 836 |
{ |
| 837 |
int totalevents = 0; |
| 838 |
unsigned i, delta; |
| 839 |
int nextrk; |
| 840 |
|
| 841 |
int totaldelta = 0; |
| 842 |
|
| 843 |
int *trackpos = (int*)Z_Calloc (file->num_tracks, sizeof (int)); |
| 844 |
int *tracktime = (int*)Z_Calloc (file->num_tracks, sizeof (int)); |
| 845 |
int trackactive = file->num_tracks; |
| 846 |
|
| 847 |
midi_event_t **ret; |
| 848 |
midi_event_t **epos; |
| 849 |
|
| 850 |
for (i = 0; i < file->num_tracks; i++) |
| 851 |
totalevents += file->tracks[i].num_events; |
| 852 |
|
| 853 |
ret = (midi_event_t**)Z_Malloc (totalevents * sizeof (midi_event_t **)); |
| 854 |
|
| 855 |
epos = ret; |
| 856 |
|
| 857 |
while (trackactive) |
| 858 |
{ |
| 859 |
delta = 0x10000000; |
| 860 |
nextrk = -1; |
| 861 |
for (i = 0; i < file->num_tracks; i++) |
| 862 |
{ |
| 863 |
if (trackpos[i] != -1 && file->tracks[i].events[trackpos[i]].delta_time - tracktime[i] < delta) |
| 864 |
{ |
| 865 |
delta = file->tracks[i].events[trackpos[i]].delta_time - tracktime[i]; |
| 866 |
nextrk = i; |
| 867 |
} |
| 868 |
} |
| 869 |
if (nextrk == -1) // unexpected EOF (not every track got end track) |
| 870 |
break; |
| 871 |
|
| 872 |
*epos = file->tracks[nextrk].events + trackpos[nextrk]; |
| 873 |
|
| 874 |
for (i = 0; i < file->num_tracks; i++) |
| 875 |
{ |
| 876 |
if (i == (unsigned) nextrk) |
| 877 |
{ |
| 878 |
tracktime[i] = 0; |
| 879 |
trackpos[i]++; |
| 880 |
} |
| 881 |
else |
| 882 |
tracktime[i] += delta; |
| 883 |
} |
| 884 |
// yes, this clobbers the original timecodes |
| 885 |
epos[0]->delta_time = delta; |
| 886 |
totaldelta += delta; |
| 887 |
|
| 888 |
if (epos[0]->event_type == MIDI_EVENT_META |
| 889 |
&& epos[0]->data.meta.type == MIDI_META_END_OF_TRACK) |
| 890 |
{ // change end of track into no op |
| 891 |
trackactive--; |
| 892 |
trackpos[nextrk] = -1; |
| 893 |
epos[0]->data.meta.type = MIDI_META_TEXT; |
| 894 |
} |
| 895 |
else if ((unsigned) trackpos[nextrk] == file->tracks[nextrk].num_events) |
| 896 |
{ |
| 897 |
lprintf (LO_WARN, "MIDI_GenerateFlatList: Unexpected end of track\n"); |
| 898 |
Z_Free (trackpos); |
| 899 |
Z_Free (tracktime); |
| 900 |
Z_Free (ret); |
| 901 |
return NULL; |
| 902 |
} |
| 903 |
epos++; |
| 904 |
} |
| 905 |
|
| 906 |
if (trackactive) |
| 907 |
{ // unexpected EOF |
| 908 |
lprintf (LO_WARN, "MIDI_GenerateFlatList: Unexpected end of midi file\n"); |
| 909 |
Z_Free (trackpos); |
| 910 |
Z_Free (tracktime); |
| 911 |
Z_Free (ret); |
| 912 |
return NULL; |
| 913 |
} |
| 914 |
|
| 915 |
// last end of track event is preserved though |
| 916 |
epos[-1]->data.meta.type = MIDI_META_END_OF_TRACK; |
| 917 |
|
| 918 |
Z_Free (trackpos); |
| 919 |
Z_Free (tracktime); |
| 920 |
|
| 921 |
if (totaldelta < 100) |
| 922 |
{ |
| 923 |
lprintf (LO_WARN, "MIDI_GeneratFlatList: very short file %i\n", totaldelta); |
| 924 |
Z_Free (ret); |
| 925 |
return NULL; |
| 926 |
} |
| 927 |
|
| 928 |
|
| 929 |
//MIDI_PrintFlatListDBG (ret); |
| 930 |
|
| 931 |
return ret; |
| 932 |
} |
| 933 |
|
| 934 |
|
| 935 |
|
| 936 |
void MIDI_DestroyFlatList (midi_event_t **evs) |
| 937 |
{ |
| 938 |
Z_Free (evs); |
| 939 |
} |
| 940 |
|
| 941 |
|
| 942 |
|
| 943 |
// NSM: timing assist functions |
| 944 |
// they compute samples per midi clock, where midi clock is the units |
| 945 |
// that the time deltas are in, and samples is an arbitrary unit of which |
| 946 |
// "sndrate" of them occur per second |
| 947 |
|
| 948 |
|
| 949 |
static double compute_spmc_normal (unsigned mpq, unsigned tempo, unsigned sndrate) |
| 950 |
{ // returns samples per midi clock |
| 951 |
|
| 952 |
// inputs: mpq (midi clocks per quarternote, from header) |
| 953 |
// tempo (from tempo event, in microseconds per quarternote) |
| 954 |
// sndrate (sound sample rate in hz) |
| 955 |
|
| 956 |
// samples quarternote microsec samples second |
| 957 |
// ------- = ----------- * ----------- * ------- * -------- |
| 958 |
// midiclk midiclk quarternote second microsec |
| 959 |
|
| 960 |
// return = (1 / mpq) * tempo * sndrate * (1 / 1000000) |
| 961 |
|
| 962 |
return (double) tempo / 1000000 * sndrate / mpq; |
| 963 |
|
| 964 |
} |
| 965 |
|
| 966 |
static double compute_spmc_smpte (unsigned smpte_fps, unsigned mpf, unsigned sndrate) |
| 967 |
{ // returns samples per midi clock |
| 968 |
|
| 969 |
// inputs: smpte_fps (24, 25, 29, 30) |
| 970 |
// mpf (midi clocks per frame, 0-255) |
| 971 |
// sndrate (sound sample rate in hz) |
| 972 |
|
| 973 |
// tempo is ignored here |
| 974 |
|
| 975 |
// samples frame seconds samples |
| 976 |
// ------- = --------- * --------- * ------- |
| 977 |
// midiclk midiclk frame second |
| 978 |
|
| 979 |
// return = (1 / mpf) * (1 / fps) * sndrate |
| 980 |
|
| 981 |
|
| 982 |
double fps; // actual frames per second |
| 983 |
switch (smpte_fps) |
| 984 |
{ |
| 985 |
case 24: |
| 986 |
case 25: |
| 987 |
case 30: |
| 988 |
fps = smpte_fps; |
| 989 |
break; |
| 990 |
case 29: |
| 991 |
// i hate NTSC, i really do |
| 992 |
fps = smpte_fps * 1000.0 / 1001.0; |
| 993 |
break; |
| 994 |
default: |
| 995 |
lprintf (LO_WARN, "MIDI_spmc: Unexpected SMPTE timestamp %i\n", smpte_fps); |
| 996 |
// assume |
| 997 |
fps = 30.0; |
| 998 |
break; |
| 999 |
} |
| 1000 |
|
| 1001 |
return (double) sndrate / fps / mpf; |
| 1002 |
|
| 1003 |
} |
| 1004 |
|
| 1005 |
// if event is NULL, compute with default starting tempo (120BPM) |
| 1006 |
|
| 1007 |
double MIDI_spmc (const midi_file_t *file, const midi_event_t *ev, unsigned sndrate) |
| 1008 |
{ |
| 1009 |
int smpte_fps; |
| 1010 |
unsigned tempo; |
| 1011 |
unsigned headerval = MIDI_GetFileTimeDivision (file); |
| 1012 |
|
| 1013 |
if (headerval & 0x8000) // SMPTE |
| 1014 |
{ // i don't really have any files to test this on... |
| 1015 |
smpte_fps = -(short) headerval >> 8; |
| 1016 |
return compute_spmc_smpte (smpte_fps, headerval & 0xff, sndrate); |
| 1017 |
} |
| 1018 |
|
| 1019 |
// normal timing |
| 1020 |
tempo = 500000; // default 120BPM |
| 1021 |
if (ev) |
| 1022 |
{ |
| 1023 |
if (ev->event_type == MIDI_EVENT_META) |
| 1024 |
{ |
| 1025 |
if (ev->data.meta.length == 3) |
| 1026 |
{ |
| 1027 |
tempo = (unsigned) ev->data.meta.data[0] << 16 | |
| 1028 |
(unsigned) ev->data.meta.data[1] << 8 | |
| 1029 |
(unsigned) ev->data.meta.data[2]; |
| 1030 |
} |
| 1031 |
else |
| 1032 |
lprintf (LO_WARN, "MIDI_spmc: wrong length tempo meta message in midi file\n"); |
| 1033 |
} |
| 1034 |
else |
| 1035 |
lprintf (LO_WARN, "MIDI_spmc: passed non-meta event\n"); |
| 1036 |
} |
| 1037 |
|
| 1038 |
return compute_spmc_normal (headerval, tempo, sndrate); |
| 1039 |
} |
| 1040 |
|
| 1041 |
#ifdef TEST |
| 1042 |
|
| 1043 |
static char *MIDI_EventTypeToString(midi_event_type_t event_type) |
| 1044 |
{ |
| 1045 |
switch (event_type) |
| 1046 |
{ |
| 1047 |
case MIDI_EVENT_NOTE_OFF: |
| 1048 |
return "MIDI_EVENT_NOTE_OFF"; |
| 1049 |
case MIDI_EVENT_NOTE_ON: |
| 1050 |
return "MIDI_EVENT_NOTE_ON"; |
| 1051 |
case MIDI_EVENT_AFTERTOUCH: |
| 1052 |
return "MIDI_EVENT_AFTERTOUCH"; |
| 1053 |
case MIDI_EVENT_CONTROLLER: |
| 1054 |
return "MIDI_EVENT_CONTROLLER"; |
| 1055 |
case MIDI_EVENT_PROGRAM_CHANGE: |
| 1056 |
return "MIDI_EVENT_PROGRAM_CHANGE"; |
| 1057 |
case MIDI_EVENT_CHAN_AFTERTOUCH: |
| 1058 |
return "MIDI_EVENT_CHAN_AFTERTOUCH"; |
| 1059 |
case MIDI_EVENT_PITCH_BEND: |
| 1060 |
return "MIDI_EVENT_PITCH_BEND"; |
| 1061 |
case MIDI_EVENT_SYSEX: |
| 1062 |
return "MIDI_EVENT_SYSEX"; |
| 1063 |
case MIDI_EVENT_SYSEX_SPLIT: |
| 1064 |
return "MIDI_EVENT_SYSEX_SPLIT"; |
| 1065 |
case MIDI_EVENT_META: |
| 1066 |
return "MIDI_EVENT_META"; |
| 1067 |
|
| 1068 |
default: |
| 1069 |
return "(unknown)"; |
| 1070 |
} |
| 1071 |
} |
| 1072 |
|
| 1073 |
void PrintTrack (midi_track_t *track) |
| 1074 |
{ |
| 1075 |
midi_event_t *event; |
| 1076 |
unsigned int i; |
| 1077 |
|
| 1078 |
for (i=0; i<track->num_events; ++i) |
| 1079 |
{ |
| 1080 |
event = &track->events[i]; |
| 1081 |
|
| 1082 |
if (event->delta_time > 0) |
| 1083 |
{ |
| 1084 |
printf("Delay: %i ticks\n", event->delta_time); |
| 1085 |
} |
| 1086 |
|
| 1087 |
printf("Event type: %s (%i)\n", |
| 1088 |
MIDI_EventTypeToString(event->event_type), |
| 1089 |
event->event_type); |
| 1090 |
|
| 1091 |
switch(event->event_type) |
| 1092 |
{ |
| 1093 |
case MIDI_EVENT_NOTE_OFF: |
| 1094 |
case MIDI_EVENT_NOTE_ON: |
| 1095 |
case MIDI_EVENT_AFTERTOUCH: |
| 1096 |
case MIDI_EVENT_CONTROLLER: |
| 1097 |
case MIDI_EVENT_PROGRAM_CHANGE: |
| 1098 |
case MIDI_EVENT_CHAN_AFTERTOUCH: |
| 1099 |
case MIDI_EVENT_PITCH_BEND: |
| 1100 |
printf("\tChannel: %i\n", event->data.channel.channel); |
| 1101 |
printf("\tParameter 1: %i\n", event->data.channel.param1); |
| 1102 |
printf("\tParameter 2: %i\n", event->data.channel.param2); |
| 1103 |
break; |
| 1104 |
|
| 1105 |
case MIDI_EVENT_SYSEX: |
| 1106 |
case MIDI_EVENT_SYSEX_SPLIT: |
| 1107 |
printf("\tLength: %i\n", event->data.sysex.length); |
| 1108 |
break; |
| 1109 |
|
| 1110 |
case MIDI_EVENT_META: |
| 1111 |
printf("\tMeta type: %i\n", event->data.meta.type); |
| 1112 |
printf("\tLength: %i\n", event->data.meta.length); |
| 1113 |
break; |
| 1114 |
} |
| 1115 |
} |
| 1116 |
} |
| 1117 |
|
| 1118 |
int main(int argc, char *argv[]) |
| 1119 |
{ |
| 1120 |
FILE *f; |
| 1121 |
midimem_t mf; |
| 1122 |
midi_file_t *file; |
| 1123 |
unsigned int i; |
| 1124 |
|
| 1125 |
if (argc < 2) |
| 1126 |
{ |
| 1127 |
printf("Usage: %s <filename>\n", argv[0]); |
| 1128 |
exit(1); |
| 1129 |
} |
| 1130 |
f = fopen (argv[1], "rb"); |
| 1131 |
if (!f) |
| 1132 |
{ |
| 1133 |
fprintf(stderr, "Failed to open %s\n", argv[1]); |
| 1134 |
exit(1); |
| 1135 |
} |
| 1136 |
fseek (f, 0, SEEK_END); |
| 1137 |
mf.len = ftell (f); |
| 1138 |
mf.pos = 0; |
| 1139 |
rewind (f); |
| 1140 |
mf.data = Z_Malloc (mf.len); |
| 1141 |
fread (mf.data, 1, mf.len, f); |
| 1142 |
fclose (f); |
| 1143 |
|
| 1144 |
file = MIDI_LoadFile (&mf); |
| 1145 |
|
| 1146 |
if (file == NULL) |
| 1147 |
{ |
| 1148 |
fprintf(stderr, "Failed to open %s\n", argv[1]); |
| 1149 |
exit(1); |
| 1150 |
} |
| 1151 |
|
| 1152 |
for (i=0; i<file->num_tracks; ++i) |
| 1153 |
{ |
| 1154 |
printf("\n== Track %i ==\n\n", i); |
| 1155 |
|
| 1156 |
PrintTrack(&file->tracks[i]); |
| 1157 |
} |
| 1158 |
|
| 1159 |
return 0; |
| 1160 |
} |
| 1161 |
|
| 1162 |
#endif |