Make SMFSource suck significantly less.

Read from MidiRingbuffer directly into model, don't read MidiRingBuffer into a new midi buffer, then into the model.
Pass rec data to UI via model instead of a separate buffer.
Read MIDI CC data into MidiModel (though not actually used yet).
Made quantization toggle edited flag so model is saved.


git-svn-id: svn://localhost/ardour2/trunk@2308 d708f5d6-7413-0410-9779-e7cbd77b26cf
This commit is contained in:
David Robillard 2007-08-16 01:25:58 +00:00
parent 356f9ba80a
commit f9a7388d7a
15 changed files with 295 additions and 166 deletions

View file

@ -394,11 +394,14 @@ MidiRegionView::display_model(boost::shared_ptr<MidiModel> model)
void void
MidiRegionView::redisplay_model() MidiRegionView::redisplay_model()
{ {
clear_events(); // Don't redisplay the model if we're currently recording and displaying that
if (_active_notes)
//cerr << "Redisplaying model " << _model << endl; return;
if (_model) { if (_model) {
clear_events();
begin_write(); begin_write();
for (size_t i=0; i < _model->n_notes(); ++i) for (size_t i=0; i < _model->n_notes(); ++i)
@ -406,7 +409,7 @@ MidiRegionView::redisplay_model()
end_write(); end_write();
} else { } else {
warning << "MidiRegionView::redisplay_model called without a model" << endmsg; cerr << "MidiRegionView::redisplay_model called without a model" << endmsg;
} }
} }
@ -414,7 +417,10 @@ MidiRegionView::redisplay_model()
MidiRegionView::~MidiRegionView () MidiRegionView::~MidiRegionView ()
{ {
in_destructor = true; in_destructor = true;
end_write(); if (_active_notes)
end_write();
clear_events();
RegionViewGoingAway (this); /* EMIT_SIGNAL */ RegionViewGoingAway (this); /* EMIT_SIGNAL */
} }
@ -488,6 +494,7 @@ MidiRegionView::add_ghost (AutomationTimeAxisView& atv)
void void
MidiRegionView::begin_write() MidiRegionView::begin_write()
{ {
assert(!_active_notes);
_active_notes = new CanvasNote*[128]; _active_notes = new CanvasNote*[128];
for (unsigned i=0; i < 128; ++i) for (unsigned i=0; i < 128; ++i)
_active_notes[i] = NULL; _active_notes[i] = NULL;
@ -512,9 +519,9 @@ MidiRegionView::end_write()
void void
MidiRegionView::add_event (const MidiEvent& ev) MidiRegionView::add_event (const MidiEvent& ev)
{ {
/*printf("Event, time = %f, size = %zu, data = ", ev.time, ev.size); /*printf("MRV add Event, time = %f, size = %u, data = ", ev.time(), ev.size());
for (size_t i=0; i < ev.size; ++i) { for (size_t i=0; i < ev.size(); ++i) {
printf("%X ", ev.buffer[i]); printf("%X ", ev.buffer()[i]);
} }
printf("\n\n");*/ printf("\n\n");*/

View file

@ -410,7 +410,7 @@ MidiStreamView::setup_rec_box ()
} }
void void
MidiStreamView::update_rec_regions (boost::shared_ptr<MidiBuffer> data, nframes_t start, nframes_t dur) MidiStreamView::update_rec_regions (boost::shared_ptr<MidiModel> data, nframes_t start, nframes_t dur)
{ {
ENSURE_GUI_THREAD (bind (mem_fun (*this, &MidiStreamView::update_rec_regions), data, start, dur)); ENSURE_GUI_THREAD (bind (mem_fun (*this, &MidiStreamView::update_rec_regions), data, start, dur));
@ -467,11 +467,24 @@ MidiStreamView::update_rec_regions (boost::shared_ptr<MidiBuffer> data, nframes_
/* draw events */ /* draw events */
MidiRegionView* mrv = (MidiRegionView*)iter->second; MidiRegionView* mrv = (MidiRegionView*)iter->second;
for (MidiBuffer::iterator i = data->begin(); i != data->end(); ++i) { // FIXME: slow
const MidiEvent& ev = *i; for (size_t i=0; i < data->n_notes(); ++i) {
mrv->add_event(ev); const MidiModel::Note& note = data->note_at(i);
mrv->extend_active_notes(); if (note.time() > start + dur)
break;
if (note.time() >= start)
if (data->note_mode() == Percussive || note.duration() > 0)
mrv->add_note(note);
else
mrv->add_event(note.on_event());
if (note.duration() > 0 && note.end_time() >= start)
mrv->add_event(note.off_event());
} }
mrv->extend_active_notes();
} }
} }
@ -508,18 +521,18 @@ MidiStreamView::update_rec_regions (boost::shared_ptr<MidiBuffer> data, nframes_
} }
void void
MidiStreamView::rec_data_range_ready (boost::shared_ptr<MidiBuffer> data, jack_nframes_t start, jack_nframes_t dur, boost::weak_ptr<Source> weak_src) MidiStreamView::rec_data_range_ready (jack_nframes_t start, jack_nframes_t dur, boost::weak_ptr<Source> weak_src)
{ {
// this is called from the butler thread for now // this is called from the butler thread for now
ENSURE_GUI_THREAD(bind (mem_fun (*this, &MidiStreamView::rec_data_range_ready), data, start, dur, weak_src)); ENSURE_GUI_THREAD(bind (mem_fun (*this, &MidiStreamView::rec_data_range_ready), start, dur, weak_src));
boost::shared_ptr<SMFSource> src (boost::dynamic_pointer_cast<SMFSource>(weak_src.lock())); boost::shared_ptr<SMFSource> src (boost::dynamic_pointer_cast<SMFSource>(weak_src.lock()));
//cerr << src.get() << " MIDI READY: " << start << " * " << dur //cerr << src.get() << " MIDI READY: " << start << " * " << dur
// << " -- " << data->size() << " events!" << endl; // << " -- " << data->size() << " events!" << endl;
this->update_rec_regions (data, start, dur); this->update_rec_regions (src->model(), start, dur);
} }
void void

