Differentiate between pitch-shift (for audio) and transpose (for MIDI). Fixes #3940.
[ardour2.git] / libs / ardour / ardour / midi_model.h
blobe32d31d2a16210ea01b509e0e134d24904a0df94
1 /*
2 Copyright (C) 2007 Paul Davis
3 Author: Dave Robillard
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.
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.
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.
21 #ifndef __ardour_midi_model_h__
22 #define __ardour_midi_model_h__
24 #include <queue>
25 #include <deque>
26 #include <utility>
27 #include <boost/utility.hpp>
28 #include <glibmm/thread.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"
38 namespace ARDOUR {
40 class Session;
41 class MidiSource;
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.
50 class MidiModel : public AutomatableSequence<Evoral::MusicalTime> {
51 public:
52 typedef Evoral::MusicalTime TimeType;
54 MidiModel (boost::shared_ptr<MidiSource>);
56 NoteMode note_mode() const { return (percussive() ? Percussive : Sustained); }
57 void set_note_mode(NoteMode mode) { set_percussive(mode == Percussive); };
59 class DiffCommand : public Command {
60 public:
62 DiffCommand (boost::shared_ptr<MidiModel> m, const std::string& name);
64 const std::string& name () const { return _name; }
66 virtual void operator() () = 0;
67 virtual void undo () = 0;
69 virtual int set_state (const XMLNode&, int version) = 0;
70 virtual XMLNode & get_state () = 0;
72 boost::shared_ptr<MidiModel> model() const { return _model; }
74 protected:
75 boost::shared_ptr<MidiModel> _model;
76 const std::string _name;
80 class NoteDiffCommand : public DiffCommand {
81 public:
83 NoteDiffCommand (boost::shared_ptr<MidiModel> m, const std::string& name) : DiffCommand (m, name) {}
84 NoteDiffCommand (boost::shared_ptr<MidiModel> m, const XMLNode& node);
86 enum Property {
87 NoteNumber,
88 Velocity,
89 StartTime,
90 Length,
91 Channel
94 void operator() ();
95 void undo ();
97 int set_state (const XMLNode&, int version);
98 XMLNode & get_state ();
100 void add (const NotePtr note);
101 void remove (const NotePtr note);
102 void side_effect_remove (const NotePtr note);
104 void change (const NotePtr note, Property prop, uint8_t new_value);
105 void change (const NotePtr note, Property prop, TimeType new_time);
107 bool adds_or_removes() const {
108 return !_added_notes.empty() || !_removed_notes.empty();
111 NoteDiffCommand& operator+= (const NoteDiffCommand& other);
113 private:
114 struct NoteChange {
115 NoteDiffCommand::Property property;
116 NotePtr note;
117 union {
118 uint8_t old_value;
119 TimeType old_time;
121 union {
122 uint8_t new_value;
123 TimeType new_time;
127 typedef std::list<NoteChange> ChangeList;
128 ChangeList _changes;
130 typedef std::list< boost::shared_ptr< Evoral::Note<TimeType> > > NoteList;
131 NoteList _added_notes;
132 NoteList _removed_notes;
134 std::set<NotePtr> side_effect_removals;
136 XMLNode &marshal_change(const NoteChange&);
137 NoteChange unmarshal_change(XMLNode *xml_note);
139 XMLNode &marshal_note(const NotePtr note);
140 NotePtr unmarshal_note(XMLNode *xml_note);
143 /* Currently this class only supports changes of sys-ex time, but could be expanded */
144 class SysExDiffCommand : public DiffCommand {
145 public:
146 SysExDiffCommand (boost::shared_ptr<MidiModel> m, const XMLNode& node);
148 enum Property {
149 Time,
152 int set_state (const XMLNode&, int version);
153 XMLNode & get_state ();
155 void operator() ();
156 void undo ();
158 void change (boost::shared_ptr<Evoral::Event<TimeType> >, TimeType);
160 private:
161 struct Change {
162 boost::shared_ptr<Evoral::Event<TimeType> > sysex;
163 SysExDiffCommand::Property property;
164 TimeType old_time;
165 TimeType new_time;
168 typedef std::list<Change> ChangeList;
169 ChangeList _changes;
171 XMLNode & marshal_change (const Change &);
172 Change unmarshal_change (XMLNode *);
175 class PatchChangeDiffCommand : public DiffCommand {
176 public:
177 PatchChangeDiffCommand (boost::shared_ptr<MidiModel>, const std::string &);
178 PatchChangeDiffCommand (boost::shared_ptr<MidiModel>, const XMLNode &);
180 int set_state (const XMLNode &, int version);
181 XMLNode & get_state ();
183 void operator() ();
184 void undo ();
186 void add (PatchChangePtr);
187 void remove (PatchChangePtr);
188 void change_time (PatchChangePtr, TimeType);
189 void change_channel (PatchChangePtr, uint8_t);
190 void change_program (PatchChangePtr, uint8_t);
191 void change_bank (PatchChangePtr, int);
193 enum Property {
194 Time,
195 Channel,
196 Program,
197 Bank
200 private:
202 struct Change {
203 PatchChangePtr patch;
204 Property property;
205 union {
206 TimeType old_time;
207 uint8_t old_channel;
208 int old_bank;
209 uint8_t old_program;
211 union {
212 uint8_t new_channel;
213 TimeType new_time;
214 uint8_t new_program;
215 int new_bank;
219 typedef std::list<Change> ChangeList;
220 ChangeList _changes;
222 std::list<PatchChangePtr> _added;
223 std::list<PatchChangePtr> _removed;
225 XMLNode & marshal_change (const Change &);
226 Change unmarshal_change (XMLNode *);
228 XMLNode & marshal_patch_change (constPatchChangePtr);
229 PatchChangePtr unmarshal_patch_change (XMLNode *);
232 MidiModel::NoteDiffCommand* new_note_diff_command (const std::string name = "midi edit");
233 MidiModel::SysExDiffCommand* new_sysex_diff_command (const std::string name = "midi edit");
234 MidiModel::PatchChangeDiffCommand* new_patch_change_diff_command (const std::string name = "midi edit");
235 void apply_command (Session& session, Command* cmd);
236 void apply_command_as_subcommand (Session& session, Command* cmd);
238 bool sync_to_source ();
239 bool write_to(boost::shared_ptr<MidiSource> source);
240 bool write_section_to (boost::shared_ptr<MidiSource> source, Evoral::MusicalTime begin = Evoral::MinMusicalTime,
241 Evoral::MusicalTime end = Evoral::MaxMusicalTime);
243 // MidiModel doesn't use the normal AutomationList serialisation code
244 // since controller data is stored in the .mid
245 XMLNode& get_state();
246 int set_state(const XMLNode&) { return 0; }
248 PBD::Signal0<void> ContentsChanged;
250 boost::shared_ptr<const MidiSource> midi_source ();
251 void set_midi_source (boost::shared_ptr<MidiSource>);
253 boost::shared_ptr<Evoral::Note<TimeType> > find_note (NotePtr);
254 PatchChangePtr find_patch_change (Evoral::event_id_t);
255 boost::shared_ptr<Evoral::Note<TimeType> > find_note (gint note_id);
256 boost::shared_ptr<Evoral::Event<TimeType> > find_sysex (gint);
258 InsertMergePolicy insert_merge_policy () const;
259 void set_insert_merge_policy (InsertMergePolicy);
261 boost::shared_ptr<Evoral::Control> control_factory(const Evoral::Parameter& id);
263 void insert_silence_at_start (TimeType);
264 void transpose (TimeType, TimeType, int);
266 protected:
267 int resolve_overlaps_unlocked (const NotePtr, void* arg = 0);
269 private:
270 struct WriteLockImpl : public AutomatableSequence<TimeType>::WriteLockImpl {
271 WriteLockImpl(Glib::Mutex::Lock* source_lock, Glib::RWLock& s, Glib::Mutex& c)
272 : AutomatableSequence<TimeType>::WriteLockImpl(s, c)
273 , source_lock(source_lock)
275 ~WriteLockImpl() {
276 delete source_lock;
278 Glib::Mutex::Lock* source_lock;
281 public:
282 virtual WriteLock edit_lock();
283 virtual WriteLock write_lock();
285 private:
286 friend class DeltaCommand;
288 void source_interpolation_changed (Evoral::Parameter, Evoral::ControlList::InterpolationStyle);
289 void source_automation_state_changed (Evoral::Parameter, AutoState);
290 void control_list_interpolation_changed (Evoral::Parameter, Evoral::ControlList::InterpolationStyle);
291 void automation_list_automation_state_changed (Evoral::Parameter, AutoState);
293 PBD::ScopedConnectionList _midi_source_connections;
295 // We cannot use a boost::shared_ptr here to avoid a retain cycle
296 boost::weak_ptr<MidiSource> _midi_source;
297 InsertMergePolicy _insert_merge_policy;
300 } /* namespace ARDOUR */
302 #endif /* __ardour_midi_model_h__ */