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 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660
|
/*-
* Copyright (c) 2007, 2008 Edward Tomasz NapieraĆa <trasz@FreeBSD.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* ALTHOUGH THIS SOFTWARE IS MADE OF WIN AND SCIENCE, IT IS PROVIDED BY THE
* AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
/**
* \file
*
* Standard MIDI File writer.
*
*/
/* Reference: http://www.borg.com/~jglatt/tech/midifile.htm */
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <math.h>
#include <errno.h>
#ifdef __MINGW32__
#include <windows.h>
#else /* ! __MINGW32__ */
#include <arpa/inet.h>
#endif /* ! __MINGW32__ */
#include "smf.h"
#include "smf_private.h"
#define MAX_VLQ_LENGTH 128
/**
* Extends (reallocates) smf->file_buffer and returns pointer to the newly added space,
* that is, pointer to the first byte after the previous buffer end. Returns NULL in case
* of error.
*/
static void *
smf_extend(smf_t *smf, const int length)
{
int i, previous_file_buffer_length = smf->file_buffer_length;
char *previous_file_buffer = smf->file_buffer;
/* XXX: Not terribly efficient. */
smf->file_buffer_length += length;
smf->file_buffer = realloc(smf->file_buffer, smf->file_buffer_length);
if (smf->file_buffer == NULL) {
g_critical("realloc(3) failed: %s", strerror(errno));
smf->file_buffer_length = 0;
return (NULL);
}
/* Fix up pointers. XXX: omgwtf. */
for (i = 1; i <= smf->number_of_tracks; i++) {
smf_track_t *track;
track = smf_get_track_by_number(smf, i);
if (track->file_buffer != NULL)
track->file_buffer = (char *)track->file_buffer + ((char *)smf->file_buffer - previous_file_buffer);
}
return ((char *)smf->file_buffer + previous_file_buffer_length);
}
/**
* Appends "buffer_length" bytes pointed to by "buffer" to the smf, reallocating storage as needed. Returns 0
* if everything went ok, different value if there was any problem.
*/
static int
smf_append(smf_t *smf, const void *buffer, const int buffer_length)
{
void *dest;
dest = smf_extend(smf, buffer_length);
if (dest == NULL) {
g_critical("Cannot extend track buffer.");
return (-1);
}
memcpy(dest, buffer, buffer_length);
return (0);
}
/**
* Appends MThd header to the track. Returns 0 if everything went ok, different value if not.
*/
static int
write_mthd_header(smf_t *smf)
{
struct mthd_chunk_struct mthd_chunk;
memcpy(mthd_chunk.mthd_header.id, "MThd", 4);
mthd_chunk.mthd_header.length = htonl(6);
mthd_chunk.format = htons(smf->format);
mthd_chunk.number_of_tracks = htons(smf->number_of_tracks);
mthd_chunk.division = htons(smf->ppqn);
return (smf_append(smf, &mthd_chunk, sizeof(mthd_chunk)));
}
/**
* Extends (reallocates) track->file_buffer and returns pointer to the newly added space,
* that is, pointer to the first byte after the previous buffer end. Returns NULL in case
* of error.
*/
static void *
track_extend(smf_track_t *track, const int length)
{
void *buf;
assert(track->smf);
buf = smf_extend(track->smf, length);
if (buf == NULL)
return (NULL);
track->file_buffer_length += length;
if (track->file_buffer == NULL)
track->file_buffer = buf;
return (buf);
}
/**
* Appends "buffer_length" bytes pointed to by "buffer" to the track, reallocating storage as needed. Returns 0
* if everything went ok, different value if there was any problem.
*/
static int
track_append(smf_track_t *track, const void *buffer, const int buffer_length)
{
void *dest;
dest = track_extend(track, buffer_length);
if (dest == NULL) {
g_critical("Cannot extend track buffer.");
return (-1);
}
memcpy(dest, buffer, buffer_length);
return (0);
}
static int
format_vlq(unsigned char *buf, int length, unsigned long value)
{
int i;
unsigned long buffer;
/* Taken from http://www.borg.com/~jglatt/tech/midifile/vari.htm */
buffer = value & 0x7F;
while ((value >>= 7)) {
buffer <<= 8;
buffer |= ((value & 0x7F) | 0x80);
}
for (i = 0;; i++) {
buf[i] = buffer;
if (buffer & 0x80)
buffer >>= 8;
else
break;
}
assert(i <= length);
/* + 1, because "i" is an offset, not a count. */
return (i + 1);
}
smf_event_t *
smf_event_new_textual(int type, const char *text)
{
int vlq_length, text_length, copied_length;
smf_event_t *event;
assert(type >= 1 && type <= 9);
text_length = strlen(text);
event = smf_event_new();
if (event == NULL)
return (NULL);
/* "2 +" is for leading 0xFF 0xtype. */
event->midi_buffer_length = 2 + text_length + MAX_VLQ_LENGTH;
event->midi_buffer = malloc(event->midi_buffer_length);
if (event->midi_buffer == NULL) {
g_critical("Cannot allocate MIDI buffer structure: %s", strerror(errno));
smf_event_delete(event);
return (NULL);
}
event->midi_buffer[0] = 0xFF;
event->midi_buffer[1] = type;
vlq_length = format_vlq(event->midi_buffer + 2, MAX_VLQ_LENGTH - 2, text_length);
copied_length = snprintf((char *)event->midi_buffer + vlq_length + 2, event->midi_buffer_length - vlq_length - 2, "%s", text);
assert(copied_length == text_length);
event->midi_buffer_length = 2 + vlq_length + text_length;
return event;
}
/**
* Appends value, expressed as Variable Length Quantity, to event->track.
*/
static int
write_vlq(smf_event_t *event, unsigned long value)
{
unsigned char buf[MAX_VLQ_LENGTH];
int vlq_length;
vlq_length = format_vlq(buf, MAX_VLQ_LENGTH, value);
return (track_append(event->track, buf, vlq_length));
}
/**
* Appends event time as Variable Length Quantity. Returns 0 if everything went ok,
* different value in case of error.
*/
static int
write_event_time(smf_event_t *event)
{
assert(event->delta_time_pulses >= 0);
return (write_vlq(event, event->delta_time_pulses));
}
static int
write_sysex_contents(smf_event_t *event)
{
int ret;
unsigned char sysex_status = 0xF0;
assert(smf_event_is_sysex(event));
ret = track_append(event->track, &sysex_status, 1);
if (ret)
return (ret);
/* -1, because length does not include status byte. */
ret = write_vlq(event, event->midi_buffer_length - 1);
if (ret)
return (ret);
ret = track_append(event->track, event->midi_buffer + 1, event->midi_buffer_length - 1);
if (ret)
return (ret);
return (0);
}
/**
* Appends contents of event->midi_buffer wrapped into 0xF7 MIDI event.
*/
static int
write_escaped_event_contents(smf_event_t *event)
{
int ret;
unsigned char escape_status = 0xF7;
if (smf_event_is_sysex(event))
return (write_sysex_contents(event));
ret = track_append(event->track, &escape_status, 1);
if (ret)
return (ret);
ret = write_vlq(event, event->midi_buffer_length);
if (ret)
return (ret);
ret = track_append(event->track, event->midi_buffer, event->midi_buffer_length);
if (ret)
return (ret);
return (0);
}
/**
* Appends contents of event->midi_buffer. Returns 0 if everything went 0,
* different value in case of error.
*/
static int
write_event_contents(smf_event_t *event)
{
if (smf_event_is_system_realtime(event) || smf_event_is_system_common(event))
return (write_escaped_event_contents(event));
return (track_append(event->track, event->midi_buffer, event->midi_buffer_length));
}
/**
* Writes out an event.
*/
static int
write_event(smf_event_t *event)
{
int ret;
ret = write_event_time(event);
if (ret)
return (ret);
ret = write_event_contents(event);
if (ret)
return (ret);
return (0);
}
/**
* Writes out MTrk header, except of MTrk chunk length, which is written by write_mtrk_length().
*/
static int
write_mtrk_header(smf_track_t *track)
{
struct chunk_header_struct mtrk_header;
memcpy(mtrk_header.id, "MTrk", 4);
return (track_append(track, &mtrk_header, sizeof(mtrk_header)));
}
/**
* Updates MTrk chunk length of a given track.
*/
static int
write_mtrk_length(smf_track_t *track)
{
struct chunk_header_struct *mtrk_header;
assert(track->file_buffer != NULL);
assert(track->file_buffer_length >= 6);
mtrk_header = (struct chunk_header_struct *)track->file_buffer;
mtrk_header->length = htonl(track->file_buffer_length - sizeof(struct chunk_header_struct));
return (0);
}
/**
* Writes out the track.
*/
static int
write_track(smf_track_t *track)
{
int ret;
smf_event_t *event;
ret = write_mtrk_header(track);
if (ret)
return (ret);
while ((event = smf_track_get_next_event(track)) != NULL) {
ret = write_event(event);
if (ret)
return (ret);
}
ret = write_mtrk_length(track);
if (ret)
return (ret);
return (0);
}
/**
* Takes smf->file_buffer and saves it to the file.
*/
static int
write_file(smf_t *smf, const char *file_name)
{
FILE *stream;
stream = fopen(file_name, "wb+");
if (stream == NULL) {
g_critical("Cannot open input file: %s", strerror(errno));
return (-1);
}
if (fwrite(smf->file_buffer, 1, smf->file_buffer_length, stream) != smf->file_buffer_length) {
g_critical("fwrite(3) failed: %s", strerror(errno));
return (-2);
}
if (fclose(stream)) {
g_critical("fclose(3) failed: %s", strerror(errno));
return (-3);
}
return (0);
}
static void
free_buffer(smf_t *smf)
{
int i;
smf_track_t *track;
/* Clear the pointers. */
memset(smf->file_buffer, 0, smf->file_buffer_length);
free(smf->file_buffer);
smf->file_buffer = NULL;
smf->file_buffer_length = 0;
for (i = 1; i <= smf->number_of_tracks; i++) {
track = smf_get_track_by_number(smf, i);
assert(track);
track->file_buffer = NULL;
track->file_buffer_length = 0;
}
}
#ifndef NDEBUG
/**
* \return Nonzero, if all pointers supposed to be NULL are NULL. Triggers assertion if not.
*/
static int
pointers_are_clear(smf_t *smf)
{
int i;
smf_track_t *track;
assert(smf->file_buffer == NULL);
assert(smf->file_buffer_length == 0);
for (i = 1; i <= smf->number_of_tracks; i++) {
track = smf_get_track_by_number(smf, i);
assert(track != NULL);
assert(track->file_buffer == NULL);
assert(track->file_buffer_length == 0);
}
return (1);
}
#endif /* !NDEBUG */
/**
* \return Nonzero, if event is End Of Track metaevent.
*/
int
smf_event_is_eot(const smf_event_t *event)
{
if (event->midi_buffer_length != 3)
return (0);
if (event->midi_buffer[0] != 0xFF || event->midi_buffer[1] != 0x2F || event->midi_buffer[2] != 0x00)
return (0);
return (1);
}
/**
* Check if SMF is valid and add missing EOT events.
*
* \return 0, if SMF is valid.
*/
static int
smf_validate(smf_t *smf)
{
int trackno, eventno, eot_found;
smf_track_t *track;
smf_event_t *event;
if (smf->format < 0 || smf->format > 2) {
g_critical("SMF error: smf->format is less than zero of greater than two.");
return (-1);
}
if (smf->number_of_tracks < 1) {
g_critical("SMF error: number of tracks is less than one.");
return (-2);
}
if (smf->format == 0 && smf->number_of_tracks > 1) {
g_critical("SMF error: format is 0, but number of tracks is more than one.");
return (-3);
}
if (smf->ppqn <= 0) {
g_critical("SMF error: PPQN has to be > 0.");
return (-4);
}
for (trackno = 1; trackno <= smf->number_of_tracks; trackno++) {
track = smf_get_track_by_number(smf, trackno);
assert(track);
eot_found = 0;
for (eventno = 1; eventno <= track->number_of_events; eventno++) {
event = smf_track_get_event_by_number(track, eventno);
assert(event);
if (!smf_event_is_valid(event)) {
g_critical("Event #%d on track #%d is invalid.", eventno, trackno);
return (-5);
}
if (smf_event_is_eot(event)) {
if (eot_found) {
g_critical("Duplicate End Of Track event on track #%d.", trackno);
return (-6);
}
eot_found = 1;
}
}
if (!eot_found) {
if (smf_track_add_eot_delta_pulses(track, 0)) {
g_critical("smf_track_add_eot_delta_pulses failed.");
return (-6);
}
}
}
return (0);
}
#ifndef NDEBUG
static void
assert_smf_event_is_identical(const smf_event_t *a, const smf_event_t *b)
{
assert(a->event_number == b->event_number);
assert(a->delta_time_pulses == b->delta_time_pulses);
assert(abs(a->time_pulses - b->time_pulses) <= 2);
assert(fabs(a->time_seconds - b->time_seconds) <= 0.01);
assert(a->track_number == b->track_number);
assert(a->midi_buffer_length == b->midi_buffer_length);
assert(memcmp(a->midi_buffer, b->midi_buffer, a->midi_buffer_length) == 0);
}
static void
assert_smf_track_is_identical(const smf_track_t *a, const smf_track_t *b)
{
int i;
assert(a->track_number == b->track_number);
assert(a->number_of_events == b->number_of_events);
for (i = 1; i <= a->number_of_events; i++)
assert_smf_event_is_identical(smf_track_get_event_by_number(a, i), smf_track_get_event_by_number(b, i));
}
static void
assert_smf_is_identical(const smf_t *a, const smf_t *b)
{
int i;
assert(a->format == b->format);
assert(a->ppqn == b->ppqn);
assert(a->frames_per_second == b->frames_per_second);
assert(a->resolution == b->resolution);
assert(a->number_of_tracks == b->number_of_tracks);
for (i = 1; i <= a->number_of_tracks; i++)
assert_smf_track_is_identical(smf_get_track_by_number(a, i), smf_get_track_by_number(b, i));
/* We do not need to compare tempos explicitly, as tempo is always computed from track contents. */
}
static void
assert_smf_saved_correctly(const smf_t *smf, const char *file_name)
{
smf_t *saved;
saved = smf_load(file_name);
assert(saved != NULL);
assert_smf_is_identical(smf, saved);
smf_delete(saved);
}
#endif /* !NDEBUG */
/**
* Writes the contents of SMF to the file given.
* \param smf SMF.
* \param file_name Path to the file.
* \return 0, if saving was successfull.
*/
int
smf_save(smf_t *smf, const char *file_name)
{
int i, error;
smf_track_t *track;
smf_rewind(smf);
assert(pointers_are_clear(smf));
if (smf_validate(smf))
return (-1);
if (write_mthd_header(smf))
return (-2);
for (i = 1; i <= smf->number_of_tracks; i++) {
track = smf_get_track_by_number(smf, i);
assert(track != NULL);
error = write_track(track);
if (error) {
free_buffer(smf);
return (error);
}
}
error = write_file(smf, file_name);
free_buffer(smf);
if (error)
return (error);
#ifndef NDEBUG
assert_smf_saved_correctly(smf, file_name);
#endif
return (0);
}
|