View file

@ -40,6 +40,7 @@ namespace ARDOUR {
class PeakData; class PeakData;
class MidiRegion; class MidiRegion;
class Source; class Source;
class MidiModel;
} }
class PublicEditor; class PublicEditor;
@ -95,8 +96,8 @@ class MidiStreamView : public StreamView
private: private:
void setup_rec_box (); void setup_rec_box ();
void rec_data_range_ready (boost::shared_ptr<ARDOUR::MidiBuffer> data, jack_nframes_t start, jack_nframes_t dur, boost::weak_ptr<ARDOUR::Source> src); void rec_data_range_ready (jack_nframes_t start, jack_nframes_t dur, boost::weak_ptr<ARDOUR::Source> src);
void update_rec_regions (boost::shared_ptr<ARDOUR::MidiBuffer> data, jack_nframes_t start, jack_nframes_t dur); void update_rec_regions (boost::shared_ptr<ARDOUR::MidiModel> data, jack_nframes_t start, jack_nframes_t dur);
RegionView* add_region_view_internal (boost::shared_ptr<ARDOUR::Region>, bool wait_for_waves); RegionView* add_region_view_internal (boost::shared_ptr<ARDOUR::Region>, bool wait_for_waves);
void display_region(MidiRegionView* region_view, bool load_model); void display_region(MidiRegionView* region_view, bool load_model);

View file

@ -1,5 +1,5 @@
/* /*
Copyright (C) 2000, 2007 Paul Davis Copyright (C) 2000-2007 Paul Davis
This program is free software; you can redistribute it and/or modify This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by it under the terms of the GNU General Public License as published by
@ -94,7 +94,7 @@ protected:
mutable Glib::Mutex _automation_lock; mutable Glib::Mutex _automation_lock;
Controls _controls; Controls _controls;
std::set<Parameter> _visible_controls; std::set<Parameter> _visible_controls;
std::set<Parameter> _can_automate_list; std::set<Parameter> _can_automate_list;

View file

@ -112,11 +112,14 @@ struct MidiEvent {
inline uint8_t channel() const { return (_buffer[0] & 0x0F); } inline uint8_t channel() const { return (_buffer[0] & 0x0F); }
inline bool is_note_on() const { return (type() == MIDI_CMD_NOTE_ON); } inline bool is_note_on() const { return (type() == MIDI_CMD_NOTE_ON); }
inline bool is_note_off() const { return (type() == MIDI_CMD_NOTE_OFF); } inline bool is_note_off() const { return (type() == MIDI_CMD_NOTE_OFF); }
inline bool is_cc() const { return (type() == MIDI_CMD_CONTROL); }
inline bool is_note() const { return (is_note_on() || is_note_off()); } inline bool is_note() const { return (is_note_on() || is_note_off()); }
inline uint8_t note() const { return (_buffer[1]); } inline uint8_t note() const { return (_buffer[1]); }
inline uint8_t velocity() const { return (_buffer[2]); } inline uint8_t velocity() const { return (_buffer[2]); }
inline uint8_t cc_number() const { return (_buffer[1]); }
inline uint8_t cc_value() const { return (_buffer[2]); }
inline const Byte* buffer() const { return _buffer; } inline const Byte* buffer() const { return _buffer; }
inline Byte* buffer() { return _buffer; } inline Byte*& buffer() { return _buffer; }
private: private:
double _time; /**< Sample index (or beat time) at which event is valid */ double _time; /**< Sample index (or beat time) at which event is valid */

View file

