diff options
author | Hans Baier <hansfbaier@googlemail.com> | 2009-02-11 09:54:31 +0000 |
---|---|---|
committer | Hans Baier <hansfbaier@googlemail.com> | 2009-02-11 09:54:31 +0000 |
commit | 947077c12e13a11d6a39b931bd21f288883568cd (patch) | |
tree | 410d8e2c096ed6bff01f9dbb11446eb3287dce69 /libs/evoral | |
parent | 567f37f8eb01aa3a4eb921873f64d1eec2924160 (diff) |
* introduced dependency: libsmf-1.2
* extracted Interface from SMF: StandardMIDIFile
* first implementation of StandardMIDIFile based on libsmf that passes basic test
git-svn-id: svn://localhost/ardour2/branches/3.0@4529 d708f5d6-7413-0410-9779-e7cbd77b26cf
Diffstat (limited to 'libs/evoral')
-rw-r--r-- | libs/evoral/evoral/LibSMF.hpp | 84 | ||||
-rw-r--r-- | libs/evoral/evoral/SMF.hpp | 5 | ||||
-rw-r--r-- | libs/evoral/evoral/StandardMIDIFile.hpp | 64 | ||||
-rw-r--r-- | libs/evoral/src/LibSMF.cpp | 160 | ||||
-rw-r--r-- | libs/evoral/test/SMFTest.cpp | 2 | ||||
-rw-r--r-- | libs/evoral/test/SMFTest.hpp | 8 | ||||
-rw-r--r-- | libs/evoral/wscript | 4 |
7 files changed, 318 insertions, 9 deletions
diff --git a/libs/evoral/evoral/LibSMF.hpp b/libs/evoral/evoral/LibSMF.hpp new file mode 100644 index 0000000000..1bc50c8259 --- /dev/null +++ b/libs/evoral/evoral/LibSMF.hpp @@ -0,0 +1,84 @@ +/* This file is part of Evoral. + * Copyright(C) 2008 Dave Robillard <http://drobilla.net> + * Copyright(C) 2000-2008 Paul Davis + * Author: Hans Baier + * + * Evoral 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. + * + * Evoral 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 EVORAL_LIB_SMF_HPP +#define EVORAL_LIB_SMF_HPP + +#include "evoral/StandardMIDIFile.hpp" + +#include <smf.h> +#include <cassert> + +namespace Evoral { + +template<typename Time> class Event; +template<typename Time> class EventRingBuffer; + +/** Standard Midi File (Type 0) + */ +template<typename Time> +class LibSMF : public StandardMIDIFile<Time> { +public: + LibSMF() : _last_ev_time(0), _smf(0), _smf_track(0), _empty(true) {}; + virtual ~LibSMF() { + if (_smf) { + smf_delete(_smf); + _smf = 0; + _smf_track = 0; + } + } + + void seek_to_start() const; + + uint16_t ppqn() const { return _ppqn; } + bool is_empty() const { return _empty; } + bool eof() const { assert(false); return true; } + + Time last_event_time() const { return _last_ev_time; } + + void begin_write(FrameTime start_time); + void append_event_unlocked(uint32_t delta_t, const Event<Time>& ev); + void end_write(); + + void flush() {}; + int flush_header() { return 0; } + int flush_footer() { return 0; } + +protected: + int open(const std::string& path); + void close(); + + int read_event(uint32_t* delta_t, uint32_t* size, uint8_t** buf) const; + +private: + static const uint16_t _ppqn = 19200; + + Time _last_ev_time; ///< last frame time written, relative to source start + + std::string _path; + smf_t* _smf; + smf_track_t* _smf_track; + + bool _empty; ///< true iff file contains(non-empty) events +}; + +}; /* namespace Evoral */ + +#endif /* EVORAL_LIB_SMF_HPP */ + diff --git a/libs/evoral/evoral/SMF.hpp b/libs/evoral/evoral/SMF.hpp index a7a2abf4bf..313035f5e3 100644 --- a/libs/evoral/evoral/SMF.hpp +++ b/libs/evoral/evoral/SMF.hpp @@ -19,8 +19,7 @@ #ifndef EVORAL_SMF_HPP #define EVORAL_SMF_HPP -#include <string> -#include "evoral/types.hpp" +#include "evoral/StandardMIDIFile.hpp" namespace Evoral { @@ -31,7 +30,7 @@ template<typename Time> class EventRingBuffer; /** Standard Midi File (Type 0) */ template<typename Time> -class SMF { +class SMF : public StandardMIDIFile<Time> { public: SMF(); virtual ~SMF(); diff --git a/libs/evoral/evoral/StandardMIDIFile.hpp b/libs/evoral/evoral/StandardMIDIFile.hpp new file mode 100644 index 0000000000..e907defe46 --- /dev/null +++ b/libs/evoral/evoral/StandardMIDIFile.hpp @@ -0,0 +1,64 @@ +/* This file is part of Evoral. + * Copyright(C) 2008 Dave Robillard <http://drobilla.net> + * Copyright(C) 2000-2008 Paul Davis + * Author: Hans Baier + * + * Evoral 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. + * + * Evoral 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 EVORAL_STANDARD_MIDI_FILE_HPP +#define EVORAL_STANDARD_MIDI_FILE_HPP + +#include <string> +#include "evoral/types.hpp" + +namespace Evoral { + +template<typename Time> class Event; +template<typename Time> class EventRingBuffer; + + +/** Standard MIDI File interface + */ +template<typename Time> +class StandardMIDIFile { +public: + virtual void seek_to_start() const = 0; + + virtual uint16_t ppqn() const = 0; + virtual bool is_empty() const = 0; + virtual bool eof() const = 0; + + virtual Time last_event_time() const = 0; + + virtual void begin_write(FrameTime start_time) = 0; + virtual void append_event_unlocked(uint32_t delta_t, const Event<Time>& ev) = 0; + virtual void end_write() = 0; + + virtual void flush() = 0; + virtual int flush_header() = 0; + virtual int flush_footer() = 0; + +protected: + virtual int open(const std::string& path) = 0; + virtual void close() = 0; + + virtual int read_event(uint32_t* delta_t, uint32_t* size, uint8_t** buf) const = 0; + +}; + +}; /* namespace Evoral */ + +#endif /* EVORAL_STANDARD_MIDI_FILE_HPP */ + diff --git a/libs/evoral/src/LibSMF.cpp b/libs/evoral/src/LibSMF.cpp new file mode 100644 index 0000000000..c0c59fa3f0 --- /dev/null +++ b/libs/evoral/src/LibSMF.cpp @@ -0,0 +1,160 @@ +#include "evoral/LibSMF.hpp" +#include "evoral/Event.hpp" +#include <cassert> + +#include <iostream> + +using namespace std; + +namespace Evoral { + + +/** Attempt to open the SMF file for reading and writing. + * + * Currently SMF is always read/write. + * + * \return 0 on success + * -1 if the file can not be opened + */ +template<typename Time> +int +LibSMF<Time>::open(const std::string& path) +{ + if (_smf) { + smf_delete(_smf); + } + + _smf = smf_load(path.c_str()); + if (!_smf) { + _smf = smf_new(); + smf_set_ppqn(_smf, _ppqn); + + if(_smf == NULL) { + return -1; + } + } + + _smf_track = smf_get_track_by_number(_smf, 1); + assert(_smf_track); + + cerr << "number of events: " << _smf_track->number_of_events << endl; + + _empty = !(_smf_track->number_of_events > 0); + + return 0; +} + +template<typename Time> +void +LibSMF<Time>::close() +{ + assert(false); + if (_smf) { + smf_save(_smf, _path.c_str()); + smf_delete(_smf); + _smf = 0; + _smf_track = 0; + } +} + +template<typename Time> +void +LibSMF<Time>::seek_to_start() const +{ + smf_rewind(_smf); +} + +/** 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, in SMF tempo-based ticks, using the + * rate given by ppqn() (it is the caller's responsibility to calculate a real time). + * + * \a size should be the capacity of \a buf. If it is not large enough, \a buf will + * be freed and a new buffer allocated in its place, the size of which will be placed + * in size. + * + * 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). + */ +template<typename Time> +int +LibSMF<Time>::read_event(uint32_t* delta_t, uint32_t* size, uint8_t** buf) const +{ + smf_event_t *event; + + assert(delta_t); + assert(size); + assert(buf); + + if ((event = smf_get_next_event(_smf)) != NULL) { + if (smf_event_is_metadata(event)) { + return 0; + } + *delta_t = event->delta_time_pulses; + + int event_size = event->midi_buffer_length; + assert(event_size > 0); + + // Make sure we have enough scratch buffer + if (*size < (unsigned)event_size) { + *buf = (uint8_t*)realloc(*buf, event_size); + } + memcpy(*buf, event->midi_buffer, size_t(event_size)); + *size = event_size; + + return event_size; + } else { + return -1; + } +} + +template<typename Time> +void +LibSMF<Time>::append_event_unlocked(uint32_t delta_t, const Event<Time>& ev) +{ + assert(ev.size() > 0); + + smf_event_t *event; + + event = smf_event_new_from_pointer((void *) ev.buffer(), int(ev.size())); + assert(event != NULL); + + memcpy(event->midi_buffer, ev.buffer(), ev.size()); + + assert(_smf_track); + smf_track_add_event_delta_pulses (_smf_track, event, int(delta_t)); + _last_ev_time = ev.time(); + + if (ev.size() > 0) { + _empty = false; + } +} + +template<typename Time> +void +LibSMF<Time>::begin_write(FrameTime start_frame) +{ + assert(_smf_track); + smf_track_delete(_smf_track); + + _smf_track = smf_track_new(); + assert(_smf_track); + + smf_add_track(_smf, _smf_track); + assert(_smf->number_of_tracks == 1); + + _last_ev_time = 0; +} + +template<typename Time> +void +LibSMF<Time>::end_write() +{ + smf_save(_smf, _path.c_str()); +} + +template class LibSMF<double>; + +} // namespace Evoral diff --git a/libs/evoral/test/SMFTest.cpp b/libs/evoral/test/SMFTest.cpp index b5891ff23f..49233101cb 100644 --- a/libs/evoral/test/SMFTest.cpp +++ b/libs/evoral/test/SMFTest.cpp @@ -24,11 +24,11 @@ SMFTest::takeFiveTest () uint8_t* buf = NULL; int ret; while ((ret = smf.read_event(&delta_t, &size, &buf)) >= 0) { - cerr << "read smf event type " << int(buf[0]) << endl; ev.set(buf, size, 0.0); time += delta_t; if (ret > 0) { // didn't skip (meta) event + cerr << "read smf event type " << hex << int(buf[0]) << endl; // make ev.time absolute time in frames ev.time() = time * frames_per_beat / (double)smf.ppqn(); ev.set_event_type(type_map->midi_event_type(buf[0])); diff --git a/libs/evoral/test/SMFTest.hpp b/libs/evoral/test/SMFTest.hpp index f63fec838a..99c3288f5a 100644 --- a/libs/evoral/test/SMFTest.hpp +++ b/libs/evoral/test/SMFTest.hpp @@ -1,7 +1,7 @@ #include <cppunit/TestFixture.h> #include <cppunit/extensions/HelperMacros.h> -#include <evoral/SMF.hpp> +#include <evoral/LibSMF.hpp> #include "SequenceTest.hpp" @@ -12,14 +12,14 @@ using namespace Evoral; template<typename Time> -class TestSMF : public SMF<Time> { +class TestSMF : public LibSMF<Time> { public: int open(const std::string& path) { - return SMF<Time>::open(path); + return LibSMF<Time>::open(path); } int read_event(uint32_t* delta_t, uint32_t* size, uint8_t** buf) const { - return SMF<Time>::read_event(delta_t, size, buf); + return LibSMF<Time>::read_event(delta_t, size, buf); } }; diff --git a/libs/evoral/wscript b/libs/evoral/wscript index 639c42b228..02a71b3f48 100644 --- a/libs/evoral/wscript +++ b/libs/evoral/wscript @@ -29,6 +29,7 @@ def configure(conf): autowaf.check_pkg(conf, 'glibmm-2.4', uselib_store='GLIBMM', atleast_version='2.14.0', mandatory=True) autowaf.check_pkg(conf, 'gthread-2.0', uselib_store='GTHREAD', atleast_version='2.14.0', mandatory=True) autowaf.check_pkg(conf, 'cppunit', uselib_store='CPPUNIT', atleast_version='1.12.0', mandatory=False) + autowaf.check_pkg(conf, 'smf', uselib_store='SMF', atleast_version='1.2', mandatory=False) def build(bld): # Headers @@ -50,13 +51,14 @@ def build(bld): src/Note.cpp src/SMF.cpp src/SMFReader.cpp + src/LibSMF.cpp src/Sequence.cpp ''' obj.export_incdirs = ['.'] obj.includes = ['.', './src'] obj.name = 'libevoral' obj.target = 'evoral' - obj.uselib = 'GLIBMM GTHREAD' + obj.uselib = 'GLIBMM GTHREAD SMF' obj.vnum = EVORAL_LIB_VERSION obj.install_path = '' |