Use set over vector for Sequence::Notes, for logarithmic search by time.
[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 <deque>
25 #include <map>
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
66         void write_lock();
67         void write_unlock();
68
69         void read_lock()   const;
70         void read_unlock() const;
71
72         void clear();
73
74         bool percussive() const     { return _percussive; }
75         void set_percussive(bool p) { _percussive = p; }
76
77         void start_write();
78         bool writing() const { return _writing; }
79         void end_write(bool delete_stuck=false);
80
81         void append(const Event<Time>& ev);
82
83         inline size_t n_notes() const { return _notes.size(); }
84         inline bool   empty()   const { return _notes.size() == 0 && ControlSet::controls_empty(); }
85
86         inline static bool note_time_comparator(const boost::shared_ptr< const Note<Time> >& a,
87                                                 const boost::shared_ptr< const Note<Time> >& b) {
88                 return a->time() < b->time();
89         }
90
91         struct NoteNumberComparator {
92                 inline bool operator()(const boost::shared_ptr< const Note<Time> > a,
93                                        const boost::shared_ptr< const Note<Time> > b) const {
94                         return a->note() < b->note();
95                 }
96         };
97
98         struct EarlierNoteComparator {
99                 inline bool operator()(const boost::shared_ptr< const Note<Time> > a,
100                                        const boost::shared_ptr< const Note<Time> > b) const {
101                         return a->time() < b->time();
102                 }
103         };
104
105         struct LaterNoteComparator {
106                 typedef const Note<Time>* value_type;
107                 inline bool operator()(const boost::shared_ptr< const Note<Time> > a,
108                                        const boost::shared_ptr< const Note<Time> > b) const {
109                         return a->time() > b->time();
110                 }
111         };
112
113         struct LaterNoteEndComparator {
114                 typedef const Note<Time>* value_type;
115                 inline bool operator()(const boost::shared_ptr< const Note<Time> > a,
116                                        const boost::shared_ptr< const Note<Time> > b) const {
117                         return a->end_time() > b->end_time();
118                 }
119         };
120
121         typedef std::set<boost::shared_ptr< Note<Time> >, EarlierNoteComparator> Notes;
122         inline       Notes& notes()       { return _notes; }
123         inline const Notes& notes() const { return _notes; }
124
125         void set_notes (const Sequence<Time>::Notes& n);
126
127         typedef std::vector< boost::shared_ptr< Event<Time> > > SysExes;
128         inline       SysExes& sysexes()       { return _sysexes; }
129         inline const SysExes& sysexes() const { return _sysexes; }
130
131 private:
132         typedef std::priority_queue< boost::shared_ptr< Note<Time> >,
133                                      std::deque< boost::shared_ptr< Note<Time> > >,
134                                      LaterNoteEndComparator >
135                 ActiveNotes;
136 public:
137
138         /** Read iterator */
139         class const_iterator {
140         public:
141                 const_iterator();
142                 const_iterator(const Sequence<Time>& seq, Time t);
143                 ~const_iterator();
144
145                 inline bool valid() const { return !_is_end && _event; }
146                 inline bool locked() const { return _locked; }
147
148                 void invalidate();
149
150                 const Event<Time>& operator*()  const { return *_event;  }
151                 const boost::shared_ptr< Event<Time> > operator->() const  { return _event; }
152                 const boost::shared_ptr< Event<Time> > get_event_pointer() { return _event; }
153
154                 const const_iterator& operator++(); // prefix only
155
156                 bool operator==(const const_iterator& other) const;
157                 bool operator!=(const const_iterator& other) const { return ! operator==(other); }
158
159                 const_iterator& operator=(const const_iterator& other);
160
161         private:
162                 friend class Sequence<Time>;
163
164                 typedef std::vector<ControlIterator> ControlIterators;
165                 enum MIDIMessageType { NIL, NOTE_ON, NOTE_OFF, CONTROL, SYSEX };
166
167                 const Sequence<Time>*            _seq;
168                 boost::shared_ptr< Event<Time> > _event;
169                 mutable ActiveNotes              _active_notes;
170                 MIDIMessageType                  _type;
171                 bool                             _is_end;
172                 bool                             _locked;
173                 typename Notes::const_iterator   _note_iter;
174                 typename SysExes::const_iterator _sysex_iter;
175                 ControlIterators                 _control_iters;
176                 ControlIterators::iterator       _control_iter;
177         };
178
179         const_iterator        begin(Time t=0) const { return const_iterator(*this, t); }
180         const const_iterator& end()           const { return _end_iter; }
181
182         bool control_to_midi_event(boost::shared_ptr< Event<Time> >& ev,
183                                    const ControlIterator&            iter) const;
184
185         bool edited() const      { return _edited; }
186         void set_edited(bool yn) { _edited = yn; }
187
188         void add_note_unlocked(const boost::shared_ptr< Note<Time> > note);
189         void remove_note_unlocked(const boost::shared_ptr< const Note<Time> > note);
190
191         uint8_t lowest_note()  const { return _lowest_note; }
192         uint8_t highest_note() const { return _highest_note; }
193
194 protected:
195         bool _edited;
196
197 private:
198         friend class const_iterator;
199
200         void append_note_on_unlocked(uint8_t chan, Time time, uint8_t note, uint8_t velocity);
201         void append_note_off_unlocked(uint8_t chan, Time time, uint8_t note);
202         void append_control_unlocked(const Parameter& param, Time time, double value);
203         void append_sysex_unlocked(const MIDIEvent<Time>& ev);
204
205         mutable Glib::RWLock _lock;
206
207         const TypeMap& _type_map;
208
209         Notes   _notes;
210         SysExes _sysexes;
211
212         typedef std::set<boost::shared_ptr< Note<Time> >, NoteNumberComparator> WriteNotes;
213         WriteNotes _write_notes[16];
214         bool       _writing;
215
216         typedef std::vector< boost::shared_ptr<const ControlList> > ControlLists;
217         ControlLists _dirty_controls;
218
219         const   const_iterator _end_iter;
220         bool                   _percussive;
221
222         uint8_t _lowest_note;
223         uint8_t _highest_note;
224 };
225
226
227 } // namespace Evoral
228
229 #endif // EVORAL_SEQUENCE_HPP
230