summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--gtk2_ardour/midi_region_view.cc23
-rw-r--r--gtk2_ardour/midi_streamview.cc29
-rw-r--r--gtk2_ardour/midi_streamview.h5
-rw-r--r--libs/ardour/ardour/automatable.h4
-rw-r--r--libs/ardour/ardour/midi_event.h5
-rw-r--r--libs/ardour/ardour/midi_model.h17
-rw-r--r--libs/ardour/ardour/midi_ring_buffer.h27
-rw-r--r--libs/ardour/ardour/midi_source.h13
-rw-r--r--libs/ardour/ardour/session_object.h20
-rw-r--r--libs/ardour/ardour/smf_source.h8
-rw-r--r--libs/ardour/midi_model.cc67
-rw-r--r--libs/ardour/midi_source.cc13
-rw-r--r--libs/ardour/quantize.cc2
-rw-r--r--libs/ardour/route.cc2
-rw-r--r--libs/ardour/smf_source.cc226
15 files changed, 295 insertions, 166 deletions
diff --git a/gtk2_ardour/midi_region_view.cc b/gtk2_ardour/midi_region_view.cc
index e96a65d893..c51cd314a2 100644
--- a/gtk2_ardour/midi_region_view.cc
+++ b/gtk2_ardour/midi_region_view.cc
@@ -394,11 +394,14 @@ MidiRegionView::display_model(boost::shared_ptr<MidiModel> model)
void
MidiRegionView::redisplay_model()
{
- clear_events();
-
- //cerr << "Redisplaying model " << _model << endl;
+ // Don't redisplay the model if we're currently recording and displaying that
+ if (_active_notes)
+ return;
if (_model) {
+
+ clear_events();
+
begin_write();
for (size_t i=0; i < _model->n_notes(); ++i)
@@ -406,7 +409,7 @@ MidiRegionView::redisplay_model()
end_write();
} 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 ()
{
in_destructor = true;
- end_write();
+ if (_active_notes)
+ end_write();
+
+ clear_events();
RegionViewGoingAway (this); /* EMIT_SIGNAL */
}
@@ -488,6 +494,7 @@ MidiRegionView::add_ghost (AutomationTimeAxisView& atv)
void
MidiRegionView::begin_write()
{
+ assert(!_active_notes);
_active_notes = new CanvasNote*[128];
for (unsigned i=0; i < 128; ++i)
_active_notes[i] = NULL;
@@ -512,9 +519,9 @@ MidiRegionView::end_write()
void
MidiRegionView::add_event (const MidiEvent& ev)
{
- /*printf("Event, time = %f, size = %zu, data = ", ev.time, ev.size);
- for (size_t i=0; i < ev.size; ++i) {
- printf("%X ", ev.buffer[i]);
+ /*printf("MRV add Event, time = %f, size = %u, data = ", ev.time(), ev.size());
+ for (size_t i=0; i < ev.size(); ++i) {
+ printf("%X ", ev.buffer()[i]);
}
printf("\n\n");*/
diff --git a/gtk2_ardour/midi_streamview.cc b/gtk2_ardour/midi_streamview.cc
index 484def2a3e..21eae23f90 100644
--- a/gtk2_ardour/midi_streamview.cc
+++ b/gtk2_ardour/midi_streamview.cc
@@ -410,7 +410,7 @@ MidiStreamView::setup_rec_box ()
}
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));
@@ -467,11 +467,24 @@ MidiStreamView::update_rec_regions (boost::shared_ptr<MidiBuffer> data, nframes_
/* draw events */
MidiRegionView* mrv = (MidiRegionView*)iter->second;
- for (MidiBuffer::iterator i = data->begin(); i != data->end(); ++i) {
- const MidiEvent& ev = *i;
- mrv->add_event(ev);
- mrv->extend_active_notes();
+ // FIXME: slow
+ for (size_t i=0; i < data->n_notes(); ++i) {
+ const MidiModel::Note& note = data->note_at(i);
+ 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
-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
- 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()));
//cerr << src.get() << " MIDI READY: " << start << " * " << dur
// << " -- " << data->size() << " events!" << endl;
- this->update_rec_regions (data, start, dur);
+ this->update_rec_regions (src->model(), start, dur);
}
void
diff --git a/gtk2_ardour/midi_streamview.h b/gtk2_ardour/midi_streamview.h
index 567844e678..fa414cfe9c 100644
--- a/gtk2_ardour/midi_streamview.h
+++ b/gtk2_ardour/midi_streamview.h
@@ -40,6 +40,7 @@ namespace ARDOUR {
class PeakData;
class MidiRegion;
class Source;
+ class MidiModel;
}
class PublicEditor;
@@ -95,8 +96,8 @@ class MidiStreamView : public StreamView
private:
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 update_rec_regions (boost::shared_ptr<ARDOUR::MidiBuffer> data, jack_nframes_t start, jack_nframes_t dur);
+ 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::MidiModel> data, jack_nframes_t start, jack_nframes_t dur);
RegionView* add_region_view_internal (boost::shared_ptr<ARDOUR::Region>, bool wait_for_waves);
void display_region(MidiRegionView* region_view, bool load_model);
diff --git a/libs/ardour/ardour/automatable.h b/libs/ardour/ardour/automatable.h
index a48029734a..0af996f6c0 100644
--- a/libs/ardour/ardour/automatable.h
+++ b/libs/ardour/ardour/automatable.h
@@ -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
it under the terms of the GNU General Public License as published by
@@ -94,7 +94,7 @@ protected:
mutable Glib::Mutex _automation_lock;
- Controls _controls;
+ Controls _controls;
std::set<Parameter> _visible_controls;
std::set<Parameter> _can_automate_list;
diff --git a/libs/ardour/ardour/midi_event.h b/libs/ardour/ardour/midi_event.h
index 3a6b89f198..61e6b8af6e 100644
--- a/libs/ardour/ardour/midi_event.h
+++ b/libs/ardour/ardour/midi_event.h
@@ -112,11 +112,14 @@ struct MidiEvent {
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_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 uint8_t note() const { return (_buffer[1]); }
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 Byte* buffer() { return _buffer; }
+ inline Byte*& buffer() { return _buffer; }
private:
double _time; /**< Sample index (or beat time) at which event is valid */
diff --git a/libs/ardour/ardour/midi_model.h b/libs/ardour/ardour/midi_model.h
index 63b87c9683..f688c0fe09 100644
--- a/libs/ardour/ardour/midi_model.h
+++ b/libs/ardour/ardour/midi_model.h
@@ -28,6 +28,7 @@
#include <ardour/types.h>
#include <ardour/midi_buffer.h>
#include <ardour/midi_ring_buffer.h>
+#include <ardour/automatable.h>
namespace ARDOUR {
@@ -41,7 +42,7 @@ class MidiSource;
* note on and off events (controller data is not here since it's represented
* as an AutomationList)
*/
-class MidiModel : public boost::noncopyable {
+class MidiModel : public boost::noncopyable, public Automatable {
public:
struct Note {
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);
/** 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 size_t n_notes() const { return _notes.size(); }
+ inline bool empty() const { return _notes.size() == 0 && _controls.size() == 0; }
typedef std::vector<Note> Notes;
@@ -146,7 +149,12 @@ public:
void apply_command(Command* cmd);
bool edited() const { return _edited; }
+ void set_edited(bool yn) { _edited = yn; }
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;
@@ -155,12 +163,13 @@ private:
void add_note_unlocked(const Note& note);
void remove_note_unlocked(const Note& note);
+#ifndef NDEBUG
bool is_sorted() const;
+#endif
void append_note_on(double time, uint8_t note, uint8_t velocity);
void append_note_off(double time, uint8_t note);
-
- Session& _session;
+ void append_cc(double time, uint8_t number, uint8_t value);
Glib::RWLock _lock;
diff --git a/libs/ardour/ardour/midi_ring_buffer.h b/libs/ardour/ardour/midi_ring_buffer.h
index 21beebacd2..9389f962fc 100644
--- a/libs/ardour/ardour/midi_ring_buffer.h
+++ b/libs/ardour/ardour/midi_ring_buffer.h
@@ -233,6 +233,9 @@ public:
size_t write(double time, size_t size, const 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);
};
@@ -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
MidiRingBuffer::write(double time, size_t size, const Byte* buf)
{
diff --git a/libs/ardour/ardour/midi_source.h b/libs/ardour/ardour/midi_source.h
index 088175ab75..95d2a38c4e 100644
--- a/libs/ardour/ardour/midi_source.h
+++ b/libs/ardour/ardour/midi_source.h
@@ -53,8 +53,6 @@ class MidiSource : public Source
virtual nframes_t write (MidiRingBuffer& src, nframes_t cnt);
virtual void append_event_unlocked(const MidiEvent& ev) = 0;
- virtual void flush() {}
-
virtual void mark_for_remove() = 0;
virtual void mark_streaming_midi_write_started (NoteMode mode);
virtual void mark_streaming_write_started ();
@@ -72,8 +70,8 @@ class MidiSource : public Source
static sigc::signal<void,MidiSource*> MidiSourceCreated;
- // The MIDI equivalent to "peaks" (but complete data)
- mutable sigc::signal<void,boost::shared_ptr<MidiBuffer>,nframes_t,nframes_t> ViewDataRangeReady;
+ // Signal a range of recorded data is available for reading from model()
+ mutable sigc::signal<void,nframes_t,nframes_t> ViewDataRangeReady;
XMLNode& get_state ();
int set_state (const XMLNode&);
@@ -82,12 +80,14 @@ class MidiSource : public Source
virtual void destroy_model() = 0;
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; }
- void set_model(boost::shared_ptr<MidiModel> m) { _model = m; _model_loaded = true; }
+ void set_model(boost::shared_ptr<MidiModel> m) { _model = m; }
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 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()
boost::shared_ptr<MidiModel> _model;
- bool _model_loaded;
bool _writing;
private:
diff --git a/libs/ardour/ardour/session_object.h b/libs/ardour/ardour/session_object.h
index 9d82b4bba0..bb726cb0d0 100644
--- a/libs/ardour/ardour/session_object.h
+++ b/libs/ardour/ardour/session_object.h
@@ -20,6 +20,7 @@
#ifndef __ardour_session_object_h__
#define __ardour_session_object_h__
+#include <string>
#include <pbd/statefuldestructible.h>
namespace ARDOUR {
@@ -36,22 +37,27 @@ class Session;
class SessionObject : public PBD::StatefulDestructible
{
public:
- SessionObject(Session& session, const string& name)
+ SessionObject(Session& session, const std::string& name)
: _session(session)
, _name(name)
{}
- Session& session() const { return _session; }
- const string& name() const { return _name; }
+ Session& session() const { return _session; }
+ const std::string& name() const { return _name; }
- virtual bool set_name (const string& str)
- { if (_name != str) { _name = str; NameChanged(); } return true; }
+ virtual bool set_name (const std::string& str) {
+ if (_name != str) {
+ _name = str;
+ NameChanged();
+ }
+ return true;
+ }
sigc::signal<void> NameChanged;
protected:
- Session& _session;
- string _name;
+ Session& _session;
+ std::string _name;
};
} // namespace ARDOUR
diff --git a/libs/ardour/ardour/smf_source.h b/libs/ardour/ardour/smf_source.h
index c852266e9c..bf5618d57a 100644
--- a/libs/ardour/ardour/smf_source.h
+++ b/libs/ardour/ardour/smf_source.h
@@ -74,8 +74,6 @@ class SMFSource : public MidiSource {
int flush_header ();
int flush_footer ();
- void flush() { flush_header(); flush_footer(); }
-
int move_to_trash (const string trash_dir_name);
bool is_empty () const;
@@ -108,13 +106,15 @@ class SMFSource : public MidiSource {
bool removable() const;
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(char id[4], uint32_t length, void* data);
size_t write_var_len(uint32_t val);
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;
diff --git a/libs/ardour/midi_model.cc b/libs/ardour/midi_model.cc
index 45a49e73ba..7c67d40d75 100644
--- a/libs/ardour/midi_model.cc
+++ b/libs/ardour/midi_model.cc
@@ -99,7 +99,7 @@ MidiModel::Note::operator=(const Note& copy)
// MidiModel
MidiModel::MidiModel(Session& s, size_t size)
- : _session(s)
+ : Automatable(s, "midi model")
, _notes(size)
, _note_mode(Sustained)
, _writing(false)
@@ -192,9 +192,10 @@ MidiModel::read (MidiRingBuffer& dst, nframes_t start, nframes_t nframes, nframe
void
MidiModel::start_write()
{
- //cerr << "MM START WRITE, MODE = " << enum_2_string(_note_mode) << endl;
- _write_notes.clear();
+ //cerr << "MM " << this << " START WRITE, MODE = " << enum_2_string(_note_mode) << endl;
+ _lock.writer_lock();
_writing = true;
+ _write_notes.clear();
}
@@ -210,7 +211,7 @@ MidiModel::end_write(bool delete_stuck)
{
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) {
for (Notes::iterator n = _notes.begin(); n != _notes.end() ; ) {
@@ -225,6 +226,7 @@ MidiModel::end_write(bool delete_stuck)
_write_notes.clear();
_writing = false;
+ _lock.writer_unlock();
}
@@ -241,20 +243,10 @@ MidiModel::append(const MidiBuffer& buf)
{
assert(_writing);
- _lock.writer_lock();
-
for (MidiBuffer::const_iterator i = buf.begin(); i != buf.end(); ++i) {
- const MidiEvent& ev = *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());
+ assert(_notes.empty() || (*i).time() >= _notes.back().time());
+ append(*i);
}
-
- _lock.writer_unlock();
}
@@ -265,21 +257,27 @@ MidiModel::append(const MidiBuffer& buf)
* and MUST be >= the latest event currently in the model.
*/
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);
- if ((buf[0] & 0xF0) == MIDI_CMD_NOTE_ON)
- append_note_on(time, buf[1], buf[2]);
- else if ((buf[0] & 0xF0) == MIDI_CMD_NOTE_OFF)
- append_note_off(time, buf[1]);
+ if (ev.is_note_on())
+ append_note_on(ev.time(), ev.note(), ev.velocity());
+ else if (ev.is_note_off())
+ 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
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);
_notes.push_back(Note(time, 0, note_num, velocity));
if (_note_mode == Sustained) {
@@ -294,6 +292,8 @@ MidiModel::append_note_on(double time, uint8_t note_num, uint8_t velocity)
void
MidiModel::append_note_off(double time, uint8_t note_num)
{
+ //cerr << "MidiModel " << this << " note " << (int)note_num << " off @ " << time << endl;
+
assert(_writing);
if (_note_mode == Percussive) {
//cerr << "MM Ignoring note off (percussive mode)" << endl;
@@ -322,6 +322,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
MidiModel::add_note_unlocked(const Note& note)
{
//cerr << "MidiModel " << this << " add note " << (int)note.note() << " @ " << note.time() << endl;
@@ -340,6 +353,7 @@ MidiModel::remove_note_unlocked(const Note& note)
}
/** Slow! for debugging only. */
+#ifndef NDEBUG
bool
MidiModel::is_sorted() const
{
@@ -352,7 +366,7 @@ MidiModel::is_sorted() const
return true;
}
-
+#endif
/** Start a new command.
*
@@ -509,3 +523,10 @@ MidiModel::write_to(boost::shared_ptr<MidiSource> source)
return true;
}
+XMLNode&
+MidiModel::get_state()
+{
+ XMLNode *node = new XMLNode("MidiModel");
+ return *node;
+}
+
diff --git a/libs/ardour/midi_source.cc b/libs/ardour/midi_source.cc
index c6e3bd0a08..4c97bb4e6d 100644
--- a/libs/ardour/midi_source.cc
+++ b/libs/ardour/midi_source.cc
@@ -50,7 +50,6 @@ MidiSource::MidiSource (Session& s, string name)
: Source (s, name, DataType::MIDI)
, _timeline_position(0)
, _model(new MidiModel(s))
- , _model_loaded (false)
, _writing (false)
{
_read_data_count = 0;
@@ -61,7 +60,6 @@ MidiSource::MidiSource (Session& s, const XMLNode& node)
: Source (s, node)
, _timeline_position(0)
, _model(new MidiModel(s))
- , _model_loaded (false)
, _writing (false)
{
_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
{
Glib::Mutex::Lock lm (_lock);
- if (_model_loaded && _model) {
+ if (_model) {
/*const size_t n_events = */_model->read(dst, start, cnt, stamp_offset);
//cout << "Read " << n_events << " events from model." << endl;
return cnt;
@@ -164,9 +162,10 @@ MidiSource::mark_streaming_write_completed ()
void
MidiSource::session_saved()
{
- flush();
+ flush_header();
+ flush_footer();
- if (_model && _model_loaded && _model->edited()) {
+ if (_model && _model->edited()) {
string newname;
const string basename = PBD::basename_nosuffix(_name);
string::size_type last_dash = basename.find_last_of("-");
@@ -193,9 +192,9 @@ MidiSource::session_saved()
newsrc->set_model(_model);
_model.reset();
- _model_loaded = false;
- newsrc->flush();
+ newsrc->flush_header();
+ newsrc->flush_footer();
Switched.emit(newsrc);
}
diff --git a/libs/ardour/quantize.cc b/libs/ardour/quantize.cc
index 5bee86d0c4..5da30e8466 100644
--- a/libs/ardour/quantize.cc
+++ b/libs/ardour/quantize.cc
@@ -79,6 +79,8 @@ Quantize::run (boost::shared_ptr<Region> r)
i->set_duration(new_dur);
}
+ model->set_edited(true);
+
return 0;
#if 0
SourceList nsrcs;
diff --git a/libs/ardour/route.cc b/libs/ardour/route.cc
index 914e936d6b..0453129f75 100644
--- a/libs/ardour/route.cc
+++ b/libs/ardour/route.cc
@@ -1445,7 +1445,7 @@ XMLNode&
Route::state(bool full_state)
{
XMLNode *node = new XMLNode("Route");
- ProcessorList:: iterator i;
+ ProcessorList::iterator i;
char buf[32];
if (_flags) {
diff --git a/libs/ardour/smf_source.cc b/libs/ardour/smf_source.cc
index dfba2d5c50..32f012e389 100644
--- a/libs/ardour/smf_source.cc
+++ b/libs/ardour/smf_source.cc
@@ -156,18 +156,24 @@ SMFSource::open()
// We're making a new file
} else {
_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
set_timeline_position(0);
flush_header();
-
- // Note file isn't a valid SMF at this point (no footer). Does it matter?
+ write_footer();
+ seek_to_end();
}
return (_fd == 0) ? -1 : 0;
}
+void
+SMFSource::seek_to_end()
+{
+ fseek(_fd, -4, SEEK_END);
+}
+
int
SMFSource::flush_header ()
{
@@ -202,14 +208,22 @@ SMFSource::flush_header ()
int
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);
char eot[3] = { 0xFF, 0x2F, 0x00 }; // end-of-track meta-event
fwrite(eot, 1, 3, _fd);
fflush(_fd);
- return 0;
}
/** 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
* 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).
*/
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)) {
return -1;
}
- uint32_t delta_time = read_var_len();
+ assert(delta_t);
+ assert(size);
+ assert(buf);
+
+ *delta_t = read_var_len();
assert(!feof(_fd));
- int status = fgetc(_fd);
+
+ const int status = fgetc(_fd);
assert(status != EOF); // FIXME die gracefully
+
+ //printf("Status @ %X = %X\n", (unsigned)ftell(_fd) - 1, status);
+
if (status == 0xFF) {
assert(!feof(_fd));
- int type = fgetc(_fd);
+ const int type = fgetc(_fd);
if ((unsigned char)type == 0x2F) {
- //cerr << "SMF - hit EOT" << endl;
- return -1; // we hit the logical EOF anyway...
+ //cerr << _name << " hit EOT" << endl;
+ return -1;
} else {
- ev.size = 0;
- ev.time = delta_time; // this is needed regardless
+ *size = 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
- if (ev.size < event_size)
- ev.buffer = (Byte*)realloc(ev.buffer, event_size);
+ if (*size < (unsigned)event_size)
+ *buf = (Byte*)realloc(*buf, event_size);
- ev.time = delta_time;
- ev.size = event_size;
+ *size = event_size;
/*if (ev.buffer == NULL)
ev.buffer = (Byte*)malloc(sizeof(Byte) * ev.size);*/
- ev.buffer[0] = (unsigned char)status;
- fread(ev.buffer+1, 1, ev.size - 1, _fd);
+ (*buf)[0] = (unsigned char)status;
+ 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);
- for (size_t i=0; i < ev.size; ++i) {
- printf("%X ", ev.buffer[i]);
+ /*printf("%s read event: delta = %u, size = %u, data = ", _name.c_str(), *delta_t, *size);
+ for (size_t i=0; i < *size; ++i) {
+ printf("%X ", (*buf)[i]);
}
printf("\n");*/
- return ev.size;
+ return (int)*size;
}
/** 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;
- jack_midi_event_t ev; // time in SMF ticks
- ev.time = 0;
- ev.size = 0;
- ev.buffer = NULL; // read_event will allocate scratch as needed
+ // Output parameters for read_event (which will allocate scratch in buffer as needed)
+ uint32_t ev_delta_t = 0;
+ uint32_t ev_size = 0;
+ 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);
// FIXME: assumes tempo never changes after start
const double frames_per_beat = _session.tempo_map().tempo_at(_timeline_position).frames_per_beat(
_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)) {
- int ret = read_event(ev);
+ int ret = read_event(&ev_delta_t, &ev_size, &ev_buffer);
if (ret == -1) { // EOF
//cerr << "SMF - EOF\n";
break;
@@ -332,29 +359,28 @@ SMFSource::read_unlocked (MidiRingBuffer& dst, nframes_t start, nframes_t cnt, n
if (ret == 0) { // meta-event (skipped)
//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;
}
- time += ev.time; // accumulate delta time
- ev.time = time; // set ev.time to actual time (relative to source start)
+ time += ev_delta_t; // accumulate delta time
+
+ if (time >= start_ticks) {
+ const nframes_t ev_frame_time = (nframes_t)(
+ ((time / (double)_ppqn) * frames_per_beat)) + stamp_offset;
- if (ev.time >= start_ticks) {
- if (ev.time < start_ticks + (cnt / frames_per_beat)) {
+ if (ev_frame_time <= start + cnt)
+ dst.write(ev_frame_time, ev_size, ev_buffer);
+ else
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)
- scratch_size = ev.size;
+ if (ev_size > scratch_size)
+ scratch_size = ev_size;
else
- ev.size = scratch_size;
+ ev_size = scratch_size; // minimize realloc in read_event
}
return cnt;
@@ -365,35 +391,54 @@ nframes_t
SMFSource::write_unlocked (MidiRingBuffer& src, nframes_t cnt)
{
_write_data_count = 0;
+
+ double time;
+ size_t size;
- boost::shared_ptr<MidiBuffer> buf_ptr(new MidiBuffer(1024)); // FIXME: size?
- MidiBuffer& buf = *buf_ptr.get();
- src.read(buf, /*_length*/0, _length + cnt); // FIXME?
-
- fseek(_fd, 0, SEEK_END);
+ size_t buf_capacity = 4;
+ Byte* buf = (Byte*)malloc(buf_capacity);
- for (MidiBuffer::iterator i = buf.begin(); i != buf.end(); ++i) {
- MidiEvent& ev = *i;
- assert(ev.time() >= _timeline_position);
- ev.time() -= _timeline_position;
- assert(ev.time() >= _last_ev_time);
+ if (_model && ! _model->currently_writing())
+ _model->start_write();
+
+ while (true) {
+ bool ret = src.full_peek(sizeof(double), (Byte*)&time);
+ if (!ret || time > _length + cnt)
+ break;
- append_event_unlocked(ev);
+ 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);
+ free(buf);
const nframes_t oldlen = _length;
update_length(oldlen, cnt);
- if (_model) {
- if ( ! _model->currently_writing()) {
- _model->start_write();
- }
- _model->append(buf);
- }
-
- ViewDataRangeReady (buf_ptr, oldlen, cnt); /* EMIT SIGNAL */
+ ViewDataRangeReady (oldlen, cnt); /* EMIT SIGNAL */
return cnt;
}
@@ -480,6 +525,7 @@ SMFSource::mark_streaming_write_completed ()
return;
}
+ flush_header();
flush_footer();
#if 0
@@ -784,9 +830,6 @@ SMFSource::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;
@@ -810,16 +853,18 @@ SMFSource::load_model(bool lock, bool force_reload)
if (lock)
Glib::Mutex::Lock lm (_lock);
- if (_model && _model_loaded && ! force_reload) {
- assert(_model);
+ if (_model && !force_reload && !_model->empty()) {
+ //cerr << _name << " NOT reloading model " << _model.get() << " (" << _model->n_notes()
+ // << " notes)" << endl;
return;
}
if (! _model) {
- cerr << "SMFSource: Loading new model " << _model.get() << endl;
_model = boost::shared_ptr<MidiModel>(new MidiModel(_session));
+ cerr << _name << " loaded new model " << _model.get() << endl;
} else {
- cerr << "SMFSource: Reloading model " << _model.get() << endl;
+ cerr << _name << " reloading model " << _model.get()
+ << " (" << _model->n_notes() << " notes)" <<endl;
_model->clear();
}
@@ -828,10 +873,7 @@ SMFSource::load_model(bool lock, bool force_reload)
fseek(_fd, _header_size, 0);
uint64_t time = 0; /* in SMF ticks */
- jack_midi_event_t ev;
- ev.time = 0;
- ev.size = 0;
- ev.buffer = NULL;
+ MidiEvent ev;
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(
_session.engine().frame_rate());
+ uint32_t delta_t = 0;
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
- _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)
- scratch_size = ev.size;
+ if (ev.size() > scratch_size)
+ scratch_size = ev.size();
else
- ev.size = scratch_size;
+ ev.size() = scratch_size;
}
- _model->end_write(false); /* FIXME: delete stuck notes iff percussion? */
-
- free(ev.buffer);
+ _model->end_write(false);
- _model_loaded = true;
+ free(ev.buffer());
}
void
SMFSource::destroy_model()
{
- cerr << "SMFSource: Destroying model " << _model.get() << endl;
+ //cerr << _name << " destroying model " << _model.get() << endl;
_model.reset();
}