1 /* This file is part of Evoral.
2 * Copyright (C) 2008 David Robillard <http://drobilla.net>
3 * Copyright (C) 2000-2008 Paul Davis
5 * Evoral is free software; you can redistribute it and/or modify it under the
6 * terms of the GNU General Public License as published by the Free Software
7 * Foundation; either version 2 of the License, or (at your option) any later
10 * Evoral is distributed in the hope that it will be useful, but WITHOUT ANY
11 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 #ifndef EVORAL_SEQUENCE_HPP
20 #define EVORAL_SEQUENCE_HPP
27 #include <boost/shared_ptr.hpp>
28 #include <glibmm/thread.h>
29 #include "evoral/types.hpp"
30 #include "evoral/Note.hpp"
31 #include "evoral/Parameter.hpp"
32 #include "evoral/ControlSet.hpp"
33 #include "evoral/ControlList.hpp"
34 #include "evoral/PatchChange.hpp"
39 template<typename Time> class EventSink;
40 template<typename Time> class Note;
41 template<typename Time> class Event;
43 /** An iterator over (the x axis of) a 2-d double coordinate space.
45 class ControlIterator {
47 ControlIterator(boost::shared_ptr<const ControlList> al, double ax, double ay)
53 boost::shared_ptr<const ControlList> list;
59 /** This is a higher level view of events, with separate representations for
60 * notes (instead of just unassociated note on/off events) and controller data.
61 * Controller data is represented as a list of time-stamped float values. */
62 template<typename Time>
63 class Sequence : virtual public ControlSet {
65 Sequence(const TypeMap& type_map);
66 Sequence(const Sequence<Time>& other);
69 struct WriteLockImpl {
70 WriteLockImpl(Glib::RWLock& s, Glib::Mutex& c)
71 : sequence_lock(new Glib::RWLock::WriterLock(s))
72 , control_lock(new Glib::Mutex::Lock(c))
78 Glib::RWLock::WriterLock* sequence_lock;
79 Glib::Mutex::Lock* control_lock;
84 typedef typename boost::shared_ptr<Evoral::Note<Time> > NotePtr;
85 typedef typename boost::shared_ptr<const Evoral::Note<Time> > constNotePtr;
87 typedef boost::shared_ptr<Glib::RWLock::ReaderLock> ReadLock;
88 typedef boost::shared_ptr<WriteLockImpl> WriteLock;
90 virtual ReadLock read_lock() const { return ReadLock(new Glib::RWLock::ReaderLock(_lock)); }
91 virtual WriteLock write_lock() { return WriteLock(new WriteLockImpl(_lock, _control_lock)); }
95 bool percussive() const { return _percussive; }
96 void set_percussive(bool p) { _percussive = p; }
99 bool writing() const { return _writing; }
101 enum StuckNoteOption {
107 void end_write (StuckNoteOption, Time when = 0);
109 void append(const Event<Time>& ev, Evoral::event_id_t evid);
111 inline size_t n_notes() const { return _notes.size(); }
112 inline bool empty() const { return _notes.empty() && _sysexes.empty() && _patch_changes.empty() && ControlSet::controls_empty(); }
114 inline static bool note_time_comparator(const boost::shared_ptr< const Note<Time> >& a,
115 const boost::shared_ptr< const Note<Time> >& b) {
116 return a->time() < b->time();
119 struct NoteNumberComparator {
120 inline bool operator()(const boost::shared_ptr< const Note<Time> > a,
121 const boost::shared_ptr< const Note<Time> > b) const {
122 return a->note() < b->note();
126 struct EarlierNoteComparator {
127 inline bool operator()(const boost::shared_ptr< const Note<Time> > a,
128 const boost::shared_ptr< const Note<Time> > b) const {
129 return a->time() < b->time();
133 struct LaterNoteComparator {
134 typedef const Note<Time>* value_type;
135 inline bool operator()(const boost::shared_ptr< const Note<Time> > a,
136 const boost::shared_ptr< const Note<Time> > b) const {
137 return a->time() > b->time();
141 struct LaterNoteEndComparator {
142 typedef const Note<Time>* value_type;
143 inline bool operator()(const boost::shared_ptr< const Note<Time> > a,
144 const boost::shared_ptr< const Note<Time> > b) const {
145 return a->end_time() > b->end_time();
149 typedef std::multiset<NotePtr, EarlierNoteComparator> Notes;
150 inline Notes& notes() { return _notes; }
151 inline const Notes& notes() const { return _notes; }
156 PitchLessThanOrEqual,
158 PitchGreaterThanOrEqual,
161 VelocityLessThanOrEqual,
163 VelocityGreaterThanOrEqual,
166 void get_notes (Notes&, NoteOperator, uint8_t val, int chan_mask = 0) const;
168 void remove_overlapping_notes ();
169 void trim_overlapping_notes ();
170 void remove_duplicate_notes ();
172 enum OverlapPitchResolution {
177 bool overlapping_pitches_accepted() const { return _overlapping_pitches_accepted; }
178 void overlapping_pitches_accepted(bool yn) { _overlapping_pitches_accepted = yn; }
179 OverlapPitchResolution overlap_pitch_resolution() const { return _overlap_pitch_resolution; }
180 void set_overlap_pitch_resolution(OverlapPitchResolution opr);
182 void set_notes (const Sequence<Time>::Notes& n);
184 typedef std::vector< boost::shared_ptr< Event<Time> > > SysExes;
185 inline SysExes& sysexes() { return _sysexes; }
186 inline const SysExes& sysexes() const { return _sysexes; }
188 typedef boost::shared_ptr<PatchChange<Time> > PatchChangePtr;
189 typedef boost::shared_ptr<const PatchChange<Time> > constPatchChangePtr;
191 struct EarlierPatchChangeComparator {
192 inline bool operator() (constPatchChangePtr a, constPatchChangePtr b) const {
193 return a->time() < b->time();
197 typedef std::multiset<PatchChangePtr, EarlierPatchChangeComparator> PatchChanges;
198 inline PatchChanges& patch_changes () { return _patch_changes; }
199 inline const PatchChanges& patch_changes () const { return _patch_changes; }
201 void dump (std::ostream&) const;
204 typedef std::priority_queue<NotePtr, std::deque<NotePtr>, LaterNoteEndComparator> ActiveNotes;
208 class const_iterator {
211 const_iterator(const Sequence<Time>& seq, Time t, bool, std::set<Evoral::Parameter> const &);
214 inline bool valid() const { return !_is_end && _event; }
215 //inline bool locked() const { return _locked; }
219 const Event<Time>& operator*() const { return *_event; }
220 const boost::shared_ptr< Event<Time> > operator->() const { return _event; }
221 const boost::shared_ptr< Event<Time> > get_event_pointer() { return _event; }
223 const const_iterator& operator++(); // prefix only
225 bool operator==(const const_iterator& other) const;
226 bool operator!=(const const_iterator& other) const { return ! operator==(other); }
228 const_iterator& operator=(const const_iterator& other);
231 friend class Sequence<Time>;
233 typedef std::vector<ControlIterator> ControlIterators;
234 enum MIDIMessageType { NIL, NOTE_ON, NOTE_OFF, CONTROL, SYSEX, PATCH_CHANGE };
236 const Sequence<Time>* _seq;
237 boost::shared_ptr< Event<Time> > _event;
238 mutable ActiveNotes _active_notes;
239 /** If the iterator is pointing at a patch change, this is the index of the
240 * sub-message within that change.
242 int _active_patch_change_message;
243 MIDIMessageType _type;
245 typename Sequence::ReadLock _lock;
246 typename Notes::const_iterator _note_iter;
247 typename SysExes::const_iterator _sysex_iter;
248 typename PatchChanges::const_iterator _patch_change_iter;
249 ControlIterators _control_iters;
250 ControlIterators::iterator _control_iter;
251 bool _force_discrete;
254 const_iterator begin (
256 bool force_discrete = false,
257 std::set<Evoral::Parameter> const & f = std::set<Evoral::Parameter> ()) const {
258 return const_iterator (*this, t, force_discrete, f);
261 const const_iterator& end() const { return _end_iter; }
263 typename Notes::const_iterator note_lower_bound (Time t) const;
264 typename PatchChanges::const_iterator patch_change_lower_bound (Time t) const;
266 bool control_to_midi_event(boost::shared_ptr< Event<Time> >& ev,
267 const ControlIterator& iter) const;
269 bool edited() const { return _edited; }
270 void set_edited(bool yn) { _edited = yn; }
272 bool overlaps (const NotePtr& ev,
273 const NotePtr& ignore_this_note) const;
274 bool contains (const NotePtr& ev) const;
276 bool add_note_unlocked (const NotePtr note, void* arg = 0);
277 void remove_note_unlocked(const constNotePtr note);
279 void add_patch_change_unlocked (const PatchChangePtr);
280 void remove_patch_change_unlocked (const constPatchChangePtr);
282 uint8_t lowest_note() const { return _lowest_note; }
283 uint8_t highest_note() const { return _highest_note; }
288 bool _overlapping_pitches_accepted;
289 OverlapPitchResolution _overlap_pitch_resolution;
290 mutable Glib::RWLock _lock;
293 virtual int resolve_overlaps_unlocked (const NotePtr, void* /* arg */ = 0) {
297 typedef std::multiset<NotePtr, NoteNumberComparator> Pitches;
298 inline Pitches& pitches(uint8_t chan) { return _pitches[chan&0xf]; }
299 inline const Pitches& pitches(uint8_t chan) const { return _pitches[chan&0xf]; }
301 virtual void control_list_marked_dirty ();
304 friend class const_iterator;
306 bool overlaps_unlocked (const NotePtr& ev, const NotePtr& ignore_this_note) const;
307 bool contains_unlocked (const NotePtr& ev) const;
309 void append_note_on_unlocked (NotePtr, Evoral::event_id_t);
310 void append_note_off_unlocked(NotePtr);
311 void append_control_unlocked(const Parameter& param, Time time, double value, Evoral::event_id_t);
312 void append_sysex_unlocked(const MIDIEvent<Time>& ev, Evoral::event_id_t);
313 void append_patch_change_unlocked (const PatchChange<Time>&, Evoral::event_id_t);
315 void get_notes_by_pitch (Notes&, NoteOperator, uint8_t val, int chan_mask = 0) const;
316 void get_notes_by_velocity (Notes&, NoteOperator, uint8_t val, int chan_mask = 0) const;
318 const TypeMap& _type_map;
320 Notes _notes; // notes indexed by time
321 Pitches _pitches[16]; // notes indexed by channel+pitch
323 PatchChanges _patch_changes;
325 typedef std::multiset<NotePtr, EarlierNoteComparator> WriteNotes;
326 WriteNotes _write_notes[16];
328 /** Current bank number on each channel so that we know what
329 * to put in PatchChange events when program changes are
334 const const_iterator _end_iter;
337 uint8_t _lowest_note;
338 uint8_t _highest_note;
342 } // namespace Evoral
344 // template<typename Time> std::ostream& operator<<(std::ostream& o, const Evoral::Sequence<Time>& s) { s.dump (o); return o; }
346 #endif // EVORAL_SEQUENCE_HPP