2 Copyright (C) 2007 Paul Davis
3 Author: David 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__
28 #include <boost/utility.hpp>
29 #include <glibmm/threads.h>
31 #include "pbd/command.h"
33 #include "ardour/automatable_sequence.h"
34 #include "ardour/libardour_visibility.h"
35 #include "ardour/libardour_visibility.h"
36 #include "ardour/types.h"
37 #include "ardour/types.h"
38 #include "ardour/variant.h"
40 #include "evoral/Note.hpp"
41 #include "evoral/Sequence.hpp"
48 /** This is a higher level (than MidiBuffer) model of MIDI data, with separate
49 * representations for notes (instead of just unassociated note on/off events)
50 * and controller data. Controller data is represented as part of the
51 * Automatable base (i.e. in a map of AutomationList, keyed by Parameter).
52 * Because of this MIDI controllers and automatable controllers/widgets/etc
53 * are easily interchangeable.
55 class LIBARDOUR_API MidiModel : public AutomatableSequence<Evoral::Beats> {
57 typedef Evoral::Beats TimeType;
59 MidiModel (boost::shared_ptr<MidiSource>);
61 NoteMode note_mode() const { return (percussive() ? Percussive : Sustained); }
62 void set_note_mode(NoteMode mode) { set_percussive(mode == Percussive); };
64 class LIBARDOUR_API DiffCommand : public Command {
67 DiffCommand (boost::shared_ptr<MidiModel> m, const std::string& name);
69 const std::string& name () const { return _name; }
71 virtual void operator() () = 0;
72 virtual void undo () = 0;
74 virtual int set_state (const XMLNode&, int version) = 0;
75 virtual XMLNode & get_state () = 0;
77 boost::shared_ptr<MidiModel> model() const { return _model; }
80 boost::shared_ptr<MidiModel> _model;
81 const std::string _name;
85 class LIBARDOUR_API NoteDiffCommand : public DiffCommand {
88 NoteDiffCommand (boost::shared_ptr<MidiModel> m, const std::string& name) : DiffCommand (m, name) {}
89 NoteDiffCommand (boost::shared_ptr<MidiModel> m, const XMLNode& node);
102 int set_state (const XMLNode&, int version);
103 XMLNode & get_state ();
105 void add (const NotePtr note);
106 void remove (const NotePtr note);
107 void side_effect_remove (const NotePtr note);
109 void change (const NotePtr note, Property prop, uint8_t new_value) {
110 change(note, prop, Variant(new_value));
113 void change (const NotePtr note, Property prop, TimeType new_time) {
114 change(note, prop, Variant(new_time));
117 void change (const NotePtr note, Property prop, const Variant& new_value);
119 bool adds_or_removes() const {
120 return !_added_notes.empty() || !_removed_notes.empty();
123 NoteDiffCommand& operator+= (const NoteDiffCommand& other);
125 static Variant get_value (const NotePtr note, Property prop);
127 static Variant::Type value_type (Property prop);
130 NoteDiffCommand::Property property;
137 typedef std::list<NoteChange> ChangeList;
138 typedef std::list< boost::shared_ptr< Evoral::Note<TimeType> > > NoteList;
140 const ChangeList& changes() const { return _changes; }
141 const NoteList& added_notes() const { return _added_notes; }
142 const NoteList& removed_notes() const { return _removed_notes; }
146 NoteList _added_notes;
147 NoteList _removed_notes;
149 std::set<NotePtr> side_effect_removals;
151 XMLNode &marshal_change(const NoteChange&);
152 NoteChange unmarshal_change(XMLNode *xml_note);
154 XMLNode &marshal_note(const NotePtr note);
155 NotePtr unmarshal_note(XMLNode *xml_note);
158 /* Currently this class only supports changes of sys-ex time, but could be expanded */
159 class LIBARDOUR_API SysExDiffCommand : public DiffCommand {
161 SysExDiffCommand (boost::shared_ptr<MidiModel> m, const XMLNode& node);
167 int set_state (const XMLNode&, int version);
168 XMLNode & get_state ();
170 void remove (SysExPtr sysex);
174 void change (boost::shared_ptr<Evoral::Event<TimeType> >, TimeType);
178 boost::shared_ptr<Evoral::Event<TimeType> > sysex;
180 SysExDiffCommand::Property property;
185 typedef std::list<Change> ChangeList;
188 std::list<SysExPtr> _removed;
190 XMLNode & marshal_change (const Change &);
191 Change unmarshal_change (XMLNode *);
194 class LIBARDOUR_API PatchChangeDiffCommand : public DiffCommand {
196 PatchChangeDiffCommand (boost::shared_ptr<MidiModel>, const std::string &);
197 PatchChangeDiffCommand (boost::shared_ptr<MidiModel>, const XMLNode &);
199 int set_state (const XMLNode &, int version);
200 XMLNode & get_state ();
205 void add (PatchChangePtr);
206 void remove (PatchChangePtr);
207 void change_time (PatchChangePtr, TimeType);
208 void change_channel (PatchChangePtr, uint8_t);
209 void change_program (PatchChangePtr, uint8_t);
210 void change_bank (PatchChangePtr, int);
221 PatchChangePtr patch;
237 Change() : patch_id (-1) {}
240 typedef std::list<Change> ChangeList;
243 std::list<PatchChangePtr> _added;
244 std::list<PatchChangePtr> _removed;
246 XMLNode & marshal_change (const Change &);
247 Change unmarshal_change (XMLNode *);
249 XMLNode & marshal_patch_change (constPatchChangePtr);
250 PatchChangePtr unmarshal_patch_change (XMLNode *);
253 MidiModel::NoteDiffCommand* new_note_diff_command (const std::string name = "midi edit");
254 MidiModel::SysExDiffCommand* new_sysex_diff_command (const std::string name = "midi edit");
255 MidiModel::PatchChangeDiffCommand* new_patch_change_diff_command (const std::string name = "midi edit");
256 void apply_command (Session& session, Command* cmd);
257 void apply_command_as_subcommand (Session& session, Command* cmd);
259 bool sync_to_source (const Glib::Threads::Mutex::Lock& source_lock);
261 bool write_to(boost::shared_ptr<MidiSource> source,
262 const Glib::Threads::Mutex::Lock& source_lock);
264 bool write_section_to(boost::shared_ptr<MidiSource> source,
265 const Glib::Threads::Mutex::Lock& source_lock,
266 Evoral::Beats begin = Evoral::MinBeats,
267 Evoral::Beats end = Evoral::MaxBeats);
269 // MidiModel doesn't use the normal AutomationList serialisation code
270 // since controller data is stored in the .mid
271 XMLNode& get_state();
272 int set_state(const XMLNode&) { return 0; }
274 PBD::Signal0<void> ContentsChanged;
276 boost::shared_ptr<const MidiSource> midi_source ();
277 void set_midi_source (boost::shared_ptr<MidiSource>);
279 boost::shared_ptr<Evoral::Note<TimeType> > find_note (NotePtr);
280 PatchChangePtr find_patch_change (Evoral::event_id_t);
281 boost::shared_ptr<Evoral::Note<TimeType> > find_note (gint note_id);
282 boost::shared_ptr<Evoral::Event<TimeType> > find_sysex (gint);
284 InsertMergePolicy insert_merge_policy () const;
285 void set_insert_merge_policy (InsertMergePolicy);
287 boost::shared_ptr<Evoral::Control> control_factory(const Evoral::Parameter& id);
289 void insert_silence_at_start (TimeType);
290 void transpose (TimeType, TimeType, int);
292 std::set<WeakNotePtr>& active_notes() { return _active_notes; }
295 int resolve_overlaps_unlocked (const NotePtr, void* arg = 0);
298 struct WriteLockImpl : public AutomatableSequence<TimeType>::WriteLockImpl {
299 WriteLockImpl(Glib::Threads::Mutex::Lock* slock, Glib::Threads::RWLock& s, Glib::Threads::Mutex& c)
300 : AutomatableSequence<TimeType>::WriteLockImpl(s, c)
301 , source_lock (slock)
306 Glib::Threads::Mutex::Lock* source_lock;
310 WriteLock edit_lock();
313 friend class DeltaCommand;
315 void source_interpolation_changed (Evoral::Parameter, Evoral::ControlList::InterpolationStyle);
316 void source_automation_state_changed (Evoral::Parameter, AutoState);
317 void control_list_interpolation_changed (Evoral::Parameter, Evoral::ControlList::InterpolationStyle);
318 void automation_list_automation_state_changed (Evoral::Parameter, AutoState);
320 void control_list_marked_dirty ();
322 PBD::ScopedConnectionList _midi_source_connections;
324 // We cannot use a boost::shared_ptr here to avoid a retain cycle
325 boost::weak_ptr<MidiSource> _midi_source;
326 InsertMergePolicy _insert_merge_policy;
328 std::set<WeakNotePtr> _active_notes;
331 } /* namespace ARDOUR */
333 #endif /* __ardour_midi_model_h__ */