Fix my name :)
[ardour.git] / libs / evoral / evoral / Sequence.hpp
1 /* This file is part of Evoral.
2  * Copyright (C) 2008 David 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 #include "evoral/PatchChange.hpp"
35
36 namespace Evoral {
37
38 class TypeMap;
39 template<typename Time> class EventSink;
40 template<typename Time> class Note;
41 template<typename Time> class Event;
42
43 /** An iterator over (the x axis of) a 2-d double coordinate space.
44  */
45 class ControlIterator {
46 public:
47         ControlIterator(boost::shared_ptr<const ControlList> al, double ax, double ay)
48                 : list(al)
49                 , x(ax)
50                 , y(ay)
51         {}
52
53         boost::shared_ptr<const ControlList> list;
54         double x;
55         double y;
56 };
57
58
59 /** This is a higher level view of events, with separate representations for
60  * notes (instead of just unassociated note on/off events) and controller data.
61  * Controller data is represented as a list of time-stamped float values. */
62 template<typename Time>
63 class Sequence : virtual public ControlSet {
64 public:
65         Sequence(const TypeMap& type_map);
66         Sequence(const Sequence<Time>& other);
67
68 protected:
69         struct WriteLockImpl {
70                 WriteLockImpl(Glib::RWLock& s, Glib::Mutex& c)
71                         : sequence_lock(new Glib::RWLock::WriterLock(s))
72                         , control_lock(new Glib::Mutex::Lock(c))
73                 { }
74                 ~WriteLockImpl() {
75                         delete sequence_lock;
76                         delete control_lock;
77                 }
78                 Glib::RWLock::WriterLock* sequence_lock;
79                 Glib::Mutex::Lock*        control_lock;
80         };
81
82 public:
83
84         typedef typename boost::shared_ptr<Evoral::Note<Time> >  NotePtr;
85         typedef typename boost::shared_ptr<const Evoral::Note<Time> >  constNotePtr;
86
87         typedef boost::shared_ptr<Glib::RWLock::ReaderLock> ReadLock;
88         typedef boost::shared_ptr<WriteLockImpl>            WriteLock;
89
90         virtual ReadLock  read_lock() const { return ReadLock(new Glib::RWLock::ReaderLock(_lock)); }
91         virtual WriteLock write_lock()      { return WriteLock(new WriteLockImpl(_lock, _control_lock)); }
92
93         void clear();
94
95         bool percussive() const     { return _percussive; }
96         void set_percussive(bool p) { _percussive = p; }
97
98         void start_write();
99         bool writing() const { return _writing; }
100         void end_write(bool delete_stuck=false);
101
102         void append(const Event<Time>& ev, Evoral::event_id_t evid);
103
104         inline size_t n_notes() const { return _notes.size(); }
105         inline bool   empty()   const { return _notes.empty() && _sysexes.empty() && _patch_changes.empty() && ControlSet::controls_empty(); }
106
107         inline static bool note_time_comparator(const boost::shared_ptr< const Note<Time> >& a,
108                                                 const boost::shared_ptr< const Note<Time> >& b) {
109                 return a->time() < b->time();
110         }
111
112         struct NoteNumberComparator {
113                 inline bool operator()(const boost::shared_ptr< const Note<Time> > a,
114                                        const boost::shared_ptr< const Note<Time> > b) const {
115                         return a->note() < b->note();
116                 }
117         };
118
119         struct EarlierNoteComparator {
120                 inline bool operator()(const boost::shared_ptr< const Note<Time> > a,
121                                        const boost::shared_ptr< const Note<Time> > b) const {
122                         return a->time() < b->time();
123                 }
124         };
125
126         struct LaterNoteComparator {
127                 typedef const Note<Time>* value_type;
128                 inline bool operator()(const boost::shared_ptr< const Note<Time> > a,
129                                        const boost::shared_ptr< const Note<Time> > b) const {
130                         return a->time() > b->time();
131                 }
132         };
133
134         struct LaterNoteEndComparator {
135                 typedef const Note<Time>* value_type;
136                 inline bool operator()(const boost::shared_ptr< const Note<Time> > a,
137                                        const boost::shared_ptr< const Note<Time> > b) const {
138                         return a->end_time() > b->end_time();
139                 }
140         };
141
142         typedef std::multiset<NotePtr, EarlierNoteComparator> Notes;
143         inline       Notes& notes()       { return _notes; }
144         inline const Notes& notes() const { return _notes; }
145
146         enum NoteOperator { 
147                 PitchEqual,
148                 PitchLessThan,
149                 PitchLessThanOrEqual,
150                 PitchGreater,
151                 PitchGreaterThanOrEqual,
152                 VelocityEqual,
153                 VelocityLessThan,
154                 VelocityLessThanOrEqual,
155                 VelocityGreater,
156                 VelocityGreaterThanOrEqual,
157         };
158
159         void get_notes (Notes&, NoteOperator, uint8_t val, int chan_mask = 0) const;
160
161         void remove_overlapping_notes ();
162         void trim_overlapping_notes ();
163         void remove_duplicate_notes ();
164
165         enum OverlapPitchResolution { 
166                 LastOnFirstOff,
167                 FirstOnFirstOff
168         };
169
170         bool overlapping_pitches_accepted() const { return _overlapping_pitches_accepted; }
171         void overlapping_pitches_accepted(bool yn)  { _overlapping_pitches_accepted = yn; }
172         OverlapPitchResolution overlap_pitch_resolution() const { return _overlap_pitch_resolution; }
173         void set_overlap_pitch_resolution(OverlapPitchResolution opr);
174
175         void set_notes (const Sequence<Time>::Notes& n);
176
177         typedef std::vector< boost::shared_ptr< Event<Time> > > SysExes;
178         inline       SysExes& sysexes()       { return _sysexes; }
179         inline const SysExes& sysexes() const { return _sysexes; }
180
181         typedef boost::shared_ptr<PatchChange<Time> > PatchChangePtr;
182         typedef boost::shared_ptr<const PatchChange<Time> > constPatchChangePtr;
183         
184         struct EarlierPatchChangeComparator {
185                 inline bool operator() (constPatchChangePtr a, constPatchChangePtr b) const {
186                         return a->time() < b->time();
187                 }
188         };
189
190         typedef std::multiset<PatchChangePtr, EarlierPatchChangeComparator> PatchChanges;
191         inline       PatchChanges& patch_changes ()       { return _patch_changes; }
192         inline const PatchChanges& patch_changes () const { return _patch_changes; }
193
194 private:
195         typedef std::priority_queue<NotePtr, std::deque<NotePtr>, LaterNoteEndComparator> ActiveNotes;
196 public:
197
198         /** Read iterator */
199         class const_iterator {
200         public:
201                 const_iterator();
202                 const_iterator(const Sequence<Time>& seq, Time t, bool, std::set<Evoral::Parameter> const &);
203                 ~const_iterator();
204
205                 inline bool valid() const { return !_is_end && _event; }
206                 //inline bool locked() const { return _locked; }
207
208                 void invalidate();
209
210                 const Event<Time>& operator*()  const { return *_event;  }
211                 const boost::shared_ptr< Event<Time> > operator->() const  { return _event; }
212                 const boost::shared_ptr< Event<Time> > get_event_pointer() { return _event; }
213
214                 const const_iterator& operator++(); // prefix only
215
216                 bool operator==(const const_iterator& other) const;
217                 bool operator!=(const const_iterator& other) const { return ! operator==(other); }
218
219                 const_iterator& operator=(const const_iterator& other);
220
221         private:
222                 friend class Sequence<Time>;
223
224                 typedef std::vector<ControlIterator> ControlIterators;
225                 enum MIDIMessageType { NIL, NOTE_ON, NOTE_OFF, CONTROL, SYSEX, PATCH_CHANGE };
226
227                 const Sequence<Time>*                 _seq;
228                 boost::shared_ptr< Event<Time> >      _event;
229                 mutable ActiveNotes                   _active_notes;
230                 /** If the iterator is pointing at a patch change, this is the index of the
231                  *  sub-message within that change.
232                  */
233                 int                                   _active_patch_change_message;
234                 MIDIMessageType                       _type;
235                 bool                                  _is_end;
236                 typename Sequence::ReadLock           _lock;
237                 typename Notes::const_iterator        _note_iter;
238                 typename SysExes::const_iterator      _sysex_iter;
239                 typename PatchChanges::const_iterator _patch_change_iter;
240                 ControlIterators                      _control_iters;
241                 ControlIterators::iterator            _control_iter;
242                 bool                                  _force_discrete;
243         };
244
245         const_iterator begin (
246                 Time t = 0,
247                 bool force_discrete = false,
248                 std::set<Evoral::Parameter> const & f = std::set<Evoral::Parameter> ()) const {
249                 return const_iterator (*this, t, force_discrete, f);
250         }
251         
252         const const_iterator& end() const { return _end_iter; }
253
254         typename Notes::const_iterator note_lower_bound (Time t) const;
255         typename PatchChanges::const_iterator patch_change_lower_bound (Time t) const;
256
257         bool control_to_midi_event(boost::shared_ptr< Event<Time> >& ev,
258                                    const ControlIterator&            iter) const;
259
260         bool edited() const      { return _edited; }
261         void set_edited(bool yn) { _edited = yn; }
262
263         bool overlaps (const NotePtr& ev, 
264                        const NotePtr& ignore_this_note) const;
265         bool contains (const NotePtr& ev) const;
266
267         bool add_note_unlocked (const NotePtr note, void* arg = 0);
268         void remove_note_unlocked(const constNotePtr note);
269
270         void add_patch_change_unlocked (const PatchChangePtr);
271         void remove_patch_change_unlocked (const constPatchChangePtr);
272
273         uint8_t lowest_note()  const { return _lowest_note; }
274         uint8_t highest_note() const { return _highest_note; }
275
276
277 protected:
278         bool                   _edited;
279         bool                   _overlapping_pitches_accepted;
280         OverlapPitchResolution _overlap_pitch_resolution;
281         mutable Glib::RWLock   _lock;
282         bool                   _writing;
283
284         virtual int resolve_overlaps_unlocked (const NotePtr, void* /* arg */ = 0) {
285                 return 0;
286         }
287
288         typedef std::multiset<NotePtr, NoteNumberComparator>  Pitches;
289         inline       Pitches& pitches(uint8_t chan)       { return _pitches[chan&0xf]; }
290         inline const Pitches& pitches(uint8_t chan) const { return _pitches[chan&0xf]; }
291
292 private:
293         friend class const_iterator;
294
295         bool overlaps_unlocked (const NotePtr& ev, const NotePtr& ignore_this_note) const;
296         bool contains_unlocked (const NotePtr& ev) const;
297
298         void append_note_on_unlocked (NotePtr, Evoral::event_id_t);
299         void append_note_off_unlocked(NotePtr);
300         void append_control_unlocked(const Parameter& param, Time time, double value, Evoral::event_id_t);
301         void append_sysex_unlocked(const MIDIEvent<Time>& ev, Evoral::event_id_t);
302         void append_patch_change_unlocked (const PatchChange<Time>&, Evoral::event_id_t);
303
304         void get_notes_by_pitch (Notes&, NoteOperator, uint8_t val, int chan_mask = 0) const;
305         void get_notes_by_velocity (Notes&, NoteOperator, uint8_t val, int chan_mask = 0) const;
306
307         void control_list_marked_dirty ();
308
309         const TypeMap& _type_map;
310
311         Notes        _notes;       // notes indexed by time
312         Pitches      _pitches[16]; // notes indexed by channel+pitch
313         SysExes      _sysexes;
314         PatchChanges _patch_changes;
315
316         typedef std::multiset<NotePtr, EarlierNoteComparator> WriteNotes;
317         WriteNotes _write_notes[16];
318
319         /** Current bank number on each channel so that we know what
320          *  to put in PatchChange events when program changes are
321          *  seen.
322          */
323         int _bank[16];
324
325         const   const_iterator _end_iter;
326         bool                   _percussive;
327
328         uint8_t _lowest_note;
329         uint8_t _highest_note;
330 };
331
332
333 } // namespace Evoral
334
335 #endif // EVORAL_SEQUENCE_HPP
336