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