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 * FIXME: Currently this stores event time stamps in frames. This is almost
53 * certainly wrong, or at least wrong most of the time (if we add an option).
54 * This reeeeeeally needs fixing, but frame time runs deep in Ardour...
56 class MidiModel : public boost::noncopyable, public Automatable {
58 MidiModel(Session& s, size_t size=0);
61 void write_lock() { _lock.writer_lock(); _automation_lock.lock(); }
62 void write_unlock() { _lock.writer_unlock(); _automation_lock.unlock(); }
63 void read_lock() const { _lock.reader_lock(); /*_automation_lock.lock();*/ }
64 void read_unlock() const { _lock.reader_unlock(); /*_automation_lock.unlock();*/ }
66 void clear() { _notes.clear(); }
68 NoteMode note_mode() const { return _note_mode; }
69 void set_note_mode(NoteMode mode) { _note_mode = mode; }
72 bool writing() const { return _writing; }
73 void end_write(bool delete_stuck=false);
75 size_t read (MidiRingBuffer& dst, nframes_t start, nframes_t nframes, nframes_t stamp_offset) const;
77 /** Resizes vector if necessary (NOT realtime safe) */
78 void append(const MidiEvent& ev);
80 inline const boost::shared_ptr<const Note> note_at(unsigned i) const { return _notes[i]; }
81 inline const boost::shared_ptr<Note> note_at(unsigned i) { return _notes[i]; }
83 inline size_t n_notes() const { return _notes.size(); }
84 inline bool empty() const { return _notes.size() == 0 && _controls.size() == 0; }
86 /* FIXME: use better data structure */
87 typedef std::vector< boost::shared_ptr<Note> > Notes;
89 inline static bool note_time_comparator (const boost::shared_ptr<const Note> a,
90 const boost::shared_ptr<const Note> b) {
91 return a->time() < b->time();
94 struct LaterNoteEndComparator {
95 typedef const Note* value_type;
96 inline bool operator()(const boost::shared_ptr<const Note> a,
97 const boost::shared_ptr<const Note> b) const {
98 return a->end_time() > b->end_time();
102 inline Notes& notes() { return _notes; }
103 inline const Notes& notes() const { return _notes; }
105 /** Add/Remove notes.
106 * Technically all operations can be implemented as one of these.
108 class DeltaCommand : public Command
111 DeltaCommand (MidiModel& m, const std::string& name)
112 : Command(name), _model(m), _name(name) {}
113 //DeltaCommand (MidiModel&, const XMLNode& node);
115 const std::string& name() const { return _name; }
120 /*int set_state (const XMLNode&);
121 XMLNode& get_state ();*/
123 void add(const boost::shared_ptr<Note> note);
124 void remove(const boost::shared_ptr<Note> note);
128 const std::string _name;
129 std::list< boost::shared_ptr<Note> > _added_notes;
130 std::list< boost::shared_ptr<Note> > _removed_notes;
133 MidiModel::DeltaCommand* new_delta_command(const std::string name="midi edit");
134 void apply_command(Command* cmd);
136 bool edited() const { return _edited; }
137 void set_edited(bool yn) { _edited = yn; }
138 bool write_to(boost::shared_ptr<MidiSource> source);
140 // MidiModel doesn't use the normal AutomationList serialisation code, as CC data is in the .mid
141 XMLNode& get_state();
142 int set_state(const XMLNode&) { return 0; }
144 sigc::signal<void> ContentsChanged;
147 class const_iterator {
149 const_iterator(const MidiModel& model, double t);
152 inline bool locked() const { return _locked; }
154 const MidiEvent& operator*() const { return _event; }
155 const MidiEvent* operator->() const { return &_event; }
157 const const_iterator& operator++(); // prefix only
158 bool operator==(const const_iterator& other) const;
159 bool operator!=(const const_iterator& other) const { return ! operator==(other); }
161 const_iterator& operator=(const const_iterator& other);
164 friend class MidiModel;
166 const MidiModel* _model;
169 typedef std::priority_queue<
170 boost::shared_ptr<Note>, std::deque< boost::shared_ptr<Note> >,
171 LaterNoteEndComparator>
174 mutable ActiveNotes _active_notes;
178 Notes::const_iterator _note_iter;
179 std::vector<MidiControlIterator> _control_iters;
180 std::vector<MidiControlIterator>::iterator _control_iter;
183 const_iterator begin() const { return const_iterator(*this, 0); }
184 const const_iterator& end() const { return _end_iter; }
187 friend class DeltaCommand;
188 void add_note_unlocked(const boost::shared_ptr<Note> note);
189 void remove_note_unlocked(const boost::shared_ptr<const Note> note);
191 friend class const_iterator;
192 bool control_to_midi_event(MidiEvent& ev, const MidiControlIterator& iter) const;
195 bool is_sorted() const;
198 void append_note_on_unlocked(uint8_t chan, double time, uint8_t note, uint8_t velocity);
199 void append_note_off_unlocked(uint8_t chan, double time, uint8_t note);
200 void append_cc_unlocked(uint8_t chan, double time, uint8_t number, uint8_t value);
202 mutable Glib::RWLock _lock;
207 typedef std::vector<size_t> WriteNotes;
208 WriteNotes _write_notes[16];
212 const const_iterator _end_iter;
214 mutable nframes_t _next_read;
215 mutable const_iterator _read_iter;
217 typedef std::priority_queue<
218 boost::shared_ptr<Note>, std::deque< boost::shared_ptr<Note> >,
219 LaterNoteEndComparator>
223 } /* namespace ARDOUR */
225 #endif /* __ardour_midi_model_h__ */