From 6d4298a1f463e94c9388ce25542f1413cac43af1 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Mon, 13 Aug 2012 20:22:15 +0000 Subject: Move SMF stuff from Raul to Machina. git-svn-id: http://svn.drobilla.net/lad/trunk/machina@4683 a436a847-0d15-0410-975c-d299462d15a1 --- src/engine/SMFReader.cpp | 311 +++++++++++++++++++++++++++++++++++++++++++++++ src/engine/SMFReader.hpp | 81 ++++++++++++ src/engine/SMFWriter.cpp | 235 +++++++++++++++++++++++++++++++++++ src/engine/SMFWriter.hpp | 71 +++++++++++ src/engine/smf_test.cpp | 80 ++++++++++++ 5 files changed, 778 insertions(+) create mode 100644 src/engine/SMFReader.cpp create mode 100644 src/engine/SMFReader.hpp create mode 100644 src/engine/SMFWriter.cpp create mode 100644 src/engine/SMFWriter.hpp create mode 100644 src/engine/smf_test.cpp (limited to 'src/engine') diff --git a/src/engine/SMFReader.cpp b/src/engine/SMFReader.cpp new file mode 100644 index 0000000..0474f88 --- /dev/null +++ b/src/engine/SMFReader.cpp @@ -0,0 +1,311 @@ +/* + This file is part of Raul. + Copyright 2007-2012 David Robillard + + Raul is free software: you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation, either version 3 of the License, or any later version. + + Raul is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with Raul. If not, see . +*/ + +#include + +#include +#include +#include +#include + +#include "raul/log.hpp" +#include "raul/SMFReader.hpp" +#include "raul/midi_events.h" + +using std::endl; + +namespace Raul { + +/** Return the size of the given event NOT including the status byte, + * or -1 if unknown (eg sysex) + */ +static int +midi_event_size(unsigned char status) +{ + if (status >= 0x80 && status <= 0xE0) { + status &= 0xF0; // mask off the channel + } + + switch (status) { + case MIDI_CMD_NOTE_OFF: + case MIDI_CMD_NOTE_ON: + case MIDI_CMD_NOTE_PRESSURE: + case MIDI_CMD_CONTROL: + case MIDI_CMD_BENDER: + case MIDI_CMD_COMMON_SONG_POS: + return 2; + + case MIDI_CMD_PGM_CHANGE: + case MIDI_CMD_CHANNEL_PRESSURE: + case MIDI_CMD_COMMON_MTC_QUARTER: + case MIDI_CMD_COMMON_SONG_SELECT: + return 1; + + case MIDI_CMD_COMMON_TUNE_REQUEST: + case MIDI_CMD_COMMON_SYSEX_END: + case MIDI_CMD_COMMON_CLOCK: + case MIDI_CMD_COMMON_START: + case MIDI_CMD_COMMON_CONTINUE: + case MIDI_CMD_COMMON_STOP: + case MIDI_CMD_COMMON_SENSING: + case MIDI_CMD_COMMON_RESET: + return 0; + + case MIDI_CMD_COMMON_SYSEX: + return -1; + } + + return -1; +} + +SMFReader::SMFReader(const std::string filename) + : _fd(NULL) + , _ppqn(0) + , _track(0) + , _track_size(0) +{ + if (filename.length() > 0) + open(filename); +} + +SMFReader::~SMFReader() +{ + if (_fd) + close(); +} + +bool +SMFReader::open(const std::string& filename) throw (std::logic_error, UnsupportedTime) +{ + if (_fd) + throw std::logic_error("Attempt to start new read while write in progress."); + + info << "Opening SMF file " << filename << " for reading." << endl; + + _fd = fopen(filename.c_str(), "r+"); + + if (_fd) { + // Read type (bytes 8..9) + fseek(_fd, 0, SEEK_SET); + char mthd[5]; + mthd[4] = '\0'; + fread(mthd, 1, 4, _fd); + if (strcmp(mthd, "MThd")) { + error << filename << " is not an SMF file, aborting." << endl; + fclose(_fd); + _fd = NULL; + return false; + } + + // Read type (bytes 8..9) + fseek(_fd, 8, SEEK_SET); + uint16_t type_be = 0; + fread(&type_be, 2, 1, _fd); + _type = GUINT16_FROM_BE(type_be); + + // Read number of tracks (bytes 10..11) + uint16_t num_tracks_be = 0; + fread(&num_tracks_be, 2, 1, _fd); + _num_tracks = GUINT16_FROM_BE(num_tracks_be); + + // Read PPQN (bytes 12..13) + uint16_t ppqn_be = 0; + fread(&ppqn_be, 2, 1, _fd); + _ppqn = GUINT16_FROM_BE(ppqn_be); + + // TODO: Absolute (SMPTE seconds) time support + if ((_ppqn & 0x8000) != 0) + throw UnsupportedTime(); + + seek_to_track(1); + + return true; + } else { + return false; + } +} + +/** Seek to the start of a given track, starting from 1. + * Returns true if specified track was found. + */ +bool +SMFReader::seek_to_track(unsigned track) throw (std::logic_error) +{ + if (track == 0) + throw std::logic_error("Seek to track 0 out of range (must be >= 1)"); + + if (!_fd) + throw std::logic_error("Attempt to seek to track on unopened SMF file."); + + unsigned track_pos = 0; + + fseek(_fd, 14, SEEK_SET); + char id[5]; + id[4] = '\0'; + uint32_t chunk_size = 0; + + while (!feof(_fd)) { + fread(id, 1, 4, _fd); + + if (!strcmp(id, "MTrk")) { + ++track_pos; + } else { + error << "Unknown chunk ID " << id << endl; + } + + uint32_t chunk_size_be; + fread(&chunk_size_be, 4, 1, _fd); + chunk_size = GUINT32_FROM_BE(chunk_size_be); + + if (track_pos == track) + break; + + fseek(_fd, chunk_size, SEEK_CUR); + } + + if (!feof(_fd) && track_pos == track) { + _track = track; + _track_size = chunk_size; + return true; + } else { + return false; + } +} + +/** Read an event from the current position in file. + * + * File position MUST be at the beginning of a delta time, or this will die very messily. + * ev.buffer must be of size ev.size, and large enough for the event. The returned event + * will have it's time field set to it's delta time (so it's the caller's responsibility + * to keep track of delta time, even for ignored events). + * + * Returns event length (including status byte) on success, 0 if event was + * skipped (eg a meta event), or -1 on EOF (or end of track). + * + * If @a buf is not large enough to hold the event, 0 will be returned, but ev_size + * set to the actual size of the event. + */ +int +SMFReader::read_event(size_t buf_len, + uint8_t* buf, + uint32_t* ev_size, + uint32_t* delta_time) + throw (std::logic_error, PrematureEOF, CorruptFile) +{ + if (_track == 0) + throw std::logic_error("Attempt to read from unopened SMF file"); + + if (!_fd || feof(_fd)) { + return -1; + } + + assert(buf_len > 0); + assert(buf); + assert(ev_size); + assert(delta_time); + + // Running status state + static uint8_t last_status = 0; + static uint32_t last_size = 0; + + *delta_time = read_var_len(_fd); + int status = fgetc(_fd); + if (status == EOF) + throw PrematureEOF(); + else if (status > 0xFF) + throw CorruptFile(); + + if (status < 0x80) { + if (last_status == 0) + throw CorruptFile(); + status = last_status; + *ev_size = last_size; + fseek(_fd, -1, SEEK_CUR); + } else { + last_status = status; + *ev_size = midi_event_size(status) + 1; + last_size = *ev_size; + } + + buf[0] = static_cast(status); + + if (status == 0xFF) { + *ev_size = 0; + if (feof(_fd)) + throw PrematureEOF(); + uint8_t type = fgetc(_fd); + const uint32_t size = read_var_len(_fd); + + if (type == 0x2F) { + return -1; // we hit the logical EOF anyway... + } else { + fseek(_fd, size, SEEK_CUR); + return 0; + } + } + + if (*ev_size > buf_len || *ev_size == 0 || feof(_fd)) { + // Skip event, return 0 + fseek(_fd, *ev_size - 1, SEEK_CUR); + return 0; + } else { + // Read event, return size + if (ferror(_fd)) + throw CorruptFile(); + + fread(buf+1, 1, *ev_size - 1, _fd); + + if ((buf[0] & 0xF0) == 0x90 && buf[2] == 0) { + buf[0] = (0x80 | (buf[0] & 0x0F)); + buf[2] = 0x40; + } + + return *ev_size; + } +} + +void +SMFReader::close() +{ + if (_fd) + fclose(_fd); + + _fd = NULL; +} + +uint32_t +SMFReader::read_var_len(FILE* fd) throw (PrematureEOF) +{ + if (feof(fd)) + throw PrematureEOF(); + + uint32_t value; + uint8_t c; + + if ( (value = getc(fd)) & 0x80 ) { + value &= 0x7F; + do { + if (feof(fd)) + throw PrematureEOF(); + value = (value << 7) + ((c = getc(fd)) & 0x7F); + } while (c & 0x80); + } + + return value; +} + +} // namespace Raul + diff --git a/src/engine/SMFReader.hpp b/src/engine/SMFReader.hpp new file mode 100644 index 0000000..dbca289 --- /dev/null +++ b/src/engine/SMFReader.hpp @@ -0,0 +1,81 @@ +/* + This file is part of Raul. + Copyright 2007-2012 David Robillard + + Raul is free software: you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation, either version 3 of the License, or any later version. + + Raul is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with Raul. If not, see . +*/ + +#ifndef RAUL_SMF_READER_HPP +#define RAUL_SMF_READER_HPP + +#include +#include +#include +#include "raul/TimeStamp.hpp" + +namespace Raul { + +/** Standard Midi File (Type 0) Reader + * + * Currently this only reads SMF files with tempo-based timing. + * \ingroup raul + */ +class SMFReader { +public: + class PrematureEOF : public std::exception { + const char* what() const throw() { return "Unexpected end of file"; } + }; + class CorruptFile : public std::exception { + const char* what() const throw() { return "Corrupted file"; } + }; + class UnsupportedTime : public std::exception { + const char* what() const throw() { return "Unsupported time stamp type (SMPTE)"; } + }; + + explicit SMFReader(const std::string filename=""); + ~SMFReader(); + + bool open(const std::string& filename) throw (std::logic_error, UnsupportedTime); + + bool seek_to_track(unsigned track) throw (std::logic_error); + + uint16_t type() const { return _type; } + uint16_t ppqn() const { return _ppqn; } + size_t num_tracks() { return _num_tracks; } + + int read_event(size_t buf_len, + uint8_t* buf, + uint32_t* ev_size, + uint32_t* ev_delta_time) + throw (std::logic_error, PrematureEOF, CorruptFile); + + void close(); + + static uint32_t read_var_len(FILE* fd) throw (PrematureEOF); + +protected: + /** size of SMF header, including MTrk chunk header */ + static const uint32_t HEADER_SIZE = 22; + + std::string _filename; + FILE* _fd; + uint16_t _type; + uint16_t _ppqn; + uint16_t _num_tracks; + uint32_t _track; + uint32_t _track_size; +}; + +} // namespace Raul + +#endif // RAUL_SMF_READER_HPP + diff --git a/src/engine/SMFWriter.cpp b/src/engine/SMFWriter.cpp new file mode 100644 index 0000000..f3a9043 --- /dev/null +++ b/src/engine/SMFWriter.cpp @@ -0,0 +1,235 @@ +/* + This file is part of Raul. + Copyright 2007-2012 David Robillard + + Raul is free software: you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation, either version 3 of the License, or any later version. + + Raul is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with Raul. If not, see . +*/ + +#include +#include +#include + +#include +#include +#include +#include +#include + +#include "raul/log.hpp" +#include "raul/SMFWriter.hpp" + +using std::endl; + +namespace Raul { + +/** Create a new SMF writer. + * + * @a unit must match the time stamp of ALL events passed to write, or + * terrible things will happen. + * + * *** NOTE: Only beat time is implemented currently. + */ +SMFWriter::SMFWriter(TimeUnit unit) + : _fd(NULL) + , _unit(unit) + , _start_time(unit, 0, 0) + , _last_ev_time(unit, 0, 0) + , _track_size(0) + , _header_size(0) +{ + if (unit.type() == TimeUnit::BEATS) + assert(unit.ppt() < std::numeric_limits::max()); +} + +SMFWriter::~SMFWriter() +{ + if (_fd) + finish(); +} + +/** Start a write to an SMF file. + * + * @a filename Filename to write to. + * @a start_time Beat time corresponding to t=0 in the file (timestamps passed + * to write_event will have this value subtracted before writing). + */ +bool +SMFWriter::start(const std::string& filename, + Raul::TimeStamp start_time) throw (std::logic_error) +{ + if (_fd) + throw std::logic_error("Attempt to start new write while write in progress."); + + info << "Opening SMF file " << filename << " for writing." << endl; + + _fd = fopen(filename.c_str(), "w+"); + + if (_fd) { + _track_size = 0; + _filename = filename; + _start_time = start_time; + _last_ev_time = 0; + // write a tentative header to pad file out so writing starts at the right offset + write_header(); + } + + return (_fd == 0) ? -1 : 0; +} + +/** Write an event at the end of the file. + * + * @a time is the absolute time of the event, relative to the start of the file + * (the start_time parameter to start). Must be monotonically increasing on + * successive calls to this method. + */ +void +SMFWriter::write_event(Raul::TimeStamp time, + size_t ev_size, + const unsigned char* ev) throw (std::logic_error) +{ + if (time < _start_time) + throw std::logic_error("Event time is before file start time"); + else if (time < _last_ev_time) + throw std::logic_error("Event time not monotonically increasing"); + else if (time.unit() != _unit) + throw std::logic_error("Event has unexpected time unit"); + + Raul::TimeStamp delta_time = time; + delta_time -= _start_time; + + fseek(_fd, 0, SEEK_END); + + uint64_t delta_ticks = delta_time.ticks() * _unit.ppt() + delta_time.subticks(); + size_t stamp_size = 0; + + /* If delta time is too long (i.e. overflows), write out empty + * "proprietary" events to reach the desired time. + * Any SMF reading application should interpret this correctly + * (by accumulating the delta time and ignoring the event) */ + while (delta_ticks > VAR_LEN_MAX) { + static unsigned char null_event[] = { 0xFF, 0x7F, 0x0 }; + stamp_size = write_var_len(VAR_LEN_MAX); + fwrite(null_event, 1, 3, _fd); + _track_size += stamp_size + 3; + delta_ticks -= VAR_LEN_MAX; + } + + assert(delta_ticks <= VAR_LEN_MAX); + stamp_size = write_var_len(static_cast(delta_ticks)); + fwrite(ev, 1, ev_size, _fd); + + _last_ev_time = time; + _track_size += stamp_size + ev_size; +} + +void +SMFWriter::flush() +{ + if (_fd) + fflush(_fd); +} + +void +SMFWriter::finish() throw (std::logic_error) +{ + if (!_fd) + throw std::logic_error("Attempt to finish write with no write in progress."); + + write_footer(); + fclose(_fd); + _fd = NULL; +} + +void +SMFWriter::write_header() +{ + info << "SMF Flushing header\n"; + + assert(_fd); + + const uint16_t type = GUINT16_TO_BE(0); // SMF Type 0 (single track) + const uint16_t ntracks = GUINT16_TO_BE(1); // Number of tracks (always 1 for Type 0) + const uint16_t division = GUINT16_TO_BE(_unit.ppt()); // PPQN + + char data[6]; + memcpy(data, &type, 2); + memcpy(data+2, &ntracks, 2); + memcpy(data+4, &division, 2); + //data[4] = _ppqn & 0xF0; + //data[5] = _ppqn & 0x0F; + + _fd = freopen(_filename.c_str(), "r+", _fd); + assert(_fd); + fseek(_fd, 0, 0); + write_chunk("MThd", 6, data); + write_chunk_header("MTrk", _track_size); +} + +void +SMFWriter::write_footer() +{ + info << "Writing EOT\n"; + + fseek(_fd, 0, SEEK_END); + write_var_len(1); // whatever... + static const unsigned char eot[4] = { 0xFF, 0x2F, 0x00 }; // end-of-track meta-event + fwrite(eot, 1, 4, _fd); +} + +void +SMFWriter::write_chunk_header(const char id[4], uint32_t length) +{ + const uint32_t length_be = GUINT32_TO_BE(length); + + fwrite(id, 1, 4, _fd); + fwrite(&length_be, 4, 1, _fd); +} + +void +SMFWriter::write_chunk(const char id[4], uint32_t length, void* data) +{ + write_chunk_header(id, length); + + fwrite(data, 1, length, _fd); +} + +/** Write an SMF variable length value. + * + * @return size (in bytes) of the value written. + */ +size_t +SMFWriter::write_var_len(uint32_t value) +{ + size_t ret = 0; + + uint32_t buffer = value & 0x7F; + + while ( (value >>= 7) ) { + buffer <<= 8; + buffer |= ((value & 0x7F) | 0x80); + } + + while (true) { + //printf("Writing var len byte %X\n", (unsigned char)buffer); + ++ret; + fputc(buffer, _fd); + if (buffer & 0x80) + buffer >>= 8; + else + break; + } + + return ret; +} + +} // namespace Raul + diff --git a/src/engine/SMFWriter.hpp b/src/engine/SMFWriter.hpp new file mode 100644 index 0000000..4c52b12 --- /dev/null +++ b/src/engine/SMFWriter.hpp @@ -0,0 +1,71 @@ +/* + This file is part of Raul. + Copyright 2007-2012 David Robillard + + Raul is free software: you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation, either version 3 of the License, or any later version. + + Raul is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with Raul. If not, see . +*/ + +#ifndef RAUL_SMF_WRITER_HPP +#define RAUL_SMF_WRITER_HPP + +#include +#include + +#include "raul/MIDISink.hpp" +#include "raul/TimeStamp.hpp" + +namespace Raul { + +/** Standard Midi File (Type 0) Writer + * \ingroup raul + */ +class SMFWriter : public Raul::MIDISink { +public: + explicit SMFWriter(TimeUnit unit); + ~SMFWriter(); + + bool start(const std::string& filename, + TimeStamp start_time) throw (std::logic_error); + + TimeUnit unit() const { return _unit; } + + void write_event(TimeStamp time, + size_t ev_size, + const unsigned char* ev) throw (std::logic_error); + + void flush(); + + void finish() throw (std::logic_error); + +protected: + static const uint32_t VAR_LEN_MAX = 0x0FFFFFFF; + + void write_header(); + void write_footer(); + + void write_chunk_header(const char id[4], uint32_t length); + void write_chunk(const char id[4], uint32_t length, void* data); + size_t write_var_len(uint32_t val); + + std::string _filename; + FILE* _fd; + TimeUnit _unit; + Raul::TimeStamp _start_time; + Raul::TimeStamp _last_ev_time; ///< Time last event was written relative to _start_time + uint32_t _track_size; + uint32_t _header_size; ///< size of SMF header, including MTrk chunk header +}; + +} // namespace Raul + +#endif // RAUL_SMF_WRITER_HPP + diff --git a/src/engine/smf_test.cpp b/src/engine/smf_test.cpp new file mode 100644 index 0000000..680f66b --- /dev/null +++ b/src/engine/smf_test.cpp @@ -0,0 +1,80 @@ +/* + This file is part of Raul. + Copyright 2007-2012 David Robillard + + Raul is free software: you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation, either version 3 of the License, or any later version. + + Raul is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with Raul. If not, see . +*/ + +#include +#include +#include "raul/log.hpp" +#include "raul/SMFReader.hpp" +#include "raul/SMFWriter.hpp" + +using namespace std; +using namespace Raul; + +int +main(int argc, char** argv) +{ +#define CHECK(cond) \ + do { if (!(cond)) { \ + error << "Test at " << __FILE__ << ":" << __LINE__ << " failed: " << __STRING(cond) << endl; \ + return 1; \ + } } while (0) + + static const uint16_t ppqn = 19200; + + const char* filename = NULL; + + if (argc < 2) { + filename = "./test.mid"; + SMFWriter writer(TimeUnit(TimeUnit::BEATS, ppqn)); + writer.start(string(filename), TimeStamp(writer.unit(), 0, 0)); + writer.finish(); + } else { + filename = argv[1]; + } + + SMFReader reader; + bool opened = reader.open(filename); + + if (!opened) { + cerr << "Unable to open SMF file " << filename << endl; + return -1; + } + + CHECK(reader.type() == 0); + CHECK(reader.num_tracks() == 1); + CHECK(reader.ppqn() == ppqn); + + for (unsigned t=1; t <= reader.num_tracks(); ++t) { + reader.seek_to_track(t); + + unsigned char buf[4]; + uint32_t ev_size; + uint32_t ev_delta_time; + while (reader.read_event(4, buf, &ev_size, &ev_delta_time) >= 0) { + + cout << t << ": Event, size = " << ev_size << ", time = " << ev_delta_time; + cout << ":\t"; + cout.flags(ios::hex); + for (uint32_t i=0; i < ev_size; ++i) { + cout << "0x" << static_cast(buf[i]) << " "; + } + cout.flags(ios::dec); + cout << endl; + } + } + + return 0; +} -- cgit v1.2.1