e7eaf64d32eab3524ef87797c67892bea3af4c90
[ardour.git] / libs / ardour / midi_model.cc
1 /*
2     Copyright (C) 2007 Paul Davis
3     Author: Dave Robillard
4
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.
9
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.
14
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.
18
19 */
20
21 #define __STDC_LIMIT_MACROS 1
22
23 #include <iostream>
24 #include <algorithm>
25 #include <stdexcept>
26 #include <stdint.h>
27 #include <pbd/error.h>
28 #include <pbd/enumwriter.h>
29 #include <midi++/events.h>
30
31 #include <ardour/midi_model.h>
32 #include <ardour/midi_source.h>
33 #include <ardour/types.h>
34 #include <ardour/session.h>
35
36 using namespace std;
37 using namespace ARDOUR;
38 using namespace PBD;
39
40 MidiModel::MidiModel(MidiSource* s, size_t size)
41         : AutomatableSequence<TimeType>(s->session(), size)
42         , _midi_source(s)
43 {
44 }
45
46 /** Start a new command.
47  *
48  * This has no side-effects on the model or Session, the returned command
49  * can be held on to for as long as the caller wishes, or discarded without
50  * formality, until apply_command is called and ownership is taken.
51  */
52 MidiModel::DeltaCommand*
53 MidiModel::new_delta_command(const string name)
54 {
55         DeltaCommand* cmd = new DeltaCommand(_midi_source->model(), name);
56         return cmd;
57 }
58
59 /** Apply a command.
60  *
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.
63  */
64 void
65 MidiModel::apply_command(Session& session, Command* cmd)
66 {
67         session.begin_reversible_command(cmd->name());
68         (*cmd)();
69         session.commit_reversible_command(cmd);
70         set_edited(true);
71 }
72
73
74 // DeltaCommand
75
76 MidiModel::DeltaCommand::DeltaCommand(boost::shared_ptr<MidiModel> m, const std::string& name)
77         : Command(name)
78         , _model(m)
79         , _name(name)
80 {
81 }
82
83 MidiModel::DeltaCommand::DeltaCommand(boost::shared_ptr<MidiModel> m, const XMLNode& node)
84         : _model(m)
85 {
86         set_state(node);
87 }
88
89 void
90 MidiModel::DeltaCommand::add(const boost::shared_ptr< Evoral::Note<TimeType> > note)
91 {
92         _removed_notes.remove(note);
93         _added_notes.push_back(note);
94 }
95
96 void
97 MidiModel::DeltaCommand::remove(const boost::shared_ptr< Evoral::Note<TimeType> > note)
98 {
99         _added_notes.remove(note);
100         _removed_notes.push_back(note);
101 }
102
103 void
104 MidiModel::DeltaCommand::operator()()
105 {
106         // This could be made much faster by using a priority_queue for added and
107         // removed notes (or sort here), and doing a single iteration over _model
108         
109         Glib::Mutex::Lock lm (_model->_midi_source->mutex());
110         _model->_midi_source->invalidate(); // release model read lock
111         _model->write_lock();
112
113         for (NoteList::iterator i = _added_notes.begin(); i != _added_notes.end(); ++i)
114                 _model->add_note_unlocked(*i);
115
116         for (NoteList::iterator i = _removed_notes.begin(); i != _removed_notes.end(); ++i)
117                 _model->remove_note_unlocked(*i);
118
119         _model->write_unlock();
120         _model->ContentsChanged(); /* EMIT SIGNAL */
121 }
122
123 void
124 MidiModel::DeltaCommand::undo()
125 {
126         // This could be made much faster by using a priority_queue for added and
127         // removed notes (or sort here), and doing a single iteration over _model
128         
129         Glib::Mutex::Lock lm (_model->_midi_source->mutex());
130         _model->_midi_source->invalidate(); // release model read lock
131         _model->write_lock();
132
133         for (NoteList::iterator i = _added_notes.begin(); i != _added_notes.end(); ++i)
134                 _model->remove_note_unlocked(*i);
135
136         for (NoteList::iterator i = _removed_notes.begin(); i != _removed_notes.end(); ++i)
137                 _model->add_note_unlocked(*i);
138
139         _model->write_unlock();
140         _model->ContentsChanged(); /* EMIT SIGNAL */
141 }
142
143 XMLNode&
144 MidiModel::DeltaCommand::marshal_note(const boost::shared_ptr< Evoral::Note<TimeType> > note)
145 {
146         XMLNode* xml_note = new XMLNode("note");
147         ostringstream note_str(ios::ate);
148         note_str << int(note->note());
149         xml_note->add_property("note", note_str.str());
150
151         ostringstream channel_str(ios::ate);
152         channel_str << int(note->channel());
153         xml_note->add_property("channel", channel_str.str());
154
155         ostringstream time_str(ios::ate);
156         time_str << int(note->time());
157         xml_note->add_property("time", time_str.str());
158
159         ostringstream length_str(ios::ate);
160         length_str <<(unsigned int) note->length();
161         xml_note->add_property("length", length_str.str());
162
163         ostringstream velocity_str(ios::ate);
164         velocity_str << (unsigned int) note->velocity();
165         xml_note->add_property("velocity", velocity_str.str());
166
167         return *xml_note;
168 }
169
170 boost::shared_ptr< Evoral::Note<MidiModel::TimeType> >
171 MidiModel::DeltaCommand::unmarshal_note(XMLNode *xml_note)
172 {
173         unsigned int note;
174         XMLProperty* prop;
175         unsigned int channel;
176         unsigned int time;
177         unsigned int length;
178         unsigned int velocity;
179
180         if ((prop = xml_note->property("note")) != 0) {
181                 istringstream note_str(prop->value());
182                 note_str >> note;
183         } else {
184                 warning << "note information missing note value" << endmsg;
185                 note = 127;
186         }
187
188         if ((prop = xml_note->property("channel")) != 0) {
189                 istringstream channel_str(prop->value());
190                 channel_str >> channel;
191         } else {
192                 warning << "note information missing channel" << endmsg;
193                 channel = 0;
194         }
195
196         if ((prop = xml_note->property("time")) != 0) {
197                 istringstream time_str(prop->value());
198                 time_str >> time;
199         } else {
200                 warning << "note information missing time" << endmsg;
201                 time = 0;
202         }
203
204         if ((prop = xml_note->property("length")) != 0) {
205                 istringstream length_str(prop->value());
206                 length_str >> length;
207         } else {
208                 warning << "note information missing length" << endmsg;
209                 note = 1;
210         }
211
212         if ((prop = xml_note->property("velocity")) != 0) {
213                 istringstream velocity_str(prop->value());
214                 velocity_str >> velocity;
215         } else {
216                 warning << "note information missing velocity" << endmsg;
217                 velocity = 127;
218         }
219
220         boost::shared_ptr< Evoral::Note<TimeType> > note_ptr(new Evoral::Note<TimeType>(
221                         channel, time, length, note, velocity));
222         return note_ptr;
223 }
224
225 #define ADDED_NOTES_ELEMENT "added_notes"
226 #define REMOVED_NOTES_ELEMENT "removed_notes"
227 #define DELTA_COMMAND_ELEMENT "DeltaCommand"
228
229 int
230 MidiModel::DeltaCommand::set_state(const XMLNode& delta_command)
231 {
232         if (delta_command.name() != string(DELTA_COMMAND_ELEMENT)) {
233                 return 1;
234         }
235
236         _added_notes.clear();
237         XMLNode* added_notes = delta_command.child(ADDED_NOTES_ELEMENT);
238         XMLNodeList notes = added_notes->children();
239         transform(notes.begin(), notes.end(), back_inserter(_added_notes),
240                         sigc::mem_fun(*this, &DeltaCommand::unmarshal_note));
241
242         _removed_notes.clear();
243         XMLNode* removed_notes = delta_command.child(REMOVED_NOTES_ELEMENT);
244         notes = removed_notes->children();
245         transform(notes.begin(), notes.end(), back_inserter(_removed_notes),
246                         sigc::mem_fun(*this, &DeltaCommand::unmarshal_note));
247
248         return 0;
249 }
250
251 XMLNode&
252 MidiModel::DeltaCommand::get_state()
253 {
254         XMLNode* delta_command = new XMLNode(DELTA_COMMAND_ELEMENT);
255         delta_command->add_property("midi-source", _model->midi_source()->id().to_s());
256
257         XMLNode* added_notes = delta_command->add_child(ADDED_NOTES_ELEMENT);
258         for_each(_added_notes.begin(), _added_notes.end(), sigc::compose(
259                         sigc::mem_fun(*added_notes, &XMLNode::add_child_nocopy),
260                         sigc::mem_fun(*this, &DeltaCommand::marshal_note)));
261
262         XMLNode* removed_notes = delta_command->add_child(REMOVED_NOTES_ELEMENT);
263         for_each(_removed_notes.begin(), _removed_notes.end(), sigc::compose(
264                         sigc::mem_fun(*removed_notes, &XMLNode::add_child_nocopy),
265                         sigc::mem_fun(*this, &DeltaCommand::marshal_note)));
266
267         return *delta_command;
268 }
269
270 /** Write the model to a MidiSource (i.e. save the model).
271  * This is different from manually using read to write to a source in that
272  * note off events are written regardless of the track mode.  This is so the
273  * user can switch a recorded track (with note durations from some instrument)
274  * to percussive, save, reload, then switch it back to sustained without
275  * destroying the original note durations.
276  */
277 bool
278 MidiModel::write_to(boost::shared_ptr<MidiSource> source)
279 {
280         read_lock();
281
282         const bool old_percussive = percussive();
283         set_percussive(false);
284
285         source->drop_model();
286         
287         for (Evoral::Sequence<TimeType>::const_iterator i = begin(); i != end(); ++i) {
288                 source->append_event_unlocked_beats(*i);
289         }
290                 
291         set_percussive(old_percussive);
292         
293         read_unlock();
294         set_edited(false);
295
296         return true;
297 }
298
299 XMLNode&
300 MidiModel::get_state()
301 {
302         XMLNode *node = new XMLNode("MidiModel");
303         return *node;
304 }
305