The great audio processing overhaul.
[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         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         /** Resizes vector if necessary (NOT realtime safe) */
82         void append(const Event<Time>& ev);
83         
84         inline const boost::shared_ptr< const Note<Time> > note_at(size_t i) const { return _notes[i]; }
85         inline const boost::shared_ptr< Note<Time> >       note_at(size_t i)       { return _notes[i]; }
86
87         inline size_t n_notes() const { return _notes.size(); }
88         inline bool   empty()   const { return _notes.size() == 0 && ControlSet::controls_empty(); }
89
90         inline static bool note_time_comparator(const boost::shared_ptr< const Note<Time> >& a,
91                                                 const boost::shared_ptr< const Note<Time> >& b) { 
92                 return a->time() < b->time();
93         }
94
95         struct LaterNoteEndComparator {
96                 typedef const Note<Time>* value_type;
97                 inline bool operator()(const boost::shared_ptr< const Note<Time> > a,
98                                        const boost::shared_ptr< const Note<Time> > b) const { 
99                         return a->end_time() > b->end_time();
100                 }
101         };
102
103         typedef std::vector< boost::shared_ptr< Note<Time> > > Notes;
104         inline       Notes& notes()       { return _notes; }
105         inline const Notes& notes() const { return _notes; }
106
107         typedef std::vector< boost::shared_ptr< Event<Time> > > SysExes;
108         inline       SysExes& sysexes()       { return _sysexes; }
109         inline const SysExes& sysexes() const { return _sysexes; }
110
111 private:
112         typedef std::priority_queue< boost::shared_ptr< Note<Time> >,
113                                      std::deque< boost::shared_ptr< Note<Time> > >,
114                                      LaterNoteEndComparator >
115                 ActiveNotes;
116 public:
117
118         /** Read iterator */
119         class const_iterator {
120         public:
121                 const_iterator();
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                 void invalidate();
129
130                 const Event<Time>& operator*()  const { return *_event;  }
131                 const boost::shared_ptr< Event<Time> > operator->() const  { return _event; }
132                 const boost::shared_ptr< Event<Time> > get_event_pointer() { return _event; }
133
134                 const const_iterator& operator++(); // prefix only
135
136                 bool operator==(const const_iterator& other) const;
137                 bool operator!=(const const_iterator& other) const { return ! operator==(other); }
138                 
139                 const_iterator& operator=(const const_iterator& other);
140
141         private:
142                 friend class Sequence<Time>;
143                 
144                 typedef std::vector<ControlIterator> ControlIterators;
145                 enum MIDIMessageType { NIL, NOTE_ON, NOTE_OFF, CONTROL, SYSEX };
146
147                 const Sequence<Time>*            _seq;
148                 boost::shared_ptr< Event<Time> > _event;
149                 mutable ActiveNotes              _active_notes;
150                 MIDIMessageType                  _type;
151                 bool                             _is_end;
152                 bool                             _locked;
153                 typename Notes::const_iterator   _note_iter;
154                 typename SysExes::const_iterator _sysex_iter;
155                 ControlIterators                 _control_iters;
156                 ControlIterators::iterator       _control_iter;
157         };
158         
159         const_iterator        begin(Time t=0) const { return const_iterator(*this, t); }
160         const const_iterator& end()           const { return _end_iter; }
161         
162         bool control_to_midi_event(boost::shared_ptr< Event<Time> >& ev,
163                                    const ControlIterator&            iter) const;
164         
165         bool edited() const      { return _edited; }
166         void set_edited(bool yn) { _edited = yn; }
167
168         void add_note_unlocked(const boost::shared_ptr< Note<Time> > note);
169         void remove_note_unlocked(const boost::shared_ptr< const Note<Time> > note);
170         
171         uint8_t lowest_note()  const { return _lowest_note; }
172         uint8_t highest_note() const { return _highest_note; }
173         
174 protected:
175         bool _edited;
176
177 private:
178         friend class const_iterator;
179         
180         void append_note_on_unlocked(uint8_t chan, Time time, uint8_t note, uint8_t velocity);
181         void append_note_off_unlocked(uint8_t chan, Time time, uint8_t note);
182         void append_control_unlocked(const Parameter& param, Time time, double value);
183         void append_sysex_unlocked(const MIDIEvent<Time>& ev);
184
185         mutable Glib::RWLock _lock;
186
187         const TypeMap& _type_map;
188         
189         Notes   _notes;
190         SysExes _sysexes;
191         
192         typedef std::vector<size_t> WriteNotes;
193         WriteNotes _write_notes[16];
194         bool       _writing;
195         
196         typedef std::vector< boost::shared_ptr<const ControlList> > ControlLists;
197         ControlLists _dirty_controls;
198
199         const   const_iterator _end_iter;
200         bool                   _percussive;
201
202         uint8_t _lowest_note;
203         uint8_t _highest_note;
204 };
205
206
207 } // namespace Evoral
208
209 #endif // EVORAL_SEQUENCE_HPP
210