Display recorded controller data (fix show all/existing automation).
[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
34 namespace Evoral {
35
36 class EventSink;
37 class Note;
38 class Event;
39 class ControlList;
40
41
42 /** This class keeps track of the current x and y for a control
43  */
44 class ControlIterator {
45 public:
46         boost::shared_ptr<const ControlList> list;
47         double x;
48         double y;
49         
50         ControlIterator(boost::shared_ptr<const ControlList> a_list,
51                         double a_x,
52                         double a_y)
53                 : list(a_list)
54                 , x(a_x)
55                 , y(a_y)
56         {}
57 };
58
59
60 /** This is a higher level view of events, with separate representations for
61  * notes (instead of just unassociated note on/off events) and controller data.
62  * Controller data is represented as a list of time-stamped float values. */
63 class Sequence : virtual public ControlSet {
64 public:
65         Sequence(size_t size=0);
66         
67         bool read_locked() { return _read_iter.locked(); }
68
69         void write_lock();
70         void write_unlock();
71
72         void read_lock()   const;
73         void read_unlock() const;
74
75         void clear();
76
77         bool percussive() const     { return _percussive; }
78         void set_percussive(bool p) { _percussive = p; }
79
80         void start_write();
81         bool writing() const { return _writing; }
82         void end_write(bool delete_stuck=false);
83
84         size_t read(EventSink&  dst,
85                     timestamp_t start,
86                     timedur_t   length,
87                     timestamp_t stamp_offset) const;
88
89         /** Resizes vector if necessary (NOT realtime safe) */
90         void append(const Event& ev);
91         
92         inline const boost::shared_ptr<const Note> note_at(unsigned i) const { return _notes[i]; }
93         inline const boost::shared_ptr<Note>       note_at(unsigned i)       { return _notes[i]; }
94
95         inline size_t n_notes() const { return _notes.size(); }
96         inline bool   empty()   const { return _notes.size() == 0 && ControlSet::empty(); }
97
98         inline static bool note_time_comparator(const boost::shared_ptr<const Note>& a,
99                                                 const boost::shared_ptr<const Note>& b) { 
100                 return a->time() < b->time();
101         }
102
103         struct LaterNoteEndComparator {
104                 typedef const Note* value_type;
105                 inline bool operator()(const boost::shared_ptr<const Note> a,
106                                        const boost::shared_ptr<const Note> b) const { 
107                         return a->end_time() > b->end_time();
108                 }
109         };
110
111         typedef std::vector< boost::shared_ptr<Note> > Notes;
112         inline       Notes& notes()       { return _notes; }
113         inline const Notes& notes() const { return _notes; }
114
115         /** Read iterator */
116         class const_iterator {
117         public:
118                 const_iterator(const Sequence& seq, double t);
119                 ~const_iterator();
120
121                 inline bool valid() const { return !_is_end && _event; }
122                 inline bool locked() const { return _locked; }
123
124                 const Event& operator*()  const { return *_event;  }
125                 const boost::shared_ptr<Event> operator->() const  { return _event; }
126                 const boost::shared_ptr<Event> get_event_pointer() { return _event; }
127
128                 const const_iterator& operator++(); // prefix only
129                 bool operator==(const const_iterator& other) const;
130                 bool operator!=(const const_iterator& other) const { return ! operator==(other); }
131                 
132                 const_iterator& operator=(const const_iterator& other);
133
134         private:
135                 friend class Sequence;
136
137                 const Sequence*          _seq;
138                 boost::shared_ptr<Event> _event;
139
140                 typedef std::priority_queue< boost::shared_ptr<Note>,
141                                              std::deque< boost::shared_ptr<Note> >,
142                                              LaterNoteEndComparator >
143                         ActiveNotes;
144                 
145                 mutable ActiveNotes _active_notes;
146
147                 bool                                   _is_end;
148                 bool                                   _locked;
149                 Notes::const_iterator                  _note_iter;
150                 std::vector<ControlIterator>           _control_iters;
151                 std::vector<ControlIterator>::iterator _control_iter;
152         };
153         
154         const_iterator        begin(double t=0) const { return const_iterator(*this, t); }
155         const const_iterator& end()             const { return _end_iter; }
156         
157         void   read_seek(double t) { _read_iter = begin(t); }
158         double 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 protected:
174         mutable const_iterator _read_iter;
175         bool                   _edited;
176
177 private:
178         friend class const_iterator;
179         
180         void append_note_on_unlocked(uint8_t chan, double time, uint8_t note, uint8_t velocity);
181         void append_note_off_unlocked(uint8_t chan, double time, uint8_t note);
182         void append_control_unlocked(const Parameter& param, double time, double value);
183
184         mutable Glib::RWLock _lock;
185
186         Notes    _notes;
187         
188         typedef std::vector<size_t> WriteNotes;
189         WriteNotes _write_notes[16];
190         bool       _writing;
191         
192         typedef std::vector< boost::shared_ptr<const ControlList> > ControlLists;
193         ControlLists _dirty_controls;
194
195         const   const_iterator _end_iter;
196         mutable nframes_t      _next_read;
197         bool                   _percussive;
198
199         /** FIXME: Make fully dynamic, map to URIs */
200         enum EventTypes {
201                 midi_cc_type=0x20, // FIXME FIXME FIXME eeww
202                 midi_pc_type,
203                 midi_pb_type,
204                 midi_ca_type
205         };
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