/*
- Copyright (C) 2007 Paul Davis
- Written by Dave Robillard, 2007
+ Copyright (C) 2007 Paul Davis
+ Author: Dave Robillard
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
*/
+#define __STDC_LIMIT_MACROS 1
+
#include <iostream>
#include <algorithm>
-#include <pbd/enumwriter.h>
-#include <ardour/midi_model.h>
-#include <ardour/midi_events.h>
-#include <ardour/types.h>
-#include <ardour/session.h>
+#include <stdexcept>
+#include <stdint.h>
+#include "pbd/error.h"
+#include "pbd/enumwriter.h"
+#include "midi++/events.h"
+
+#include "ardour/midi_model.h"
+#include "ardour/midi_source.h"
+#include "ardour/types.h"
+#include "ardour/session.h"
using namespace std;
using namespace ARDOUR;
+using namespace PBD;
-// Note
-
-MidiModel::Note::Note(double t, double d, uint8_t n, uint8_t v)
- : _on_event(t, 3, NULL, true)
- , _off_event(t + d, 3, NULL, true)
+MidiModel::MidiModel(MidiSource* s, size_t size)
+ : AutomatableSequence<TimeType>(s->session(), size)
+ , _midi_source(s)
{
- _on_event.buffer()[0] = MIDI_CMD_NOTE_ON;
- _on_event.buffer()[1] = n;
- _on_event.buffer()[2] = v;
-
- _off_event.buffer()[0] = MIDI_CMD_NOTE_OFF;
- _off_event.buffer()[1] = n;
- _off_event.buffer()[2] = 0x40;
-
- assert(time() == t);
- assert(duration() == d);
- assert(note() == n);
- assert(velocity() == v);
}
-
-MidiModel::Note::Note(const Note& copy)
- : _on_event(copy._on_event, true)
- , _off_event(copy._off_event, true)
+/** Start a new command.
+ *
+ * This has no side-effects on the model or Session, the returned command
+ * can be held on to for as long as the caller wishes, or discarded without
+ * formality, until apply_command is called and ownership is taken.
+ */
+MidiModel::DeltaCommand*
+MidiModel::new_delta_command(const string name)
{
- /*
- assert(copy._on_event.size == 3);
- _on_event.buffer = _on_event_buffer;
- memcpy(_on_event_buffer, copy._on_event_buffer, 3);
-
- assert(copy._off_event.size == 3);
- _off_event.buffer = _off_event_buffer;
- memcpy(_off_event_buffer, copy._off_event_buffer, 3);
- */
-
- assert(time() == copy.time());
- assert(end_time() == copy.end_time());
- assert(note() == copy.note());
- assert(velocity() == copy.velocity());
- assert(duration() == copy.duration());
+ DeltaCommand* cmd = new DeltaCommand(_midi_source->model(), name);
+ return cmd;
}
-
-const MidiModel::Note&
-MidiModel::Note::operator=(const Note& copy)
+/** Apply a command.
+ *
+ * Ownership of cmd is taken, it must not be deleted by the caller.
+ * The command will constitute one item on the undo stack.
+ */
+void
+MidiModel::apply_command(Session& session, Command* cmd)
{
- _on_event = copy._on_event;
- _off_event = copy._off_event;
- /*_on_event.time = copy._on_event.time;
- assert(copy._on_event.size == 3);
- memcpy(_on_event_buffer, copy._on_event_buffer, 3);
-
- _off_event.time = copy._off_event.time;
- assert(copy._off_event.size == 3);
- memcpy(_off_event_buffer, copy._off_event_buffer, 3);
- */
-
- assert(time() == copy.time());
- assert(end_time() == copy.end_time());
- assert(note() == copy.note());
- assert(velocity() == copy.velocity());
- assert(duration() == copy.duration());
-
- return *this;
+ session.begin_reversible_command(cmd->name());
+ (*cmd)();
+ session.commit_reversible_command(cmd);
+ set_edited(true);
}
-// MidiModel
-MidiModel::MidiModel(Session& s, size_t size)
- : _session(s)
- , _notes(size)
- , _note_mode(Sustained)
- , _writing(false)
- , _active_notes(LaterNoteEndComparator())
+// DeltaCommand
+
+MidiModel::DeltaCommand::DeltaCommand(boost::shared_ptr<MidiModel> m, const std::string& name)
+ : Command(name)
+ , _model(m)
+ , _name(name)
{
+ assert(_model);
}
-
-/** Read events in frame range \a start .. \a start+cnt into \a dst,
- * adding \a stamp_offset to each event's timestamp.
- * \return number of events written to \a dst
- */
-size_t
-MidiModel::read (MidiRingBuffer& dst, nframes_t start, nframes_t nframes, nframes_t stamp_offset) const
+MidiModel::DeltaCommand::DeltaCommand(boost::shared_ptr<MidiModel> m, const XMLNode& node)
+ : _model(m)
{
- size_t read_events = 0;
-
- //cerr << "MM READ @ " << start << " + " << nframes << endl;
-
- /* FIXME: cache last lookup value to avoid the search */
-
- if (_note_mode == Sustained) {
-
- /* FIXME: cache last lookup value to avoid the search */
- for (Notes::const_iterator n = _notes.begin(); n != _notes.end(); ++n) {
-
- //cerr << "MM ON " << n->time() << endl;
-
- while ( ! _active_notes.empty() ) {
- const Note* const earliest_off = _active_notes.top();
- const MidiEvent& ev = earliest_off->off_event();
- if (ev.time() < start + nframes && ev.time() <= n->time()) {
- dst.write(ev.time() + stamp_offset, ev.size(), ev.buffer());
- _active_notes.pop();
- ++read_events;
- } else {
- break;
- }
- }
-
- if (n->time() >= start + nframes)
- break;
-
- // Note on
- if (n->time() >= start) {
- const MidiEvent& ev = n->on_event();
- dst.write(ev.time() + stamp_offset, ev.size(), ev.buffer());
- _active_notes.push(&(*n));
- ++read_events;
- }
-
- }
-
- // Percussive
- } else {
- for (Notes::const_iterator n = _notes.begin(); n != _notes.end(); ++n) {
- // Note on
- if (n->time() >= start) {
- if (n->time() < start + nframes) {
- const MidiEvent& ev = n->on_event();
- dst.write(ev.time() + stamp_offset, ev.size(), ev.buffer());
- ++read_events;
- } else {
- break;
- }
- }
- }
- }
-
- //if (read_events > 0)
- // cerr << "MM READ " << read_events << " EVENTS" << endl;
-
- return read_events;
+ assert(_model);
+ set_state(node);
}
-
-/** Begin a write of events to the model.
- *
- * If \a mode is Sustained, complete notes with duration are constructed as note
- * on/off events are received. Otherwise (Percussive), only note on events are
- * stored; note off events are discarded entirely and all contained notes will
- * have duration 0.
- */
void
-MidiModel::start_write()
+MidiModel::DeltaCommand::add(const boost::shared_ptr< Evoral::Note<TimeType> > note)
{
- //cerr << "MM START WRITE, MODE = " << enum_2_string(_note_mode) << endl;
- _write_notes.clear();
- _writing = true;
+ _removed_notes.remove(note);
+ _added_notes.push_back(note);
}
+void
+MidiModel::DeltaCommand::remove(const boost::shared_ptr< Evoral::Note<TimeType> > note)
+{
+ _added_notes.remove(note);
+ _removed_notes.push_back(note);
+}
-
-/** Finish a write of events to the model.
- *
- * If \a delete_stuck is true and the current mode is Sustained, note on events
- * that were never resolved with a corresonding note off will be deleted.
- * Otherwise they will remain as notes with duration 0.
- */
void
-MidiModel::end_write(bool delete_stuck)
+MidiModel::DeltaCommand::operator()()
{
- assert(_writing);
+ // This could be made much faster by using a priority_queue for added and
+ // removed notes (or sort here), and doing a single iteration over _model
- //cerr << "MM END WRITE\n";
-
- if (_note_mode == Sustained && delete_stuck) {
- for (Notes::iterator n = _notes.begin(); n != _notes.end() ; ) {
- if (n->duration() == 0) {
- cerr << "WARNING: Stuck note lost: " << n->note() << endl;
- n = _notes.erase(n);
- } else {
- ++n;
- }
- }
- }
+ Glib::Mutex::Lock lm (_model->_midi_source->mutex());
+ _model->_midi_source->invalidate(); // release model read lock
+ _model->write_lock();
- _write_notes.clear();
- _writing = false;
-}
+ for (NoteList::iterator i = _added_notes.begin(); i != _added_notes.end(); ++i)
+ _model->add_note_unlocked(*i);
+ for (NoteList::iterator i = _removed_notes.begin(); i != _removed_notes.end(); ++i)
+ _model->remove_note_unlocked(*i);
+
+ _model->write_unlock();
+ _model->ContentsChanged(); /* EMIT SIGNAL */
+}
-/** Append contents of \a buf to model. NOT realtime safe.
- *
- * Timestamps of events in \a buf are expected to be relative to
- * the start of this model (t=0) and MUST be monotonically increasing
- * and MUST be >= the latest event currently in the model.
- *
- * Events in buf are deep copied.
- */
void
-MidiModel::append(const MidiBuffer& buf)
-{
- assert(_writing);
+MidiModel::DeltaCommand::undo()
+{
+ // This could be made much faster by using a priority_queue for added and
+ // removed notes (or sort here), and doing a single iteration over _model
+
+ Glib::Mutex::Lock lm (_model->_midi_source->mutex());
+ _model->_midi_source->invalidate(); // release model read lock
+ _model->write_lock();
- for (MidiBuffer::const_iterator i = buf.begin(); i != buf.end(); ++i) {
- const MidiEvent& ev = *i;
-
- assert(_notes.empty() || ev.time() >= _notes.back().time());
+ for (NoteList::iterator i = _added_notes.begin(); i != _added_notes.end(); ++i)
+ _model->remove_note_unlocked(*i);
- 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());
- }
-}
+ for (NoteList::iterator i = _removed_notes.begin(); i != _removed_notes.end(); ++i)
+ _model->add_note_unlocked(*i);
+ _model->write_unlock();
+ _model->ContentsChanged(); /* EMIT SIGNAL */
+}
-/** Append \a in_event to model. NOT realtime safe.
- *
- * Timestamps of events in \a buf are expected to be relative to
- * the start of this model (t=0) and MUST be monotonically increasing
- * and MUST be >= the latest event currently in the model.
- */
-void
-MidiModel::append(double time, size_t size, const Byte* buf)
+XMLNode&
+MidiModel::DeltaCommand::marshal_note(const boost::shared_ptr< Evoral::Note<TimeType> > note)
{
- assert(_notes.empty() || time >= _notes.back().time());
- assert(_writing);
+ XMLNode* xml_note = new XMLNode("note");
+ ostringstream note_str(ios::ate);
+ note_str << int(note->note());
+ xml_note->add_property("note", note_str.str());
- 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]);
-}
+ ostringstream channel_str(ios::ate);
+ channel_str << int(note->channel());
+ xml_note->add_property("channel", channel_str.str());
+ ostringstream time_str(ios::ate);
+ time_str << int(note->time());
+ xml_note->add_property("time", time_str.str());
-void
-MidiModel::append_note_on(double time, uint8_t note_num, uint8_t velocity)
+ ostringstream length_str(ios::ate);
+ length_str <<(unsigned int) note->length();
+ xml_note->add_property("length", length_str.str());
+
+ ostringstream velocity_str(ios::ate);
+ velocity_str << (unsigned int) note->velocity();
+ xml_note->add_property("velocity", velocity_str.str());
+
+ return *xml_note;
+}
+
+boost::shared_ptr< Evoral::Note<MidiModel::TimeType> >
+MidiModel::DeltaCommand::unmarshal_note(XMLNode *xml_note)
{
- assert(_writing);
- _notes.push_back(Note(time, 0, note_num, velocity));
- if (_note_mode == Sustained) {
- //cerr << "MM Appending note on " << (unsigned)(uint8_t)note_num << endl;
- _write_notes.push_back(_notes.size() - 1);
+ unsigned int note;
+ XMLProperty* prop;
+ unsigned int channel;
+ unsigned int time;
+ unsigned int length;
+ unsigned int velocity;
+
+ if ((prop = xml_note->property("note")) != 0) {
+ istringstream note_str(prop->value());
+ note_str >> note;
} else {
- //cerr << "MM NOT appending note on" << endl;
+ warning << "note information missing note value" << endmsg;
+ note = 127;
}
-}
+ if ((prop = xml_note->property("channel")) != 0) {
+ istringstream channel_str(prop->value());
+ channel_str >> channel;
+ } else {
+ warning << "note information missing channel" << endmsg;
+ channel = 0;
+ }
-void
-MidiModel::append_note_off(double time, uint8_t note_num)
-{
- assert(_writing);
- if (_note_mode == Percussive) {
- //cerr << "MM Ignoring note off (percussive mode)" << endl;
- return;
+ if ((prop = xml_note->property("time")) != 0) {
+ istringstream time_str(prop->value());
+ time_str >> time;
} else {
- //cerr << "MM Attempting to resolve note off " << (unsigned)(uint8_t)note_num << endl;
+ warning << "note information missing time" << endmsg;
+ time = 0;
}
- /* FIXME: make _write_notes fixed size (127 noted) for speed */
-
- /* FIXME: note off velocity for that one guy out there who actually has
- * keys that send it */
-
- for (WriteNotes::iterator n = _write_notes.begin(); n != _write_notes.end(); ++n) {
- Note& note = _notes[*n];
- //cerr << (unsigned)(uint8_t)note.note() << " ? " << (unsigned)note_num << endl;
- if (note.note() == note_num) {
- assert(time > note.time());
- note.set_duration(time - note.time());
- _write_notes.erase(n);
- //cerr << "MidiModel resolved note, duration: " << note.duration() << endl;
- break;
- }
+ if ((prop = xml_note->property("length")) != 0) {
+ istringstream length_str(prop->value());
+ length_str >> length;
+ } else {
+ warning << "note information missing length" << endmsg;
+ note = 1;
}
-}
+ if ((prop = xml_note->property("velocity")) != 0) {
+ istringstream velocity_str(prop->value());
+ velocity_str >> velocity;
+ } else {
+ warning << "note information missing velocity" << endmsg;
+ velocity = 127;
+ }
-void
-MidiModel::add_note(const Note& note)
-{
- Notes::iterator i = upper_bound(_notes.begin(), _notes.end(), note, note_time_comparator);
- _notes.insert(i, note);
+ boost::shared_ptr< Evoral::Note<TimeType> > note_ptr(new Evoral::Note<TimeType>(
+ channel, time, length, note, velocity));
+ return note_ptr;
}
+#define ADDED_NOTES_ELEMENT "added_notes"
+#define REMOVED_NOTES_ELEMENT "removed_notes"
+#define DELTA_COMMAND_ELEMENT "DeltaCommand"
-void
-MidiModel::remove_note(const Note& note)
+int
+MidiModel::DeltaCommand::set_state(const XMLNode& delta_command)
{
- Notes::iterator n = find(_notes.begin(), _notes.end(), note);
- if (n != _notes.end())
- _notes.erase(n);
-}
-
-/** Slow! for debugging only. */
-bool
-MidiModel::is_sorted() const
-{
- bool t = 0;
- for (Notes::const_iterator n = _notes.begin(); n != _notes.end(); ++n)
- if (n->time() < t)
- return false;
- else
- t = n->time();
+ if (delta_command.name() != string(DELTA_COMMAND_ELEMENT)) {
+ return 1;
+ }
- return true;
-}
+ _added_notes.clear();
+ XMLNode* added_notes = delta_command.child(ADDED_NOTES_ELEMENT);
+ XMLNodeList notes = added_notes->children();
+ transform(notes.begin(), notes.end(), back_inserter(_added_notes),
+ sigc::mem_fun(*this, &DeltaCommand::unmarshal_note));
+ _removed_notes.clear();
+ XMLNode* removed_notes = delta_command.child(REMOVED_NOTES_ELEMENT);
+ notes = removed_notes->children();
+ transform(notes.begin(), notes.end(), back_inserter(_removed_notes),
+ sigc::mem_fun(*this, &DeltaCommand::unmarshal_note));
-/** Start a new command.
- *
- * This has no side-effects on the model or Session, the returned command
- * can be held on to for as long as the caller wishes, or discarded without
- * formality, until apply_command is called and ownership is taken.
- */
-MidiModel::DeltaCommand*
-MidiModel::new_delta_command(const string name)
-{
- DeltaCommand* cmd = new DeltaCommand(*this, name);
- return cmd;
+ return 0;
}
-
-/** Apply a command.
- *
- * Ownership of cmd is taken, it must not be deleted by the caller.
- * The command will constitute one item on the undo stack.
- */
-void
-MidiModel::apply_command(Command* cmd)
+XMLNode&
+MidiModel::DeltaCommand::get_state()
{
- _session.begin_reversible_command(cmd->name());
- (*cmd)();
- assert(is_sorted());
- _session.commit_reversible_command(cmd);
-}
-
+ XMLNode* delta_command = new XMLNode(DELTA_COMMAND_ELEMENT);
+ delta_command->add_property("midi-source", _model->midi_source()->id().to_s());
-// MidiEditCommand
+ XMLNode* added_notes = delta_command->add_child(ADDED_NOTES_ELEMENT);
+ for_each(_added_notes.begin(), _added_notes.end(), sigc::compose(
+ sigc::mem_fun(*added_notes, &XMLNode::add_child_nocopy),
+ sigc::mem_fun(*this, &DeltaCommand::marshal_note)));
+ XMLNode* removed_notes = delta_command->add_child(REMOVED_NOTES_ELEMENT);
+ for_each(_removed_notes.begin(), _removed_notes.end(), sigc::compose(
+ sigc::mem_fun(*removed_notes, &XMLNode::add_child_nocopy),
+ sigc::mem_fun(*this, &DeltaCommand::marshal_note)));
-void
-MidiModel::DeltaCommand::add(const Note& note)
-{
- //cerr << "MEC: apply" << endl;
-
- _removed_notes.remove(note);
- _added_notes.push_back(note);
+ return *delta_command;
}
-
-void
-MidiModel::DeltaCommand::remove(const Note& note)
+/** Write the model to a MidiSource (i.e. save the model).
+ * This is different from manually using read to write to a source in that
+ * note off events are written regardless of the track mode. This is so the
+ * user can switch a recorded track (with note durations from some instrument)
+ * to percussive, save, reload, then switch it back to sustained without
+ * destroying the original note durations.
+ */
+bool
+MidiModel::write_to(boost::shared_ptr<MidiSource> source)
{
- //cerr << "MEC: remove" << endl;
+ read_lock();
- _added_notes.remove(note);
- _removed_notes.push_back(note);
-}
+ const bool old_percussive = percussive();
+ set_percussive(false);
-
-void
-MidiModel::DeltaCommand::operator()()
-{
- // This could be made much faster by using a priority_queue for added and
- // removed notes (or sort here), and doing a single iteration over _model
-
- for (std::list<Note>::iterator i = _added_notes.begin(); i != _added_notes.end(); ++i)
- _model.add_note(*i);
+ source->drop_model();
- for (std::list<Note>::iterator i = _removed_notes.begin(); i != _removed_notes.end(); ++i)
- _model.remove_note(*i);
+ for (Evoral::Sequence<TimeType>::const_iterator i = begin(); i != end(); ++i) {
+ source->append_event_unlocked_beats(*i);
+ }
+
+ set_percussive(old_percussive);
- _model.ContentsChanged(); /* EMIT SIGNAL */
-}
+ read_unlock();
+ set_edited(false);
+ return true;
+}
-void
-MidiModel::DeltaCommand::undo()
+XMLNode&
+MidiModel::get_state()
{
- // This could be made much faster by using a priority_queue for added and
- // removed notes (or sort here), and doing a single iteration over _model
-
- for (std::list<Note>::iterator i = _added_notes.begin(); i != _added_notes.end(); ++i)
- _model.remove_note(*i);
-
- for (std::list<Note>::iterator i = _removed_notes.begin(); i != _removed_notes.end(); ++i)
- _model.add_note(*i);
-
- _model.ContentsChanged(); /* EMIT SIGNAL */
+ XMLNode *node = new XMLNode("MidiModel");
+ return *node;
}