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