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/threads.h>
30 #include "evoral/visibility.h"
31 #include "evoral/types.hpp"
32 #include "evoral/Note.hpp"
33 #include "evoral/ControlSet.hpp"
34 #include "evoral/ControlList.hpp"
35 #include "evoral/PatchChange.hpp"
41 template<typename Time> class EventSink;
42 template<typename Time> class Note;
43 template<typename Time> class Event;
45 /** An iterator over (the x axis of) a 2-d double coordinate space.
47 class /*LIBEVORAL_API*/ ControlIterator {
49 ControlIterator(boost::shared_ptr<const ControlList> al, double ax, double ay)
55 boost::shared_ptr<const ControlList> list;
61 /** This is a higher level view of events, with separate representations for
62 * notes (instead of just unassociated note on/off events) and controller data.
63 * Controller data is represented as a list of time-stamped float values. */
64 template<typename Time>
65 class LIBEVORAL_API Sequence : virtual public ControlSet {
67 Sequence(const TypeMap& type_map);
68 Sequence(const Sequence<Time>& other);
71 struct WriteLockImpl {
72 WriteLockImpl(Glib::Threads::RWLock& s, Glib::Threads::Mutex& c)
73 : sequence_lock(new Glib::Threads::RWLock::WriterLock(s))
74 , control_lock(new Glib::Threads::Mutex::Lock(c)) { }
79 Glib::Threads::RWLock::WriterLock* sequence_lock;
80 Glib::Threads::Mutex::Lock* control_lock;
85 typedef typename boost::shared_ptr<Evoral::Note<Time> > NotePtr;
86 typedef typename boost::weak_ptr<Evoral::Note<Time> > WeakNotePtr;
87 typedef typename boost::shared_ptr<const Evoral::Note<Time> > constNotePtr;
89 typedef boost::shared_ptr<Glib::Threads::RWLock::ReaderLock> ReadLock;
90 typedef boost::shared_ptr<WriteLockImpl> WriteLock;
92 virtual ReadLock read_lock() const { return ReadLock(new Glib::Threads::RWLock::ReaderLock(_lock)); }
93 virtual WriteLock write_lock() { return WriteLock(new WriteLockImpl(_lock, _control_lock)); }
97 bool percussive() const { return _percussive; }
98 void set_percussive(bool p) { _percussive = p; }
101 bool writing() const { return _writing; }
103 enum StuckNoteOption {
109 void end_write (StuckNoteOption, Time when = Time());
111 void append(const Event<Time>& ev, Evoral::event_id_t evid);
113 const TypeMap& type_map() const { return _type_map; }
115 inline size_t n_notes() const { return _notes.size(); }
116 inline bool empty() const { return _notes.empty() && _sysexes.empty() && _patch_changes.empty() && ControlSet::controls_empty(); }
118 inline static bool note_time_comparator(const boost::shared_ptr< const Note<Time> >& a,
119 const boost::shared_ptr< const Note<Time> >& b) {
120 return a->time() < b->time();
123 struct NoteNumberComparator {
124 inline bool operator()(const boost::shared_ptr< const Note<Time> > a,
125 const boost::shared_ptr< const Note<Time> > b) const {
126 return a->note() < b->note();
130 struct EarlierNoteComparator {
131 inline bool operator()(const boost::shared_ptr< const Note<Time> > a,
132 const boost::shared_ptr< const Note<Time> > b) const {
133 return a->time() < b->time();
137 struct LaterNoteComparator {
138 typedef const Note<Time>* value_type;
139 inline bool operator()(const boost::shared_ptr< const Note<Time> > a,
140 const boost::shared_ptr< const Note<Time> > b) const {
141 return a->time() > b->time();
145 struct LaterNoteEndComparator {
146 typedef const Note<Time>* value_type;
147 inline bool operator()(const boost::shared_ptr< const Note<Time> > a,
148 const boost::shared_ptr< const Note<Time> > b) const {
149 return a->end_time() > b->end_time();
153 typedef std::multiset<NotePtr, EarlierNoteComparator> Notes;
154 inline Notes& notes() { return _notes; }
155 inline const Notes& notes() const { return _notes; }
160 PitchLessThanOrEqual,
162 PitchGreaterThanOrEqual,
165 VelocityLessThanOrEqual,
167 VelocityGreaterThanOrEqual,
170 void get_notes (Notes&, NoteOperator, uint8_t val, int chan_mask = 0) const;
172 void remove_overlapping_notes ();
173 void trim_overlapping_notes ();
174 void remove_duplicate_notes ();
176 enum OverlapPitchResolution {
181 bool overlapping_pitches_accepted() const { return _overlapping_pitches_accepted; }
182 void overlapping_pitches_accepted(bool yn) { _overlapping_pitches_accepted = yn; }
183 OverlapPitchResolution overlap_pitch_resolution() const { return _overlap_pitch_resolution; }
184 void set_overlap_pitch_resolution(OverlapPitchResolution opr);
186 void set_notes (const typename Sequence<Time>::Notes& n);
188 typedef boost::shared_ptr< Event<Time> > SysExPtr;
189 typedef boost::shared_ptr<const Event<Time> > constSysExPtr;
191 struct EarlierSysExComparator {
192 inline bool operator() (constSysExPtr a, constSysExPtr b) const {
193 return a->time() < b->time();
197 typedef std::multiset<SysExPtr, EarlierSysExComparator> SysExes;
198 inline SysExes& sysexes() { return _sysexes; }
199 inline const SysExes& sysexes() const { return _sysexes; }
201 typedef boost::shared_ptr<PatchChange<Time> > PatchChangePtr;
202 typedef boost::shared_ptr<const PatchChange<Time> > constPatchChangePtr;
204 struct EarlierPatchChangeComparator {
205 inline bool operator() (constPatchChangePtr a, constPatchChangePtr b) const {
206 return a->time() < b->time();
210 typedef std::multiset<PatchChangePtr, EarlierPatchChangeComparator> PatchChanges;
211 inline PatchChanges& patch_changes () { return _patch_changes; }
212 inline const PatchChanges& patch_changes () const { return _patch_changes; }
214 void dump (std::ostream&) const;
217 typedef std::priority_queue<NotePtr, std::deque<NotePtr>, LaterNoteEndComparator> ActiveNotes;
221 class LIBEVORAL_API const_iterator {
224 const_iterator(const Sequence<Time>& seq,
227 const std::set<Evoral::Parameter>& filtered,
228 const std::set<WeakNotePtr>* active_notes=NULL);
230 inline bool valid() const { return !_is_end && _event; }
232 void invalidate(std::set<WeakNotePtr>* notes);
234 const Event<Time>& operator*() const { return *_event; }
235 const boost::shared_ptr< Event<Time> > operator->() const { return _event; }
236 const boost::shared_ptr< Event<Time> > get_event_pointer() { return _event; }
238 const const_iterator& operator++(); // prefix only
240 bool operator==(const const_iterator& other) const;
241 bool operator!=(const const_iterator& other) const { return ! operator==(other); }
243 const_iterator& operator=(const const_iterator& other);
246 friend class Sequence<Time>;
248 Time choose_next(Time earliest_t);
251 typedef std::vector<ControlIterator> ControlIterators;
252 enum MIDIMessageType { NIL, NOTE_ON, NOTE_OFF, CONTROL, SYSEX, PATCH_CHANGE };
254 const Sequence<Time>* _seq;
255 boost::shared_ptr< Event<Time> > _event;
256 mutable ActiveNotes _active_notes;
257 /** If the iterator is pointing at a patch change, this is the index of the
258 * sub-message within that change.
260 int _active_patch_change_message;
261 MIDIMessageType _type;
263 typename Sequence::ReadLock _lock;
264 typename Notes::const_iterator _note_iter;
265 typename SysExes::const_iterator _sysex_iter;
266 typename PatchChanges::const_iterator _patch_change_iter;
267 ControlIterators _control_iters;
268 ControlIterators::iterator _control_iter;
269 bool _force_discrete;
272 const_iterator begin (
274 bool force_discrete = false,
275 const std::set<Evoral::Parameter>& f = std::set<Evoral::Parameter>(),
276 const std::set<WeakNotePtr>* active_notes = NULL) const {
277 return const_iterator (*this, t, force_discrete, f, active_notes);
280 const const_iterator& end() const { return _end_iter; }
282 // CONST iterator implementations (x3)
283 typename Notes::const_iterator note_lower_bound (Time t) const;
284 typename PatchChanges::const_iterator patch_change_lower_bound (Time t) const;
285 typename SysExes::const_iterator sysex_lower_bound (Time t) const;
287 // NON-CONST iterator implementations (x3)
288 typename Notes::iterator note_lower_bound (Time t);
289 typename PatchChanges::iterator patch_change_lower_bound (Time t);
290 typename SysExes::iterator sysex_lower_bound (Time t);
292 bool control_to_midi_event(boost::shared_ptr< Event<Time> >& ev,
293 const ControlIterator& iter) const;
295 bool edited() const { return _edited; }
296 void set_edited(bool yn) { _edited = yn; }
298 bool overlaps (const NotePtr& ev,
299 const NotePtr& ignore_this_note) const;
300 bool contains (const NotePtr& ev) const;
302 bool add_note_unlocked (const NotePtr note, void* arg = 0);
303 void remove_note_unlocked(const constNotePtr note);
305 void add_patch_change_unlocked (const PatchChangePtr);
306 void remove_patch_change_unlocked (const constPatchChangePtr);
308 void add_sysex_unlocked (const SysExPtr);
309 void remove_sysex_unlocked (const SysExPtr);
311 uint8_t lowest_note() const { return _lowest_note; }
312 uint8_t highest_note() const { return _highest_note; }
317 bool _overlapping_pitches_accepted;
318 OverlapPitchResolution _overlap_pitch_resolution;
319 mutable Glib::Threads::RWLock _lock;
322 virtual int resolve_overlaps_unlocked (const NotePtr, void* /* arg */ = 0) {
326 typedef std::multiset<NotePtr, NoteNumberComparator> Pitches;
327 inline Pitches& pitches(uint8_t chan) { return _pitches[chan&0xf]; }
328 inline const Pitches& pitches(uint8_t chan) const { return _pitches[chan&0xf]; }
330 virtual void control_list_marked_dirty ();
333 friend class const_iterator;
335 bool overlaps_unlocked (const NotePtr& ev, const NotePtr& ignore_this_note) const;
336 bool contains_unlocked (const NotePtr& ev) const;
338 void append_note_on_unlocked(const MIDIEvent<Time>& event, Evoral::event_id_t);
339 void append_note_off_unlocked(const MIDIEvent<Time>& event);
340 void append_control_unlocked(const Parameter& param, Time time, double value, Evoral::event_id_t);
341 void append_sysex_unlocked(const MIDIEvent<Time>& ev, Evoral::event_id_t);
342 void append_patch_change_unlocked(const PatchChange<Time>&, Evoral::event_id_t);
344 void get_notes_by_pitch (Notes&, NoteOperator, uint8_t val, int chan_mask = 0) const;
345 void get_notes_by_velocity (Notes&, NoteOperator, uint8_t val, int chan_mask = 0) const;
347 const TypeMap& _type_map;
349 Notes _notes; // notes indexed by time
350 Pitches _pitches[16]; // notes indexed by channel+pitch
352 PatchChanges _patch_changes;
354 typedef std::multiset<NotePtr, EarlierNoteComparator> WriteNotes;
355 WriteNotes _write_notes[16];
357 /** Current bank number on each channel so that we know what
358 * to put in PatchChange events when program changes are
363 const const_iterator _end_iter;
366 uint8_t _lowest_note;
367 uint8_t _highest_note;
371 } // namespace Evoral
373 template<typename Time> /*LIBEVORAL_API*/ std::ostream& operator<<(std::ostream& o, const Evoral::Sequence<Time>& s) { s.dump (o); return o; }
376 #endif // EVORAL_SEQUENCE_HPP