'libs/ardour' - DLL visibility stuff and associated changes needed for building with...
[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                     
121                         union {
122                                 uint8_t  old_value;
123                                 TimeType old_time;
124                         };
125                         union {
126                                 uint8_t  new_value;
127                                 TimeType new_time;
128                         };
129                 };
130
131                 typedef std::list<NoteChange> ChangeList;
132                 ChangeList _changes;
133
134                 typedef std::list< boost::shared_ptr< Evoral::Note<TimeType> > > NoteList;
135                 NoteList _added_notes;
136                 NoteList _removed_notes;
137
138                 std::set<NotePtr> side_effect_removals;
139
140                 XMLNode &marshal_change(const NoteChange&);
141                 NoteChange unmarshal_change(XMLNode *xml_note);
142
143                 XMLNode &marshal_note(const NotePtr note);
144                 NotePtr unmarshal_note(XMLNode *xml_note);
145         };
146
147         /* Currently this class only supports changes of sys-ex time, but could be expanded */
148         class LIBARDOUR_API SysExDiffCommand : public DiffCommand {
149         public:
150                 SysExDiffCommand (boost::shared_ptr<MidiModel> m, const XMLNode& node);
151
152                 enum Property {
153                         Time,
154                 };
155
156                 int set_state (const XMLNode&, int version);
157                 XMLNode & get_state ();
158
159                 void remove (SysExPtr sysex);
160                 void operator() ();
161                 void undo ();
162
163                 void change (boost::shared_ptr<Evoral::Event<TimeType> >, TimeType);
164
165         private:
166                 struct Change {
167                         boost::shared_ptr<Evoral::Event<TimeType> > sysex;
168                         gint sysex_id;
169                         SysExDiffCommand::Property property;
170                         TimeType old_time;
171                         TimeType new_time;
172                 };
173
174                 typedef std::list<Change> ChangeList;
175                 ChangeList _changes;
176
177                 std::list<SysExPtr> _removed;
178
179                 XMLNode & marshal_change (const Change &);
180                 Change unmarshal_change (XMLNode *);
181         };
182
183         class LIBARDOUR_API PatchChangeDiffCommand : public DiffCommand {
184         public:
185                 PatchChangeDiffCommand (boost::shared_ptr<MidiModel>, const std::string &);
186                 PatchChangeDiffCommand (boost::shared_ptr<MidiModel>, const XMLNode &);
187
188                 int set_state (const XMLNode &, int version);
189                 XMLNode & get_state ();
190
191                 void operator() ();
192                 void undo ();
193
194                 void add (PatchChangePtr);
195                 void remove (PatchChangePtr);
196                 void change_time (PatchChangePtr, TimeType);
197                 void change_channel (PatchChangePtr, uint8_t);
198                 void change_program (PatchChangePtr, uint8_t);
199                 void change_bank (PatchChangePtr, int);
200
201                 enum Property {
202                         Time,
203                         Channel,
204                         Program,
205                         Bank
206                 };
207
208         private:
209                 struct Change {
210                         PatchChangePtr patch;
211                         Property       property;
212                         gint           patch_id;
213                         union {
214                                 TimeType   old_time;
215                                 uint8_t    old_channel;
216                                 int        old_bank;
217                                 uint8_t    old_program;
218                         };
219                         union {
220                                 uint8_t    new_channel;
221                                 TimeType   new_time;
222                                 uint8_t    new_program;
223                                 int        new_bank;
224                         };
225
226                     Change() : patch_id (-1) {}
227                 };
228
229                 typedef std::list<Change> ChangeList;
230                 ChangeList _changes;
231
232                 std::list<PatchChangePtr> _added;
233                 std::list<PatchChangePtr> _removed;
234
235                 XMLNode & marshal_change (const Change &);
236                 Change unmarshal_change (XMLNode *);
237
238                 XMLNode & marshal_patch_change (constPatchChangePtr);
239                 PatchChangePtr unmarshal_patch_change (XMLNode *);
240         };
241
242         MidiModel::NoteDiffCommand* new_note_diff_command (const std::string name = "midi edit");
243         MidiModel::SysExDiffCommand* new_sysex_diff_command (const std::string name = "midi edit");
244         MidiModel::PatchChangeDiffCommand* new_patch_change_diff_command (const std::string name = "midi edit");
245         void apply_command (Session& session, Command* cmd);
246         void apply_command_as_subcommand (Session& session, Command* cmd);
247
248         bool sync_to_source ();
249         bool write_to(boost::shared_ptr<MidiSource> source);
250         bool write_section_to (boost::shared_ptr<MidiSource> source, Evoral::MusicalTime begin = Evoral::MinMusicalTime,
251         Evoral::MusicalTime end = Evoral::MaxMusicalTime);
252
253         // MidiModel doesn't use the normal AutomationList serialisation code
254         // since controller data is stored in the .mid
255         XMLNode& get_state();
256         int set_state(const XMLNode&) { return 0; }
257
258         PBD::Signal0<void> ContentsChanged;
259
260         boost::shared_ptr<const MidiSource> midi_source ();
261         void set_midi_source (boost::shared_ptr<MidiSource>);
262
263         boost::shared_ptr<Evoral::Note<TimeType> > find_note (NotePtr);
264         PatchChangePtr find_patch_change (Evoral::event_id_t);
265         boost::shared_ptr<Evoral::Note<TimeType> > find_note (gint note_id);
266         boost::shared_ptr<Evoral::Event<TimeType> > find_sysex (gint);
267
268         InsertMergePolicy insert_merge_policy () const;
269         void set_insert_merge_policy (InsertMergePolicy);
270
271         boost::shared_ptr<Evoral::Control> control_factory(const Evoral::Parameter& id);
272
273         void insert_silence_at_start (TimeType);
274         void transpose (TimeType, TimeType, int);
275
276 protected:
277         int resolve_overlaps_unlocked (const NotePtr, void* arg = 0);
278
279 private:
280         struct WriteLockImpl : public AutomatableSequence<TimeType>::WriteLockImpl {
281                 WriteLockImpl(Glib::Threads::Mutex::Lock* slock, Glib::Threads::RWLock& s, Glib::Threads::Mutex& c)
282                         : AutomatableSequence<TimeType>::WriteLockImpl(s, c)
283                         , source_lock (slock)
284                 {}
285                 ~WriteLockImpl() {
286                         delete source_lock;
287                 }
288                 Glib::Threads::Mutex::Lock* source_lock;
289         };
290
291 public:
292         WriteLock edit_lock();
293         WriteLock write_lock();
294
295 private:
296         friend class DeltaCommand;
297
298         void source_interpolation_changed (Evoral::Parameter, Evoral::ControlList::InterpolationStyle);
299         void source_automation_state_changed (Evoral::Parameter, AutoState);
300         void control_list_interpolation_changed (Evoral::Parameter, Evoral::ControlList::InterpolationStyle);
301         void automation_list_automation_state_changed (Evoral::Parameter, AutoState);
302
303         void control_list_marked_dirty ();
304
305         PBD::ScopedConnectionList _midi_source_connections;
306
307         // We cannot use a boost::shared_ptr here to avoid a retain cycle
308         boost::weak_ptr<MidiSource> _midi_source;
309         InsertMergePolicy _insert_merge_policy;
310 };
311
312 } /* namespace ARDOUR */
313
314 /* 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*/
315
316 #endif /* __ardour_midi_model_h__ */
317