pipewire/src/tools/midifile.c

235 lines
5 KiB
C
Raw Normal View History

2020-02-18 10:55:05 +01:00
/* PipeWire
*
* Copyright © 2020 Wim Taymans
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#include <errno.h>
#include "midifile.h"
#define DEFAULT_TEMPO 500000 /* 500ms per quarter note (120 BPM) is the default */
static inline uint16_t parse_be16(const uint8_t *in)
{
return (in[0] << 8) | in[1];
}
static inline uint32_t parse_be32(const uint8_t *in)
{
return (in[0] << 24) | (in[1] << 16) | (in[2] << 8) | in[3];
}
2020-02-19 10:48:16 +01:00
static inline int mf_avail(struct midi_file *mf)
2020-02-18 10:55:05 +01:00
{
if (mf->p < mf->data + mf->size)
return mf->size + mf->data - mf->p;
return 0;
}
2020-02-18 10:55:05 +01:00
2020-02-19 10:48:16 +01:00
static inline int tr_avail(struct midi_track *tr)
{
2020-02-19 10:48:16 +01:00
if (tr->eof)
return 0;
if (tr->p < tr->data + tr->size)
return tr->size + tr->data - tr->p;
tr->eof = true;
return 0;
}
2020-02-18 10:55:05 +01:00
2020-02-19 10:48:16 +01:00
static int read_mthd(struct midi_file *mf)
{
if (mf_avail(mf) < 14 ||
memcmp(mf->p, "MThd", 4) != 0)
return -EINVAL;
2020-02-18 10:55:05 +01:00
mf->length = parse_be32(mf->p + 4);
mf->format = parse_be16(mf->p + 8);
mf->ntracks = parse_be16(mf->p + 10);
mf->division = parse_be16(mf->p + 12);
2020-02-19 10:48:16 +01:00
mf->p += 14;
2020-02-18 10:55:05 +01:00
return 0;
}
int midi_file_init(struct midi_file *mf, const char *mode,
void *data, size_t size)
2020-02-18 10:55:05 +01:00
{
int res;
spa_zero(*mf);
2020-02-19 10:48:16 +01:00
mf->data = mf->p = data;
mf->size = size;
2020-02-18 10:55:05 +01:00
if ((res = read_mthd(mf)) < 0)
return res;
spa_list_init(&mf->tracks);
2020-02-18 10:55:05 +01:00
mf->tempo = DEFAULT_TEMPO;
mf->tick = 0;
2020-02-18 10:55:05 +01:00
return 0;
}
static int read_mtrk(struct midi_file *mf, struct midi_track *track)
{
2020-02-19 10:48:16 +01:00
if (mf_avail(mf) < 8 ||
memcmp(mf->p, "MTrk", 4) != 0)
return -EINVAL;
2020-02-18 10:55:05 +01:00
2020-02-19 10:48:16 +01:00
track->data = track->p = mf->p + 8;
track->size = parse_be32(mf->p + 4);
2020-02-19 10:48:16 +01:00
mf->p = track->data + track->size;
2020-02-18 10:55:05 +01:00
return 0;
}
static int parse_varlen(struct midi_file *mf, struct midi_track *tr, uint32_t *result)
{
2020-02-19 10:48:16 +01:00
uint32_t value = 0;
2020-02-18 10:55:05 +01:00
2020-02-19 10:48:16 +01:00
while (tr_avail(tr) > 0) {
uint8_t b = *tr->p++;
value = (value << 7) | (b & 0x7f);
if ((b & 0x80) == 0)
2020-02-18 10:55:05 +01:00
break;
}
*result = value;
return 0;
}
static int peek_event(struct midi_file *mf, struct midi_track *tr, struct midi_event *event)
{
uint8_t *save, status;
2020-02-19 10:48:16 +01:00
uint32_t size;
2020-02-18 10:55:05 +01:00
int res;
2020-02-19 10:48:16 +01:00
if (tr_avail(tr) == 0)
return 0;
2020-02-18 10:55:05 +01:00
save = tr->p;
status = *tr->p;
2020-02-18 10:55:05 +01:00
event->track = tr;
event->sec = mf->tick_sec + ((tr->tick - mf->tick_start) * (double)mf->tempo) / (1000000.0 * mf->division);
2020-02-18 10:55:05 +01:00
if ((status & 0x80) == 0) {
status = tr->running_status;
} else {
tr->running_status = status;
2020-02-19 10:48:16 +01:00
tr->p++;
2020-02-18 10:55:05 +01:00
}
event->status = status;
2020-02-19 10:48:16 +01:00
switch (status) {
case 0xc0 ... 0xdf:
size = 1;
break;
case 0x00 ... 0xbf:
case 0xe0 ... 0xef:
size = 2;
break;
case 0xff:
event->meta = *tr->p++;
if ((res = parse_varlen(mf, tr, &size)) < 0)
return res;
switch (event->meta) {
case 0x2f:
tr->eof = true;
break;
case 0x51:
if (size < 3)
return -EINVAL;
2020-02-19 10:48:16 +01:00
mf->tick_sec = event->sec;
mf->tick_start = tr->tick;
mf->tempo = (tr->p[0]<<16) | (tr->p[1]<<8) | tr->p[2];
break;
2020-02-18 10:55:05 +01:00
}
2020-02-19 10:48:16 +01:00
break;
case 0xf0:
case 0xf7:
if ((res = parse_varlen(mf, tr, &size)) < 0)
return res;
break;
default:
return -ENOENT;
2020-02-18 10:55:05 +01:00
}
2020-02-19 10:48:16 +01:00
event->data = tr->p;
tr->p = save;
2020-02-18 10:55:05 +01:00
event->size = size;
return 1;
2020-02-18 10:55:05 +01:00
}
int midi_file_add_track(struct midi_file *mf, struct midi_track *track)
{
int res;
uint32_t delta_time;
if ((res = read_mtrk(mf, track)) < 0)
return res;
if ((res = parse_varlen(mf, track, &delta_time)) < 0)
return res;
track->tick = delta_time;
spa_list_append(&mf->tracks, &track->link);
return 0;
}
int midi_file_peek_event(struct midi_file *mf, struct midi_event *event)
{
struct midi_track *tr, *found = NULL;
spa_list_for_each(tr, &mf->tracks, link) {
2020-02-19 10:48:16 +01:00
if (tr_avail(tr) == 0)
2020-02-18 10:55:05 +01:00
continue;
if (found == NULL || tr->tick < found->tick)
found = tr;
}
if (found == NULL)
return 0;
2020-02-18 10:55:05 +01:00
return peek_event(mf, found, event);
}
int midi_file_consume_event(struct midi_file *mf, struct midi_event *event)
{
struct midi_track *tr = event->track;
uint32_t delta_time;
int res;
tr->p = event->data + event->size;
2020-02-19 10:48:16 +01:00
2020-02-18 10:55:05 +01:00
if ((res = parse_varlen(mf, tr, &delta_time)) < 0)
return res;
2020-02-19 10:48:16 +01:00
tr->tick += delta_time;
2020-02-18 10:55:05 +01:00
return 0;
}