Factor out sequencing related things into an independant new library: "evoral".
[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 <boost/utility.hpp>
29 #include <glibmm/thread.h>
30 #include <evoral/types.hpp>
31 #include <evoral/Note.hpp>
32 #include <evoral/Parameter.hpp>
33 #include <evoral/ControlSet.hpp>
34
35 namespace Evoral {
36
37 class EventSink;
38 class Note;
39 class Event;
40 class ControlList;
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  */
64 class Sequence : public boost::noncopyable, virtual public ControlSet {
65 public:
66         Sequence(size_t size);
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&  dst,
84                     timestamp_t start,
85                     timedur_t   length,
86                     timestamp_t stamp_offset) const;
87
88         /** Resizes vector if necessary (NOT realtime safe) */
89         void append(const Event& ev);
90         
91         inline const boost::shared_ptr<const Note> note_at(unsigned i) const { return _notes[i]; }
92         inline const boost::shared_ptr<Note>       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 && _controls.size() == 0; }
96
97         inline static bool note_time_comparator(const boost::shared_ptr<const Note> a,
98                                                 const boost::shared_ptr<const Note> b) { 
99                 return a->time() < b->time();
100         }
101
102         struct LaterNoteEndComparator {
103                 typedef const Note* value_type;
104                 inline bool operator()(const boost::shared_ptr<const Note> a,
105                                        const boost::shared_ptr<const Note> b) const { 
106                         return a->end_time() > b->end_time();
107                 }
108         };
109
110         typedef std::vector< boost::shared_ptr<Note> > Notes;
111         inline       Notes& notes()       { return _notes; }
112         inline const Notes& notes() const { return _notes; }
113
114         /** Read iterator */
115         class const_iterator {
116         public:
117                 const_iterator(const Sequence& seq, double t);
118                 ~const_iterator();
119
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                 bool                                   _is_end;
146                 bool                                   _locked;
147                 Notes::const_iterator                  _note_iter;
148                 std::vector<ControlIterator>           _control_iters;
149                 std::vector<ControlIterator>::iterator _control_iter;
150         };
151         
152         const_iterator        begin() const { return const_iterator(*this, 0); }
153         const const_iterator& end()   const { return _end_iter; }
154         
155         bool control_to_midi_event(boost::shared_ptr<Event>& ev,
156                                    const ControlIterator&    iter) const;
157         
158         typedef std::map< Parameter, boost::shared_ptr<Control> > Controls;
159         Controls&       controls()       { return _controls; }
160         const Controls& controls() const { return _controls; }
161         
162         bool edited() const      { return _edited; }
163         void set_edited(bool yn) { _edited = yn; }
164         
165 protected:
166         void add_note_unlocked(const boost::shared_ptr<Note> note);
167         void remove_note_unlocked(const boost::shared_ptr<const Note> note);
168         
169         mutable const_iterator _read_iter;
170         bool                   _edited;
171 #ifndef NDEBUG
172         bool is_sorted() const;
173 #endif
174
175 private:
176         friend class const_iterator;
177         
178         void append_note_on_unlocked(uint8_t chan, double time, uint8_t note, uint8_t velocity);
179         void append_note_off_unlocked(uint8_t chan, double time, uint8_t note);
180         void append_control_unlocked(Parameter param, double time, double value);
181
182         mutable Glib::RWLock _lock;
183
184         Notes    _notes;
185         Controls _controls;
186         
187         typedef std::vector<size_t> WriteNotes;
188         WriteNotes _write_notes[16];
189         bool       _writing;
190         
191         typedef std::vector< boost::shared_ptr<const ControlList> > ControlLists;
192         ControlLists _dirty_controls;
193
194         const   const_iterator _end_iter;
195         mutable nframes_t      _next_read;
196         bool                   _percussive;
197
198         /** FIXME: Make fully dynamic, map to URIs */
199         enum EventTypes {
200                 midi_cc_type,
201                 midi_pc_type,
202                 midi_pb_type,
203                 midi_ca_type
204         };
205
206         typedef std::priority_queue<
207                         boost::shared_ptr<Note>, std::deque< boost::shared_ptr<Note> >,
208                         LaterNoteEndComparator>
209                 ActiveNotes;
210 };
211
212 } // namespace Evoral
213
214 #endif // EVORAL_SEQUENCE_HPP
215