diff options
author | David Robillard <d@drobilla.net> | 2007-03-09 22:28:33 +0000 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2007-03-09 22:28:33 +0000 |
commit | 8b6b314cce7a721e828ecb022f2b5400aa05c07c (patch) | |
tree | bef7ba6a7208d3006b790733dfc6f881c27eab8b | |
parent | b2aae23b3443a75cf163c3ee7a1c1477394fbc80 (diff) | |
download | raul-8b6b314cce7a721e828ecb022f2b5400aa05c07c.tar.gz raul-8b6b314cce7a721e828ecb022f2b5400aa05c07c.tar.bz2 raul-8b6b314cce7a721e828ecb022f2b5400aa05c07c.zip |
SMF reading/writing fixes.
git-svn-id: http://svn.drobilla.net/lad/raul@345 a436a847-0d15-0410-975c-d299462d15a1
-rw-r--r-- | raul/Makefile.am | 2 | ||||
-rw-r--r-- | raul/SMFReader.h | 67 | ||||
-rw-r--r-- | raul/SMFWriter.h | 2 | ||||
-rw-r--r-- | raul/midi_events.h | 136 | ||||
-rw-r--r-- | src/Makefile.am | 1 | ||||
-rw-r--r-- | src/SMFReader.cpp | 229 | ||||
-rw-r--r-- | src/SMFWriter.cpp | 46 | ||||
-rw-r--r-- | tests/Makefile.am | 4 | ||||
-rw-r--r-- | tests/smf_test.cpp | 41 |
9 files changed, 498 insertions, 30 deletions
diff --git a/raul/Makefile.am b/raul/Makefile.am index b1d5f12..94ec10b 100644 --- a/raul/Makefile.am +++ b/raul/Makefile.am @@ -1,6 +1,7 @@ raulincludedir = $(includedir)/raul raulinclude_HEADERS = \ + midi_events.h \ Array.h \ Atom.h \ AtomLiblo.h \ @@ -30,6 +31,7 @@ raulinclude_HEADERS = \ TimeSlice.h \ Quantizer.h \ MIDISink.h \ + SMFReader.h \ SMFWriter.h if WITH_LIBLO diff --git a/raul/SMFReader.h b/raul/SMFReader.h new file mode 100644 index 0000000..3878044 --- /dev/null +++ b/raul/SMFReader.h @@ -0,0 +1,67 @@ +/* This file is part of Raul. + * Copyright (C) 2007 Dave Robillard <http://drobilla.net> + * + * 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 2 of the License, or (at your option) 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 details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef RAUL_SMFREADER_H +#define RAUL_SMFREADER_H + +#include <stdexcept> + +namespace Raul { + + +/** Standard Midi File (Type 0) Reader + * + * Currently this only reads SMF files with tempo-based timing. + */ +class SMFReader { +public: + SMFReader(); + ~SMFReader(); + + bool open(const std::string& filename); + + uint16_t ppqn() const { return _ppqn; } + + size_t num_tracks() throw (std::logic_error); + + int read_event(size_t buf_len, char* buf, size_t* ev_size, uint64_t* ev_time); + + void close(); + +protected: + //static const uint32_t VAR_LEN_MAX = 0x0FFFFFFF; + + /** size of SMF header, including MTrk chunk header */ + static const uint32_t HEADER_SIZE = 22; + + uint32_t read_var_len() const; + + std::string _filename; + FILE* _fd; + uint16_t _ppqn; + uint64_t _last_ev_time; + uint32_t _track_size; +/* Raul::BeatTime _start_time; + Raul::BeatTime _last_ev_time; ///< Time last event was written relative to _start_time + */ +}; + + +} // namespace Raul + +#endif // RAUL_SMFREADER_H + diff --git a/raul/SMFWriter.h b/raul/SMFWriter.h index b512cff..875c151 100644 --- a/raul/SMFWriter.h +++ b/raul/SMFWriter.h @@ -34,6 +34,8 @@ public: bool start(const std::string& filename, BeatTime start_time=0) throw (std::logic_error); + uint16_t ppqn() const { return _ppqn; } + void write_event(BeatTime time, size_t ev_size, const unsigned char* ev) throw (std::logic_error); diff --git a/raul/midi_events.h b/raul/midi_events.h new file mode 100644 index 0000000..5c1183e --- /dev/null +++ b/raul/midi_events.h @@ -0,0 +1,136 @@ +/* Definitions to ease working with raw MIDI. + * + * Adapted from ALSA's asounddef.h + * + * This library is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This program 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 Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +#ifndef RAUL_MIDIEVENTS_H +#define RAUL_MIDIEVENTS_H + + +/** + * \defgroup midi MIDI Definitions + * MIDI command and controller number definitions. + * \{ + */ + +// Commands: + +#define MIDI_CMD_NOTE_OFF 0x80 /**< note off */ +#define MIDI_CMD_NOTE_ON 0x90 /**< note on */ +#define MIDI_CMD_NOTE_PRESSURE 0xA0 /**< key pressure */ +#define MIDI_CMD_CONTROL 0xB0 /**< control change */ +#define MIDI_CMD_PGM_CHANGE 0xC0 /**< program change */ +#define MIDI_CMD_CHANNEL_PRESSURE 0xD0 /**< channel pressure */ +#define MIDI_CMD_BENDER 0xE0 /**< pitch bender */ + +#define MIDI_CMD_COMMON_SYSEX 0xF0 /**< sysex (system exclusive) begin */ +#define MIDI_CMD_COMMON_MTC_QUARTER 0xF1 /**< MTC quarter frame */ +#define MIDI_CMD_COMMON_SONG_POS 0xF2 /**< song position */ +#define MIDI_CMD_COMMON_SONG_SELECT 0xF3 /**< song select */ +#define MIDI_CMD_COMMON_TUNE_REQUEST 0xF6 /**< tune request */ +#define MIDI_CMD_COMMON_SYSEX_END 0xF7 /**< end of sysex */ +#define MIDI_CMD_COMMON_CLOCK 0xF8 /**< clock */ +#define MIDI_CMD_COMMON_TICK 0xF9 /**< tick */ +#define MIDI_CMD_COMMON_START 0xFA /**< start */ +#define MIDI_CMD_COMMON_CONTINUE 0xFB /**< continue */ +#define MIDI_CMD_COMMON_STOP 0xFC /**< stop */ +#define MIDI_CMD_COMMON_SENSING 0xFE /**< active sensing */ +#define MIDI_CMD_COMMON_RESET 0xFF /**< reset */ + + +// Controllers: + +#define MIDI_CTL_MSB_BANK 0x00 /**< Bank selection */ +#define MIDI_CTL_MSB_MODWHEEL 0x01 /**< Modulation */ +#define MIDI_CTL_MSB_BREATH 0x02 /**< Breath */ +#define MIDI_CTL_MSB_FOOT 0x04 /**< Foot */ +#define MIDI_CTL_MSB_PORTAMENTO_TIME 0x05 /**< Portamento time */ +#define MIDI_CTL_MSB_DATA_ENTRY 0x06 /**< Data entry */ +#define MIDI_CTL_MSB_MAIN_VOLUME 0x07 /**< Main volume */ +#define MIDI_CTL_MSB_BALANCE 0x08 /**< Balance */ +#define MIDI_CTL_MSB_PAN 0x0A /**< Panpot */ +#define MIDI_CTL_MSB_EXPRESSION 0x0B /**< Expression */ +#define MIDI_CTL_MSB_EFFECT1 0x0C /**< Effect1 */ +#define MIDI_CTL_MSB_EFFECT2 0x0D /**< Effect2 */ +#define MIDI_CTL_MSB_GENERAL_PURPOSE1 0x10 /**< General purpose 1 */ +#define MIDI_CTL_MSB_GENERAL_PURPOSE2 0x11 /**< General purpose 2 */ +#define MIDI_CTL_MSB_GENERAL_PURPOSE3 0x12 /**< General purpose 3 */ +#define MIDI_CTL_MSB_GENERAL_PURPOSE4 0x13 /**< General purpose 4 */ +#define MIDI_CTL_LSB_BANK 0x20 /**< Bank selection */ +#define MIDI_CTL_LSB_MODWHEEL 0x21 /**< Modulation */ +#define MIDI_CTL_LSB_BREATH 0x22 /**< Breath */ +#define MIDI_CTL_LSB_FOOT 0x24 /**< Foot */ +#define MIDI_CTL_LSB_PORTAMENTO_TIME 0x25 /**< Portamento time */ +#define MIDI_CTL_LSB_DATA_ENTRY 0x26 /**< Data entry */ +#define MIDI_CTL_LSB_MAIN_VOLUME 0x27 /**< Main volume */ +#define MIDI_CTL_LSB_BALANCE 0x28 /**< Balance */ +#define MIDI_CTL_LSB_PAN 0x2A /**< Panpot */ +#define MIDI_CTL_LSB_EXPRESSION 0x2B /**< Expression */ +#define MIDI_CTL_LSB_EFFECT1 0x2C /**< Effect1 */ +#define MIDI_CTL_LSB_EFFECT2 0x2D /**< Effect2 */ +#define MIDI_CTL_LSB_GENERAL_PURPOSE1 0x30 /**< General purpose 1 */ +#define MIDI_CTL_LSB_GENERAL_PURPOSE2 0x31 /**< General purpose 2 */ +#define MIDI_CTL_LSB_GENERAL_PURPOSE3 0x32 /**< General purpose 3 */ +#define MIDI_CTL_LSB_GENERAL_PURPOSE4 0x33 /**< General purpose 4 */ +#define MIDI_CTL_SUSTAIN 0x40 /**< Sustain pedal */ +#define MIDI_CTL_PORTAMENTO 0x41 /**< Portamento */ +#define MIDI_CTL_SOSTENUTO 0x42 /**< Sostenuto */ +#define MIDI_CTL_SUSTENUTO 0x42 /**< Sostenuto (a typo in the older version) */ +#define MIDI_CTL_SOFT_PEDAL 0x43 /**< Soft pedal */ +#define MIDI_CTL_LEGATO_FOOTSWITCH 0x44 /**< Legato foot switch */ +#define MIDI_CTL_HOLD2 0x45 /**< Hold2 */ +#define MIDI_CTL_SC1_SOUND_VARIATION 0x46 /**< SC1 Sound Variation */ +#define MIDI_CTL_SC2_TIMBRE 0x47 /**< SC2 Timbre */ +#define MIDI_CTL_SC3_RELEASE_TIME 0x48 /**< SC3 Release Time */ +#define MIDI_CTL_SC4_ATTACK_TIME 0x49 /**< SC4 Attack Time */ +#define MIDI_CTL_SC5_BRIGHTNESS 0x4A /**< SC5 Brightness */ +#define MIDI_CTL_SC6 0x4B /**< SC6 */ +#define MIDI_CTL_SC7 0x4C /**< SC7 */ +#define MIDI_CTL_SC8 0x4D /**< SC8 */ +#define MIDI_CTL_SC9 0x4E /**< SC9 */ +#define MIDI_CTL_SC10 0x4F /**< SC10 */ +#define MIDI_CTL_GENERAL_PURPOSE5 0x50 /**< General purpose 5 */ +#define MIDI_CTL_GENERAL_PURPOSE6 0x51 /**< General purpose 6 */ +#define MIDI_CTL_GENERAL_PURPOSE7 0x52 /**< General purpose 7 */ +#define MIDI_CTL_GENERAL_PURPOSE8 0x53 /**< General purpose 8 */ +#define MIDI_CTL_PORTAMENTO_CONTROL 0x54 /**< Portamento control */ +#define MIDI_CTL_E1_REVERB_DEPTH 0x5B /**< E1 Reverb Depth */ +#define MIDI_CTL_E2_TREMOLO_DEPTH 0x5C /**< E2 Tremolo Depth */ +#define MIDI_CTL_E3_CHORUS_DEPTH 0x5D /**< E3 Chorus Depth */ +#define MIDI_CTL_E4_DETUNE_DEPTH 0x5E /**< E4 Detune Depth */ +#define MIDI_CTL_E5_PHASER_DEPTH 0x5F /**< E5 Phaser Depth */ +#define MIDI_CTL_DATA_INCREMENT 0x60 /**< Data Increment */ +#define MIDI_CTL_DATA_DECREMENT 0x61 /**< Data Decrement */ +#define MIDI_CTL_NONREG_PARM_NUM_LSB 0x62 /**< Non-registered parameter number */ +#define MIDI_CTL_NONREG_PARM_NUM_MSB 0x63 /**< Non-registered parameter number */ +#define MIDI_CTL_REGIST_PARM_NUM_LSB 0x64 /**< Registered parameter number */ +#define MIDI_CTL_REGIST_PARM_NUM_MSB 0x65 /**< Registered parameter number */ +#define MIDI_CTL_ALL_SOUNDS_OFF 0x78 /**< All sounds off */ +#define MIDI_CTL_RESET_CONTROLLERS 0x79 /**< Reset Controllers */ +#define MIDI_CTL_LOCAL_CONTROL_SWITCH 0x7A /**< Local control switch */ +#define MIDI_CTL_ALL_NOTES_OFF 0x7B /**< All notes off */ +#define MIDI_CTL_OMNI_OFF 0x7C /**< Omni off */ +#define MIDI_CTL_OMNI_ON 0x7D /**< Omni on */ +#define MIDI_CTL_MONO1 0x7E /**< Mono1 */ +#define MIDI_CTL_MONO2 0x7F /**< Mono2 */ +//@} + + +/** \} */ + +#endif /* RAUL_MIDIEVENTS_H */ diff --git a/src/Makefile.am b/src/Makefile.am index ada7848..646eacc 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -8,6 +8,7 @@ libraul_la_SOURCES = \ Path.cpp \ Namespaces.cpp \ Maid.cpp \ + SMFReader.cpp \ SMFWriter.cpp if WITH_RAPTOR diff --git a/src/SMFReader.cpp b/src/SMFReader.cpp new file mode 100644 index 0000000..ce4825c --- /dev/null +++ b/src/SMFReader.cpp @@ -0,0 +1,229 @@ +/* This file is part of Raul. + * Copyright (C) 2007 Dave Robillard <http://drobilla.net> + * + * 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 2 of the License, or (at your option) 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 details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include <cstdio> +#include <cassert> +#include <iostream> +#include <glibmm/miscutils.h> +#include "raul/SMFReader.h" +#include "raul/midi_events.h" + +using namespace std; + +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() + : _fd(NULL) + , _ppqn(0) + , _last_ev_time(0) + , _track_size(0) +{ +} + + +SMFReader::~SMFReader() +{ + if (_fd) + close(); +} + + +bool +SMFReader::open(const string& filename) +{ + if (_fd) + throw logic_error("Attempt to start new read while write in progress."); + + cerr << "Opening SMF file " << filename << " for reading." << endl; + + _fd = fopen(filename.c_str(), "r+"); + + if (_fd) { + // Read PPQN + // FIXME: broken for time-based divisions + fseek(_fd, 12, SEEK_SET); + uint16_t ppqn_be = 0; + fread(&ppqn_be, 2, 1, _fd); + _ppqn = GUINT16_FROM_BE(ppqn_be); + + // Read Track size + fseek(_fd, 18, SEEK_SET); + uint32_t track_size_be = 0; + fread(&track_size_be, 4, 1, _fd); + _track_size = GUINT32_FROM_BE(track_size_be); + std::cerr << "SMF - read track size " << _track_size << std::endl; + + _last_ev_time = 0; + return true; + } else { + return false; + } +} + + +size_t +SMFReader::num_tracks() throw (logic_error) +{ + if (!_fd) + throw logic_error("num_tracks called without an open SMF file."); + + return 1; +} + + +/** 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 calculate a real time for the event). + * + * 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, char* buf, size_t* ev_size, uint64_t* ev_time) +{ + // - 4 is for the EOT event, which we don't actually want to read + //if (feof(_fd) || ftell(_fd) >= HEADER_SIZE + _track_size - 4) { + if (feof(_fd)) { + return -1; + } + + assert(buf_len > 0); + assert(buf); + assert(ev_size); + assert(ev_time); + + uint32_t delta_time = read_var_len(); + int status = fgetc(_fd); + assert(status != EOF); // FIXME die gracefully + if (status == 0xFF) { + assert(!feof(_fd)); + int type = fgetc(_fd); + if ((unsigned char)type == 0x2F) { + //cerr << "SMF - hit EOT" << endl; + return -1; // we hit the logical EOF anyway... + } else { + ev_size = 0; + *ev_time = _last_ev_time + delta_time; // this is needed regardless + _last_ev_time = *ev_time; + return 0; + } + } + + buf[0] = (unsigned char)status; + *ev_size = midi_event_size(buf[0]) + 1; + if (*ev_size > buf_len) + return 0; + fread(buf+1, 1, *ev_size - 1, _fd); + *ev_time = _last_ev_time + delta_time; + _last_ev_time = *ev_time; + + /*printf("SMF - read event, delta = %u, size = %zu, data = ", + delta_time, ev.size); + for (size_t i=0; i < ev.size; ++i) { + printf("%X ", ev.buffer[i]); + } + printf("\n");*/ + + return *ev_size; +} + + +void +SMFReader::close() +{ + if (_fd) + fclose(_fd); + + _fd = NULL; +} + + +uint32_t +SMFReader::read_var_len() const +{ + assert(!feof(_fd)); + + //int offset = ftell(_fd); + //cerr << "SMF - reading var len at " << offset << endl; + + uint32_t value; + unsigned char c; + + if ( (value = getc(_fd)) & 0x80 ) { + value &= 0x7F; + do { + assert(!feof(_fd)); + value = (value << 7) + ((c = getc(_fd)) & 0x7F); + } while (c & 0x80); + } + + return value; +} + + +} // namespace Raul + diff --git a/src/SMFWriter.cpp b/src/SMFWriter.cpp index 7db6c5f..0af7688 100644 --- a/src/SMFWriter.cpp +++ b/src/SMFWriter.cpp @@ -49,37 +49,23 @@ SMFWriter::~SMFWriter() * to write_event will have this value subtracted before writing). */ bool -SMFWriter::start(const string& filename, - Raul::BeatTime start_time) throw (logic_error) +SMFWriter::start(const string& filename, + Raul::BeatTime start_time) throw (logic_error) { if (_fd) throw logic_error("Attempt to start new write while write in progress."); - cerr << "Opening SMF file " << filename << endl; - - _fd = fopen(filename.c_str(), "r+"); - - // File already exists - if (_fd) { - return false; - /*fseek(_fd, _header_size - 4, 0); - uint32_t track_size_be = 0; - fread(&track_size_be, 4, 1, _fd); - _track_size = GUINT32_FROM_BE(track_size_be); - cerr << "SMF - read track size " << _track_size;*/ - - // Make a new file - } else { - _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(); - } + cerr << "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; @@ -192,7 +178,7 @@ SMFWriter::finish() throw (logic_error) void -SMFWriter::write_header () +SMFWriter::write_header() { cerr << "SMF Flushing header\n"; @@ -200,12 +186,14 @@ SMFWriter::write_header () 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(1920); // FIXME FIXME FIXME PPQN + const uint16_t division = GUINT16_TO_BE(_ppqn); // Number of tracks (always 1 for Type 0) 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); diff --git a/tests/Makefile.am b/tests/Makefile.am index 9e8e6c8..cbc3582 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -3,7 +3,7 @@ if BUILD_TESTS AM_CXXFLAGS = -I.. -lpthread @RASQAL_CFLAGS@ @GLIBMM_CFLAGS@ ALL_LIBS = @RASQAL_LIBS@ @GLIBMM_LIBS@ ../src/libraul.la -bin_PROGRAMS = path_test thread_test queue_test atomic_test list_test time_test quantize_test +bin_PROGRAMS = path_test thread_test queue_test atomic_test list_test time_test quantize_test smf_test thread_test_LDADD = $(ALL_LIBS) path_test_LDADD = $(ALL_LIBS) @@ -12,6 +12,7 @@ atomic_test_LDADD = $(ALL_LIBS) list_test_LDADD = $(ALL_LIBS) time_test_LDADD = $(ALL_LIBS) quantize_test_LDADD = $(ALL_LIBS) +smf_test_LDADD = $(ALL_LIBS) path_test_SOURCES = path_test.cpp thread_test_SOURCES = thread_test.cpp @@ -20,5 +21,6 @@ atomic_test_SOURCES = atomic_test.cpp list_test_SOURCES = list_test.cpp time_test_SOURCES = time_test.cpp quantize_test_SOURCES = quantize_test.cpp +smf_test_SOURCES = smf_test.cpp endif diff --git a/tests/smf_test.cpp b/tests/smf_test.cpp new file mode 100644 index 0000000..84f61cc --- /dev/null +++ b/tests/smf_test.cpp @@ -0,0 +1,41 @@ +#include <iostream> +#include <string> +#include <raul/SMFReader.h> +#include <raul/SMFWriter.h> + +using namespace std; +using namespace Raul; + + +int +main(int argc, char** argv) +{ + char* filename = NULL; + + if (argc < 2) { + filename = "./test.mid"; + SMFWriter writer(32768); + writer.start(string(filename)); + writer.finish(); + cout << "Wrote " << filename << " with PPQN = " << writer.ppqn() << endl; + + } else { + filename = argv[1]; + } + + + SMFReader reader; + bool opened = reader.open(filename); + + if (!opened) { + cerr << "Unable to open SMF file " << filename << endl; + return -1; + } + + cout << "Opened SMF file " << filename << endl; + + cout << "Num tracks: " << reader.num_tracks() << endl; + cout << "PPQN: " << reader.ppqn() << endl; + + return 0; +} |