introduce the notion that note additions and property changes can cause the removal...
[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 <list>
26 #include <utility>
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
35 namespace Evoral {
36
37 class TypeMap;
38 template<typename Time> class EventSink;
39 template<typename Time> class Note;
40 template<typename Time> class Event;
41
42 /** An iterator over (the x axis of) a 2-d double coordinate space.
43  */
44 class ControlIterator {
45 public:
46         ControlIterator(boost::shared_ptr<const ControlList> al, double ax, double ay)
47                 : list(al)
48                 , x(ax)
49                 , y(ay)
50         {}
51
52         boost::shared_ptr<const ControlList> list;
53         double x;
54         double y;
55 };
56
57
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 {
63 public:
64         Sequence(const TypeMap& type_map);
65         Sequence(const Sequence<Time>& other);
66
67 protected:
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))
72                 { }
73                 ~WriteLockImpl() {
74                         delete sequence_lock;
75                         delete control_lock;
76                 }
77                 Glib::RWLock::WriterLock* sequence_lock;
78                 Glib::Mutex::Lock*        control_lock;
79         };
80
81 public:
82         typedef typename boost::shared_ptr<Evoral::Note<Time> >  NotePtr;
83         typedef typename boost::shared_ptr<const Evoral::Note<Time> >  constNotePtr;
84
85         typedef boost::shared_ptr<Glib::RWLock::ReaderLock> ReadLock;
86         typedef boost::shared_ptr<WriteLockImpl>            WriteLock;
87
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)); }
90
91         void clear();
92
93         bool percussive() const     { return _percussive; }
94         void set_percussive(bool p) { _percussive = p; }
95
96         void start_write();
97         bool writing() const { return _writing; }
98         void end_write(bool delete_stuck=false);
99
100         void append(const Event<Time>& ev);
101
102         inline size_t n_notes() const { return _notes.size(); }
103         inline bool   empty()   const { return _notes.size() == 0 && ControlSet::controls_empty(); }
104
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();
108         }
109
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();
114                 }
115         };
116
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();
121                 }
122         };
123
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();
129                 }
130         };
131
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();
137                 }
138         };
139
140         typedef std::multiset<NotePtr, EarlierNoteComparator> Notes;
141         inline       Notes& notes()       { return _notes; }
142         inline const Notes& notes() const { return _notes; }
143
144         enum NoteOperator { 
145                 PitchEqual,
146                 PitchLessThan,
147                 PitchLessThanOrEqual,
148                 PitchGreater,
149                 PitchGreaterThanOrEqual,
150                 VelocityEqual,
151                 VelocityLessThan,
152                 VelocityLessThanOrEqual,
153                 VelocityGreater,
154                 VelocityGreaterThanOrEqual,
155         };
156
157         void get_notes (Notes&, NoteOperator, uint8_t val, int chan_mask = 0) const;
158
159         void remove_overlapping_notes ();
160         void trim_overlapping_notes ();
161         void remove_duplicate_notes ();
162
163         enum OverlapPitchResolution { 
164                 LastOnFirstOff,
165                 FirstOnFirstOff
166         };
167
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);
172
173         void set_notes (const Sequence<Time>::Notes& n);
174
175         typedef std::vector< boost::shared_ptr< Event<Time> > > SysExes;
176         inline       SysExes& sysexes()       { return _sysexes; }
177         inline const SysExes& sysexes() const { return _sysexes; }
178
179 private:
180         typedef std::priority_queue<NotePtr, std::deque<NotePtr>, LaterNoteEndComparator> ActiveNotes;
181 public:
182
183         /** Read iterator */
184         class const_iterator {
185         public:
186                 const_iterator();
187                 const_iterator(const Sequence<Time>& seq, Time t);
188                 ~const_iterator();
189
190                 inline bool valid() const { return !_is_end && _event; }
191                 //inline bool locked() const { return _locked; }
192
193                 void invalidate();
194
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; }
198
199                 const const_iterator& operator++(); // prefix only
200
201                 bool operator==(const const_iterator& other) const;
202                 bool operator!=(const const_iterator& other) const { return ! operator==(other); }
203
204                 const_iterator& operator=(const const_iterator& other);
205
206         private:
207                 friend class Sequence<Time>;
208
209                 typedef std::vector<ControlIterator> ControlIterators;
210                 enum MIDIMessageType { NIL, NOTE_ON, NOTE_OFF, CONTROL, SYSEX };
211
212                 const Sequence<Time>*            _seq;
213                 boost::shared_ptr< Event<Time> > _event;
214                 mutable ActiveNotes              _active_notes;
215                 MIDIMessageType                  _type;
216                 bool                             _is_end;
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;
222         };
223
224         const_iterator        begin(Time t=0) const { return const_iterator(*this, t); }
225         const const_iterator& end()           const { return _end_iter; }
226
227         typename Notes::const_iterator note_lower_bound (Time t) const;
228
229         bool control_to_midi_event(boost::shared_ptr< Event<Time> >& ev,
230                                    const ControlIterator&            iter) const;
231
232         bool edited() const      { return _edited; }
233         void set_edited(bool yn) { _edited = yn; }
234
235         bool overlaps (const NotePtr& ev, 
236                        const NotePtr& ignore_this_note) const;
237         bool contains (const NotePtr& ev) const;
238
239         bool add_note_unlocked (const NotePtr note, std::set<NotePtr>* removed = 0);
240         void remove_note_unlocked(const constNotePtr note);
241
242         uint8_t lowest_note()  const { return _lowest_note; }
243         uint8_t highest_note() const { return _highest_note; }
244
245
246 protected:
247         bool                   _edited;
248         bool                   _overlapping_pitches_accepted;
249         OverlapPitchResolution _overlap_pitch_resolution;
250         mutable Glib::RWLock   _lock;
251         bool                   _writing;
252
253         virtual int resolve_overlaps_unlocked (const NotePtr, std::set<NotePtr>* removed = 0) {
254                 return 0;
255         }
256
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]; }
260
261 private:
262         friend class const_iterator;
263
264         bool overlaps_unlocked (const NotePtr& ev, const NotePtr& ignore_this_note) const;
265         bool contains_unlocked (const NotePtr& ev) const;
266
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);
271
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;
274
275         const TypeMap& _type_map;
276
277         Notes   _notes;       // notes indexed by time
278         Pitches _pitches[16]; // notes indexed by channel+pitch
279         SysExes _sysexes;
280
281         typedef std::multiset<NotePtr, EarlierNoteComparator> WriteNotes;
282         WriteNotes _write_notes[16];
283
284         typedef std::vector< boost::shared_ptr<const ControlList> > ControlLists;
285         ControlLists _dirty_controls;
286
287         const   const_iterator _end_iter;
288         bool                   _percussive;
289
290         uint8_t _lowest_note;
291         uint8_t _highest_note;
292 };
293
294
295 } // namespace Evoral
296
297 #endif // EVORAL_SEQUENCE_HPP
298