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;
83 typedef typename boost::shared_ptr<Evoral::Note<Time> > NotePtr;
84 typedef typename boost::shared_ptr<const Evoral::Note<Time> > constNotePtr;
86 typedef boost::shared_ptr<Glib::RWLock::ReaderLock> ReadLock;
87 typedef boost::shared_ptr<WriteLockImpl> WriteLock;
89 virtual ReadLock read_lock() const { return ReadLock(new Glib::RWLock::ReaderLock(_lock)); }
90 virtual WriteLock write_lock() { return WriteLock(new WriteLockImpl(_lock, _control_lock)); }
94 bool percussive() const { return _percussive; }
95 void set_percussive(bool p) { _percussive = p; }
98 bool writing() const { return _writing; }
99 void end_write(bool delete_stuck=false);
101 void append(const Event<Time>& ev);
103 inline size_t n_notes() const { return _notes.size(); }
104 inline bool empty() const { return _notes.size() == 0 && ControlSet::controls_empty(); }
106 inline static bool note_time_comparator(const boost::shared_ptr< const Note<Time> >& a,
107 const boost::shared_ptr< const Note<Time> >& b) {
108 return a->time() < b->time();
111 struct NoteNumberComparator {
112 inline bool operator()(const boost::shared_ptr< const Note<Time> > a,
113 const boost::shared_ptr< const Note<Time> > b) const {
114 return a->note() < b->note();
118 struct EarlierNoteComparator {
119 inline bool operator()(const boost::shared_ptr< const Note<Time> > a,
120 const boost::shared_ptr< const Note<Time> > b) const {
121 return a->time() < b->time();
125 struct LaterNoteComparator {
126 typedef const Note<Time>* value_type;
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 LaterNoteEndComparator {
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->end_time() > b->end_time();
141 typedef std::multiset<NotePtr, EarlierNoteComparator> Notes;
142 inline Notes& notes() { return _notes; }
143 inline const Notes& notes() const { return _notes; }
148 PitchLessThanOrEqual,
150 PitchGreaterThanOrEqual,
153 VelocityLessThanOrEqual,
155 VelocityGreaterThanOrEqual,
158 void get_notes (Notes&, NoteOperator, uint8_t val, int chan_mask = 0) const;
160 void remove_overlapping_notes ();
161 void trim_overlapping_notes ();
162 void remove_duplicate_notes ();
164 enum OverlapPitchResolution {
169 bool overlapping_pitches_accepted() const { return _overlapping_pitches_accepted; }
170 void overlapping_pitches_accepted(bool yn) { _overlapping_pitches_accepted = yn; }
171 OverlapPitchResolution overlap_pitch_resolution() const { return _overlap_pitch_resolution; }
172 void set_overlap_pitch_resolution(OverlapPitchResolution opr);
174 void set_notes (const Sequence<Time>::Notes& n);
176 typedef std::vector< boost::shared_ptr< Event<Time> > > SysExes;
177 inline SysExes& sysexes() { return _sysexes; }
178 inline const SysExes& sysexes() const { return _sysexes; }
181 typedef std::priority_queue<NotePtr, std::deque<NotePtr>, LaterNoteEndComparator> ActiveNotes;
185 class const_iterator {
188 const_iterator(const Sequence<Time>& seq, Time t, bool, std::set<Evoral::Parameter> const &);
191 inline bool valid() const { return !_is_end && _event; }
192 //inline bool locked() const { return _locked; }
196 const Event<Time>& operator*() const { return *_event; }
197 const boost::shared_ptr< Event<Time> > operator->() const { return _event; }
198 const boost::shared_ptr< Event<Time> > get_event_pointer() { return _event; }
200 const const_iterator& operator++(); // prefix only
202 bool operator==(const const_iterator& other) const;
203 bool operator!=(const const_iterator& other) const { return ! operator==(other); }
205 const_iterator& operator=(const const_iterator& other);
208 friend class Sequence<Time>;
210 typedef std::vector<ControlIterator> ControlIterators;
211 enum MIDIMessageType { NIL, NOTE_ON, NOTE_OFF, CONTROL, SYSEX };
213 const Sequence<Time>* _seq;
214 boost::shared_ptr< Event<Time> > _event;
215 mutable ActiveNotes _active_notes;
216 MIDIMessageType _type;
218 typename Sequence::ReadLock _lock;
219 typename Notes::const_iterator _note_iter;
220 typename SysExes::const_iterator _sysex_iter;
221 ControlIterators _control_iters;
222 ControlIterators::iterator _control_iter;
223 bool _force_discrete;
226 const_iterator begin (Time t = 0, bool force_discrete = false, std::set<Evoral::Parameter> const & f = std::set<Evoral::Parameter> ()) const {
227 return const_iterator (*this, t, force_discrete, f);
229 const const_iterator& end() const { return _end_iter; }
231 typename Notes::const_iterator note_lower_bound (Time t) const;
233 bool control_to_midi_event(boost::shared_ptr< Event<Time> >& ev,
234 const ControlIterator& iter) const;
236 bool edited() const { return _edited; }
237 void set_edited(bool yn) { _edited = yn; }
239 bool overlaps (const NotePtr& ev,
240 const NotePtr& ignore_this_note) const;
241 bool contains (const NotePtr& ev) const;
243 bool add_note_unlocked (const NotePtr note, void* arg = 0);
244 void remove_note_unlocked(const constNotePtr note);
246 uint8_t lowest_note() const { return _lowest_note; }
247 uint8_t highest_note() const { return _highest_note; }
252 bool _overlapping_pitches_accepted;
253 OverlapPitchResolution _overlap_pitch_resolution;
254 mutable Glib::RWLock _lock;
257 virtual int resolve_overlaps_unlocked (const NotePtr, void* arg = 0) {
261 typedef std::multiset<NotePtr, NoteNumberComparator> Pitches;
262 inline Pitches& pitches(uint8_t chan) { return _pitches[chan&0xf]; }
263 inline const Pitches& pitches(uint8_t chan) const { return _pitches[chan&0xf]; }
266 friend class const_iterator;
268 bool overlaps_unlocked (const NotePtr& ev, const NotePtr& ignore_this_note) const;
269 bool contains_unlocked (const NotePtr& ev) const;
271 void append_note_on_unlocked (NotePtr);
272 void append_note_off_unlocked(NotePtr);
273 void append_control_unlocked(const Parameter& param, Time time, double value);
274 void append_sysex_unlocked(const MIDIEvent<Time>& ev);
276 void get_notes_by_pitch (Notes&, NoteOperator, uint8_t val, int chan_mask = 0) const;
277 void get_notes_by_velocity (Notes&, NoteOperator, uint8_t val, int chan_mask = 0) const;
279 void control_list_marked_dirty ();
281 const TypeMap& _type_map;
283 Notes _notes; // notes indexed by time
284 Pitches _pitches[16]; // notes indexed by channel+pitch
287 typedef std::multiset<NotePtr, EarlierNoteComparator> WriteNotes;
288 WriteNotes _write_notes[16];
290 const const_iterator _end_iter;
293 uint8_t _lowest_note;
294 uint8_t _highest_note;
298 } // namespace Evoral
300 #endif // EVORAL_SEQUENCE_HPP