Sequence::contains() and Sequence::overlaps() now use pitch-based indexing to speed...
[ardour.git] / libs / evoral / evoral / Sequence.hpp
1 /* This file is part of Evoral.
2  * Copyright (C) 2008 Dave Robillard <http://drobilla.net>
3  * Copyright (C) 2000-2008 Paul Davis
4  *
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
8  * version.
9  *
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.
13  *
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
17  */
18
19 #ifndef EVORAL_SEQUENCE_HPP
20 #define EVORAL_SEQUENCE_HPP
21
22 #include <vector>
23 #include <queue>
24 #include <set>
25 #include <utility>
26 #include <boost/shared_ptr.hpp>
27 #include <glibmm/thread.h>
28 #include "evoral/types.hpp"
29 #include "evoral/Note.hpp"
30 #include "evoral/Parameter.hpp"
31 #include "evoral/ControlSet.hpp"
32 #include "evoral/ControlList.hpp"
33
34 namespace Evoral {
35
36 class TypeMap;
37 template<typename Time> class EventSink;
38 template<typename Time> class Note;
39 template<typename Time> class Event;
40
41 /** An iterator over (the x axis of) a 2-d double coordinate space.
42  */
43 class ControlIterator {
44 public:
45         ControlIterator(boost::shared_ptr<const ControlList> al, double ax, double ay)
46                 : list(al)
47                 , x(ax)
48                 , y(ay)
49         {}
50
51         boost::shared_ptr<const ControlList> list;
52         double x;
53         double y;
54 };
55
56
57 /** This is a higher level view of events, with separate representations for
58  * notes (instead of just unassociated note on/off events) and controller data.
59  * Controller data is represented as a list of time-stamped float values. */
60 template<typename Time>
61 class Sequence : virtual public ControlSet {
62 public:
63         Sequence(const TypeMap& type_map);
64         Sequence(const Sequence<Time>& other);
65
66 protected:
67         struct WriteLockImpl {
68                 WriteLockImpl(Glib::RWLock& s, Glib::Mutex& c)
69                         : sequence_lock(new Glib::RWLock::WriterLock(s))
70                         , control_lock(new Glib::Mutex::Lock(c))
71                 { }
72                 ~WriteLockImpl() {
73                         delete sequence_lock;
74                         delete control_lock;
75                 }
76                 Glib::RWLock::WriterLock* sequence_lock;
77                 Glib::Mutex::Lock*        control_lock;
78         };
79
80 public:
81         typedef boost::shared_ptr<Glib::RWLock::ReaderLock> ReadLock;
82         typedef boost::shared_ptr<WriteLockImpl>            WriteLock;
83
84         virtual ReadLock  read_lock() const { return ReadLock(new Glib::RWLock::ReaderLock(_lock)); }
85         virtual WriteLock write_lock()      { return WriteLock(new WriteLockImpl(_lock, _control_lock)); }
86
87         void clear();
88
89         bool percussive() const     { return _percussive; }
90         void set_percussive(bool p) { _percussive = p; }
91
92         void start_write();
93         bool writing() const { return _writing; }
94         void end_write(bool delete_stuck=false);
95
96         void append(const Event<Time>& ev);
97
98         inline size_t n_notes() const { return _notes.size(); }
99         inline bool   empty()   const { return _notes.size() == 0 && ControlSet::controls_empty(); }
100
101         inline static bool note_time_comparator(const boost::shared_ptr< const Note<Time> >& a,
102                                                 const boost::shared_ptr< const Note<Time> >& b) {
103                 return a->time() < b->time();
104         }
105
106         struct NoteNumberComparator {
107                 inline bool operator()(const boost::shared_ptr< const Note<Time> > a,
108                                        const boost::shared_ptr< const Note<Time> > b) const {
109                         return a->note() < b->note();
110                 }
111         };
112
113         struct EarlierNoteComparator {
114                 inline bool operator()(const boost::shared_ptr< const Note<Time> > a,
115                                        const boost::shared_ptr< const Note<Time> > b) const {
116                         return a->time() < b->time();
117                 }
118         };
119
120         struct LaterNoteComparator {
121                 typedef const Note<Time>* value_type;
122                 inline bool operator()(const boost::shared_ptr< const Note<Time> > a,
123                                        const boost::shared_ptr< const Note<Time> > b) const {
124                         return a->time() > b->time();
125                 }
126         };
127
128         struct LaterNoteEndComparator {
129                 typedef const Note<Time>* value_type;
130                 inline bool operator()(const boost::shared_ptr< const Note<Time> > a,
131                                        const boost::shared_ptr< const Note<Time> > b) const {
132                         return a->end_time() > b->end_time();
133                 }
134         };
135
136         typedef std::multiset<boost::shared_ptr< Note<Time> >, EarlierNoteComparator> Notes;
137         inline       Notes& notes()       { return _notes; }
138         inline const Notes& notes() const { return _notes; }
139
140         enum NoteOperator { 
141                 PitchEqual,
142                 PitchLessThan,
143                 PitchLessThanOrEqual,
144                 PitchGreater,
145                 PitchGreaterThanOrEqual,
146                 VelocityEqual,
147                 VelocityLessThan,
148                 VelocityLessThanOrEqual,
149                 VelocityGreater,
150                 VelocityGreaterThanOrEqual,
151         };
152
153         void get_notes (Notes&, NoteOperator, uint8_t val, int chan_mask = 0) const;
154
155         void remove_overlapping_notes ();
156         void trim_overlapping_notes ();
157         void remove_duplicate_notes ();
158
159         enum OverlapPitchResolution { 
160                 LastOnFirstOff,
161                 FirstOnFirstOff
162         };
163
164         bool overlapping_pitches_accepted() const { return _overlapping_pitches_accepted; }
165         void overlapping_pitches_accepted(bool yn)  { _overlapping_pitches_accepted = yn; }
166         OverlapPitchResolution overlap_pitch_resolution() const { return _overlap_pitch_resolution; }
167         void set_overlap_pitch_resolution(OverlapPitchResolution opr);
168
169         void set_notes (const Sequence<Time>::Notes& n);
170
171         typedef std::vector< boost::shared_ptr< Event<Time> > > SysExes;
172         inline       SysExes& sysexes()       { return _sysexes; }
173         inline const SysExes& sysexes() const { return _sysexes; }
174
175 private:
176         typedef std::priority_queue< boost::shared_ptr< Note<Time> >,
177                                      std::deque< boost::shared_ptr< Note<Time> > >,
178                                      LaterNoteEndComparator >
179                 ActiveNotes;
180 public:
181
182         /** Read iterator */
183         class const_iterator {
184         public:
185                 const_iterator();
186                 const_iterator(const Sequence<Time>& seq, Time t);
187                 ~const_iterator();
188
189                 inline bool valid() const { return !_is_end && _event; }
190                 //inline bool locked() const { return _locked; }
191
192                 void invalidate();
193
194                 const Event<Time>& operator*()  const { return *_event;  }
195                 const boost::shared_ptr< Event<Time> > operator->() const  { return _event; }
196                 const boost::shared_ptr< Event<Time> > get_event_pointer() { return _event; }
197
198                 const const_iterator& operator++(); // prefix only
199
200                 bool operator==(const const_iterator& other) const;
201                 bool operator!=(const const_iterator& other) const { return ! operator==(other); }
202
203                 const_iterator& operator=(const const_iterator& other);
204
205         private:
206                 friend class Sequence<Time>;
207
208                 typedef std::vector<ControlIterator> ControlIterators;
209                 enum MIDIMessageType { NIL, NOTE_ON, NOTE_OFF, CONTROL, SYSEX };
210
211                 const Sequence<Time>*            _seq;
212                 boost::shared_ptr< Event<Time> > _event;
213                 mutable ActiveNotes              _active_notes;
214                 MIDIMessageType                  _type;
215                 bool                             _is_end;
216                 typename Sequence::ReadLock      _lock;
217                 typename Notes::const_iterator   _note_iter;
218                 typename SysExes::const_iterator _sysex_iter;
219                 ControlIterators                 _control_iters;
220                 ControlIterators::iterator       _control_iter;
221         };
222
223         const_iterator        begin(Time t=0) const { return const_iterator(*this, t); }
224         const const_iterator& end()           const { return _end_iter; }
225
226         typename Notes::const_iterator note_lower_bound (Time t) const;
227
228         bool control_to_midi_event(boost::shared_ptr< Event<Time> >& ev,
229                                    const ControlIterator&            iter) const;
230
231         bool edited() const      { return _edited; }
232         void set_edited(bool yn) { _edited = yn; }
233
234         bool overlaps (const boost::shared_ptr< Note<Time> >& ev) const;
235         bool contains (const boost::shared_ptr< Note<Time> >& ev) const;
236
237         bool add_note_unlocked(const boost::shared_ptr< Note<Time> > note);
238         void remove_note_unlocked(const boost::shared_ptr< const Note<Time> > note);
239
240         uint8_t lowest_note()  const { return _lowest_note; }
241         uint8_t highest_note() const { return _highest_note; }
242
243
244 protected:
245         bool                   _edited;
246         bool                   _overlapping_pitches_accepted;
247         OverlapPitchResolution _overlap_pitch_resolution;
248         mutable Glib::RWLock   _lock;
249
250 private:
251         friend class const_iterator;
252
253         typedef std::multiset<boost::shared_ptr< Note<Time> >, NoteNumberComparator>  Pitches;
254         inline       Pitches& pitches(uint8_t chan)       { return _pitches[chan&0xf]; }
255         inline const Pitches& pitches(uint8_t chan) const { return _pitches[chan&0xf]; }
256
257         bool overlaps_unlocked (const boost::shared_ptr< Note<Time> >& ev) const;
258         bool contains_unlocked (const boost::shared_ptr< Note<Time> >& ev) const;
259
260         void append_note_on_unlocked (boost::shared_ptr< Note<Time> >);
261         void append_note_off_unlocked(boost::shared_ptr< Note<Time> >);
262         void append_control_unlocked(const Parameter& param, Time time, double value);
263         void append_sysex_unlocked(const MIDIEvent<Time>& ev);
264
265         void get_notes_by_pitch (Notes&, NoteOperator, uint8_t val, int chan_mask = 0) const;
266         void get_notes_by_velocity (Notes&, NoteOperator, uint8_t val, int chan_mask = 0) const;
267
268         const TypeMap& _type_map;
269
270         Notes   _notes;       // notes indexed by time
271         Pitches _pitches[16]; // notes indexed by channel+pitch
272         SysExes _sysexes;
273
274         typedef std::multiset<boost::shared_ptr< Note<Time> >, EarlierNoteComparator> WriteNotes;
275         WriteNotes _write_notes[16];
276         bool       _writing;
277
278         typedef std::vector< boost::shared_ptr<const ControlList> > ControlLists;
279         ControlLists _dirty_controls;
280
281         const   const_iterator _end_iter;
282         bool                   _percussive;
283
284         uint8_t _lowest_note;
285         uint8_t _highest_note;
286 };
287
288
289 } // namespace Evoral
290
291 #endif // EVORAL_SEQUENCE_HPP
292