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 Change () : sysex_id (0) {}
179 boost::shared_ptr<Evoral::Event<TimeType> > sysex;
181 SysExDiffCommand::Property property;
186 typedef std::list<Change> ChangeList;
189 std::list<SysExPtr> _removed;
191 XMLNode & marshal_change (const Change &);
192 Change unmarshal_change (XMLNode *);
195 class LIBARDOUR_API PatchChangeDiffCommand : public DiffCommand {
197 PatchChangeDiffCommand (boost::shared_ptr<MidiModel>, const std::string &);
198 PatchChangeDiffCommand (boost::shared_ptr<MidiModel>, const XMLNode &);
200 int set_state (const XMLNode &, int version);
201 XMLNode & get_state ();
206 void add (PatchChangePtr);
207 void remove (PatchChangePtr);
208 void change_time (PatchChangePtr, TimeType);
209 void change_channel (PatchChangePtr, uint8_t);
210 void change_program (PatchChangePtr, uint8_t);
211 void change_bank (PatchChangePtr, int);
222 PatchChangePtr patch;
238 Change() : patch_id (-1) {}
241 typedef std::list<Change> ChangeList;
244 std::list<PatchChangePtr> _added;
245 std::list<PatchChangePtr> _removed;
247 XMLNode & marshal_change (const Change &);
248 Change unmarshal_change (XMLNode *);
250 XMLNode & marshal_patch_change (constPatchChangePtr);
251 PatchChangePtr unmarshal_patch_change (XMLNode *);
254 MidiModel::NoteDiffCommand* new_note_diff_command (const std::string& name = "midi edit");
255 MidiModel::SysExDiffCommand* new_sysex_diff_command (const std::string& name = "midi edit");
256 MidiModel::PatchChangeDiffCommand* new_patch_change_diff_command (const std::string& name = "midi edit");
257 void apply_command (Session& session, Command* cmd);
258 void apply_command (Session* session, Command* cmd) { if (session) { apply_command (*session, cmd); } }
259 void apply_command_as_subcommand (Session& session, Command* cmd);
261 bool sync_to_source (const Glib::Threads::Mutex::Lock& source_lock);
263 bool write_to(boost::shared_ptr<MidiSource> source,
264 const Glib::Threads::Mutex::Lock& source_lock);
266 bool write_section_to(boost::shared_ptr<MidiSource> source,
267 const Glib::Threads::Mutex::Lock& source_lock,
268 Evoral::Beats begin = Evoral::Beats(),
269 Evoral::Beats end = std::numeric_limits<Evoral::Beats>::max(),
270 bool offset_events = false);
272 // MidiModel doesn't use the normal AutomationList serialisation code
273 // since controller data is stored in the .mid
274 XMLNode& get_state();
275 int set_state(const XMLNode&) { return 0; }
277 PBD::Signal0<void> ContentsChanged;
278 PBD::Signal1<void, double> ContentsShifted;
280 boost::shared_ptr<const MidiSource> midi_source ();
281 void set_midi_source (boost::shared_ptr<MidiSource>);
283 boost::shared_ptr<Evoral::Note<TimeType> > find_note (NotePtr);
284 PatchChangePtr find_patch_change (Evoral::event_id_t);
285 boost::shared_ptr<Evoral::Note<TimeType> > find_note (gint note_id);
286 boost::shared_ptr<Evoral::Event<TimeType> > find_sysex (gint);
288 InsertMergePolicy insert_merge_policy () const;
289 void set_insert_merge_policy (InsertMergePolicy);
291 boost::shared_ptr<Evoral::Control> control_factory(const Evoral::Parameter& id);
293 void insert_silence_at_start (TimeType);
294 void transpose (NoteDiffCommand *, const NotePtr, int);
297 int resolve_overlaps_unlocked (const NotePtr, void* arg = 0);
300 struct WriteLockImpl : public AutomatableSequence<TimeType>::WriteLockImpl {
301 WriteLockImpl(Glib::Threads::Mutex::Lock* slock, Glib::Threads::RWLock& s, Glib::Threads::Mutex& c)
302 : AutomatableSequence<TimeType>::WriteLockImpl(s, c)
303 , source_lock (slock)
308 Glib::Threads::Mutex::Lock* source_lock;
312 WriteLock edit_lock();
315 friend class DeltaCommand;
317 void source_interpolation_changed (Evoral::Parameter, Evoral::ControlList::InterpolationStyle);
318 void source_automation_state_changed (Evoral::Parameter, AutoState);
319 void control_list_interpolation_changed (Evoral::Parameter, Evoral::ControlList::InterpolationStyle);
320 void automation_list_automation_state_changed (Evoral::Parameter, AutoState);
322 void control_list_marked_dirty ();
324 PBD::ScopedConnectionList _midi_source_connections;
326 // We cannot use a boost::shared_ptr here to avoid a retain cycle
327 boost::weak_ptr<MidiSource> _midi_source;
328 InsertMergePolicy _insert_merge_policy;
331 } /* namespace ARDOUR */
333 #endif /* __ardour_midi_model_h__ */