2 * Copyright (C) 2007-2016 David Robillard <d@drobilla.net>
3 * Copyright (C) 2008-2012 Hans Baier <hansfbaier@googlemail.com>
4 * Copyright (C) 2008-2017 Paul Davis <paul@linuxaudiosystems.com>
5 * Copyright (C) 2009-2011 Carl Hetherington <carl@carlh.net>
6 * Copyright (C) 2015 André Nusser <andre.nusser@googlemail.com>
7 * Copyright (C) 2016 Robin Gareus <robin@gareus.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #ifndef __ardour_midi_model_h__
25 #define __ardour_midi_model_h__
31 #include <boost/utility.hpp>
32 #include <glibmm/threads.h>
34 #include "pbd/command.h"
36 #include "ardour/automatable_sequence.h"
37 #include "ardour/libardour_visibility.h"
38 #include "ardour/libardour_visibility.h"
39 #include "ardour/types.h"
40 #include "ardour/types.h"
41 #include "ardour/variant.h"
43 #include "evoral/Note.h"
44 #include "evoral/Sequence.h"
51 /** This is a higher level (than MidiBuffer) model of MIDI data, with separate
52 * representations for notes (instead of just unassociated note on/off events)
53 * and controller data. Controller data is represented as part of the
54 * Automatable base (i.e. in a map of AutomationList, keyed by Parameter).
55 * Because of this MIDI controllers and automatable controllers/widgets/etc
56 * are easily interchangeable.
58 class LIBARDOUR_API MidiModel : public AutomatableSequence<Temporal::Beats> {
60 typedef Temporal::Beats TimeType;
62 MidiModel (boost::shared_ptr<MidiSource>);
64 NoteMode note_mode() const { return (percussive() ? Percussive : Sustained); }
65 void set_note_mode(NoteMode mode) { set_percussive(mode == Percussive); };
67 class LIBARDOUR_API DiffCommand : public Command {
70 DiffCommand (boost::shared_ptr<MidiModel> m, const std::string& name);
72 const std::string& name () const { return _name; }
74 virtual void operator() () = 0;
75 virtual void undo () = 0;
77 virtual int set_state (const XMLNode&, int version) = 0;
78 virtual XMLNode & get_state () = 0;
80 boost::shared_ptr<MidiModel> model() const { return _model; }
83 boost::shared_ptr<MidiModel> _model;
84 const std::string _name;
88 class LIBARDOUR_API NoteDiffCommand : public DiffCommand {
91 NoteDiffCommand (boost::shared_ptr<MidiModel> m, const std::string& name) : DiffCommand (m, name) {}
92 NoteDiffCommand (boost::shared_ptr<MidiModel> m, const XMLNode& node);
105 int set_state (const XMLNode&, int version);
106 XMLNode & get_state ();
108 void add (const NotePtr note);
109 void remove (const NotePtr note);
110 void side_effect_remove (const NotePtr note);
112 void change (const NotePtr note, Property prop, uint8_t new_value) {
113 change(note, prop, Variant(new_value));
116 void change (const NotePtr note, Property prop, TimeType new_time) {
117 change(note, prop, Variant(new_time));
120 void change (const NotePtr note, Property prop, const Variant& new_value);
122 bool adds_or_removes() const {
123 return !_added_notes.empty() || !_removed_notes.empty();
126 NoteDiffCommand& operator+= (const NoteDiffCommand& other);
128 static Variant get_value (const NotePtr note, Property prop);
130 static Variant::Type value_type (Property prop);
133 NoteDiffCommand::Property property;
140 typedef std::list<NoteChange> ChangeList;
141 typedef std::list< boost::shared_ptr< Evoral::Note<TimeType> > > NoteList;
143 const ChangeList& changes() const { return _changes; }
144 const NoteList& added_notes() const { return _added_notes; }
145 const NoteList& removed_notes() const { return _removed_notes; }
149 NoteList _added_notes;
150 NoteList _removed_notes;
152 std::set<NotePtr> side_effect_removals;
154 XMLNode &marshal_change(const NoteChange&);
155 NoteChange unmarshal_change(XMLNode *xml_note);
157 XMLNode &marshal_note(const NotePtr note);
158 NotePtr unmarshal_note(XMLNode *xml_note);
161 /* Currently this class only supports changes of sys-ex time, but could be expanded */
162 class LIBARDOUR_API SysExDiffCommand : public DiffCommand {
164 SysExDiffCommand (boost::shared_ptr<MidiModel> m, const XMLNode& node);
170 int set_state (const XMLNode&, int version);
171 XMLNode & get_state ();
173 void remove (SysExPtr sysex);
177 void change (boost::shared_ptr<Evoral::Event<TimeType> >, TimeType);
181 Change () : sysex_id (0) {}
182 boost::shared_ptr<Evoral::Event<TimeType> > sysex;
184 SysExDiffCommand::Property property;
189 typedef std::list<Change> ChangeList;
192 std::list<SysExPtr> _removed;
194 XMLNode & marshal_change (const Change &);
195 Change unmarshal_change (XMLNode *);
198 class LIBARDOUR_API PatchChangeDiffCommand : public DiffCommand {
200 PatchChangeDiffCommand (boost::shared_ptr<MidiModel>, const std::string &);
201 PatchChangeDiffCommand (boost::shared_ptr<MidiModel>, const XMLNode &);
203 int set_state (const XMLNode &, int version);
204 XMLNode & get_state ();
209 void add (PatchChangePtr);
210 void remove (PatchChangePtr);
211 void change_time (PatchChangePtr, TimeType);
212 void change_channel (PatchChangePtr, uint8_t);
213 void change_program (PatchChangePtr, uint8_t);
214 void change_bank (PatchChangePtr, int);
225 PatchChangePtr patch;
241 Change() : patch_id (-1) {}
244 typedef std::list<Change> ChangeList;
247 std::list<PatchChangePtr> _added;
248 std::list<PatchChangePtr> _removed;
250 XMLNode & marshal_change (const Change &);
251 Change unmarshal_change (XMLNode *);
253 XMLNode & marshal_patch_change (constPatchChangePtr);
254 PatchChangePtr unmarshal_patch_change (XMLNode *);
257 /** Start a new NoteDiff command.
259 * This has no side-effects on the model or Session, the returned command
260 * can be held on to for as long as the caller wishes, or discarded without
261 * formality, until apply_command is called and ownership is taken.
263 MidiModel::NoteDiffCommand* new_note_diff_command (const std::string& name = "midi edit");
264 /** Start a new SysExDiff command */
265 MidiModel::SysExDiffCommand* new_sysex_diff_command (const std::string& name = "midi edit");
267 /** Start a new PatchChangeDiff command */
268 MidiModel::PatchChangeDiffCommand* new_patch_change_diff_command (const std::string& name = "midi edit");
272 * Ownership of cmd is taken, it must not be deleted by the caller.
273 * The command will constitute one item on the undo stack.
275 void apply_command (Session& session, Command* cmd);
277 void apply_command (Session* session, Command* cmd) { if (session) { apply_command (*session, cmd); } }
279 /** Apply a command as part of a larger reversible transaction
281 * Ownership of cmd is taken, it must not be deleted by the caller.
282 * The command will constitute one item on the undo stack.
284 void apply_command_as_subcommand (Session& session, Command* cmd);
286 bool sync_to_source (const Glib::Threads::Mutex::Lock& source_lock);
288 bool write_to(boost::shared_ptr<MidiSource> source,
289 const Glib::Threads::Mutex::Lock& source_lock);
291 bool write_section_to(boost::shared_ptr<MidiSource> source,
292 const Glib::Threads::Mutex::Lock& source_lock,
293 Temporal::Beats begin = Temporal::Beats(),
294 Temporal::Beats end = std::numeric_limits<Temporal::Beats>::max(),
295 bool offset_events = false);
297 // MidiModel doesn't use the normal AutomationList serialisation code
298 // since controller data is stored in the .mid
299 XMLNode& get_state();
300 int set_state(const XMLNode&) { return 0; }
302 PBD::Signal0<void> ContentsChanged;
303 PBD::Signal1<void, double> ContentsShifted;
305 boost::shared_ptr<const MidiSource> midi_source ();
306 void set_midi_source (boost::shared_ptr<MidiSource>);
308 boost::shared_ptr<Evoral::Note<TimeType> > find_note (NotePtr);
309 PatchChangePtr find_patch_change (Evoral::event_id_t);
310 boost::shared_ptr<Evoral::Note<TimeType> > find_note (gint note_id);
311 boost::shared_ptr<Evoral::Event<TimeType> > find_sysex (gint);
313 InsertMergePolicy insert_merge_policy () const;
314 void set_insert_merge_policy (InsertMergePolicy);
316 boost::shared_ptr<Evoral::Control> control_factory(const Evoral::Parameter& id);
318 void insert_silence_at_start (TimeType);
319 void transpose (NoteDiffCommand *, const NotePtr, int);
322 int resolve_overlaps_unlocked (const NotePtr, void* arg = 0);
325 struct WriteLockImpl : public AutomatableSequence<TimeType>::WriteLockImpl {
326 WriteLockImpl(Glib::Threads::Mutex::Lock* slock, Glib::Threads::RWLock& s, Glib::Threads::Mutex& c)
327 : AutomatableSequence<TimeType>::WriteLockImpl(s, c)
328 , source_lock (slock)
333 Glib::Threads::Mutex::Lock* source_lock;
337 WriteLock edit_lock();
340 friend class DeltaCommand;
342 void source_interpolation_changed (Evoral::Parameter, Evoral::ControlList::InterpolationStyle);
343 void source_automation_state_changed (Evoral::Parameter, AutoState);
344 void control_list_interpolation_changed (Evoral::Parameter, Evoral::ControlList::InterpolationStyle);
345 void automation_list_automation_state_changed (Evoral::Parameter, AutoState);
347 void control_list_marked_dirty ();
349 PBD::ScopedConnectionList _midi_source_connections;
351 // We cannot use a boost::shared_ptr here to avoid a retain cycle
352 boost::weak_ptr<MidiSource> _midi_source;
353 InsertMergePolicy _insert_merge_policy;
356 } /* namespace ARDOUR */
358 #endif /* __ardour_midi_model_h__ */