Eliminate a ton of unnecessary complete redrawing in MIDI stream views:
[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 class EventSink;
39 class Note;
40 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 class Sequence : virtual public ControlSet {
62 public:
63         Sequence(const TypeMap& type_map, size_t size=0);
64         
65         bool read_locked() { return _read_iter.locked(); }
66
67         void write_lock();
68         void write_unlock();
69
70         void read_lock()   const;
71         void read_unlock() const;
72
73         void clear();
74
75         bool percussive() const     { return _percussive; }
76         void set_percussive(bool p) { _percussive = p; }
77
78         void start_write();
79         bool writing() const { return _writing; }
80         void end_write(bool delete_stuck=false);
81
82         size_t read(EventSink&  dst,
83                     timestamp_t start,
84                     timedur_t   length,
85                     timestamp_t stamp_offset) const;
86
87         /** Resizes vector if necessary (NOT realtime safe) */
88         void append(const Event& ev);
89         
90         inline const boost::shared_ptr<const Note> note_at(unsigned i) const { return _notes[i]; }
91         inline const boost::shared_ptr<Note>       note_at(unsigned i)       { return _notes[i]; }
92
93         inline size_t n_notes() const { return _notes.size(); }
94         inline bool   empty()   const { return _notes.size() == 0 && ControlSet::empty(); }
95
96         inline static bool note_time_comparator(const boost::shared_ptr<const Note>& a,
97                                                 const boost::shared_ptr<const Note>& b) { 
98                 return a->time() < b->time();
99         }
100
101         struct LaterNoteEndComparator {
102                 typedef const Note* value_type;
103                 inline bool operator()(const boost::shared_ptr<const Note> a,
104                                        const boost::shared_ptr<const Note> b) const { 
105                         return a->end_time() > b->end_time();
106                 }
107         };
108
109         typedef std::vector< boost::shared_ptr<Note> > Notes;
110         inline       Notes& notes()       { return _notes; }
111         inline const Notes& notes() const { return _notes; }
112
113         /** Read iterator */
114         class const_iterator {
115         public:
116                 const_iterator(const Sequence& seq, EventTime t);
117                 ~const_iterator();
118
119                 inline bool valid() const { return !_is_end && _event; }
120                 inline bool locked() const { return _locked; }
121
122                 const Event& operator*()  const { return *_event;  }
123                 const boost::shared_ptr<Event> operator->() const  { return _event; }
124                 const boost::shared_ptr<Event> get_event_pointer() { return _event; }
125
126                 const const_iterator& operator++(); // prefix only
127                 bool operator==(const const_iterator& other) const;
128                 bool operator!=(const const_iterator& other) const { return ! operator==(other); }
129                 
130                 const_iterator& operator=(const const_iterator& other);
131
132         private:
133                 friend class Sequence;
134
135                 const Sequence*          _seq;
136                 boost::shared_ptr<Event> _event;
137
138                 typedef std::priority_queue< boost::shared_ptr<Note>,
139                                              std::deque< boost::shared_ptr<Note> >,
140                                              LaterNoteEndComparator >
141                         ActiveNotes;
142                 
143                 mutable ActiveNotes _active_notes;
144
145                 typedef std::vector<ControlIterator> ControlIterators;
146
147                 bool                       _is_end;
148                 bool                       _locked;
149                 Notes::const_iterator      _note_iter;
150                 ControlIterators           _control_iters;
151                 ControlIterators::iterator _control_iter;
152         };
153         
154         const_iterator        begin(EventTime t=0) const { return const_iterator(*this, t); }
155         const const_iterator& end()                const { return _end_iter; }
156         
157         void      read_seek(EventTime t) { _read_iter = begin(t); }
158         EventTime read_time() const      { return _read_iter.valid() ? _read_iter->time() : 0.0; }
159
160         bool control_to_midi_event(boost::shared_ptr<Event>& ev,
161                                    const ControlIterator&    iter) const;
162         
163         bool edited() const      { return _edited; }
164         void set_edited(bool yn) { _edited = yn; }
165
166 #ifndef NDEBUG
167         bool is_sorted() const;
168 #endif
169         
170         void add_note_unlocked(const boost::shared_ptr<Note> note);
171         void remove_note_unlocked(const boost::shared_ptr<const Note> note);
172         
173         uint8_t lowest_note()  const { return _lowest_note; }
174         uint8_t highest_note() const { return _highest_note; }
175         
176 protected:
177         mutable const_iterator _read_iter;
178         bool                   _edited;
179
180 private:
181         friend class const_iterator;
182         
183         void append_note_on_unlocked(uint8_t chan, EventTime time, uint8_t note, uint8_t velocity);
184         void append_note_off_unlocked(uint8_t chan, EventTime time, uint8_t note);
185         void append_control_unlocked(const Parameter& param, EventTime time, double value);
186
187         mutable Glib::RWLock _lock;
188
189         const TypeMap& _type_map;
190         
191         Notes _notes;
192         
193         typedef std::vector<size_t> WriteNotes;
194         WriteNotes _write_notes[16];
195         bool       _writing;
196         
197         typedef std::vector< boost::shared_ptr<const ControlList> > ControlLists;
198         ControlLists _dirty_controls;
199
200         const   const_iterator _end_iter;
201         mutable nframes_t      _next_read;
202         bool                   _percussive;
203
204         uint8_t _lowest_note;
205         uint8_t _highest_note;
206
207         typedef std::priority_queue<
208                         boost::shared_ptr<Note>, std::deque< boost::shared_ptr<Note> >,
209                         LaterNoteEndComparator>
210                 ActiveNotes;
211 };
212
213
214 } // namespace Evoral
215
216 #endif // EVORAL_SEQUENCE_HPP
217