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__
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"
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.
52 class LIBARDOUR_API MidiModel : public AutomatableSequence<Evoral::MusicalTime> {
54 typedef Evoral::MusicalTime TimeType;
56 MidiModel (boost::shared_ptr<MidiSource>);
58 NoteMode note_mode() const { return (percussive() ? Percussive : Sustained); }
59 void set_note_mode(NoteMode mode) { set_percussive(mode == Percussive); };
61 class DiffCommand : public Command {
64 DiffCommand (boost::shared_ptr<MidiModel> m, const std::string& name);
66 const std::string& name () const { return _name; }
68 virtual void operator() () = 0;
69 virtual void undo () = 0;
71 virtual int set_state (const XMLNode&, int version) = 0;
72 virtual XMLNode & get_state () = 0;
74 boost::shared_ptr<MidiModel> model() const { return _model; }
77 boost::shared_ptr<MidiModel> _model;
78 const std::string _name;
82 class NoteDiffCommand : public DiffCommand {
85 NoteDiffCommand (boost::shared_ptr<MidiModel> m, const std::string& name) : DiffCommand (m, name) {}
86 NoteDiffCommand (boost::shared_ptr<MidiModel> m, const XMLNode& node);
99 int set_state (const XMLNode&, int version);
100 XMLNode & get_state ();
102 void add (const NotePtr note);
103 void remove (const NotePtr note);
104 void side_effect_remove (const NotePtr note);
106 void change (const NotePtr note, Property prop, uint8_t new_value);
107 void change (const NotePtr note, Property prop, TimeType new_time);
109 bool adds_or_removes() const {
110 return !_added_notes.empty() || !_removed_notes.empty();
113 NoteDiffCommand& operator+= (const NoteDiffCommand& other);
117 NoteDiffCommand::Property property;
131 typedef std::list<NoteChange> ChangeList;
134 typedef std::list< boost::shared_ptr< Evoral::Note<TimeType> > > NoteList;
135 NoteList _added_notes;
136 NoteList _removed_notes;
138 std::set<NotePtr> side_effect_removals;
140 XMLNode &marshal_change(const NoteChange&);
141 NoteChange unmarshal_change(XMLNode *xml_note);
143 XMLNode &marshal_note(const NotePtr note);
144 NotePtr unmarshal_note(XMLNode *xml_note);
147 /* Currently this class only supports changes of sys-ex time, but could be expanded */
148 class SysExDiffCommand : public DiffCommand {
150 SysExDiffCommand (boost::shared_ptr<MidiModel> m, const XMLNode& node);
156 int set_state (const XMLNode&, int version);
157 XMLNode & get_state ();
159 void remove (SysExPtr sysex);
163 void change (boost::shared_ptr<Evoral::Event<TimeType> >, TimeType);
167 boost::shared_ptr<Evoral::Event<TimeType> > sysex;
169 SysExDiffCommand::Property property;
174 typedef std::list<Change> ChangeList;
177 std::list<SysExPtr> _removed;
179 XMLNode & marshal_change (const Change &);
180 Change unmarshal_change (XMLNode *);
183 class PatchChangeDiffCommand : public DiffCommand {
185 PatchChangeDiffCommand (boost::shared_ptr<MidiModel>, const std::string &);
186 PatchChangeDiffCommand (boost::shared_ptr<MidiModel>, const XMLNode &);
188 int set_state (const XMLNode &, int version);
189 XMLNode & get_state ();
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);
210 PatchChangePtr patch;
226 Change() : patch_id (-1) {}
229 typedef std::list<Change> ChangeList;
232 std::list<PatchChangePtr> _added;
233 std::list<PatchChangePtr> _removed;
235 XMLNode & marshal_change (const Change &);
236 Change unmarshal_change (XMLNode *);
238 XMLNode & marshal_patch_change (constPatchChangePtr);
239 PatchChangePtr unmarshal_patch_change (XMLNode *);
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);
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);
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; }
258 PBD::Signal0<void> ContentsChanged;
260 boost::shared_ptr<const MidiSource> midi_source ();
261 void set_midi_source (boost::shared_ptr<MidiSource>);
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);
268 InsertMergePolicy insert_merge_policy () const;
269 void set_insert_merge_policy (InsertMergePolicy);
271 boost::shared_ptr<Evoral::Control> control_factory(const Evoral::Parameter& id);
273 void insert_silence_at_start (TimeType);
274 void transpose (TimeType, TimeType, int);
277 int resolve_overlaps_unlocked (const NotePtr, void* arg = 0);
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)
288 Glib::Threads::Mutex::Lock* source_lock;
292 WriteLock edit_lock();
293 WriteLock write_lock();
296 friend class DeltaCommand;
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);
303 void control_list_marked_dirty ();
305 PBD::ScopedConnectionList _midi_source_connections;
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;
312 } /* namespace ARDOUR */
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*/
316 #endif /* __ardour_midi_model_h__ */