Merge branch 'master' of git.ardour.org:ardour/ardour
[ardour.git] / libs / ardour / ardour / midi_model.h
1 /*
2     Copyright (C) 2007 Paul Davis
3     Author: David 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 #ifndef __ardour_midi_model_h__
22 #define __ardour_midi_model_h__
23
24 #include <queue>
25 #include <deque>
26 #include <utility>
27 #include <boost/utility.hpp>
28 #include <glibmm/threads.h>
29 #include "pbd/command.h"
30 #include "ardour/types.h"
31 #include "ardour/midi_buffer.h"
32 #include "ardour/midi_ring_buffer.h"
33 #include "ardour/automatable_sequence.h"
34 #include "ardour/types.h"
35 #include "evoral/Note.hpp"
36 #include "evoral/Sequence.hpp"
37
38 namespace ARDOUR {
39
40 class Session;
41 class MidiSource;
42
43 /** This is a higher level (than MidiBuffer) model of MIDI data, with separate
44  * representations for notes (instead of just unassociated note on/off events)
45  * and controller data.  Controller data is represented as part of the
46  * Automatable base (i.e. in a map of AutomationList, keyed by Parameter).
47  * Because of this MIDI controllers and automatable controllers/widgets/etc
48  * are easily interchangeable.
49  */
50 class MidiModel : public AutomatableSequence<Evoral::MusicalTime> {
51 public:
52         typedef Evoral::MusicalTime TimeType;
53
54         MidiModel (boost::shared_ptr<MidiSource>);
55
56         NoteMode note_mode() const { return (percussive() ? Percussive : Sustained); }
57         void set_note_mode(NoteMode mode) { set_percussive(mode == Percussive); };
58
59         class DiffCommand : public Command {
60         public:
61
62                 DiffCommand (boost::shared_ptr<MidiModel> m, const std::string& name);
63
64                 const std::string& name () const { return _name; }
65
66                 virtual void operator() () = 0;
67                 virtual void undo () = 0;
68
69                 virtual int set_state (const XMLNode&, int version) = 0;
70                 virtual XMLNode & get_state () = 0;
71
72                 boost::shared_ptr<MidiModel> model() const { return _model; }
73
74         protected:
75                 boost::shared_ptr<MidiModel> _model;
76                 const std::string            _name;
77
78         };
79
80         class NoteDiffCommand : public DiffCommand {
81         public:
82
83                 NoteDiffCommand (boost::shared_ptr<MidiModel> m, const std::string& name) : DiffCommand (m, name) {}
84                 NoteDiffCommand (boost::shared_ptr<MidiModel> m, const XMLNode& node);
85
86                 enum Property {
87                         NoteNumber,
88                         Velocity,
89                         StartTime,
90                         Length,
91                         Channel
92                 };
93
94                 void operator() ();
95                 void undo ();
96
97                 int set_state (const XMLNode&, int version);
98                 XMLNode & get_state ();
99
100                 void add (const NotePtr note);
101                 void remove (const NotePtr note);
102                 void side_effect_remove (const NotePtr note);
103
104                 void change (const NotePtr note, Property prop, uint8_t new_value);
105                 void change (const NotePtr note, Property prop, TimeType new_time);
106
107                 bool adds_or_removes() const {
108                         return !_added_notes.empty() || !_removed_notes.empty();
109                 }
110
111                 NoteDiffCommand& operator+= (const NoteDiffCommand& other);
112
113         private:
114                 struct NoteChange {
115                         NoteDiffCommand::Property property;
116                         NotePtr note;
117                         uint32_t note_id; 
118                     
119                         union {
120                                 uint8_t  old_value;
121                                 TimeType old_time;
122                         };
123                         union {
124                                 uint8_t  new_value;
125                                 TimeType new_time;
126                         };
127                 };
128
129                 typedef std::list<NoteChange> ChangeList;
130                 ChangeList _changes;
131
132                 typedef std::list< boost::shared_ptr< Evoral::Note<TimeType> > > NoteList;
133                 NoteList _added_notes;
134                 NoteList _removed_notes;
135
136                 std::set<NotePtr> side_effect_removals;
137
138                 XMLNode &marshal_change(const NoteChange&);
139                 NoteChange unmarshal_change(XMLNode *xml_note);
140
141                 XMLNode &marshal_note(const NotePtr note);
142                 NotePtr unmarshal_note(XMLNode *xml_note);
143         };
144
145         /* Currently this class only supports changes of sys-ex time, but could be expanded */
146         class SysExDiffCommand : public DiffCommand {
147         public:
148                 SysExDiffCommand (boost::shared_ptr<MidiModel> m, const XMLNode& node);
149
150                 enum Property {
151                         Time,
152                 };
153
154                 int set_state (const XMLNode&, int version);
155                 XMLNode & get_state ();
156
157                 void remove (SysExPtr sysex);
158                 void operator() ();
159                 void undo ();
160
161                 void change (boost::shared_ptr<Evoral::Event<TimeType> >, TimeType);
162
163         private:
164                 struct Change {
165                         boost::shared_ptr<Evoral::Event<TimeType> > sysex;
166                         gint sysex_id;
167                         SysExDiffCommand::Property property;
168                         TimeType old_time;
169                         TimeType new_time;
170                 };
171
172                 typedef std::list<Change> ChangeList;
173                 ChangeList _changes;
174
175                 std::list<SysExPtr> _removed;
176
177                 XMLNode & marshal_change (const Change &);
178                 Change unmarshal_change (XMLNode *);
179         };
180
181         class PatchChangeDiffCommand : public DiffCommand {
182         public:
183                 PatchChangeDiffCommand (boost::shared_ptr<MidiModel>, const std::string &);
184                 PatchChangeDiffCommand (boost::shared_ptr<MidiModel>, const XMLNode &);
185
186                 int set_state (const XMLNode &, int version);
187                 XMLNode & get_state ();
188
189                 void operator() ();
190                 void undo ();
191
192                 void add (PatchChangePtr);
193                 void remove (PatchChangePtr);
194                 void change_time (PatchChangePtr, TimeType);
195                 void change_channel (PatchChangePtr, uint8_t);
196                 void change_program (PatchChangePtr, uint8_t);
197                 void change_bank (PatchChangePtr, int);
198
199                 enum Property {
200                         Time,
201                         Channel,
202                         Program,
203                         Bank
204                 };
205
206         private:
207                 struct Change {
208                         PatchChangePtr patch;
209                         Property       property;
210                         gint           patch_id;
211                         union {
212                                 TimeType   old_time;
213                                 uint8_t    old_channel;
214                                 int        old_bank;
215                                 uint8_t    old_program;
216                         };
217                         union {
218                                 uint8_t    new_channel;
219                                 TimeType   new_time;
220                                 uint8_t    new_program;
221                                 int        new_bank;
222                         };
223                 };
224
225                 typedef std::list<Change> ChangeList;
226                 ChangeList _changes;
227
228                 std::list<PatchChangePtr> _added;
229                 std::list<PatchChangePtr> _removed;
230
231                 XMLNode & marshal_change (const Change &);
232                 Change unmarshal_change (XMLNode *);
233
234                 XMLNode & marshal_patch_change (constPatchChangePtr);
235                 PatchChangePtr unmarshal_patch_change (XMLNode *);
236         };
237
238         MidiModel::NoteDiffCommand* new_note_diff_command (const std::string name = "midi edit");
239         MidiModel::SysExDiffCommand* new_sysex_diff_command (const std::string name = "midi edit");
240         MidiModel::PatchChangeDiffCommand* new_patch_change_diff_command (const std::string name = "midi edit");
241         void apply_command (Session& session, Command* cmd);
242         void apply_command_as_subcommand (Session& session, Command* cmd);
243
244         bool sync_to_source ();
245         bool write_to(boost::shared_ptr<MidiSource> source);
246         bool write_section_to (boost::shared_ptr<MidiSource> source, Evoral::MusicalTime begin = Evoral::MinMusicalTime,
247         Evoral::MusicalTime end = Evoral::MaxMusicalTime);
248
249         // MidiModel doesn't use the normal AutomationList serialisation code
250         // since controller data is stored in the .mid
251         XMLNode& get_state();
252         int set_state(const XMLNode&) { return 0; }
253
254         PBD::Signal0<void> ContentsChanged;
255
256         boost::shared_ptr<const MidiSource> midi_source ();
257         void set_midi_source (boost::shared_ptr<MidiSource>);
258
259         boost::shared_ptr<Evoral::Note<TimeType> > find_note (NotePtr);
260         PatchChangePtr find_patch_change (Evoral::event_id_t);
261         boost::shared_ptr<Evoral::Note<TimeType> > find_note (gint note_id);
262         boost::shared_ptr<Evoral::Event<TimeType> > find_sysex (gint);
263
264         InsertMergePolicy insert_merge_policy () const;
265         void set_insert_merge_policy (InsertMergePolicy);
266
267         boost::shared_ptr<Evoral::Control> control_factory(const Evoral::Parameter& id);
268
269         void insert_silence_at_start (TimeType);
270         void transpose (TimeType, TimeType, int);
271
272 protected:
273         int resolve_overlaps_unlocked (const NotePtr, void* arg = 0);
274
275 private:
276         struct WriteLockImpl : public AutomatableSequence<TimeType>::WriteLockImpl {
277                 WriteLockImpl(Glib::Threads::Mutex::Lock* slock, Glib::Threads::RWLock& s, Glib::Threads::Mutex& c)
278                         : AutomatableSequence<TimeType>::WriteLockImpl(s, c)
279                         , source_lock (slock)
280                 {}
281                 ~WriteLockImpl() {
282                         delete source_lock;
283                 }
284                 Glib::Threads::Mutex::Lock* source_lock;
285         };
286
287 public:
288         WriteLock edit_lock();
289         WriteLock write_lock();
290
291 private:
292         friend class DeltaCommand;
293
294         void source_interpolation_changed (Evoral::Parameter, Evoral::ControlList::InterpolationStyle);
295         void source_automation_state_changed (Evoral::Parameter, AutoState);
296         void control_list_interpolation_changed (Evoral::Parameter, Evoral::ControlList::InterpolationStyle);
297         void automation_list_automation_state_changed (Evoral::Parameter, AutoState);
298
299         void control_list_marked_dirty ();
300
301         PBD::ScopedConnectionList _midi_source_connections;
302
303         // We cannot use a boost::shared_ptr here to avoid a retain cycle
304         boost::weak_ptr<MidiSource> _midi_source;
305         InsertMergePolicy _insert_merge_policy;
306 };
307
308 } /* namespace ARDOUR */
309
310 /* This is a very long comment and stuff oh my god it's so long what are we going to do oh no oh no*/
311
312 #endif /* __ardour_midi_model_h__ */
313