remove file manager LRU cache from code.
[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/ControlSet.hpp"
34 #include "evoral/ControlList.hpp"
35 #include "evoral/PatchChange.hpp"
36
37 namespace Evoral {
38
39 class Parameter;
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 = Time());
109
110         void append(const Event<Time>& ev, Evoral::event_id_t evid);
111
112         const TypeMap& type_map() const { return _type_map; }
113
114         inline size_t n_notes() const { return _notes.size(); }
115         inline bool   empty()   const { return _notes.empty() && _sysexes.empty() && _patch_changes.empty() && ControlSet::controls_empty(); }
116
117         inline static bool note_time_comparator(const boost::shared_ptr< const Note<Time> >& a,
118                                                 const boost::shared_ptr< const Note<Time> >& b) {
119                 return a->time() < b->time();
120         }
121
122         struct NoteNumberComparator {
123                 inline bool operator()(const boost::shared_ptr< const Note<Time> > a,
124                                        const boost::shared_ptr< const Note<Time> > b) const {
125                         return a->note() < b->note();
126                 }
127         };
128
129         struct EarlierNoteComparator {
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->time() < b->time();
133                 }
134         };
135
136         struct LaterNoteComparator {
137                 typedef const Note<Time>* value_type;
138                 inline bool operator()(const boost::shared_ptr< const Note<Time> > a,
139                                        const boost::shared_ptr< const Note<Time> > b) const {
140                         return a->time() > b->time();
141                 }
142         };
143
144         struct LaterNoteEndComparator {
145                 typedef const Note<Time>* value_type;
146                 inline bool operator()(const boost::shared_ptr< const Note<Time> > a,
147                                        const boost::shared_ptr< const Note<Time> > b) const {
148                         return a->end_time() > b->end_time();
149                 }
150         };
151
152         typedef std::multiset<NotePtr, EarlierNoteComparator> Notes;
153         inline       Notes& notes()       { return _notes; }
154         inline const Notes& notes() const { return _notes; }
155
156         enum NoteOperator {
157                 PitchEqual,
158                 PitchLessThan,
159                 PitchLessThanOrEqual,
160                 PitchGreater,
161                 PitchGreaterThanOrEqual,
162                 VelocityEqual,
163                 VelocityLessThan,
164                 VelocityLessThanOrEqual,
165                 VelocityGreater,
166                 VelocityGreaterThanOrEqual,
167         };
168
169         void get_notes (Notes&, NoteOperator, uint8_t val, int chan_mask = 0) const;
170
171         void remove_overlapping_notes ();
172         void trim_overlapping_notes ();
173         void remove_duplicate_notes ();
174
175         enum OverlapPitchResolution {
176                 LastOnFirstOff,
177                 FirstOnFirstOff
178         };
179
180         bool overlapping_pitches_accepted() const { return _overlapping_pitches_accepted; }
181         void overlapping_pitches_accepted(bool yn)  { _overlapping_pitches_accepted = yn; }
182         OverlapPitchResolution overlap_pitch_resolution() const { return _overlap_pitch_resolution; }
183         void set_overlap_pitch_resolution(OverlapPitchResolution opr);
184
185         void set_notes (const typename Sequence<Time>::Notes& n);
186
187         typedef boost::shared_ptr< Event<Time> > SysExPtr;
188         typedef boost::shared_ptr<const Event<Time> > constSysExPtr;
189
190         struct EarlierSysExComparator {
191                 inline bool operator() (constSysExPtr a, constSysExPtr b) const {
192                         return a->time() < b->time();
193                 }
194         };
195
196         typedef std::multiset<SysExPtr, EarlierSysExComparator> SysExes;
197         inline       SysExes& sysexes()       { return _sysexes; }
198         inline const SysExes& sysexes() const { return _sysexes; }
199
200         typedef boost::shared_ptr<PatchChange<Time> > PatchChangePtr;
201         typedef boost::shared_ptr<const PatchChange<Time> > constPatchChangePtr;
202
203         struct EarlierPatchChangeComparator {
204                 inline bool operator() (constPatchChangePtr a, constPatchChangePtr b) const {
205                         return a->time() < b->time();
206                 }
207         };
208
209         typedef std::multiset<PatchChangePtr, EarlierPatchChangeComparator> PatchChanges;
210         inline       PatchChanges& patch_changes ()       { return _patch_changes; }
211         inline const PatchChanges& patch_changes () const { return _patch_changes; }
212
213         void dump (std::ostream&) const;
214
215 private:
216         typedef std::priority_queue<NotePtr, std::deque<NotePtr>, LaterNoteEndComparator> ActiveNotes;
217 public:
218
219         /** Read iterator */
220         class LIBEVORAL_API /* Added by JE - */ const_iterator {
221         public:
222                 const_iterator();
223                 const_iterator(const Sequence<Time>& seq, Time t, bool, std::set<Evoral::Parameter> const &);
224                 ~const_iterator();
225
226                 inline bool valid() const { return !_is_end && _event; }
227                 //inline bool locked() const { return _locked; }
228
229                 void invalidate();
230
231                 const Event<Time>& operator*()  const { return *_event;  }
232                 const boost::shared_ptr< Event<Time> > operator->() const  { return _event; }
233                 const boost::shared_ptr< Event<Time> > get_event_pointer() { return _event; }
234
235                 const const_iterator& operator++(); // prefix only
236
237                 bool operator==(const const_iterator& other) const;
238                 bool operator!=(const const_iterator& other) const { return ! operator==(other); }
239
240                 const_iterator& operator=(const const_iterator& other);
241
242         private:
243                 friend class Sequence<Time>;
244
245                 typedef std::vector<ControlIterator> ControlIterators;
246                 enum MIDIMessageType { NIL, NOTE_ON, NOTE_OFF, CONTROL, SYSEX, PATCH_CHANGE };
247
248                 const Sequence<Time>*                 _seq;
249                 boost::shared_ptr< Event<Time> >      _event;
250                 mutable ActiveNotes                   _active_notes;
251                 /** If the iterator is pointing at a patch change, this is the index of the
252                  *  sub-message within that change.
253                  */
254                 int                                   _active_patch_change_message;
255                 MIDIMessageType                       _type;
256                 bool                                  _is_end;
257                 typename Sequence::ReadLock           _lock;
258                 typename Notes::const_iterator        _note_iter;
259                 typename SysExes::const_iterator      _sysex_iter;
260                 typename PatchChanges::const_iterator _patch_change_iter;
261                 ControlIterators                      _control_iters;
262                 ControlIterators::iterator            _control_iter;
263                 bool                                  _force_discrete;
264         };
265
266         const_iterator begin (
267                 Time t = Time(),
268                 bool force_discrete = false,
269                 std::set<Evoral::Parameter> const & f = std::set<Evoral::Parameter> ()) const {
270                 return const_iterator (*this, t, force_discrete, f);
271         }
272
273         const const_iterator& end() const { return _end_iter; }
274
275         // CONST iterator implementations (x3)
276         typename Notes::const_iterator note_lower_bound (Time t) const;
277         typename PatchChanges::const_iterator patch_change_lower_bound (Time t) const;
278         typename SysExes::const_iterator sysex_lower_bound (Time t) const;
279
280         // NON-CONST iterator implementations (x3)
281         typename Notes::iterator note_lower_bound (Time t);
282         typename PatchChanges::iterator patch_change_lower_bound (Time t);
283         typename SysExes::iterator sysex_lower_bound (Time t);
284
285         bool control_to_midi_event(boost::shared_ptr< Event<Time> >& ev,
286                                    const ControlIterator&            iter) const;
287
288         bool edited() const      { return _edited; }
289         void set_edited(bool yn) { _edited = yn; }
290
291         bool overlaps (const NotePtr& ev,
292                        const NotePtr& ignore_this_note) const;
293         bool contains (const NotePtr& ev) const;
294
295         bool add_note_unlocked (const NotePtr note, void* arg = 0);
296         void remove_note_unlocked(const constNotePtr note);
297
298         void add_patch_change_unlocked (const PatchChangePtr);
299         void remove_patch_change_unlocked (const constPatchChangePtr);
300
301         void add_sysex_unlocked (const SysExPtr);
302         void remove_sysex_unlocked (const SysExPtr);
303
304         uint8_t lowest_note()  const { return _lowest_note; }
305         uint8_t highest_note() const { return _highest_note; }
306
307
308 protected:
309         bool                   _edited;
310         bool                   _overlapping_pitches_accepted;
311         OverlapPitchResolution _overlap_pitch_resolution;
312         mutable Glib::Threads::RWLock   _lock;
313         bool                   _writing;
314
315         virtual int resolve_overlaps_unlocked (const NotePtr, void* /* arg */ = 0) {
316                 return 0;
317         }
318
319         typedef std::multiset<NotePtr, NoteNumberComparator>  Pitches;
320         inline       Pitches& pitches(uint8_t chan)       { return _pitches[chan&0xf]; }
321         inline const Pitches& pitches(uint8_t chan) const { return _pitches[chan&0xf]; }
322
323         virtual void control_list_marked_dirty ();
324
325 private:
326         friend class const_iterator;
327
328         bool overlaps_unlocked (const NotePtr& ev, const NotePtr& ignore_this_note) const;
329         bool contains_unlocked (const NotePtr& ev) const;
330
331         void append_note_on_unlocked (NotePtr, Evoral::event_id_t);
332         void append_note_off_unlocked(NotePtr);
333         void append_control_unlocked(const Parameter& param, Time time, double value, Evoral::event_id_t);
334         void append_sysex_unlocked(const MIDIEvent<Time>& ev, Evoral::event_id_t);
335         void append_patch_change_unlocked (const PatchChange<Time>&, Evoral::event_id_t);
336
337         void get_notes_by_pitch (Notes&, NoteOperator, uint8_t val, int chan_mask = 0) const;
338         void get_notes_by_velocity (Notes&, NoteOperator, uint8_t val, int chan_mask = 0) const;
339
340         const TypeMap& _type_map;
341
342         Notes        _notes;       // notes indexed by time
343         Pitches      _pitches[16]; // notes indexed by channel+pitch
344         SysExes      _sysexes;
345         PatchChanges _patch_changes;
346
347         typedef std::multiset<NotePtr, EarlierNoteComparator> WriteNotes;
348         WriteNotes _write_notes[16];
349
350         /** Current bank number on each channel so that we know what
351          *  to put in PatchChange events when program changes are
352          *  seen.
353          */
354         int _bank[16];
355
356         const   const_iterator _end_iter;
357         bool                   _percussive;
358
359         uint8_t _lowest_note;
360         uint8_t _highest_note;
361 };
362
363
364 } // namespace Evoral
365
366 template<typename Time> /*LIBEVORAL_API*/ std::ostream& operator<<(std::ostream& o, const Evoral::Sequence<Time>& s) { s.dump (o); return o; }
367
368
369 #endif // EVORAL_SEQUENCE_HPP
370