2 Copyright (C) 2007 Paul Davis
3 Written by Dave Robillard, 2007
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #define __STDC_LIMIT_MACROS 1
27 #include <pbd/error.h>
28 #include <pbd/enumwriter.h>
29 #include <midi++/events.h>
31 #include <ardour/midi_model.h>
32 #include <ardour/midi_source.h>
33 #include <ardour/types.h>
34 #include <ardour/session.h>
37 using namespace ARDOUR;
40 MidiModel::MidiModel(MidiSource *s, size_t size)
41 : AutomatableSequence<TimeType>(s->session(), size)
44 //cerr << "MidiModel \"" << s->name() << "\" constructed: " << this << endl;
47 /** Start a new command.
49 * This has no side-effects on the model or Session, the returned command
50 * can be held on to for as long as the caller wishes, or discarded without
51 * formality, until apply_command is called and ownership is taken.
53 MidiModel::DeltaCommand* MidiModel::new_delta_command(const string name)
55 DeltaCommand* cmd = new DeltaCommand(_midi_source->model(), name);
61 * Ownership of cmd is taken, it must not be deleted by the caller.
62 * The command will constitute one item on the undo stack.
65 MidiModel::apply_command(Session& session, Command* cmd)
67 session.begin_reversible_command(cmd->name());
69 session.commit_reversible_command(cmd);
76 MidiModel::DeltaCommand::DeltaCommand(boost::shared_ptr<MidiModel> m,
77 const std::string& name)
84 MidiModel::DeltaCommand::DeltaCommand(boost::shared_ptr<MidiModel> m,
92 MidiModel::DeltaCommand::add(const boost::shared_ptr< Evoral::Note<TimeType> > note)
94 //cerr << "MEC: apply" << endl;
95 _removed_notes.remove(note);
96 _added_notes.push_back(note);
100 MidiModel::DeltaCommand::remove(const boost::shared_ptr< Evoral::Note<TimeType> > note)
102 //cerr << "MEC: remove" << endl;
103 _added_notes.remove(note);
104 _removed_notes.push_back(note);
108 MidiModel::DeltaCommand::operator()()
110 // This could be made much faster by using a priority_queue for added and
111 // removed notes (or sort here), and doing a single iteration over _model
113 _model->write_lock();
115 // Store the current seek position so we can restore the read iterator
116 // after modifying the contents of the model
117 const TimeType read_time = _model->read_time();
119 for (NoteList::iterator i = _added_notes.begin(); i != _added_notes.end(); ++i)
120 _model->add_note_unlocked(*i);
122 for (NoteList::iterator i = _removed_notes.begin(); i != _removed_notes.end(); ++i)
123 _model->remove_note_unlocked(*i);
125 _model->write_unlock();
127 _model->read_seek(read_time); // restore read position
129 _model->ContentsChanged(); /* EMIT SIGNAL */
133 MidiModel::DeltaCommand::undo()
135 // This could be made much faster by using a priority_queue for added and
136 // removed notes (or sort here), and doing a single iteration over _model
138 _model->write_lock();
140 // Store the current seek position so we can restore the read iterator
141 // after modifying the contents of the model
142 const TimeType read_time = _model->read_time();
144 for (NoteList::iterator i = _added_notes.begin(); i != _added_notes.end(); ++i)
145 _model->remove_note_unlocked(*i);
147 for (NoteList::iterator i = _removed_notes.begin(); i != _removed_notes.end(); ++i)
148 _model->add_note_unlocked(*i);
150 _model->write_unlock();
152 _model->read_seek(read_time); // restore read position
154 _model->ContentsChanged(); /* EMIT SIGNAL */
158 MidiModel::DeltaCommand::marshal_note(const boost::shared_ptr< Evoral::Note<TimeType> > note)
160 XMLNode *xml_note = new XMLNode("note");
161 ostringstream note_str(ios::ate);
162 note_str << int(note->note());
163 xml_note->add_property("note", note_str.str());
165 ostringstream channel_str(ios::ate);
166 channel_str << int(note->channel());
167 xml_note->add_property("channel", channel_str.str());
169 ostringstream time_str(ios::ate);
170 time_str << int(note->time());
171 xml_note->add_property("time", time_str.str());
173 ostringstream length_str(ios::ate);
174 length_str <<(unsigned int) note->length();
175 xml_note->add_property("length", length_str.str());
177 ostringstream velocity_str(ios::ate);
178 velocity_str << (unsigned int) note->velocity();
179 xml_note->add_property("velocity", velocity_str.str());
184 boost::shared_ptr< Evoral::Note<MidiModel::TimeType> >
185 MidiModel::DeltaCommand::unmarshal_note(XMLNode *xml_note)
189 unsigned int channel;
192 unsigned int velocity;
194 if ((prop = xml_note->property("note")) != 0) {
195 istringstream note_str(prop->value());
198 warning << "note information missing note value" << endmsg;
202 if ((prop = xml_note->property("channel")) != 0) {
203 istringstream channel_str(prop->value());
204 channel_str >> channel;
206 warning << "note information missing channel" << endmsg;
210 if ((prop = xml_note->property("time")) != 0) {
211 istringstream time_str(prop->value());
214 warning << "note information missing time" << endmsg;
218 if ((prop = xml_note->property("length")) != 0) {
219 istringstream length_str(prop->value());
220 length_str >> length;
222 warning << "note information missing length" << endmsg;
226 if ((prop = xml_note->property("velocity")) != 0) {
227 istringstream velocity_str(prop->value());
228 velocity_str >> velocity;
230 warning << "note information missing velocity" << endmsg;
234 boost::shared_ptr< Evoral::Note<TimeType> > note_ptr(new Evoral::Note<TimeType>(
235 channel, time, length, note, velocity));
239 #define ADDED_NOTES_ELEMENT "added_notes"
240 #define REMOVED_NOTES_ELEMENT "removed_notes"
241 #define DELTA_COMMAND_ELEMENT "DeltaCommand"
244 MidiModel::DeltaCommand::set_state(const XMLNode& delta_command)
246 if (delta_command.name() != string(DELTA_COMMAND_ELEMENT)) {
250 _added_notes.clear();
251 XMLNode *added_notes = delta_command.child(ADDED_NOTES_ELEMENT);
252 XMLNodeList notes = added_notes->children();
253 transform(notes.begin(), notes.end(), back_inserter(_added_notes),
254 sigc::mem_fun(*this, &DeltaCommand::unmarshal_note));
256 _removed_notes.clear();
257 XMLNode *removed_notes = delta_command.child(REMOVED_NOTES_ELEMENT);
258 notes = removed_notes->children();
259 transform(notes.begin(), notes.end(), back_inserter(_removed_notes),
260 sigc::mem_fun(*this, &DeltaCommand::unmarshal_note));
266 MidiModel::DeltaCommand::get_state()
268 XMLNode *delta_command = new XMLNode(DELTA_COMMAND_ELEMENT);
269 delta_command->add_property("midi-source", _model->midi_source()->id().to_s());
271 XMLNode *added_notes = delta_command->add_child(ADDED_NOTES_ELEMENT);
272 for_each(_added_notes.begin(), _added_notes.end(), sigc::compose(
273 sigc::mem_fun(*added_notes, &XMLNode::add_child_nocopy),
274 sigc::mem_fun(*this, &DeltaCommand::marshal_note)));
276 XMLNode *removed_notes = delta_command->add_child(REMOVED_NOTES_ELEMENT);
277 for_each(_removed_notes.begin(), _removed_notes.end(), sigc::compose(
278 sigc::mem_fun(*removed_notes, &XMLNode::add_child_nocopy),
279 sigc::mem_fun(*this, &DeltaCommand::marshal_note)));
281 return *delta_command;
284 /** Write the model to a MidiSource (i.e. save the model).
285 * This is different from manually using read to write to a source in that
286 * note off events are written regardless of the track mode. This is so the
287 * user can switch a recorded track (with note durations from some instrument)
288 * to percussive, save, reload, then switch it back to sustained without
289 * destroying the original note durations.
292 MidiModel::write_to(boost::shared_ptr<MidiSource> source)
296 const bool old_percussive = percussive();
297 set_percussive(false);
299 source->drop_model();
301 for (Evoral::Sequence<TimeType>::const_iterator i = begin(); i != end(); ++i) {
302 source->append_event_unlocked_beats(*i);
305 set_percussive(old_percussive);
314 MidiModel::get_state()
316 XMLNode *node = new XMLNode("MidiModel");