1 /* This file is part of Evoral.
2 * Copyright (C) 2008 Dave 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"
38 template<typename Time> class EventSink;
39 template<typename Time> class Note;
40 template<typename Time> class Event;
42 /** An iterator over (the x axis of) a 2-d double coordinate space.
44 class ControlIterator {
46 ControlIterator(boost::shared_ptr<const ControlList> al, double ax, double ay)
52 boost::shared_ptr<const ControlList> list;
58 /** This is a higher level view of events, with separate representations for
59 * notes (instead of just unassociated note on/off events) and controller data.
60 * Controller data is represented as a list of time-stamped float values. */
61 template<typename Time>
62 class Sequence : virtual public ControlSet {
64 Sequence(const TypeMap& type_map);
65 Sequence(const Sequence<Time>& other);
68 struct WriteLockImpl {
69 WriteLockImpl(Glib::RWLock& s, Glib::Mutex& c)
70 : sequence_lock(new Glib::RWLock::WriterLock(s))
71 , control_lock(new Glib::Mutex::Lock(c))
77 Glib::RWLock::WriterLock* sequence_lock;
78 Glib::Mutex::Lock* control_lock;
82 typedef typename boost::shared_ptr<Evoral::Note<Time> > NotePtr;
83 typedef typename boost::shared_ptr<const Evoral::Note<Time> > constNotePtr;
85 typedef boost::shared_ptr<Glib::RWLock::ReaderLock> ReadLock;
86 typedef boost::shared_ptr<WriteLockImpl> WriteLock;
88 virtual ReadLock read_lock() const { return ReadLock(new Glib::RWLock::ReaderLock(_lock)); }
89 virtual WriteLock write_lock() { return WriteLock(new WriteLockImpl(_lock, _control_lock)); }
93 bool percussive() const { return _percussive; }
94 void set_percussive(bool p) { _percussive = p; }
97 bool writing() const { return _writing; }
98 void end_write(bool delete_stuck=false);
100 void append(const Event<Time>& ev);
102 inline size_t n_notes() const { return _notes.size(); }
103 inline bool empty() const { return _notes.size() == 0 && ControlSet::controls_empty(); }
105 inline static bool note_time_comparator(const boost::shared_ptr< const Note<Time> >& a,
106 const boost::shared_ptr< const Note<Time> >& b) {
107 return a->time() < b->time();
110 struct NoteNumberComparator {
111 inline bool operator()(const boost::shared_ptr< const Note<Time> > a,
112 const boost::shared_ptr< const Note<Time> > b) const {
113 return a->note() < b->note();
117 struct EarlierNoteComparator {
118 inline bool operator()(const boost::shared_ptr< const Note<Time> > a,
119 const boost::shared_ptr< const Note<Time> > b) const {
120 return a->time() < b->time();
124 struct LaterNoteComparator {
125 typedef const Note<Time>* value_type;
126 inline bool operator()(const boost::shared_ptr< const Note<Time> > a,
127 const boost::shared_ptr< const Note<Time> > b) const {
128 return a->time() > b->time();
132 struct LaterNoteEndComparator {
133 typedef const Note<Time>* value_type;
134 inline bool operator()(const boost::shared_ptr< const Note<Time> > a,
135 const boost::shared_ptr< const Note<Time> > b) const {
136 return a->end_time() > b->end_time();
140 typedef std::multiset<NotePtr, EarlierNoteComparator> Notes;
141 inline Notes& notes() { return _notes; }
142 inline const Notes& notes() const { return _notes; }
147 PitchLessThanOrEqual,
149 PitchGreaterThanOrEqual,
152 VelocityLessThanOrEqual,
154 VelocityGreaterThanOrEqual,
157 void get_notes (Notes&, NoteOperator, uint8_t val, int chan_mask = 0) const;
159 void remove_overlapping_notes ();
160 void trim_overlapping_notes ();
161 void remove_duplicate_notes ();
163 enum OverlapPitchResolution {
168 bool overlapping_pitches_accepted() const { return _overlapping_pitches_accepted; }
169 void overlapping_pitches_accepted(bool yn) { _overlapping_pitches_accepted = yn; }
170 OverlapPitchResolution overlap_pitch_resolution() const { return _overlap_pitch_resolution; }
171 void set_overlap_pitch_resolution(OverlapPitchResolution opr);
173 void set_notes (const Sequence<Time>::Notes& n);
175 typedef std::vector< boost::shared_ptr< Event<Time> > > SysExes;
176 inline SysExes& sysexes() { return _sysexes; }
177 inline const SysExes& sysexes() const { return _sysexes; }
180 typedef std::priority_queue<NotePtr, std::deque<NotePtr>, LaterNoteEndComparator> ActiveNotes;
184 class const_iterator {
187 const_iterator(const Sequence<Time>& seq, Time t);
190 inline bool valid() const { return !_is_end && _event; }
191 //inline bool locked() const { return _locked; }
195 const Event<Time>& operator*() const { return *_event; }
196 const boost::shared_ptr< Event<Time> > operator->() const { return _event; }
197 const boost::shared_ptr< Event<Time> > get_event_pointer() { return _event; }
199 const const_iterator& operator++(); // prefix only
201 bool operator==(const const_iterator& other) const;
202 bool operator!=(const const_iterator& other) const { return ! operator==(other); }
204 const_iterator& operator=(const const_iterator& other);
207 friend class Sequence<Time>;
209 typedef std::vector<ControlIterator> ControlIterators;
210 enum MIDIMessageType { NIL, NOTE_ON, NOTE_OFF, CONTROL, SYSEX };
212 const Sequence<Time>* _seq;
213 boost::shared_ptr< Event<Time> > _event;
214 mutable ActiveNotes _active_notes;
215 MIDIMessageType _type;
217 typename Sequence::ReadLock _lock;
218 typename Notes::const_iterator _note_iter;
219 typename SysExes::const_iterator _sysex_iter;
220 ControlIterators _control_iters;
221 ControlIterators::iterator _control_iter;
224 const_iterator begin(Time t=0) const { return const_iterator(*this, t); }
225 const const_iterator& end() const { return _end_iter; }
227 typename Notes::const_iterator note_lower_bound (Time t) const;
229 bool control_to_midi_event(boost::shared_ptr< Event<Time> >& ev,
230 const ControlIterator& iter) const;
232 bool edited() const { return _edited; }
233 void set_edited(bool yn) { _edited = yn; }
235 bool overlaps (const NotePtr& ev,
236 const NotePtr& ignore_this_note) const;
237 bool contains (const NotePtr& ev) const;
239 bool add_note_unlocked (const NotePtr note, std::set<NotePtr>* removed = 0);
240 void remove_note_unlocked(const constNotePtr note);
242 uint8_t lowest_note() const { return _lowest_note; }
243 uint8_t highest_note() const { return _highest_note; }
248 bool _overlapping_pitches_accepted;
249 OverlapPitchResolution _overlap_pitch_resolution;
250 mutable Glib::RWLock _lock;
253 virtual int resolve_overlaps_unlocked (const NotePtr, std::set<NotePtr>* removed = 0) {
257 typedef std::multiset<NotePtr, NoteNumberComparator> Pitches;
258 inline Pitches& pitches(uint8_t chan) { return _pitches[chan&0xf]; }
259 inline const Pitches& pitches(uint8_t chan) const { return _pitches[chan&0xf]; }
262 friend class const_iterator;
264 bool overlaps_unlocked (const NotePtr& ev, const NotePtr& ignore_this_note) const;
265 bool contains_unlocked (const NotePtr& ev) const;
267 void append_note_on_unlocked (NotePtr);
268 void append_note_off_unlocked(NotePtr);
269 void append_control_unlocked(const Parameter& param, Time time, double value);
270 void append_sysex_unlocked(const MIDIEvent<Time>& ev);
272 void get_notes_by_pitch (Notes&, NoteOperator, uint8_t val, int chan_mask = 0) const;
273 void get_notes_by_velocity (Notes&, NoteOperator, uint8_t val, int chan_mask = 0) const;
275 const TypeMap& _type_map;
277 Notes _notes; // notes indexed by time
278 Pitches _pitches[16]; // notes indexed by channel+pitch
281 typedef std::multiset<NotePtr, EarlierNoteComparator> WriteNotes;
282 WriteNotes _write_notes[16];
284 typedef std::vector< boost::shared_ptr<const ControlList> > ControlLists;
285 ControlLists _dirty_controls;
287 const const_iterator _end_iter;
290 uint8_t _lowest_note;
291 uint8_t _highest_note;
295 } // namespace Evoral
297 #endif // EVORAL_SEQUENCE_HPP