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