2009-02-11 12:38:40 -05:00
|
|
|
/*-
|
|
|
|
* 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 format loader.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Reference: http://www.borg.com/~jglatt/tech/midifile.htm */
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <math.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include "smf.h"
|
|
|
|
#include "smf_private.h"
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns pointer to the next SMF chunk in smf->buffer, based on length of the previous one.
|
|
|
|
* Returns NULL in case of error.
|
|
|
|
*/
|
|
|
|
static struct chunk_header_struct *
|
|
|
|
next_chunk(smf_t *smf)
|
|
|
|
{
|
|
|
|
struct chunk_header_struct *chunk;
|
|
|
|
void *next_chunk_ptr;
|
|
|
|
|
|
|
|
assert(smf->file_buffer != NULL);
|
|
|
|
assert(smf->file_buffer_length > 0);
|
|
|
|
|
|
|
|
if (smf->next_chunk_offset + sizeof(struct chunk_header_struct) >= smf->file_buffer_length) {
|
2018-07-05 13:33:46 -04:00
|
|
|
g_warning("SMF warning: no more chunks left.");
|
2009-02-11 12:38:40 -05:00
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
next_chunk_ptr = (unsigned char *)smf->file_buffer + smf->next_chunk_offset;
|
|
|
|
|
|
|
|
chunk = (struct chunk_header_struct *)next_chunk_ptr;
|
|
|
|
|
|
|
|
if (!isalpha(chunk->id[0]) || !isalpha(chunk->id[1]) || !isalpha(chunk->id[2]) || !isalpha(chunk->id[3])) {
|
2018-07-05 13:33:46 -04:00
|
|
|
g_warning("SMF error: chunk signature contains at least one non-alphanumeric byte.");
|
2009-02-11 12:38:40 -05:00
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* XXX: On SPARC, after compiling with "-fast" option there will be SIGBUS here.
|
|
|
|
* Please compile with -xmemalign=8i".
|
|
|
|
*/
|
2020-07-16 10:55:45 -04:00
|
|
|
smf->next_chunk_offset += sizeof(struct chunk_header_struct) + GUINT32_FROM_BE(chunk->length);
|
2009-02-11 12:38:40 -05:00
|
|
|
|
|
|
|
if (smf->next_chunk_offset > smf->file_buffer_length) {
|
2020-07-16 12:00:40 -04:00
|
|
|
g_warning("SMF warning: malformed chunk; truncated file?");
|
|
|
|
smf->next_chunk_offset = smf->file_buffer_length;
|
2009-02-11 12:38:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return (chunk);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns 1, iff signature of the "chunk" is the same as string passed as "signature".
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
chunk_signature_matches(const struct chunk_header_struct *chunk, const char *signature)
|
|
|
|
{
|
|
|
|
if (!memcmp(chunk->id, signature, 4))
|
|
|
|
return (1);
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Verifies if MThd header looks OK. Returns 0 iff it does.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
parse_mthd_header(smf_t *smf)
|
|
|
|
{
|
|
|
|
int len;
|
|
|
|
struct chunk_header_struct *mthd, *tmp_mthd;
|
|
|
|
|
|
|
|
/* Make sure compiler didn't do anything stupid. */
|
|
|
|
assert(sizeof(struct chunk_header_struct) == 8);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We could just do "mthd = smf->file_buffer;" here, but this way we wouldn't
|
|
|
|
* get useful error messages.
|
|
|
|
*/
|
|
|
|
if (smf->file_buffer_length < 6) {
|
2018-07-05 13:33:46 -04:00
|
|
|
g_warning ("SMF error: file is too short, it cannot be a MIDI file.");
|
2009-02-11 12:38:40 -05:00
|
|
|
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
2013-07-31 20:08:43 -04:00
|
|
|
tmp_mthd = (struct chunk_header_struct*)smf->file_buffer;
|
2009-02-11 12:38:40 -05:00
|
|
|
|
|
|
|
if (!chunk_signature_matches(tmp_mthd, "MThd")) {
|
2018-07-05 13:33:46 -04:00
|
|
|
g_warning("SMF error: MThd signature not found, is that a MIDI file?");
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2009-02-11 12:38:40 -05:00
|
|
|
return (-2);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Ok, now use next_chunk(). */
|
|
|
|
mthd = next_chunk(smf);
|
|
|
|
if (mthd == NULL)
|
|
|
|
return (-3);
|
|
|
|
|
|
|
|
assert(mthd == tmp_mthd);
|
|
|
|
|
2020-07-16 10:55:45 -04:00
|
|
|
len = GUINT32_FROM_BE(mthd->length);
|
2009-02-11 12:38:40 -05:00
|
|
|
if (len != 6) {
|
2018-07-05 13:33:46 -04:00
|
|
|
g_warning("SMF error: MThd chunk length %d, must be 6.", len);
|
2009-02-11 12:38:40 -05:00
|
|
|
|
|
|
|
return (-4);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parses MThd chunk, filling "smf" structure with values extracted from it. Returns 0 iff everything went OK.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
parse_mthd_chunk(smf_t *smf)
|
|
|
|
{
|
|
|
|
signed char first_byte_of_division, second_byte_of_division;
|
|
|
|
|
|
|
|
struct mthd_chunk_struct *mthd;
|
|
|
|
|
|
|
|
assert(sizeof(struct mthd_chunk_struct) == 14);
|
|
|
|
|
|
|
|
if (parse_mthd_header(smf))
|
|
|
|
return (1);
|
|
|
|
|
|
|
|
mthd = (struct mthd_chunk_struct *)smf->file_buffer;
|
|
|
|
|
2020-07-16 10:55:45 -04:00
|
|
|
smf->format = GUINT16_FROM_BE(mthd->format);
|
2009-02-11 12:38:40 -05:00
|
|
|
if (smf->format < 0 || smf->format > 2) {
|
2018-07-05 13:33:46 -04:00
|
|
|
g_warning("SMF error: bad MThd format field value: %d, valid values are 0-2, inclusive.", smf->format);
|
2009-02-11 12:38:40 -05:00
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (smf->format == 2) {
|
2018-07-05 13:33:46 -04:00
|
|
|
g_warning("SMF file uses format #2, no support for that yet.");
|
2009-02-11 12:38:40 -05:00
|
|
|
return (-2);
|
|
|
|
}
|
|
|
|
|
2020-07-16 10:55:45 -04:00
|
|
|
smf->expected_number_of_tracks = GUINT16_FROM_BE(mthd->number_of_tracks);
|
2009-02-11 12:38:40 -05:00
|
|
|
if (smf->expected_number_of_tracks <= 0) {
|
2018-07-05 13:33:46 -04:00
|
|
|
g_warning("SMF error: bad number of tracks: %d, must be greater than zero.", smf->expected_number_of_tracks);
|
2009-02-11 12:38:40 -05:00
|
|
|
return (-3);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* XXX: endianess? */
|
|
|
|
first_byte_of_division = *((signed char *)&(mthd->division));
|
|
|
|
second_byte_of_division = *((signed char *)&(mthd->division) + 1);
|
|
|
|
|
|
|
|
if (first_byte_of_division >= 0) {
|
2020-07-16 10:55:45 -04:00
|
|
|
smf->ppqn = GUINT16_FROM_BE(mthd->division);
|
2009-02-11 12:38:40 -05:00
|
|
|
smf->frames_per_second = 0;
|
|
|
|
smf->resolution = 0;
|
|
|
|
} else {
|
|
|
|
smf->ppqn = 0;
|
|
|
|
smf->frames_per_second = - first_byte_of_division;
|
|
|
|
smf->resolution = second_byte_of_division;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (smf->ppqn == 0) {
|
2018-07-05 13:33:46 -04:00
|
|
|
g_warning("SMF file uses FPS timing instead of PPQN, no support for that yet.");
|
2009-02-11 12:38:40 -05:00
|
|
|
return (-4);
|
|
|
|
}
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2009-02-11 12:38:40 -05:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Interprets Variable Length Quantity pointed at by "buf" and puts its value into "value" and number
|
|
|
|
* of bytes consumed into "len", making sure it does not read past "buf" + "buffer_length".
|
|
|
|
* Explanation of Variable Length Quantities is here: http://www.borg.com/~jglatt/tech/midifile/vari.htm
|
|
|
|
* Returns 0 iff everything went OK, different value in case of error.
|
|
|
|
*/
|
2010-11-02 20:06:42 -04:00
|
|
|
int
|
|
|
|
smf_extract_vlq(const unsigned char *buf, const size_t buffer_length, uint32_t *value, uint32_t *len)
|
2009-02-11 12:38:40 -05:00
|
|
|
{
|
2009-02-14 17:22:55 -05:00
|
|
|
uint32_t val = 0;
|
2009-02-11 12:38:40 -05:00
|
|
|
const unsigned char *c = buf;
|
2015-12-26 21:40:48 -05:00
|
|
|
int i = 0;
|
2009-02-11 12:38:40 -05:00
|
|
|
|
2020-07-16 12:00:40 -04:00
|
|
|
assert(buffer_length > 0);
|
|
|
|
|
2015-12-26 21:40:48 -05:00
|
|
|
for (;; ++i) {
|
2009-02-11 12:38:40 -05:00
|
|
|
if (c >= buf + buffer_length) {
|
2018-07-05 13:33:46 -04:00
|
|
|
g_warning("End of buffer in extract_vlq().");
|
2009-02-11 12:38:40 -05:00
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
2015-12-26 21:40:48 -05:00
|
|
|
if (i == 4 && (val & 0xfe000000)) {
|
2018-07-05 13:33:46 -04:00
|
|
|
g_warning("SMF error: Variable Length Quantities longer than four bytes are not supported yet.");
|
2015-12-26 21:40:48 -05:00
|
|
|
return (-2);
|
|
|
|
}
|
|
|
|
|
2009-02-11 12:38:40 -05:00
|
|
|
val = (val << 7) + (*c & 0x7F);
|
|
|
|
|
|
|
|
if (*c & 0x80)
|
|
|
|
c++;
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
};
|
|
|
|
|
2009-02-14 17:22:55 -05:00
|
|
|
assert(c >= buf);
|
2009-02-11 12:38:40 -05:00
|
|
|
*value = val;
|
|
|
|
*len = c - buf + 1;
|
|
|
|
|
2015-12-26 21:40:48 -05:00
|
|
|
if (*len > 5) {
|
2018-07-05 13:33:46 -04:00
|
|
|
g_warning("SMF error: Variable Length Quantities longer than four bytes are not supported yet.");
|
2009-02-11 12:38:40 -05:00
|
|
|
return (-2);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns 1 if the given byte is a valid status byte, 0 otherwise.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
is_status_byte(const unsigned char status)
|
|
|
|
{
|
|
|
|
return (status & 0x80);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
is_sysex_byte(const unsigned char status)
|
|
|
|
{
|
|
|
|
if (status == 0xF0)
|
|
|
|
return (1);
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
is_escape_byte(const unsigned char status)
|
|
|
|
{
|
|
|
|
if (status == 0xF7)
|
|
|
|
return (1);
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Just like expected_message_length(), but only for System Exclusive messages.
|
|
|
|
* Note that value returned by this thing here is the length of SysEx "on the wire",
|
|
|
|
* not the number of bytes that this sysex takes in the file - in SMF format sysex
|
|
|
|
* contains VLQ telling how many bytes it takes, "on the wire" format does not have
|
|
|
|
* this.
|
|
|
|
*/
|
2009-02-14 17:22:55 -05:00
|
|
|
static int32_t
|
|
|
|
expected_sysex_length(const unsigned char status, const unsigned char *second_byte, const size_t buffer_length, int32_t *consumed_bytes)
|
2009-02-11 12:38:40 -05:00
|
|
|
{
|
2011-09-30 13:55:14 -04:00
|
|
|
uint32_t sysex_length = 0;
|
|
|
|
uint32_t len = 0;
|
2009-02-11 12:38:40 -05:00
|
|
|
|
2013-11-04 21:30:40 -05:00
|
|
|
#ifndef NDEBUG
|
|
|
|
(void) status;
|
|
|
|
#else
|
2020-07-16 12:00:40 -04:00
|
|
|
assert(status == 0xF0 || status == 0xF7);
|
2013-11-04 21:30:40 -05:00
|
|
|
#endif
|
2009-02-11 12:38:40 -05:00
|
|
|
|
|
|
|
if (buffer_length < 3) {
|
2018-07-05 13:33:46 -04:00
|
|
|
g_warning("SMF error: end of buffer in expected_sysex_length().");
|
2009-02-11 12:38:40 -05:00
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
2020-07-16 12:00:40 -04:00
|
|
|
if (smf_extract_vlq(second_byte, buffer_length, &sysex_length, &len)) {
|
|
|
|
return (-1);
|
|
|
|
}
|
2009-02-11 12:38:40 -05:00
|
|
|
|
|
|
|
if (consumed_bytes != NULL)
|
|
|
|
*consumed_bytes = len;
|
|
|
|
|
|
|
|
/* +1, because the length does not include status byte. */
|
|
|
|
return (sysex_length + 1);
|
|
|
|
}
|
|
|
|
|
2009-02-14 17:22:55 -05:00
|
|
|
static int32_t
|
|
|
|
expected_escaped_length(const unsigned char status, const unsigned char *second_byte, const size_t buffer_length, int32_t *consumed_bytes)
|
2009-02-11 12:38:40 -05:00
|
|
|
{
|
|
|
|
/* -1, because we do not want to account for 0x7F status. */
|
|
|
|
return (expected_sysex_length(status, second_byte, buffer_length, consumed_bytes) - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns expected length of the midi message (including the status byte), in bytes, for the given status byte.
|
|
|
|
* The "second_byte" points to the expected second byte of the MIDI message. "buffer_length" is the buffer
|
|
|
|
* length limit, counting from "second_byte". Returns value < 0 iff there was an error.
|
|
|
|
*/
|
2009-02-14 17:22:55 -05:00
|
|
|
static int32_t
|
|
|
|
expected_message_length(unsigned char status, const unsigned char *second_byte, const size_t buffer_length)
|
2009-02-11 12:38:40 -05:00
|
|
|
{
|
|
|
|
/* Make sure this really is a valid status byte. */
|
|
|
|
assert(is_status_byte(status));
|
|
|
|
|
|
|
|
/* We cannot use this routine for sysexes. */
|
|
|
|
assert(!is_sysex_byte(status));
|
|
|
|
|
|
|
|
/* We cannot use this routine for escaped events. */
|
|
|
|
assert(!is_escape_byte(status));
|
|
|
|
|
|
|
|
/* Is this a metamessage? */
|
|
|
|
if (status == 0xFF) {
|
|
|
|
if (buffer_length < 2) {
|
2018-07-05 13:33:46 -04:00
|
|
|
g_warning("SMF error: end of buffer in expected_message_length().");
|
2009-02-11 12:38:40 -05:00
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2017-02-28 10:24:07 -05:00
|
|
|
* Format of this kind of messages is like this: 0xFF 0xTYPE 0xlength and then "length" bytes.
|
|
|
|
* TYPE is < 127, length may be 0
|
|
|
|
*
|
|
|
|
* "lenght" is a 7bit value, the 8th bit is used to extend the length.
|
|
|
|
* eg. ff02 8266 <0x166 byte (C) message follows>
|
2009-02-11 12:38:40 -05:00
|
|
|
*/
|
2017-02-28 10:24:07 -05:00
|
|
|
int32_t mlen = 0;
|
2017-02-28 15:33:13 -05:00
|
|
|
int32_t off;
|
|
|
|
for (off = 1; off < 4; ++off) {
|
2017-02-28 10:24:07 -05:00
|
|
|
uint8_t val = *(second_byte + off);
|
|
|
|
mlen = mlen << 7 | (val & 0x7f);
|
|
|
|
if (0 == (val & 0x80)) {
|
|
|
|
mlen += 2 + off; // 2 byte "header" 0xff <type> + <length of length>
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return mlen;
|
2009-02-11 12:38:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((status & 0xF0) == 0xF0) {
|
|
|
|
switch (status) {
|
|
|
|
case 0xF2: /* Song Position Pointer. */
|
|
|
|
return (3);
|
|
|
|
|
|
|
|
case 0xF1: /* MTC Quarter Frame. */
|
|
|
|
case 0xF3: /* Song Select. */
|
|
|
|
return (2);
|
|
|
|
|
|
|
|
case 0xF6: /* Tune Request. */
|
|
|
|
case 0xF8: /* MIDI Clock. */
|
|
|
|
case 0xF9: /* Tick. */
|
|
|
|
case 0xFA: /* MIDI Start. */
|
|
|
|
case 0xFB: /* MIDI Continue. */
|
|
|
|
case 0xFC: /* MIDI Stop. */
|
|
|
|
case 0xFE: /* Active Sense. */
|
|
|
|
return (1);
|
|
|
|
|
|
|
|
default:
|
2018-07-05 13:33:46 -04:00
|
|
|
g_warning("SMF error: unknown 0xFx-type status byte '0x%x'.", status);
|
2009-02-11 12:38:40 -05:00
|
|
|
return (-2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Filter out the channel. */
|
|
|
|
status &= 0xF0;
|
|
|
|
|
|
|
|
switch (status) {
|
|
|
|
case 0x80: /* Note Off. */
|
|
|
|
case 0x90: /* Note On. */
|
|
|
|
case 0xA0: /* AfterTouch. */
|
|
|
|
case 0xB0: /* Control Change. */
|
|
|
|
case 0xE0: /* Pitch Wheel. */
|
2015-10-05 10:17:49 -04:00
|
|
|
return (3);
|
2009-02-11 12:38:40 -05:00
|
|
|
|
|
|
|
case 0xC0: /* Program Change. */
|
|
|
|
case 0xD0: /* Channel Pressure. */
|
|
|
|
return (2);
|
|
|
|
|
|
|
|
default:
|
2018-07-05 13:33:46 -04:00
|
|
|
g_warning("SMF error: unknown status byte '0x%x'.", status);
|
2009-02-11 12:38:40 -05:00
|
|
|
return (-3);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2009-02-14 17:22:55 -05:00
|
|
|
extract_sysex_event(const unsigned char *buf, const size_t buffer_length, smf_event_t *event, uint32_t *len, int last_status)
|
2009-02-11 12:38:40 -05:00
|
|
|
{
|
2009-07-21 11:55:17 -04:00
|
|
|
(void) last_status;
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2009-02-14 17:22:55 -05:00
|
|
|
int status;
|
|
|
|
int32_t vlq_length, message_length;
|
2009-02-11 12:38:40 -05:00
|
|
|
const unsigned char *c = buf;
|
|
|
|
|
|
|
|
status = *buf;
|
|
|
|
|
2014-12-30 23:07:19 -05:00
|
|
|
if (!(is_sysex_byte(status))) {
|
2018-07-05 13:33:46 -04:00
|
|
|
g_warning("Corrupt sysex status byte in extract_sysex_event().");
|
2014-12-30 23:07:19 -05:00
|
|
|
return (-6);
|
|
|
|
}
|
2009-02-11 12:38:40 -05:00
|
|
|
|
|
|
|
c++;
|
|
|
|
|
|
|
|
message_length = expected_sysex_length(status, c, buffer_length - 1, &vlq_length);
|
|
|
|
|
|
|
|
if (message_length < 0)
|
|
|
|
return (-3);
|
|
|
|
|
|
|
|
c += vlq_length;
|
|
|
|
|
2009-02-14 17:22:55 -05:00
|
|
|
if (vlq_length + (size_t)message_length >= buffer_length) {
|
2018-07-05 13:33:46 -04:00
|
|
|
g_warning("End of buffer in extract_sysex_event().");
|
2009-02-11 12:38:40 -05:00
|
|
|
return (-5);
|
|
|
|
}
|
|
|
|
|
|
|
|
event->midi_buffer_length = message_length;
|
2013-07-23 02:47:10 -04:00
|
|
|
event->midi_buffer = (uint8_t*)malloc(event->midi_buffer_length);
|
2009-02-11 12:38:40 -05:00
|
|
|
if (event->midi_buffer == NULL) {
|
2018-07-05 13:33:46 -04:00
|
|
|
g_warning("Cannot allocate memory in extract_sysex_event(): %s", strerror(errno));
|
2009-02-11 12:38:40 -05:00
|
|
|
return (-4);
|
|
|
|
}
|
|
|
|
|
|
|
|
event->midi_buffer[0] = status;
|
|
|
|
memcpy(event->midi_buffer + 1, c, message_length - 1);
|
|
|
|
|
|
|
|
*len = vlq_length + message_length;
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2009-02-14 17:22:55 -05:00
|
|
|
extract_escaped_event(const unsigned char *buf, const size_t buffer_length, smf_event_t *event, uint32_t *len, int last_status)
|
2009-02-11 12:38:40 -05:00
|
|
|
{
|
2009-07-21 11:55:17 -04:00
|
|
|
(void) last_status;
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2009-02-14 17:22:55 -05:00
|
|
|
int status;
|
2011-09-30 13:55:14 -04:00
|
|
|
int32_t message_length = 0;
|
|
|
|
int32_t vlq_length = 0;
|
2009-02-11 12:38:40 -05:00
|
|
|
const unsigned char *c = buf;
|
|
|
|
|
|
|
|
status = *buf;
|
|
|
|
|
2014-12-30 23:07:19 -05:00
|
|
|
if (!(is_escape_byte(status))) {
|
2018-07-05 13:33:46 -04:00
|
|
|
g_warning("Corrupt escape status byte in extract_escaped_event().");
|
2014-12-30 23:07:19 -05:00
|
|
|
return (-6);
|
|
|
|
}
|
2009-02-11 12:38:40 -05:00
|
|
|
|
|
|
|
c++;
|
|
|
|
|
|
|
|
message_length = expected_escaped_length(status, c, buffer_length - 1, &vlq_length);
|
|
|
|
|
2020-07-16 12:00:40 -04:00
|
|
|
if (message_length <= 0)
|
2009-02-11 12:38:40 -05:00
|
|
|
return (-3);
|
|
|
|
|
|
|
|
c += vlq_length;
|
|
|
|
|
2009-02-14 17:22:55 -05:00
|
|
|
if (vlq_length + (size_t)message_length >= buffer_length) {
|
2018-07-05 13:33:46 -04:00
|
|
|
g_warning("End of buffer in extract_escaped_event().");
|
2009-02-11 12:38:40 -05:00
|
|
|
return (-5);
|
|
|
|
}
|
|
|
|
|
|
|
|
event->midi_buffer_length = message_length;
|
2013-07-23 02:47:10 -04:00
|
|
|
event->midi_buffer = (uint8_t*)malloc(event->midi_buffer_length);
|
2009-02-11 12:38:40 -05:00
|
|
|
if (event->midi_buffer == NULL) {
|
2018-07-05 13:33:46 -04:00
|
|
|
g_warning("Cannot allocate memory in extract_escaped_event(): %s", strerror(errno));
|
2009-02-11 12:38:40 -05:00
|
|
|
return (-4);
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(event->midi_buffer, c, message_length);
|
|
|
|
|
2020-07-16 12:00:40 -04:00
|
|
|
if (!smf_event_is_valid(event)) {
|
2018-07-05 13:33:46 -04:00
|
|
|
g_warning("Escaped event is invalid.");
|
2009-02-11 12:38:40 -05:00
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
2020-07-16 12:00:40 -04:00
|
|
|
if (!(smf_event_is_system_realtime(event) || smf_event_is_system_common(event))) {
|
2009-02-11 12:38:40 -05:00
|
|
|
g_warning("Escaped event is not System Realtime nor System Common.");
|
|
|
|
}
|
|
|
|
|
|
|
|
*len = vlq_length + message_length;
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Puts MIDI data extracted from from "buf" into "event" and number of consumed bytes into "len".
|
|
|
|
* In case valid status is not found, it uses "last_status" (so called "running status").
|
|
|
|
* Returns 0 iff everything went OK, value < 0 in case of error.
|
|
|
|
*/
|
|
|
|
static int
|
2009-02-14 17:22:55 -05:00
|
|
|
extract_midi_event(const unsigned char *buf, const size_t buffer_length, smf_event_t *event, uint32_t *len, int last_status)
|
2009-02-11 12:38:40 -05:00
|
|
|
{
|
2009-02-14 17:22:55 -05:00
|
|
|
int status;
|
|
|
|
int32_t message_length;
|
2009-02-11 12:38:40 -05:00
|
|
|
const unsigned char *c = buf;
|
|
|
|
|
|
|
|
assert(buffer_length > 0);
|
|
|
|
|
|
|
|
/* Is the first byte the status byte? */
|
|
|
|
if (is_status_byte(*c)) {
|
|
|
|
status = *c;
|
|
|
|
c++;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
/* No, we use running status then. */
|
|
|
|
status = last_status;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!is_status_byte(status)) {
|
2018-07-05 13:33:46 -04:00
|
|
|
g_warning("SMF error: bad status byte (MSB is zero).");
|
2009-02-11 12:38:40 -05:00
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
2020-07-16 12:00:40 -04:00
|
|
|
if (is_sysex_byte(status)) {
|
|
|
|
if (c == buf) {
|
|
|
|
g_critical("SMF error: running status is not applicable to System Exclusive events.");
|
|
|
|
return (-2);
|
|
|
|
}
|
2009-02-11 12:38:40 -05:00
|
|
|
return (extract_sysex_event(buf, buffer_length, event, len, last_status));
|
2020-07-16 12:00:40 -04:00
|
|
|
}
|
2009-02-11 12:38:40 -05:00
|
|
|
|
2020-07-16 12:00:40 -04:00
|
|
|
if (is_escape_byte(status)) {
|
|
|
|
if (c == buf) {
|
|
|
|
g_critical("SMF error: running status is not applicable to Escape events.");
|
|
|
|
return (-2);
|
|
|
|
}
|
2009-02-11 12:38:40 -05:00
|
|
|
return (extract_escaped_event(buf, buffer_length, event, len, last_status));
|
2020-07-16 12:00:40 -04:00
|
|
|
}
|
2009-02-11 12:38:40 -05:00
|
|
|
|
|
|
|
/* At this point, "c" points to first byte following the status byte. */
|
|
|
|
message_length = expected_message_length(status, c, buffer_length - (c - buf));
|
|
|
|
|
|
|
|
if (message_length < 0)
|
|
|
|
return (-3);
|
|
|
|
|
2009-02-14 17:22:55 -05:00
|
|
|
if ((size_t)message_length > buffer_length - (c - buf) + 1) {
|
2018-07-05 13:33:46 -04:00
|
|
|
g_warning("End of buffer in extract_midi_event().");
|
2009-02-11 12:38:40 -05:00
|
|
|
return (-5);
|
|
|
|
}
|
|
|
|
|
|
|
|
event->midi_buffer_length = message_length;
|
2013-07-23 02:47:10 -04:00
|
|
|
event->midi_buffer = (uint8_t*)malloc(event->midi_buffer_length);
|
2009-02-11 12:38:40 -05:00
|
|
|
if (event->midi_buffer == NULL) {
|
2018-07-05 13:33:46 -04:00
|
|
|
g_warning("Cannot allocate memory in extract_midi_event(): %s", strerror(errno));
|
2009-02-11 12:38:40 -05:00
|
|
|
return (-4);
|
|
|
|
}
|
|
|
|
|
|
|
|
event->midi_buffer[0] = status;
|
|
|
|
memcpy(event->midi_buffer + 1, c, message_length - 1);
|
|
|
|
|
|
|
|
*len = c + message_length - 1 - buf;
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Locates, basing on track->next_event_offset, the next event data in track->buffer,
|
|
|
|
* interprets it, allocates smf_event_t and fills it properly. Returns smf_event_t
|
|
|
|
* or NULL, if there was an error. Allocating event means adding it to the track;
|
|
|
|
* see smf_event_new().
|
|
|
|
*/
|
|
|
|
static smf_event_t *
|
|
|
|
parse_next_event(smf_track_t *track)
|
|
|
|
{
|
2013-11-04 21:30:40 -05:00
|
|
|
uint32_t etime = 0;
|
2009-02-14 17:22:55 -05:00
|
|
|
uint32_t len;
|
|
|
|
size_t buffer_length;
|
2009-02-11 12:38:40 -05:00
|
|
|
unsigned char *c, *start;
|
|
|
|
|
|
|
|
smf_event_t *event = smf_event_new();
|
|
|
|
if (event == NULL)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
c = start = (unsigned char *)track->file_buffer + track->next_event_offset;
|
|
|
|
|
|
|
|
assert(track->file_buffer != NULL);
|
|
|
|
assert(track->file_buffer_length > 0);
|
|
|
|
assert(track->next_event_offset > 0);
|
|
|
|
|
|
|
|
buffer_length = track->file_buffer_length - track->next_event_offset;
|
2020-07-16 12:00:40 -04:00
|
|
|
|
2014-10-15 15:12:02 -04:00
|
|
|
/* if there was no meta-EOT event, buffer_length can be zero. This is
|
|
|
|
an error in the SMF file, but it shouldn't be treated as fatal.
|
|
|
|
*/
|
|
|
|
if (buffer_length == 0) {
|
|
|
|
g_warning ("SMF warning: expected EOT at end of track, but none found");
|
|
|
|
goto error;
|
|
|
|
}
|
2009-02-11 12:38:40 -05:00
|
|
|
/* First, extract time offset from previous event. */
|
2014-10-15 15:12:02 -04:00
|
|
|
if (smf_extract_vlq(c, buffer_length, &etime, &len)) {
|
2009-02-11 12:38:40 -05:00
|
|
|
goto error;
|
2014-10-15 15:12:02 -04:00
|
|
|
}
|
2009-02-11 12:38:40 -05:00
|
|
|
|
|
|
|
c += len;
|
|
|
|
buffer_length -= len;
|
|
|
|
|
|
|
|
if (buffer_length <= 0)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
/* Now, extract the actual event. */
|
2014-10-15 15:12:02 -04:00
|
|
|
if (extract_midi_event(c, buffer_length, event, &len, track->last_status)) {
|
2009-02-11 12:38:40 -05:00
|
|
|
goto error;
|
2014-10-15 15:12:02 -04:00
|
|
|
}
|
2009-02-11 12:38:40 -05:00
|
|
|
|
|
|
|
c += len;
|
|
|
|
buffer_length -= len;
|
2021-06-22 16:08:58 -04:00
|
|
|
if (!smf_event_is_metadata (event)) {
|
|
|
|
track->last_status = event->midi_buffer[0];
|
|
|
|
}
|
2009-02-11 12:38:40 -05:00
|
|
|
track->next_event_offset += c - start;
|
|
|
|
|
2013-11-04 21:30:40 -05:00
|
|
|
smf_track_add_event_delta_pulses(track, event, etime);
|
2009-02-11 12:38:40 -05:00
|
|
|
|
|
|
|
return (event);
|
|
|
|
|
|
|
|
error:
|
|
|
|
if (event != NULL)
|
|
|
|
smf_event_delete(event);
|
|
|
|
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Takes "len" characters starting in "buf", making sure it does not access past the length of the buffer,
|
|
|
|
* and makes ordinary, zero-terminated string from it. May return NULL if there was any problem.
|
2015-10-04 14:51:05 -04:00
|
|
|
*/
|
2009-02-11 12:38:40 -05:00
|
|
|
static char *
|
2009-02-14 17:22:55 -05:00
|
|
|
make_string(const unsigned char *buf, const size_t buffer_length, uint32_t len)
|
2009-02-11 12:38:40 -05:00
|
|
|
{
|
|
|
|
char *str;
|
|
|
|
|
|
|
|
assert(buffer_length > 0);
|
|
|
|
assert(len > 0);
|
|
|
|
|
|
|
|
if (len > buffer_length) {
|
2018-07-05 13:33:46 -04:00
|
|
|
g_warning("End of buffer in make_string().");
|
2009-02-11 12:38:40 -05:00
|
|
|
|
|
|
|
len = buffer_length;
|
|
|
|
}
|
|
|
|
|
2013-07-23 02:47:10 -04:00
|
|
|
str = (char*)malloc(len + 1);
|
2009-02-11 12:38:40 -05:00
|
|
|
if (str == NULL) {
|
2018-07-05 13:33:46 -04:00
|
|
|
g_warning("Cannot allocate memory in make_string().");
|
2009-02-11 12:38:40 -05:00
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(str, buf, len);
|
|
|
|
str[len] = '\0';
|
|
|
|
|
|
|
|
return (str);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \return 1, if passed a metaevent containing text, that is, Text, Copyright,
|
|
|
|
* Sequence/Track Name, Instrument, Lyric, Marker, Cue Point, Program Name,
|
|
|
|
* or Device Name; 0 otherwise.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
smf_event_is_textual(const smf_event_t *event)
|
|
|
|
{
|
2016-12-14 06:40:07 -05:00
|
|
|
if (!smf_event_is_metadata(event)) {
|
2009-02-11 12:38:40 -05:00
|
|
|
return (0);
|
2016-12-14 06:40:07 -05:00
|
|
|
}
|
2009-02-11 12:38:40 -05:00
|
|
|
|
2016-12-14 06:40:27 -05:00
|
|
|
if (event->midi_buffer_length < 4) {
|
2009-02-11 12:38:40 -05:00
|
|
|
return (0);
|
2016-12-14 06:40:27 -05:00
|
|
|
}
|
2009-02-11 12:38:40 -05:00
|
|
|
|
2016-12-14 06:40:44 -05:00
|
|
|
if (event->midi_buffer[1] < 1 || event->midi_buffer[1] > 7) {
|
2009-02-11 12:38:40 -05:00
|
|
|
return (0);
|
2016-12-14 06:40:44 -05:00
|
|
|
}
|
2009-02-11 12:38:40 -05:00
|
|
|
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Extracts text from "textual metaevents", such as Text or Lyric.
|
|
|
|
*
|
|
|
|
* \return Zero-terminated string extracted from "text events" or NULL, if there was any problem.
|
|
|
|
*/
|
|
|
|
char *
|
|
|
|
smf_event_extract_text(const smf_event_t *event)
|
|
|
|
{
|
2011-09-30 13:55:14 -04:00
|
|
|
uint32_t string_length = 0;
|
|
|
|
uint32_t length_length = 0;
|
2009-02-11 12:38:40 -05:00
|
|
|
|
2016-12-14 06:41:09 -05:00
|
|
|
if (!smf_event_is_textual(event)) {
|
|
|
|
g_warning ("smf_event_extract_text: event is not textual.");
|
2009-02-11 12:38:40 -05:00
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
2013-07-23 02:47:10 -04:00
|
|
|
smf_extract_vlq((const unsigned char*)(void *)&(event->midi_buffer[2]), event->midi_buffer_length - 2, &string_length, &length_length);
|
2009-02-11 12:38:40 -05:00
|
|
|
|
|
|
|
if (string_length <= 0) {
|
2018-07-05 13:33:46 -04:00
|
|
|
g_warning("smf_event_extract_text: truncated MIDI message.");
|
2009-02-11 12:38:40 -05:00
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
2013-07-23 02:47:10 -04:00
|
|
|
return (make_string((const unsigned char*)(void *)(&event->midi_buffer[2] + length_length), event->midi_buffer_length - 2 - length_length, string_length));
|
2009-02-11 12:38:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Verify if the next chunk really is MTrk chunk, and if so, initialize some track variables and return 0.
|
|
|
|
* Return different value otherwise.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
parse_mtrk_header(smf_track_t *track)
|
|
|
|
{
|
|
|
|
struct chunk_header_struct *mtrk;
|
|
|
|
|
|
|
|
/* Make sure compiler didn't do anything stupid. */
|
|
|
|
assert(sizeof(struct chunk_header_struct) == 8);
|
|
|
|
assert(track->smf != NULL);
|
|
|
|
|
|
|
|
mtrk = next_chunk(track->smf);
|
|
|
|
|
|
|
|
if (mtrk == NULL)
|
|
|
|
return (-1);
|
|
|
|
|
|
|
|
if (!chunk_signature_matches(mtrk, "MTrk")) {
|
|
|
|
g_warning("SMF warning: Expected MTrk signature, got %c%c%c%c instead; ignoring this chunk.",
|
|
|
|
mtrk->id[0], mtrk->id[1], mtrk->id[2], mtrk->id[3]);
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2009-02-11 12:38:40 -05:00
|
|
|
return (-2);
|
|
|
|
}
|
|
|
|
|
|
|
|
track->file_buffer = mtrk;
|
2020-07-16 10:55:45 -04:00
|
|
|
track->file_buffer_length = sizeof(struct chunk_header_struct) + GUINT32_FROM_BE(mtrk->length);
|
2009-02-11 12:38:40 -05:00
|
|
|
track->next_event_offset = sizeof(struct chunk_header_struct);
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return 1 if event is end-of-the-track, 0 otherwise.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
event_is_end_of_track(const smf_event_t *event)
|
|
|
|
{
|
|
|
|
if (event->midi_buffer[0] == 0xFF && event->midi_buffer[1] == 0x2F)
|
|
|
|
return (1);
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \return Nonzero, if event is as long as it should be, from the MIDI specification point of view.
|
|
|
|
* Does not work for SysExes - it doesn't recognize internal structure of SysEx.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
smf_event_length_is_valid(const smf_event_t *event)
|
|
|
|
{
|
|
|
|
assert(event);
|
|
|
|
assert(event->midi_buffer);
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2009-02-14 17:22:55 -05:00
|
|
|
int32_t expected;
|
2009-02-11 12:38:40 -05:00
|
|
|
|
|
|
|
if (event->midi_buffer_length < 1)
|
|
|
|
return (0);
|
|
|
|
|
|
|
|
/* We cannot use expected_message_length on sysexes. */
|
|
|
|
if (smf_event_is_sysex(event))
|
|
|
|
return (1);
|
|
|
|
|
|
|
|
|
2009-02-14 17:22:55 -05:00
|
|
|
expected = expected_message_length(event->midi_buffer[0],
|
|
|
|
&(event->midi_buffer[1]), event->midi_buffer_length - 1);
|
|
|
|
if (expected < 0 || event->midi_buffer_length != (size_t)expected) {
|
2009-02-11 12:38:40 -05:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \return Nonzero, if MIDI data in the event is valid, 0 otherwise. For example,
|
|
|
|
* it checks if event length is correct.
|
|
|
|
*/
|
|
|
|
/* XXX: this routine requires some more work to detect more errors. */
|
|
|
|
int
|
|
|
|
smf_event_is_valid(const smf_event_t *event)
|
|
|
|
{
|
|
|
|
assert(event);
|
|
|
|
assert(event->midi_buffer);
|
|
|
|
assert(event->midi_buffer_length >= 1);
|
|
|
|
|
|
|
|
if (!is_status_byte(event->midi_buffer[0])) {
|
2018-07-05 13:33:46 -04:00
|
|
|
g_warning("First byte of MIDI message is not a valid status byte.");
|
2009-02-11 12:38:40 -05:00
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!smf_event_length_is_valid(event))
|
|
|
|
return (0);
|
|
|
|
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse events and put it on the track.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
parse_mtrk_chunk(smf_track_t *track)
|
|
|
|
{
|
|
|
|
smf_event_t *event;
|
|
|
|
|
|
|
|
if (parse_mtrk_header(track))
|
|
|
|
return (-1);
|
|
|
|
|
|
|
|
for (;;) {
|
2020-07-16 12:00:40 -04:00
|
|
|
if (track->next_event_offset == track->file_buffer_length) {
|
|
|
|
g_warning("SMF warning: The track did not finish with the End of Track event.");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-02-11 12:38:40 -05:00
|
|
|
event = parse_next_event(track);
|
|
|
|
|
|
|
|
/* Couldn't parse an event? */
|
2020-07-16 12:00:40 -04:00
|
|
|
if (event == NULL) {
|
|
|
|
g_warning("Unable to parse MIDI event; truncating track.");
|
|
|
|
if (smf_track_add_eot_delta_pulses(track, 0) != 0) {
|
|
|
|
g_critical("smf_track_add_eot_delta_pulses failed.");
|
|
|
|
return (-2);
|
|
|
|
}
|
2014-12-30 23:07:19 -05:00
|
|
|
break;
|
|
|
|
}
|
2009-02-11 12:38:40 -05:00
|
|
|
|
2020-07-16 12:00:40 -04:00
|
|
|
assert(smf_event_is_valid(event));
|
|
|
|
|
2016-12-14 06:42:10 -05:00
|
|
|
if (event_is_end_of_track(event)) {
|
2009-02-11 12:38:40 -05:00
|
|
|
break;
|
2016-12-14 06:42:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (smf_event_is_metadata (event)) {
|
|
|
|
switch (event->midi_buffer[1]) {
|
|
|
|
case 0x03:
|
|
|
|
track->name = smf_event_extract_text (event);
|
|
|
|
break;
|
|
|
|
case 0x04:
|
|
|
|
track->instrument = smf_event_extract_text (event);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2009-02-11 12:38:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
track->file_buffer = NULL;
|
|
|
|
track->file_buffer_length = 0;
|
|
|
|
track->next_event_offset = -1;
|
|
|
|
|
2020-07-16 12:00:40 -04:00
|
|
|
return (0);
|
2009-02-11 12:38:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-05-16 16:54:50 -04:00
|
|
|
* Allocate buffer of proper size and read file contents into it.
|
2009-02-11 12:38:40 -05:00
|
|
|
*/
|
|
|
|
static int
|
2010-05-16 16:54:50 -04:00
|
|
|
load_file_into_buffer(void **file_buffer, size_t *file_buffer_length, FILE* stream)
|
2009-02-11 12:38:40 -05:00
|
|
|
{
|
2009-02-14 17:22:55 -05:00
|
|
|
long offset;
|
2009-02-11 12:38:40 -05:00
|
|
|
|
|
|
|
if (stream == NULL) {
|
2018-07-05 13:33:46 -04:00
|
|
|
g_warning("Cannot open input file: %s", strerror(errno));
|
2009-02-11 12:38:40 -05:00
|
|
|
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fseek(stream, 0, SEEK_END)) {
|
2018-07-05 13:33:46 -04:00
|
|
|
g_warning("fseek(3) failed: %s", strerror(errno));
|
2009-02-11 12:38:40 -05:00
|
|
|
|
|
|
|
return (-2);
|
|
|
|
}
|
|
|
|
|
2009-02-14 17:22:55 -05:00
|
|
|
offset = ftell(stream);
|
|
|
|
if (offset < 0) {
|
2018-07-05 13:33:46 -04:00
|
|
|
g_warning("ftell(3) failed: %s", strerror(errno));
|
2009-02-11 12:38:40 -05:00
|
|
|
|
|
|
|
return (-3);
|
|
|
|
}
|
2009-02-14 17:22:55 -05:00
|
|
|
*file_buffer_length = (size_t)offset;
|
2009-02-11 12:38:40 -05:00
|
|
|
|
|
|
|
if (fseek(stream, 0, SEEK_SET)) {
|
2018-07-05 13:33:46 -04:00
|
|
|
g_warning("fseek(3) failed: %s", strerror(errno));
|
2009-02-11 12:38:40 -05:00
|
|
|
|
|
|
|
return (-4);
|
|
|
|
}
|
|
|
|
|
|
|
|
*file_buffer = malloc(*file_buffer_length);
|
|
|
|
if (*file_buffer == NULL) {
|
2018-07-05 13:33:46 -04:00
|
|
|
g_warning("malloc(3) failed: %s", strerror(errno));
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2009-02-11 12:38:40 -05:00
|
|
|
return (-5);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fread(*file_buffer, 1, *file_buffer_length, stream) != *file_buffer_length) {
|
2018-07-05 13:33:46 -04:00
|
|
|
g_warning("fread(3) failed: %s", strerror(errno));
|
2012-04-01 11:06:24 -04:00
|
|
|
free (*file_buffer);
|
|
|
|
*file_buffer = NULL;
|
2009-02-11 12:38:40 -05:00
|
|
|
return (-6);
|
|
|
|
}
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2009-02-11 12:38:40 -05:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates new SMF and fills it with data loaded from the given buffer.
|
|
|
|
* \return SMF or NULL, if loading failed.
|
|
|
|
*/
|
|
|
|
smf_t *
|
2015-06-24 11:55:41 -04:00
|
|
|
smf_load_from_memory(void *buffer, const size_t buffer_length)
|
2009-02-11 12:38:40 -05:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
smf_t *smf = smf_new();
|
|
|
|
|
2015-06-24 11:55:41 -04:00
|
|
|
smf->file_buffer = (void *) buffer;
|
2009-02-11 12:38:40 -05:00
|
|
|
smf->file_buffer_length = buffer_length;
|
|
|
|
smf->next_chunk_offset = 0;
|
|
|
|
|
2020-07-16 12:00:40 -04:00
|
|
|
if (parse_mthd_chunk(smf)) {
|
|
|
|
smf_delete(smf);
|
2009-02-11 12:38:40 -05:00
|
|
|
return (NULL);
|
2020-07-16 12:00:40 -04:00
|
|
|
}
|
2009-02-11 12:38:40 -05:00
|
|
|
|
|
|
|
for (i = 1; i <= smf->expected_number_of_tracks; i++) {
|
|
|
|
smf_track_t *track = smf_track_new();
|
2020-07-16 12:00:40 -04:00
|
|
|
if (track == NULL) {
|
|
|
|
smf_delete(smf);
|
2009-02-11 12:38:40 -05:00
|
|
|
return (NULL);
|
2020-07-16 12:00:40 -04:00
|
|
|
}
|
2009-02-11 12:38:40 -05:00
|
|
|
|
|
|
|
smf_add_track(smf, track);
|
|
|
|
|
2020-07-16 12:00:40 -04:00
|
|
|
/* Skip unparseable chunks. */
|
|
|
|
if (parse_mtrk_chunk(track)) {
|
|
|
|
g_warning("SMF warning: Cannot load track.");
|
|
|
|
smf_track_delete(track);
|
|
|
|
break;
|
|
|
|
}
|
2009-02-11 12:38:40 -05:00
|
|
|
|
|
|
|
track->file_buffer = NULL;
|
|
|
|
track->file_buffer_length = 0;
|
|
|
|
track->next_event_offset = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (smf->expected_number_of_tracks != smf->number_of_tracks) {
|
|
|
|
g_warning("SMF warning: MThd header declared %d tracks, but only %d found; continuing anyway.",
|
|
|
|
smf->expected_number_of_tracks, smf->number_of_tracks);
|
|
|
|
|
|
|
|
smf->expected_number_of_tracks = smf->number_of_tracks;
|
|
|
|
}
|
|
|
|
|
2022-02-05 11:03:42 -05:00
|
|
|
/* process tempo-map */
|
|
|
|
if (smf->need_tempo_map_compute) {
|
|
|
|
smf_create_tempo_map_and_compute_seconds(smf);
|
|
|
|
}
|
|
|
|
|
2009-02-11 12:38:40 -05:00
|
|
|
smf->file_buffer = NULL;
|
|
|
|
smf->file_buffer_length = 0;
|
2009-02-14 17:22:55 -05:00
|
|
|
smf->next_chunk_offset = 0;
|
2009-02-11 12:38:40 -05:00
|
|
|
|
|
|
|
return (smf);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Loads SMF file.
|
|
|
|
*
|
2010-05-16 16:54:50 -04:00
|
|
|
* \param file Open file.
|
2009-02-11 12:38:40 -05:00
|
|
|
* \return SMF or NULL, if loading failed.
|
|
|
|
*/
|
|
|
|
smf_t *
|
2010-05-16 16:54:50 -04:00
|
|
|
smf_load(FILE *file)
|
2009-02-11 12:38:40 -05:00
|
|
|
{
|
2009-02-14 17:22:55 -05:00
|
|
|
size_t file_buffer_length;
|
2009-02-11 12:38:40 -05:00
|
|
|
void *file_buffer;
|
|
|
|
smf_t *smf;
|
|
|
|
|
2010-05-16 16:54:50 -04:00
|
|
|
if (load_file_into_buffer(&file_buffer, &file_buffer_length, file))
|
2009-02-11 12:38:40 -05:00
|
|
|
return (NULL);
|
|
|
|
|
|
|
|
smf = smf_load_from_memory(file_buffer, file_buffer_length);
|
|
|
|
|
|
|
|
memset(file_buffer, 0, file_buffer_length);
|
|
|
|
free(file_buffer);
|
|
|
|
|
|
|
|
if (smf == NULL)
|
|
|
|
return (NULL);
|
|
|
|
|
|
|
|
smf_rewind(smf);
|
|
|
|
|
|
|
|
return (smf);
|
|
|
|
}
|
|
|
|
|