verbosity--
[ardour.git] / libs / ardour / midi_model.cc
1 /*
2  Copyright (C) 2007 Paul Davis 
3  Written by Dave Robillard, 2007
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         //cerr << "MidiModel \"" << s->name() << "\" constructed: " << this << endl;
45 }
46
47 /** Start a new command.
48  *
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.
52  */
53 MidiModel::DeltaCommand* 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         assert(is_sorted());
70         session.commit_reversible_command(cmd);
71         set_edited(true);
72 }
73
74
75 // DeltaCommand
76
77 MidiModel::DeltaCommand::DeltaCommand(boost::shared_ptr<MidiModel> m,
78                 const std::string& name)
79         : Command(name)
80         , _model(m)
81         , _name(name)
82 {
83 }
84
85 MidiModel::DeltaCommand::DeltaCommand(boost::shared_ptr<MidiModel> m,
86                 const XMLNode& node)
87         : _model(m)
88 {
89         set_state(node);
90 }
91
92 void
93 MidiModel::DeltaCommand::add(const boost::shared_ptr< Evoral::Note<TimeType> > note)
94 {
95         //cerr << "MEC: apply" << endl;
96         _removed_notes.remove(note);
97         _added_notes.push_back(note);
98 }
99
100 void
101 MidiModel::DeltaCommand::remove(const boost::shared_ptr< Evoral::Note<TimeType> > note)
102 {
103         //cerr << "MEC: remove" << endl;
104         _added_notes.remove(note);
105         _removed_notes.push_back(note);
106 }
107
108 void
109 MidiModel::DeltaCommand::operator()()
110 {
111         // This could be made much faster by using a priority_queue for added and
112         // removed notes (or sort here), and doing a single iteration over _model
113         
114         _model->write_lock();
115
116         // Store the current seek position so we can restore the read iterator
117         // after modifying the contents of the model
118         const double read_time = _model->read_time();
119
120         for (NoteList::iterator i = _added_notes.begin(); i != _added_notes.end(); ++i)
121                 _model->add_note_unlocked(*i);
122
123         for (NoteList::iterator i = _removed_notes.begin(); i != _removed_notes.end(); ++i)
124                 _model->remove_note_unlocked(*i);
125
126         _model->write_unlock();
127         // FIXME: race?
128         _model->read_seek(read_time); // restore read position
129
130         _model->ContentsChanged(); /* EMIT SIGNAL */
131 }
132
133 void
134 MidiModel::DeltaCommand::undo()
135 {
136         // This could be made much faster by using a priority_queue for added and
137         // removed notes (or sort here), and doing a single iteration over _model
138         
139         _model->write_lock();
140
141         // Store the current seek position so we can restore the read iterator
142         // after modifying the contents of the model
143         const double read_time = _model->read_time();
144
145         for (NoteList::iterator i = _added_notes.begin(); i != _added_notes.end(); ++i)
146                 _model->remove_note_unlocked(*i);
147
148         for (NoteList::iterator i = _removed_notes.begin(); i != _removed_notes.end(); ++i)
149                 _model->add_note_unlocked(*i);
150
151         _model->write_unlock();
152         // FIXME: race?
153         _model->read_seek(read_time); // restore read position
154
155         _model->ContentsChanged(); /* EMIT SIGNAL */
156 }
157
158 XMLNode&
159 MidiModel::DeltaCommand::marshal_note(const boost::shared_ptr< Evoral::Note<TimeType> > note)
160 {
161         XMLNode *xml_note = new XMLNode("note");
162         ostringstream note_str(ios::ate);
163         note_str << int(note->note());
164         xml_note->add_property("note", note_str.str());
165
166         ostringstream channel_str(ios::ate);
167         channel_str << int(note->channel());
168         xml_note->add_property("channel", channel_str.str());
169
170         ostringstream time_str(ios::ate);
171         time_str << int(note->time());
172         xml_note->add_property("time", time_str.str());
173
174         ostringstream length_str(ios::ate);
175         length_str <<(unsigned int) note->length();
176         xml_note->add_property("length", length_str.str());
177
178         ostringstream velocity_str(ios::ate);
179         velocity_str << (unsigned int) note->velocity();
180         xml_note->add_property("velocity", velocity_str.str());
181
182         return *xml_note;
183 }
184
185 boost::shared_ptr< Evoral::Note<double> >
186 MidiModel::DeltaCommand::unmarshal_note(XMLNode *xml_note)
187 {
188         unsigned int note;
189         XMLProperty* prop;
190         unsigned int channel;
191         unsigned int time;
192         unsigned int length;
193         unsigned int velocity;
194
195         if ((prop = xml_note->property("note")) != 0) {
196                 istringstream note_str(prop->value());
197                 note_str >> note;
198         } else {
199                 warning << "note information missing note value" << endmsg;
200                 note = 127;
201         }
202
203         if ((prop = xml_note->property("channel")) != 0) {
204                 istringstream channel_str(prop->value());
205                 channel_str >> channel;
206         } else {
207                 warning << "note information missing channel" << endmsg;
208                 channel = 0;
209         }
210
211         if ((prop = xml_note->property("time")) != 0) {
212                 istringstream time_str(prop->value());
213                 time_str >> time;
214         } else {
215                 warning << "note information missing time" << endmsg;
216                 time = 0;
217         }
218
219         if ((prop = xml_note->property("length")) != 0) {
220                 istringstream length_str(prop->value());
221                 length_str >> length;
222         } else {
223                 warning << "note information missing length" << endmsg;
224                 note = 1;
225         }
226
227         if ((prop = xml_note->property("velocity")) != 0) {
228                 istringstream velocity_str(prop->value());
229                 velocity_str >> velocity;
230         } else {
231                 warning << "note information missing velocity" << endmsg;
232                 velocity = 127;
233         }
234
235         boost::shared_ptr< Evoral::Note<TimeType> > note_ptr(new Evoral::Note<TimeType>(
236                         channel, time, length, note, velocity));
237         return note_ptr;
238 }
239
240 #define ADDED_NOTES_ELEMENT "added_notes"
241 #define REMOVED_NOTES_ELEMENT "removed_notes"
242 #define DELTA_COMMAND_ELEMENT "DeltaCommand"
243
244 int
245 MidiModel::DeltaCommand::set_state(const XMLNode& delta_command)
246 {
247         if (delta_command.name() != string(DELTA_COMMAND_ELEMENT)) {
248                 return 1;
249         }
250
251         _added_notes.clear();
252         XMLNode *added_notes = delta_command.child(ADDED_NOTES_ELEMENT);
253         XMLNodeList notes = added_notes->children();
254         transform(notes.begin(), notes.end(), back_inserter(_added_notes),
255                         sigc::mem_fun(*this, &DeltaCommand::unmarshal_note));
256
257         _removed_notes.clear();
258         XMLNode *removed_notes = delta_command.child(REMOVED_NOTES_ELEMENT);
259         notes = removed_notes->children();
260         transform(notes.begin(), notes.end(), back_inserter(_removed_notes),
261                         sigc::mem_fun(*this, &DeltaCommand::unmarshal_note));
262
263         return 0;
264 }
265
266 XMLNode&
267 MidiModel::DeltaCommand::get_state()
268 {
269         XMLNode *delta_command = new XMLNode(DELTA_COMMAND_ELEMENT);
270         delta_command->add_property("midi-source", _model->midi_source()->id().to_s());
271
272         XMLNode *added_notes = delta_command->add_child(ADDED_NOTES_ELEMENT);
273         for_each(_added_notes.begin(), _added_notes.end(), sigc::compose(
274                         sigc::mem_fun(*added_notes, &XMLNode::add_child_nocopy),
275                         sigc::mem_fun(*this, &DeltaCommand::marshal_note)));
276
277         XMLNode *removed_notes = delta_command->add_child(REMOVED_NOTES_ELEMENT);
278         for_each(_removed_notes.begin(), _removed_notes.end(), sigc::compose(
279                         sigc::mem_fun(*removed_notes, &XMLNode::add_child_nocopy),
280                         sigc::mem_fun(*this, &DeltaCommand::marshal_note)));
281
282         return *delta_command;
283 }
284
285 /** Write the model to a MidiSource (i.e. save the model).
286  * This is different from manually using read to write to a source in that
287  * note off events are written regardless of the track mode.  This is so the
288  * user can switch a recorded track (with note durations from some instrument)
289  * to percussive, save, reload, then switch it back to sustained without
290  * destroying the original note durations.
291  */
292 bool
293 MidiModel::write_to(boost::shared_ptr<MidiSource> source)
294 {
295         read_lock();
296
297         const bool old_percussive = percussive();
298         set_percussive(false);
299         
300         for (Evoral::Sequence<TimeType>::const_iterator i = begin(); i != end(); ++i) {
301                 source->append_event_unlocked(Frames, *i);
302         }
303                 
304         set_percussive(old_percussive);
305         
306         read_unlock();
307         set_edited(false);
308
309         return true;
310 }
311
312 XMLNode&
313 MidiModel::get_state()
314 {
315         XMLNode *node = new XMLNode("MidiModel");
316         return *node;
317 }
318