3cddeb38cac2d92d582d9cb1f1d3e8227b1a0392
[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
83         typedef typename boost::shared_ptr<Evoral::Note<Time> >  NotePtr;
84         typedef typename boost::shared_ptr<const Evoral::Note<Time> >  constNotePtr;
85
86         typedef boost::shared_ptr<Glib::RWLock::ReaderLock> ReadLock;
87         typedef boost::shared_ptr<WriteLockImpl>            WriteLock;
88
89         virtual ReadLock  read_lock() const { return ReadLock(new Glib::RWLock::ReaderLock(_lock)); }
90         virtual WriteLock write_lock()      { return WriteLock(new WriteLockImpl(_lock, _control_lock)); }
91
92         void clear();
93
94         bool percussive() const     { return _percussive; }
95         void set_percussive(bool p) { _percussive = p; }
96
97         void start_write();
98         bool writing() const { return _writing; }
99         void end_write(bool delete_stuck=false);
100
101         void append(const Event<Time>& ev);
102
103         inline size_t n_notes() const { return _notes.size(); }
104         inline bool   empty()   const { return _notes.size() == 0 && ControlSet::controls_empty(); }
105
106         inline static bool note_time_comparator(const boost::shared_ptr< const Note<Time> >& a,
107                                                 const boost::shared_ptr< const Note<Time> >& b) {
108                 return a->time() < b->time();
109         }
110
111         struct NoteNumberComparator {
112                 inline bool operator()(const boost::shared_ptr< const Note<Time> > a,
113                                        const boost::shared_ptr< const Note<Time> > b) const {
114                         return a->note() < b->note();
115                 }
116         };
117
118         struct EarlierNoteComparator {
119                 inline bool operator()(const boost::shared_ptr< const Note<Time> > a,
120                                        const boost::shared_ptr< const Note<Time> > b) const {
121                         return a->time() < b->time();
122                 }
123         };
124
125         struct LaterNoteComparator {
126                 typedef const Note<Time>* value_type;
127                 inline bool operator()(const boost::shared_ptr< const Note<Time> > a,
128                                        const boost::shared_ptr< const Note<Time> > b) const {
129                         return a->time() > b->time();
130                 }
131         };
132
133         struct LaterNoteEndComparator {
134                 typedef const Note<Time>* value_type;
135                 inline bool operator()(const boost::shared_ptr< const Note<Time> > a,
136                                        const boost::shared_ptr< const Note<Time> > b) const {
137                         return a->end_time() > b->end_time();
138                 }
139         };
140
141         typedef std::multiset<NotePtr, EarlierNoteComparator> Notes;
142         inline       Notes& notes()       { return _notes; }
143         inline const Notes& notes() const { return _notes; }
144
145         enum NoteOperator { 
146                 PitchEqual,
147                 PitchLessThan,
148                 PitchLessThanOrEqual,
149                 PitchGreater,
150                 PitchGreaterThanOrEqual,
151                 VelocityEqual,
152                 VelocityLessThan,
153                 VelocityLessThanOrEqual,
154                 VelocityGreater,
155                 VelocityGreaterThanOrEqual,
156         };
157
158         void get_notes (Notes&, NoteOperator, uint8_t val, int chan_mask = 0) const;
159
160         void remove_overlapping_notes ();
161         void trim_overlapping_notes ();
162         void remove_duplicate_notes ();
163
164         enum OverlapPitchResolution { 
165                 LastOnFirstOff,
166                 FirstOnFirstOff
167         };
168
169         bool overlapping_pitches_accepted() const { return _overlapping_pitches_accepted; }
170         void overlapping_pitches_accepted(bool yn)  { _overlapping_pitches_accepted = yn; }
171         OverlapPitchResolution overlap_pitch_resolution() const { return _overlap_pitch_resolution; }
172         void set_overlap_pitch_resolution(OverlapPitchResolution opr);
173
174         void set_notes (const Sequence<Time>::Notes& n);
175
176         typedef std::vector< boost::shared_ptr< Event<Time> > > SysExes;
177         inline       SysExes& sysexes()       { return _sysexes; }
178         inline const SysExes& sysexes() const { return _sysexes; }
179
180 private:
181         typedef std::priority_queue<NotePtr, std::deque<NotePtr>, LaterNoteEndComparator> ActiveNotes;
182 public:
183
184         /** Read iterator */
185         class const_iterator {
186         public:
187                 const_iterator();
188                 const_iterator(const Sequence<Time>& seq, Time t, std::set<Evoral::Parameter> const &);
189                 ~const_iterator();
190
191                 inline bool valid() const { return !_is_end && _event; }
192                 //inline bool locked() const { return _locked; }
193
194                 void invalidate();
195
196                 const Event<Time>& operator*()  const { return *_event;  }
197                 const boost::shared_ptr< Event<Time> > operator->() const  { return _event; }
198                 const boost::shared_ptr< Event<Time> > get_event_pointer() { return _event; }
199
200                 const const_iterator& operator++(); // prefix only
201
202                 bool operator==(const const_iterator& other) const;
203                 bool operator!=(const const_iterator& other) const { return ! operator==(other); }
204
205                 const_iterator& operator=(const const_iterator& other);
206
207         private:
208                 friend class Sequence<Time>;
209
210                 typedef std::vector<ControlIterator> ControlIterators;
211                 enum MIDIMessageType { NIL, NOTE_ON, NOTE_OFF, CONTROL, SYSEX };
212
213                 const Sequence<Time>*            _seq;
214                 boost::shared_ptr< Event<Time> > _event;
215                 mutable ActiveNotes              _active_notes;
216                 MIDIMessageType                  _type;
217                 bool                             _is_end;
218                 typename Sequence::ReadLock      _lock;
219                 typename Notes::const_iterator   _note_iter;
220                 typename SysExes::const_iterator _sysex_iter;
221                 ControlIterators                 _control_iters;
222                 ControlIterators::iterator       _control_iter;
223         };
224
225         const_iterator begin (Time t=0, std::set<Evoral::Parameter> const & f = std::set<Evoral::Parameter> ()) const {
226                 return const_iterator (*this, t, f);
227         }
228         const const_iterator& end()           const { return _end_iter; }
229
230         typename Notes::const_iterator note_lower_bound (Time t) const;
231
232         bool control_to_midi_event(boost::shared_ptr< Event<Time> >& ev,
233                                    const ControlIterator&            iter) const;
234
235         bool edited() const      { return _edited; }
236         void set_edited(bool yn) { _edited = yn; }
237
238         bool overlaps (const NotePtr& ev, 
239                        const NotePtr& ignore_this_note) const;
240         bool contains (const NotePtr& ev) const;
241
242         bool add_note_unlocked (const NotePtr note, void* arg = 0);
243         void remove_note_unlocked(const constNotePtr note);
244
245         uint8_t lowest_note()  const { return _lowest_note; }
246         uint8_t highest_note() const { return _highest_note; }
247
248
249 protected:
250         bool                   _edited;
251         bool                   _overlapping_pitches_accepted;
252         OverlapPitchResolution _overlap_pitch_resolution;
253         mutable Glib::RWLock   _lock;
254         bool                   _writing;
255
256         virtual int resolve_overlaps_unlocked (const NotePtr, void* arg = 0) {
257                 return 0;
258         }
259
260         typedef std::multiset<NotePtr, NoteNumberComparator>  Pitches;
261         inline       Pitches& pitches(uint8_t chan)       { return _pitches[chan&0xf]; }
262         inline const Pitches& pitches(uint8_t chan) const { return _pitches[chan&0xf]; }
263
264 private:
265         friend class const_iterator;
266
267         bool overlaps_unlocked (const NotePtr& ev, const NotePtr& ignore_this_note) const;
268         bool contains_unlocked (const NotePtr& ev) const;
269
270         void append_note_on_unlocked (NotePtr);
271         void append_note_off_unlocked(NotePtr);
272         void append_control_unlocked(const Parameter& param, Time time, double value);
273         void append_sysex_unlocked(const MIDIEvent<Time>& ev);
274
275         void get_notes_by_pitch (Notes&, NoteOperator, uint8_t val, int chan_mask = 0) const;
276         void get_notes_by_velocity (Notes&, NoteOperator, uint8_t val, int chan_mask = 0) const;
277
278         void control_list_marked_dirty ();
279
280         const TypeMap& _type_map;
281
282         Notes   _notes;       // notes indexed by time
283         Pitches _pitches[16]; // notes indexed by channel+pitch
284         SysExes _sysexes;
285
286         typedef std::multiset<NotePtr, EarlierNoteComparator> WriteNotes;
287         WriteNotes _write_notes[16];
288
289         const   const_iterator _end_iter;
290         bool                   _percussive;
291
292         uint8_t _lowest_note;
293         uint8_t _highest_note;
294 };
295
296
297 } // namespace Evoral
298
299 #endif // EVORAL_SEQUENCE_HPP
300