2 Copyright (C) 2007 Paul Davis
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/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.h>
34 #include <ardour/note.h>
42 typedef std::pair<boost::shared_ptr<const AutomationList>, std::pair<double,double> >
46 /** This is a slightly higher level (than MidiBuffer) model of MIDI note data.
47 * Currently it only represents note data, which is represented as complete
48 * note events (ie with a start time and a duration) rather than separate
49 * note on and off events (controller data is not here since it's represented
50 * as an AutomationList)
52 class MidiModel : public boost::noncopyable, public Automatable {
54 MidiModel(Session& s, size_t size=0);
57 void write_lock() { _lock.writer_lock(); _automation_lock.lock(); }
58 void write_unlock() { _lock.writer_unlock(); _automation_lock.unlock(); }
59 void read_lock() const { _lock.reader_lock(); /*_automation_lock.lock();*/ }
60 void read_unlock() const { _lock.reader_unlock(); /*_automation_lock.unlock();*/ }
62 void clear() { _notes.clear(); }
64 NoteMode note_mode() const { return _note_mode; }
65 void set_note_mode(NoteMode mode) { _note_mode = mode; }
68 bool writing() const { return _writing; }
69 void end_write(bool delete_stuck=false);
71 size_t read (MidiRingBuffer& dst, nframes_t start, nframes_t nframes, nframes_t stamp_offset) const;
73 /** Resizes vector if necessary (NOT realtime safe) */
74 void append(const MidiEvent& ev);
76 inline const boost::shared_ptr<const Note> note_at(unsigned i) const { return _notes[i]; }
77 inline const boost::shared_ptr<Note> note_at(unsigned i) { return _notes[i]; }
79 inline size_t n_notes() const { return _notes.size(); }
80 inline bool empty() const { return _notes.size() == 0 && _controls.size() == 0; }
82 /* FIXME: use better data structure */
83 typedef std::vector< boost::shared_ptr<Note> > Notes;
85 inline static bool note_time_comparator (const boost::shared_ptr<const Note> a,
86 const boost::shared_ptr<const Note> b) {
87 return a->time() < b->time();
90 struct LaterNoteEndComparator {
91 typedef const Note* value_type;
92 inline bool operator()(const boost::shared_ptr<const Note> a,
93 const boost::shared_ptr<const Note> b) const {
94 return a->end_time() > b->end_time();
98 inline Notes& notes() { return _notes; }
99 inline const Notes& notes() const { return _notes; }
101 /** Add/Remove notes.
102 * Technically all operations can be implemented as one of these.
104 class DeltaCommand : public Command
107 DeltaCommand (MidiModel& m, const std::string& name)
108 : Command(name), _model(m), _name(name) {}
109 //DeltaCommand (MidiModel&, const XMLNode& node);
111 const std::string& name() const { return _name; }
116 /*int set_state (const XMLNode&);
117 XMLNode& get_state ();*/
119 void add(const boost::shared_ptr<Note> note);
120 void remove(const boost::shared_ptr<Note> note);
124 const std::string _name;
125 std::list< boost::shared_ptr<Note> > _added_notes;
126 std::list< boost::shared_ptr<Note> > _removed_notes;
129 MidiModel::DeltaCommand* new_delta_command(const std::string name="midi edit");
130 void apply_command(Command* cmd);
132 bool edited() const { return _edited; }
133 void set_edited(bool yn) { _edited = yn; }
134 bool write_to(boost::shared_ptr<MidiSource> source);
136 // MidiModel doesn't use the normal AutomationList serialisation code, as CC data is in the .mid
137 XMLNode& get_state();
138 int set_state(const XMLNode&) { return 0; }
140 sigc::signal<void> ContentsChanged;
143 class const_iterator {
145 const_iterator(const MidiModel& model, double t);
148 inline bool locked() const { return _locked; }
150 const MidiEvent& operator*() const { return _event; }
151 const MidiEvent* operator->() const { return &_event; }
153 const const_iterator& operator++(); // prefix only
154 bool operator==(const const_iterator& other) const;
155 bool operator!=(const const_iterator& other) const { return ! operator==(other); }
157 const_iterator& operator=(const const_iterator& other);
160 friend class MidiModel;
162 const MidiModel* _model;
165 typedef std::priority_queue<
166 boost::shared_ptr<Note>, std::deque< boost::shared_ptr<Note> >,
167 LaterNoteEndComparator>
170 mutable ActiveNotes _active_notes;
174 Notes::const_iterator _note_iter;
175 std::vector<MidiControlIterator> _control_iters;
176 std::vector<MidiControlIterator>::iterator _control_iter;
179 const_iterator begin() const { return const_iterator(*this, 0); }
180 const const_iterator& end() const { return _end_iter; }
183 friend class DeltaCommand;
184 void add_note_unlocked(const boost::shared_ptr<Note> note);
185 void remove_note_unlocked(const boost::shared_ptr<const Note> note);
187 friend class const_iterator;
188 bool control_to_midi_event(MidiEvent& ev, const MidiControlIterator& iter) const;
191 bool is_sorted() const;
194 void append_note_on_unlocked(double time, uint8_t note, uint8_t velocity);
195 void append_note_off_unlocked(double time, uint8_t note);
196 void append_cc_unlocked(double time, uint8_t number, uint8_t value);
198 mutable Glib::RWLock _lock;
203 typedef std::vector<size_t> WriteNotes;
204 WriteNotes _write_notes;
208 const const_iterator _end_iter;
210 mutable nframes_t _next_read;
211 mutable const_iterator _read_iter;
213 typedef std::priority_queue<
214 boost::shared_ptr<Note>, std::deque< boost::shared_ptr<Note> >,
215 LaterNoteEndComparator>
219 } /* namespace ARDOUR */
221 #endif /* __ardour_midi_model_h__ */