@ -28,6 +28,7 @@
#include <ardour/types.h> #include <ardour/types.h>
#include <ardour/midi_buffer.h> #include <ardour/midi_buffer.h>
#include <ardour/midi_ring_buffer.h> #include <ardour/midi_ring_buffer.h>
#include <ardour/automatable.h>
namespace ARDOUR { namespace ARDOUR {
@ -41,7 +42,7 @@ class MidiSource;
* note on and off events (controller data is not here since it's represented * note on and off events (controller data is not here since it's represented
* as an AutomationList) * as an AutomationList)
*/ */
class MidiModel : public boost::noncopyable { class MidiModel : public boost::noncopyable, public Automatable {
public: public:
struct Note { struct Note {
Note(double time=0, double dur=0, uint8_t note=0, uint8_t vel=0x40); Note(double time=0, double dur=0, uint8_t note=0, uint8_t vel=0x40);
@ -92,11 +93,13 @@ public:
void append(const MidiBuffer& data); void append(const MidiBuffer& data);
/** Resizes vector if necessary (NOT realtime safe) */ /** Resizes vector if necessary (NOT realtime safe) */
void append(double time, size_t size, const Byte* in_buffer); //void append(double time, size_t size, const Byte* in_buffer);
void append(const MidiEvent& ev);
inline const Note& note_at(unsigned i) const { return _notes[i]; } inline const Note& note_at(unsigned i) const { return _notes[i]; }
inline size_t n_notes() const { return _notes.size(); } inline size_t n_notes() const { return _notes.size(); }
inline bool empty() const { return _notes.size() == 0 && _controls.size() == 0; }
typedef std::vector<Note> Notes; typedef std::vector<Note> Notes;
@ -146,7 +149,12 @@ public:
void apply_command(Command* cmd); void apply_command(Command* cmd);
bool edited() const { return _edited; } bool edited() const { return _edited; }
void set_edited(bool yn) { _edited = yn; }
bool write_to(boost::shared_ptr<MidiSource> source); bool write_to(boost::shared_ptr<MidiSource> source);
// MidiModel doesn't use the normal AutomationList serialisation code, as CC data is in the .mid
XMLNode& get_state();
int set_state(const XMLNode&) { return 0; }
sigc::signal<void> ContentsChanged; sigc::signal<void> ContentsChanged;
@ -155,12 +163,13 @@ private:
void add_note_unlocked(const Note& note); void add_note_unlocked(const Note& note);
void remove_note_unlocked(const Note& note); void remove_note_unlocked(const Note& note);
#ifndef NDEBUG
bool is_sorted() const; bool is_sorted() const;
#endif
void append_note_on(double time, uint8_t note, uint8_t velocity); void append_note_on(double time, uint8_t note, uint8_t velocity);
void append_note_off(double time, uint8_t note); void append_note_off(double time, uint8_t note);
void append_cc(double time, uint8_t number, uint8_t value);
Session& _session;
Glib::RWLock _lock; Glib::RWLock _lock;

View file

@ -233,6 +233,9 @@ public:
size_t write(double time, size_t size, const Byte* buf); size_t write(double time, size_t size, const Byte* buf);
bool read(double* time, size_t* size, Byte* buf); bool read(double* time, size_t* size, Byte* buf);
bool read_prefix(double* time, size_t* size);
bool read_contents(size_t size, Byte* buf);
size_t read(MidiBuffer& dst, nframes_t start, nframes_t end, nframes_t offset=0); size_t read(MidiBuffer& dst, nframes_t start, nframes_t end, nframes_t offset=0);
}; };
@ -250,6 +253,30 @@ MidiRingBuffer::read(double* time, size_t* size, Byte* buf)
} }
/** Read the time and size of an event. This call MUST be immediately proceeded
* by a call to read_contents (or the read pointer will be garabage).
*/
inline bool
MidiRingBuffer::read_prefix(double* time, size_t* size)
{
bool success = MidiRingBufferBase<Byte>::full_read(sizeof(double), (Byte*)time);
if (success)
success = MidiRingBufferBase<Byte>::full_read(sizeof(size_t), (Byte*)size);
return success;
}
/** Read the contenst of an event. This call MUST be immediately preceeded
* by a call to read_prefix (or the returned even will be garabage).
*/
inline bool
MidiRingBuffer::read_contents(size_t size, Byte* buf)
{
return MidiRingBufferBase<Byte>::full_read(size, buf);
}
inline size_t inline size_t
MidiRingBuffer::write(double time, size_t size, const Byte* buf) MidiRingBuffer::write(double time, size_t size, const Byte* buf)
{ {

View file

@ -53,8 +53,6 @@ class MidiSource : public Source
virtual nframes_t write (MidiRingBuffer& src, nframes_t cnt); virtual nframes_t write (MidiRingBuffer& src, nframes_t cnt);
virtual void append_event_unlocked(const MidiEvent& ev) = 0; virtual void append_event_unlocked(const MidiEvent& ev) = 0;
virtual void flush() {}
virtual void mark_for_remove() = 0; virtual void mark_for_remove() = 0;
virtual void mark_streaming_midi_write_started (NoteMode mode); virtual void mark_streaming_midi_write_started (NoteMode mode);
virtual void mark_streaming_write_started (); virtual void mark_streaming_write_started ();
@ -72,8 +70,8 @@ class MidiSource : public Source
static sigc::signal<void,MidiSource*> MidiSourceCreated; static sigc::signal<void,MidiSource*> MidiSourceCreated;
// The MIDI equivalent to "peaks" (but complete data) // Signal a range of recorded data is available for reading from model()
mutable sigc::signal<void,boost::shared_ptr<MidiBuffer>,nframes_t,nframes_t> ViewDataRangeReady; mutable sigc::signal<void,nframes_t,nframes_t> ViewDataRangeReady;
XMLNode& get_state (); XMLNode& get_state ();
int set_state (const XMLNode&); int set_state (const XMLNode&);
@ -82,12 +80,14 @@ class MidiSource : public Source
virtual void destroy_model() = 0; virtual void destroy_model() = 0;
void set_note_mode(NoteMode mode) { if (_model) _model->set_note_mode(mode); } void set_note_mode(NoteMode mode) { if (_model) _model->set_note_mode(mode); }
virtual bool model_loaded() const { return _model_loaded; }
boost::shared_ptr<MidiModel> model() { return _model; } boost::shared_ptr<MidiModel> model() { return _model; }
void set_model(boost::shared_ptr<MidiModel> m) { _model = m; _model_loaded = true; } void set_model(boost::shared_ptr<MidiModel> m) { _model = m; }
protected: protected:
virtual int flush_header() = 0;
virtual int flush_footer() = 0;
virtual nframes_t read_unlocked (MidiRingBuffer& dst, nframes_t start, nframes_t cnt, nframes_t stamp_offset) const = 0; virtual nframes_t read_unlocked (MidiRingBuffer& dst, nframes_t start, nframes_t cnt, nframes_t stamp_offset) const = 0;
virtual nframes_t write_unlocked (MidiRingBuffer& dst, nframes_t cnt) = 0; virtual nframes_t write_unlocked (MidiRingBuffer& dst, nframes_t cnt) = 0;
@ -98,7 +98,6 @@ class MidiSource : public Source
mutable uint32_t _write_data_count; ///< modified in write() mutable uint32_t _write_data_count; ///< modified in write()
boost::shared_ptr<MidiModel> _model; boost::shared_ptr<MidiModel> _model;
bool _model_loaded;
bool _writing; bool _writing;
private: private:

View file

@ -20,6 +20,7 @@
#ifndef __ardour_session_object_h__ #ifndef __ardour_session_object_h__
#define __ardour_session_object_h__ #define __ardour_session_object_h__
#include <string>
#include <pbd/statefuldestructible.h> #include <pbd/statefuldestructible.h>
namespace ARDOUR { namespace ARDOUR {
@ -36,22 +37,27 @@ class Session;
class SessionObject : public PBD::StatefulDestructible class SessionObject : public PBD::StatefulDestructible
{ {
public: public:
SessionObject(Session& session, const string& name) SessionObject(Session& session, const std::string& name)
: _session(session) : _session(session)
, _name(name) , _name(name)
{} {}
Session& session() const { return _session; } Session& session() const { return _session; }
const string& name() const { return _name; } const std::string& name() const { return _name; }
virtual bool set_name (const string& str) virtual bool set_name (const std::string& str) {
{ if (_name != str) { _name = str; NameChanged(); } return true; } if (_name != str) {
_name = str;
NameChanged();
}
return true;
}
sigc::signal<void> NameChanged; sigc::signal<void> NameChanged;
protected: protected:
Session& _session; Session& _session;
string _name; std::string _name;
}; };
} // namespace ARDOUR } // namespace ARDOUR

View file

@ -74,8 +74,6 @@ class SMFSource : public MidiSource {
int flush_header (); int flush_header ();
int flush_footer (); int flush_footer ();
void flush() { flush_header(); flush_footer(); }
int move_to_trash (const string trash_dir_name); int move_to_trash (const string trash_dir_name);
bool is_empty () const; bool is_empty () const;
@ -108,13 +106,15 @@ class SMFSource : public MidiSource {
bool removable() const; bool removable() const;
bool writable() const { return _flags & Writable; } bool writable() const { return _flags & Writable; }
int open(); int open();
void seek_to_end();
void write_footer();
void write_chunk_header(char id[4], uint32_t length); void write_chunk_header(char id[4], uint32_t length);
void write_chunk(char id[4], uint32_t length, void* data); void write_chunk(char id[4], uint32_t length, void* data);
size_t write_var_len(uint32_t val); size_t write_var_len(uint32_t val);
uint32_t read_var_len() const; uint32_t read_var_len() const;
int read_event(jack_midi_event_t& ev) const; int read_event(uint32_t* delta_t, uint32_t* size, Byte** buf) const;
static const uint16_t _ppqn = 19200; static const uint16_t _ppqn = 19200;

View file

@ -99,7 +99,7 @@ MidiModel::Note::operator=(const Note& copy)
// MidiModel // MidiModel
MidiModel::MidiModel(Session& s, size_t size) MidiModel::MidiModel(Session& s, size_t size)
: _session(s) : Automatable(s, "midi model")
, _notes(size) , _notes(size)
, _note_mode(Sustained) , _note_mode(Sustained)
, _writing(false) , _writing(false)
@ -192,9 +192,10 @@ MidiModel::read (MidiRingBuffer& dst, nframes_t start, nframes_t nframes, nframe
void void
MidiModel::start_write() MidiModel::start_write()
{ {
//cerr << "MM START WRITE, MODE = " << enum_2_string(_note_mode) << endl; //cerr << "MM " << this << " START WRITE, MODE = " << enum_2_string(_note_mode) << endl;
_write_notes.clear(); _lock.writer_lock();
_writing = true; _writing = true;
_write_notes.clear();
} }
@ -210,7 +211,7 @@ MidiModel::end_write(bool delete_stuck)
{ {
assert(_writing); assert(_writing);
//cerr << "MM END WRITE: " << _notes.size() << " STUCK NOTES\n"; //cerr << "MM " << this << " END WRITE: " << _notes.size() << " NOTES\n";
if (_note_mode == Sustained && delete_stuck) { if (_note_mode == Sustained && delete_stuck) {
for (Notes::iterator n = _notes.begin(); n != _notes.end() ; ) { for (Notes::iterator n = _notes.begin(); n != _notes.end() ; ) {
@ -225,6 +226,7 @@ MidiModel::end_write(bool delete_stuck)
_write_notes.clear(); _write_notes.clear();
_writing = false; _writing = false;
_lock.writer_unlock();
} }
@ -241,20 +243,10 @@ MidiModel::append(const MidiBuffer& buf)
{ {
assert(_writing); assert(_writing);
_lock.writer_lock();
for (MidiBuffer::const_iterator i = buf.begin(); i != buf.end(); ++i) { for (MidiBuffer::const_iterator i = buf.begin(); i != buf.end(); ++i) {
const MidiEvent& ev = *i; assert(_notes.empty() || (*i).time() >= _notes.back().time());
append(*i);
assert(_notes.empty() || ev.time() >= _notes.back().time());
if (ev.type() == MIDI_CMD_NOTE_ON)
append_note_on(ev.time(), ev.note(), ev.velocity());
else if (ev.type() == MIDI_CMD_NOTE_OFF)
append_note_off(ev.time(), ev.note());
} }
_lock.writer_unlock();
} }
@ -265,21 +257,27 @@ MidiModel::append(const MidiBuffer& buf)
* and MUST be >= the latest event currently in the model. * and MUST be >= the latest event currently in the model.
*/ */
void void
MidiModel::append(double time, size_t size, const Byte* buf) MidiModel::append(const MidiEvent& ev)
{ {
assert(_notes.empty() || time >= _notes.back().time()); assert(_notes.empty() || ev.time() >= _notes.back().time());
assert(_writing); assert(_writing);
if ((buf[0] & 0xF0) == MIDI_CMD_NOTE_ON) if (ev.is_note_on())
append_note_on(time, buf[1], buf[2]); append_note_on(ev.time(), ev.note(), ev.velocity());
else if ((buf[0] & 0xF0) == MIDI_CMD_NOTE_OFF) else if (ev.is_note_off())
append_note_off(time, buf[1]); append_note_off(ev.time(), ev.note());
else if (ev.is_cc())
append_cc(ev.time(), ev.cc_number(), ev.cc_value());
else
printf("MM Unknown event type %X\n", ev.type());
} }
void void
MidiModel::append_note_on(double time, uint8_t note_num, uint8_t velocity) MidiModel::append_note_on(double time, uint8_t note_num, uint8_t velocity)
{ {
//cerr << "MidiModel " << this << " note " << (int)note_num << " on @ " << time << endl;
assert(_writing); assert(_writing);
_notes.push_back(Note(time, 0, note_num, velocity)); _notes.push_back(Note(time, 0, note_num, velocity));
if (_note_mode == Sustained) { if (_note_mode == Sustained) {
@ -294,6 +292,8 @@ MidiModel::append_note_on(double time, uint8_t note_num, uint8_t velocity)
void void
MidiModel::append_note_off(double time, uint8_t note_num) MidiModel::append_note_off(double time, uint8_t note_num)
{ {
//cerr << "MidiModel " << this << " note " << (int)note_num << " off @ " << time << endl;
assert(_writing); assert(_writing);
if (_note_mode == Percussive) { if (_note_mode == Percussive) {
//cerr << "MM Ignoring note off (percussive mode)" << endl; //cerr << "MM Ignoring note off (percussive mode)" << endl;
@ -321,6 +321,19 @@ MidiModel::append_note_off(double time, uint8_t note_num)
} }
void
MidiModel::append_cc(double time, uint8_t number, uint8_t value)
{
Parameter param(MidiCCAutomation, number);
//cerr << "MidiModel " << this << " add CC " << (int)number << " = " << (int)value
// << " @ " << time << endl;
boost::shared_ptr<AutomationControl> control = Automatable::control(param, true);
control->list()->fast_simple_add(time, (double)value);
}
void void
MidiModel::add_note_unlocked(const Note& note) MidiModel::add_note_unlocked(const Note& note)
{ {
@ -340,6 +353,7 @@ MidiModel::remove_note_unlocked(const Note& note)
} }
/** Slow! for debugging only. */ /** Slow! for debugging only. */
#ifndef NDEBUG
bool bool
MidiModel::is_sorted() const MidiModel::is_sorted() const
{ {
@ -352,7 +366,7 @@ MidiModel::is_sorted() const
return true; return true;
} }
#endif
/** Start a new command. /** Start a new command.
* *
@ -509,3 +523,10 @@ MidiModel::write_to(boost::shared_ptr<MidiSource> source)
return true; return true;
} }
XMLNode&
MidiModel::get_state()
{
XMLNode *node = new XMLNode("MidiModel");
return *node;
}

View file

@ -50,7 +50,6 @@ MidiSource::MidiSource (Session& s, string name)
: Source (s, name, DataType::MIDI) : Source (s, name, DataType::MIDI)
, _timeline_position(0) , _timeline_position(0)
, _model(new MidiModel(s)) , _model(new MidiModel(s))
, _model_loaded (false)
, _writing (false) , _writing (false)
{ {
_read_data_count = 0; _read_data_count = 0;
@ -61,7 +60,6 @@ MidiSource::MidiSource (Session& s, const XMLNode& node)
: Source (s, node) : Source (s, node)
, _timeline_position(0) , _timeline_position(0)
, _model(new MidiModel(s)) , _model(new MidiModel(s))
, _model_loaded (false)
, _writing (false) , _writing (false)
{ {
_read_data_count = 0; _read_data_count = 0;
@ -106,7 +104,7 @@ nframes_t
MidiSource::read (MidiRingBuffer& dst, nframes_t start, nframes_t cnt, nframes_t stamp_offset) const MidiSource::read (MidiRingBuffer& dst, nframes_t start, nframes_t cnt, nframes_t stamp_offset) const
{ {
Glib::Mutex::Lock lm (_lock); Glib::Mutex::Lock lm (_lock);
if (_model_loaded && _model) { if (_model) {
/*const size_t n_events = */_model->read(dst, start, cnt, stamp_offset); /*const size_t n_events = */_model->read(dst, start, cnt, stamp_offset);
//cout << "Read " << n_events << " events from model." << endl; //cout << "Read " << n_events << " events from model." << endl;
return cnt; return cnt;
@ -164,9 +162,10 @@ MidiSource::mark_streaming_write_completed ()
void void
MidiSource::session_saved() MidiSource::session_saved()
{ {
flush(); flush_header();
flush_footer();
if (_model && _model_loaded && _model->edited()) { if (_model && _model->edited()) {
string newname; string newname;
const string basename = PBD::basename_nosuffix(_name); const string basename = PBD::basename_nosuffix(_name);
string::size_type last_dash = basename.find_last_of("-"); string::size_type last_dash = basename.find_last_of("-");
@ -193,9 +192,9 @@ MidiSource::session_saved()
newsrc->set_model(_model); newsrc->set_model(_model);
_model.reset(); _model.reset();
_model_loaded = false;
newsrc->flush(); newsrc->flush_header();
newsrc->flush_footer();
Switched.emit(newsrc); Switched.emit(newsrc);
} }

View file

@ -79,6 +79,8 @@ Quantize::run (boost::shared_ptr<Region> r)
i->set_duration(new_dur); i->set_duration(new_dur);
} }
model->set_edited(true);
return 0; return 0;
#if 0 #if 0
SourceList nsrcs; SourceList nsrcs;

View file

@ -1445,7 +1445,7 @@ XMLNode&
Route::state(bool full_state) Route::state(bool full_state)
{ {
XMLNode *node = new XMLNode("Route"); XMLNode *node = new XMLNode("Route");
ProcessorList:: iterator i; ProcessorList::iterator i;
char buf[32]; char buf[32];
if (_flags) { if (_flags) {

View file

@ -156,18 +156,24 @@ SMFSource::open()
// We're making a new file // We're making a new file
} else { } else {
_fd = fopen(path().c_str(), "w+"); _fd = fopen(path().c_str(), "w+");
_track_size = 0; _track_size = 4;
// Write a tentative header just to pad things out so writing happens in the right spot // Write a tentative header just to pad things out so writing happens in the right spot
set_timeline_position(0); set_timeline_position(0);
flush_header(); flush_header();
write_footer();
// Note file isn't a valid SMF at this point (no footer). Does it matter? seek_to_end();
} }
return (_fd == 0) ? -1 : 0; return (_fd == 0) ? -1 : 0;
} }
void
SMFSource::seek_to_end()
{
fseek(_fd, -4, SEEK_END);
}
int int
SMFSource::flush_header () SMFSource::flush_header ()
{ {
@ -202,14 +208,22 @@ SMFSource::flush_header ()
int int
SMFSource::flush_footer() SMFSource::flush_footer()
{ {
//cerr << "SMF " << name() << " writing EOT\n"; seek_to_end();
write_footer();
seek_to_end();
fseek(_fd, 0, SEEK_END); return 0;
}
void
SMFSource::write_footer()
{
//cerr << "SMF " << name() << " writing EOT at byte " << ftell(_fd) << endl;
write_var_len(0); write_var_len(0);
char eot[3] = { 0xFF, 0x2F, 0x00 }; // end-of-track meta-event char eot[3] = { 0xFF, 0x2F, 0x00 }; // end-of-track meta-event
fwrite(eot, 1, 3, _fd); fwrite(eot, 1, 3, _fd);
fflush(_fd); fflush(_fd);
return 0;
} }
/** Returns the offset of the first event in the file with a time past @a start, /** Returns the offset of the first event in the file with a time past @a start,
@ -243,57 +257,70 @@ SMFSource::find_first_event_after(nframes_t start)
* will have it's time field set to it's delta time, in SMF tempo-based ticks, using the * 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). * 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 * 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). * skipped (eg a meta event), or -1 on EOF (or end of track).
*/ */
int int
SMFSource::read_event(jack_midi_event_t& ev) const SMFSource::read_event(uint32_t* delta_t, uint32_t* size, Byte** buf) const
{ {
// - 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)) { if (feof(_fd)) {
return -1; return -1;
} }
uint32_t delta_time = read_var_len(); assert(delta_t);
assert(size);
assert(buf);
*delta_t = read_var_len();
assert(!feof(_fd)); assert(!feof(_fd));
int status = fgetc(_fd);
const int status = fgetc(_fd);
assert(status != EOF); // FIXME die gracefully assert(status != EOF); // FIXME die gracefully
//printf("Status @ %X = %X\n", (unsigned)ftell(_fd) - 1, status);
if (status == 0xFF) { if (status == 0xFF) {
assert(!feof(_fd)); assert(!feof(_fd));
int type = fgetc(_fd); const int type = fgetc(_fd);
if ((unsigned char)type == 0x2F) { if ((unsigned char)type == 0x2F) {
//cerr << "SMF - hit EOT" << endl; //cerr << _name << " hit EOT" << endl;
return -1; // we hit the logical EOF anyway... return -1;
} else { } else {
ev.size = 0; *size = 0;
ev.time = delta_time; // this is needed regardless
return 0; return 0;
} }
} }
size_t event_size = midi_event_size((unsigned char)status) + 1; const int event_size = midi_event_size((unsigned char)status) + 1;
if (event_size <= 0) {
*size = 0;
return 0;
}
// Make sure we have enough scratch buffer // Make sure we have enough scratch buffer
if (ev.size < event_size) if (*size < (unsigned)event_size)
ev.buffer = (Byte*)realloc(ev.buffer, event_size); *buf = (Byte*)realloc(*buf, event_size);
ev.time = delta_time; *size = event_size;
ev.size = event_size;
/*if (ev.buffer == NULL) /*if (ev.buffer == NULL)
ev.buffer = (Byte*)malloc(sizeof(Byte) * ev.size);*/ ev.buffer = (Byte*)malloc(sizeof(Byte) * ev.size);*/
ev.buffer[0] = (unsigned char)status; (*buf)[0] = (unsigned char)status;
fread(ev.buffer+1, 1, ev.size - 1, _fd); if (event_size > 1)
fread((*buf) + 1, 1, *size - 1, _fd);
/*printf("%s read event: delta = %u, size = %zu, data = ", _name.c_str(), delta_time, ev.size); /*printf("%s read event: delta = %u, size = %u, data = ", _name.c_str(), *delta_t, *size);
for (size_t i=0; i < ev.size; ++i) { for (size_t i=0; i < *size; ++i) {
printf("%X ", ev.buffer[i]); printf("%X ", (*buf)[i]);
} }
printf("\n");*/ printf("\n");*/
return ev.size; return (int)*size;
} }
/** All stamps in audio frames */ /** All stamps in audio frames */
@ -307,24 +334,24 @@ SMFSource::read_unlocked (MidiRingBuffer& dst, nframes_t start, nframes_t cnt, n
_read_data_count = 0; _read_data_count = 0;
jack_midi_event_t ev; // time in SMF ticks // Output parameters for read_event (which will allocate scratch in buffer as needed)
ev.time = 0; uint32_t ev_delta_t = 0;
ev.size = 0; uint32_t ev_size = 0;
ev.buffer = NULL; // read_event will allocate scratch as needed Byte* ev_buffer = 0;
size_t scratch_size = 0; // keep track of scratch and minimize reallocs size_t scratch_size = 0; // keep track of scratch to minimize reallocs
// FIXME: don't seek to start every read // FIXME: don't seek to start and search every read (brutal!)
fseek(_fd, _header_size, 0); fseek(_fd, _header_size, 0);
// FIXME: assumes tempo never changes after start // FIXME: assumes tempo never changes after start
const double frames_per_beat = _session.tempo_map().tempo_at(_timeline_position).frames_per_beat( const double frames_per_beat = _session.tempo_map().tempo_at(_timeline_position).frames_per_beat(
_session.engine().frame_rate()); _session.engine().frame_rate());
uint64_t start_ticks = (uint64_t)((start / frames_per_beat) * _ppqn); const uint64_t start_ticks = (uint64_t)((start / frames_per_beat) * _ppqn);
while (!feof(_fd)) { while (!feof(_fd)) {
int ret = read_event(ev); int ret = read_event(&ev_delta_t, &ev_size, &ev_buffer);
if (ret == -1) { // EOF if (ret == -1) { // EOF
//cerr << "SMF - EOF\n"; //cerr << "SMF - EOF\n";
break; break;
@ -332,29 +359,28 @@ SMFSource::read_unlocked (MidiRingBuffer& dst, nframes_t start, nframes_t cnt, n
if (ret == 0) { // meta-event (skipped) if (ret == 0) { // meta-event (skipped)
//cerr << "SMF - META\n"; //cerr << "SMF - META\n";
time += ev.time; // just accumulate delta time and ignore event time += ev_delta_t; // just accumulate delta time and ignore event
continue; continue;
} }
time += ev.time; // accumulate delta time time += ev_delta_t; // accumulate delta time
ev.time = time; // set ev.time to actual time (relative to source start)
if (ev.time >= start_ticks) { if (time >= start_ticks) {
if (ev.time < start_ticks + (cnt / frames_per_beat)) { const nframes_t ev_frame_time = (nframes_t)(
((time / (double)_ppqn) * frames_per_beat)) + stamp_offset;
if (ev_frame_time <= start + cnt)
dst.write(ev_frame_time, ev_size, ev_buffer);
else
break; break;
} else {
ev.time = (nframes_t)(((ev.time / (double)_ppqn) * frames_per_beat)) + stamp_offset;
// write event time in absolute frames
dst.write(ev.time, ev.size, ev.buffer);
}
} }
_read_data_count += ev.size; _read_data_count += ev_size;
if (ev.size > scratch_size) if (ev_size > scratch_size)
scratch_size = ev.size; scratch_size = ev_size;
else else
ev.size = scratch_size; ev_size = scratch_size; // minimize realloc in read_event
} }
return cnt; return cnt;
@ -365,35 +391,54 @@ nframes_t
SMFSource::write_unlocked (MidiRingBuffer& src, nframes_t cnt) SMFSource::write_unlocked (MidiRingBuffer& src, nframes_t cnt)
{ {
_write_data_count = 0; _write_data_count = 0;
double time;
size_t size;
boost::shared_ptr<MidiBuffer> buf_ptr(new MidiBuffer(1024)); // FIXME: size? size_t buf_capacity = 4;
MidiBuffer& buf = *buf_ptr.get(); Byte* buf = (Byte*)malloc(buf_capacity);
src.read(buf, /*_length*/0, _length + cnt); // FIXME?
fseek(_fd, 0, SEEK_END);
for (MidiBuffer::iterator i = buf.begin(); i != buf.end(); ++i) { if (_model && ! _model->currently_writing())
MidiEvent& ev = *i; _model->start_write();
assert(ev.time() >= _timeline_position);
ev.time() -= _timeline_position;
assert(ev.time() >= _last_ev_time);
append_event_unlocked(ev); while (true) {
bool ret = src.full_peek(sizeof(double), (Byte*)&time);
if (!ret || time > _length + cnt)
break;
ret = src.read_prefix(&time, &size);
if (!ret)
break;
if (size > buf_capacity) {
buf_capacity = size;
buf = (Byte*)realloc(buf, size);
}
ret = src.read_contents(size, buf);
if (!ret) {
cerr << "ERROR: Read time/size but not buffer, corrupt MIDI ring buffer" << endl;
break;
}
assert(time >= _timeline_position);
time -= _timeline_position;
assert(time >= _last_ev_time);
const MidiEvent ev(time, size, buf);
append_event_unlocked(MidiEvent(ev));
if (_model)
_model->append(ev);
} }
fflush(_fd); fflush(_fd);
free(buf);
const nframes_t oldlen = _length; const nframes_t oldlen = _length;
update_length(oldlen, cnt); update_length(oldlen, cnt);
if (_model) { ViewDataRangeReady (oldlen, cnt); /* EMIT SIGNAL */
if ( ! _model->currently_writing()) {
_model->start_write();
}
_model->append(buf);
}
ViewDataRangeReady (buf_ptr, oldlen, cnt); /* EMIT SIGNAL */
return cnt; return cnt;
} }
@ -480,6 +525,7 @@ SMFSource::mark_streaming_write_completed ()
return; return;
} }
flush_header();
flush_footer(); flush_footer();
#if 0 #if 0
@ -784,9 +830,6 @@ SMFSource::read_var_len() const
{ {
assert(!feof(_fd)); assert(!feof(_fd));
//int offset = ftell(_fd);
//cerr << "SMF - reading var len at " << offset << endl;
uint32_t value; uint32_t value;
unsigned char c; unsigned char c;
@ -810,16 +853,18 @@ SMFSource::load_model(bool lock, bool force_reload)
if (lock) if (lock)
Glib::Mutex::Lock lm (_lock); Glib::Mutex::Lock lm (_lock);
if (_model && _model_loaded && ! force_reload) { if (_model && !force_reload && !_model->empty()) {
assert(_model); //cerr << _name << " NOT reloading model " << _model.get() << " (" << _model->n_notes()
// << " notes)" << endl;
return; return;
} }
if (! _model) { if (! _model) {
cerr << "SMFSource: Loading new model " << _model.get() << endl;
_model = boost::shared_ptr<MidiModel>(new MidiModel(_session)); _model = boost::shared_ptr<MidiModel>(new MidiModel(_session));
cerr << _name << " loaded new model " << _model.get() << endl;
} else { } else {
cerr << "SMFSource: Reloading model " << _model.get() << endl; cerr << _name << " reloading model " << _model.get()
<< " (" << _model->n_notes() << " notes)" <<endl;
_model->clear(); _model->clear();
} }
@ -828,10 +873,7 @@ SMFSource::load_model(bool lock, bool force_reload)
fseek(_fd, _header_size, 0); fseek(_fd, _header_size, 0);
uint64_t time = 0; /* in SMF ticks */ uint64_t time = 0; /* in SMF ticks */
jack_midi_event_t ev; MidiEvent ev;
ev.time = 0;
ev.size = 0;
ev.buffer = NULL;
size_t scratch_size = 0; // keep track of scratch and minimize reallocs size_t scratch_size = 0; // keep track of scratch and minimize reallocs
@ -839,35 +881,35 @@ SMFSource::load_model(bool lock, bool force_reload)
const double frames_per_beat = _session.tempo_map().tempo_at(_timeline_position).frames_per_beat( const double frames_per_beat = _session.tempo_map().tempo_at(_timeline_position).frames_per_beat(
_session.engine().frame_rate()); _session.engine().frame_rate());
uint32_t delta_t = 0;
int ret; int ret;
while ((ret = read_event(ev)) >= 0) { while ((ret = read_event(&delta_t, &ev.size(), &ev.buffer())) >= 0) {
time += ev.time; time += delta_t;
const double ev_time = (double)(time * frames_per_beat / (double)_ppqn); // in frames
if (ret > 0) { // didn't skip (meta) event if (ret > 0) { // didn't skip (meta) event
_model->append(ev_time, ev.size, ev.buffer); // make ev.time absolute time in frames
ev.time() = (double)time * frames_per_beat / (double)_ppqn;
_model->append(ev);
} }
if (ev.size > scratch_size) if (ev.size() > scratch_size)
scratch_size = ev.size; scratch_size = ev.size();
else else
ev.size = scratch_size; ev.size() = scratch_size;
} }
_model->end_write(false); /* FIXME: delete stuck notes iff percussion? */ _model->end_write(false);
free(ev.buffer); free(ev.buffer());
_model_loaded = true;
} }
void void
SMFSource::destroy_model() SMFSource::destroy_model()
{ {
cerr << "SMFSource: Destroying model " << _model.get() << endl; //cerr << _name << " destroying model " << _model.get() << endl;
_model.reset(); _model.reset();
} }