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