Move event specific ringbuffer stuff to evoral.
[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/enumwriter.h>
28 #include <midi++/events.h>
29
30 #include <ardour/midi_model.h>
31 #include <ardour/midi_source.h>
32 #include <ardour/types.h>
33 #include <ardour/session.h>
34
35 using namespace std;
36 using namespace ARDOUR;
37
38
39 MidiModel::MidiModel(MidiSource *s, size_t size)
40         : AutomatableSequence(s->session(), size)
41         , _midi_source(s)
42 {
43         cerr << "MidiModel \"" << s->name() << "\" constructed: " << this << endl;
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* MidiModel::new_delta_command(const string name)
53 {
54         DeltaCommand* cmd = new DeltaCommand(_midi_source->model(), name);
55         return cmd;
56 }
57
58 /** Apply a command.
59  *
60  * Ownership of cmd is taken, it must not be deleted by the caller.
61  * The command will constitute one item on the undo stack.
62  */
63 void
64 MidiModel::apply_command(Session& session, Command* cmd)
65 {
66         session.begin_reversible_command(cmd->name());
67         (*cmd)();
68         assert(is_sorted());
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,
77                 const std::string& name)
78         : Command(name)
79         , _model(m)
80         , _name(name)
81 {
82 }
83
84 MidiModel::DeltaCommand::DeltaCommand(boost::shared_ptr<MidiModel> m,
85                 const XMLNode& node)
86         : _model(m)
87 {
88         set_state(node);
89 }
90
91 void
92 MidiModel::DeltaCommand::add(const boost::shared_ptr<Evoral::Note> note)
93 {
94         //cerr << "MEC: apply" << endl;
95         _removed_notes.remove(note);
96         _added_notes.push_back(note);
97 }
98
99 void
100 MidiModel::DeltaCommand::remove(const boost::shared_ptr<Evoral::Note> note)
101 {
102         //cerr << "MEC: remove" << endl;
103         _added_notes.remove(note);
104         _removed_notes.push_back(note);
105 }
106
107 void
108 MidiModel::DeltaCommand::operator()()
109 {
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
112         
113         _model->write_lock();
114
115         // Store the current seek position so we can restore the read iterator
116         // after modifying the contents of the model
117         const double read_time = _model->read_time();
118
119         for (NoteList::iterator i = _added_notes.begin(); i != _added_notes.end(); ++i)
120                 _model->add_note_unlocked(*i);
121
122         for (NoteList::iterator i = _removed_notes.begin(); i != _removed_notes.end(); ++i)
123                 _model->remove_note_unlocked(*i);
124
125         _model->write_unlock();
126         // FIXME: race?
127         _model->read_seek(read_time); // restore read position
128
129         _model->ContentsChanged(); /* EMIT SIGNAL */
130 }
131
132 void
133 MidiModel::DeltaCommand::undo()
134 {
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
137         
138         _model->write_lock();
139
140         // Store the current seek position so we can restore the read iterator
141         // after modifying the contents of the model
142         const double read_time = _model->read_time();
143
144         for (NoteList::iterator i = _added_notes.begin(); i != _added_notes.end(); ++i)
145                 _model->remove_note_unlocked(*i);
146
147         for (NoteList::iterator i = _removed_notes.begin(); i != _removed_notes.end(); ++i)
148                 _model->add_note_unlocked(*i);
149
150         _model->write_unlock();
151         // FIXME: race?
152         _model->read_seek(read_time); // restore read position
153
154         _model->ContentsChanged(); /* EMIT SIGNAL */
155 }
156
157 XMLNode&
158 MidiModel::DeltaCommand::marshal_note(const boost::shared_ptr<Evoral::Note> note)
159 {
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());
164
165         ostringstream channel_str(ios::ate);
166         channel_str << int(note->channel());
167         xml_note->add_property("channel", channel_str.str());
168
169         ostringstream time_str(ios::ate);
170         time_str << int(note->time());
171         xml_note->add_property("time", time_str.str());
172
173         ostringstream length_str(ios::ate);
174         length_str <<(unsigned int) note->length();
175         xml_note->add_property("length", length_str.str());
176
177         ostringstream velocity_str(ios::ate);
178         velocity_str << (unsigned int) note->velocity();
179         xml_note->add_property("velocity", velocity_str.str());
180
181         return *xml_note;
182 }
183
184 boost::shared_ptr<Evoral::Note> MidiModel::DeltaCommand::unmarshal_note(XMLNode *xml_note)
185 {
186         unsigned int note;
187         istringstream note_str(xml_note->property("note")->value());
188         note_str >> note;
189
190         unsigned int channel;
191         istringstream channel_str(xml_note->property("channel")->value());
192         channel_str >> channel;
193
194         unsigned int time;
195         istringstream time_str(xml_note->property("time")->value());
196         time_str >> time;
197
198         unsigned int length;
199         istringstream length_str(xml_note->property("length")->value());
200         length_str >> length;
201
202         unsigned int velocity;
203         istringstream velocity_str(xml_note->property("velocity")->value());
204         velocity_str >> velocity;
205
206         boost::shared_ptr<Evoral::Note> note_ptr(new Evoral::Note(channel, time, length, note, velocity));
207         return note_ptr;
208 }
209
210 #define ADDED_NOTES_ELEMENT "added_notes"
211 #define REMOVED_NOTES_ELEMENT "removed_notes"
212 #define DELTA_COMMAND_ELEMENT "DeltaCommand"
213
214 int MidiModel::DeltaCommand::set_state(const XMLNode& delta_command)
215 {
216         if (delta_command.name() != string(DELTA_COMMAND_ELEMENT)) {
217                 return 1;
218         }
219
220         _added_notes.clear();
221         XMLNode *added_notes = delta_command.child(ADDED_NOTES_ELEMENT);
222         XMLNodeList notes = added_notes->children();
223         transform(notes.begin(), notes.end(), back_inserter(_added_notes),
224                         sigc::mem_fun(*this, &DeltaCommand::unmarshal_note));
225
226         _removed_notes.clear();
227         XMLNode *removed_notes = delta_command.child(REMOVED_NOTES_ELEMENT);
228         notes = removed_notes->children();
229         transform(notes.begin(), notes.end(), back_inserter(_removed_notes),
230                         sigc::mem_fun(*this, &DeltaCommand::unmarshal_note));
231
232         return 0;
233 }
234
235 XMLNode& MidiModel::DeltaCommand::get_state()
236 {
237         XMLNode *delta_command = new XMLNode(DELTA_COMMAND_ELEMENT);
238         delta_command->add_property("midi_source", _model->midi_source()->id().to_s());
239
240         XMLNode *added_notes = delta_command->add_child(ADDED_NOTES_ELEMENT);
241         for_each(_added_notes.begin(), _added_notes.end(), sigc::compose(
242                         sigc::mem_fun(*added_notes, &XMLNode::add_child_nocopy),
243                         sigc::mem_fun(*this, &DeltaCommand::marshal_note)));
244
245         XMLNode *removed_notes = delta_command->add_child(REMOVED_NOTES_ELEMENT);
246         for_each(_removed_notes.begin(), _removed_notes.end(), sigc::compose(
247                         sigc::mem_fun(*removed_notes, &XMLNode::add_child_nocopy),
248                         sigc::mem_fun(*this, &DeltaCommand::marshal_note)));
249
250         return *delta_command;
251 }
252
253 struct EventTimeComparator {
254         typedef const Evoral::Event* value_type;
255         inline bool operator()(const Evoral::Event& a, const Evoral::Event& b) const {
256                 return a.time() >= b.time();
257         }
258 };
259
260 /** Write the model to a MidiSource (i.e. save the model).
261  * This is different from manually using read to write to a source in that
262  * note off events are written regardless of the track mode.  This is so the
263  * user can switch a recorded track (with note durations from some instrument)
264  * to percussive, save, reload, then switch it back to sustained without
265  * destroying the original note durations.
266  */
267 bool MidiModel::write_to(boost::shared_ptr<MidiSource> source)
268 {
269         read_lock();
270
271         const bool old_percussive = percussive();
272         set_percussive(false);
273         
274         for (Evoral::Sequence::const_iterator i = begin(); i != end(); ++i) {
275                 source->append_event_unlocked(Frames, *i);
276         }
277                 
278         set_percussive(old_percussive);
279         
280         read_unlock();
281         set_edited(false);
282
283         return true;
284 }
285
286 XMLNode& MidiModel::get_state()
287 {
288         XMLNode *node = new XMLNode("MidiModel");
289         return *node;
290 }
291