Fix adding multiple notes with the same time stamp (geeze).
[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 <set>
25 #include <utility>
26 #include <boost/shared_ptr.hpp>
27 #include <glibmm/thread.h>
28 #include "evoral/types.hpp"
29 #include "evoral/Note.hpp"
30 #include "evoral/Parameter.hpp"
31 #include "evoral/ControlSet.hpp"
32 #include "evoral/ControlList.hpp"
33
34 namespace Evoral {
35
36 class TypeMap;
37 template<typename Time> class EventSink;
38 template<typename Time> class Note;
39 template<typename Time> class Event;
40
41 /** An iterator over (the x axis of) a 2-d double coordinate space.
42  */
43 class ControlIterator {
44 public:
45         ControlIterator(boost::shared_ptr<const ControlList> al, double ax, double ay)
46                 : list(al)
47                 , x(ax)
48                 , y(ay)
49         {}
50
51         boost::shared_ptr<const ControlList> list;
52         double x;
53         double y;
54 };
55
56
57 /** This is a higher level view of events, with separate representations for
58  * notes (instead of just unassociated note on/off events) and controller data.
59  * Controller data is represented as a list of time-stamped float values. */
60 template<typename Time>
61 class Sequence : virtual public ControlSet {
62 public:
63         Sequence(const TypeMap& type_map);
64
65 protected:
66         struct WriteLockImpl {
67                 WriteLockImpl(Glib::RWLock& s, Glib::Mutex& c)
68                         : sequence_lock(new Glib::RWLock::WriterLock(s))
69                         , control_lock(new Glib::Mutex::Lock(c))
70                 { }
71                 ~WriteLockImpl() {
72                         delete sequence_lock;
73                         delete control_lock;
74                 }
75                 Glib::RWLock::WriterLock* sequence_lock;
76                 Glib::Mutex::Lock*        control_lock;
77         };
78
79 public:
80         typedef boost::shared_ptr<Glib::RWLock::ReaderLock> ReadLock;
81         typedef boost::shared_ptr<WriteLockImpl>            WriteLock;
82
83         virtual ReadLock  read_lock() const { return ReadLock(new Glib::RWLock::ReaderLock(_lock)); }
84         virtual WriteLock write_lock()      { return WriteLock(new WriteLockImpl(_lock, _control_lock)); }
85
86         void clear();
87
88         bool percussive() const     { return _percussive; }
89         void set_percussive(bool p) { _percussive = p; }
90
91         void start_write();
92         bool writing() const { return _writing; }
93         void end_write(bool delete_stuck=false);
94
95         void append(const Event<Time>& ev);
96
97         inline size_t n_notes() const { return _notes.size(); }
98         inline bool   empty()   const { return _notes.size() == 0 && ControlSet::controls_empty(); }
99
100         inline static bool note_time_comparator(const boost::shared_ptr< const Note<Time> >& a,
101                                                 const boost::shared_ptr< const Note<Time> >& b) {
102                 return a->time() < b->time();
103         }
104
105         struct NoteNumberComparator {
106                 inline bool operator()(const boost::shared_ptr< const Note<Time> > a,
107                                        const boost::shared_ptr< const Note<Time> > b) const {
108                         return a->note() < b->note();
109                 }
110         };
111
112         struct EarlierNoteComparator {
113                 inline bool operator()(const boost::shared_ptr< const Note<Time> > a,
114                                        const boost::shared_ptr< const Note<Time> > b) const {
115                         return a->time() < b->time();
116                 }
117         };
118
119         struct LaterNoteComparator {
120                 typedef const Note<Time>* value_type;
121                 inline bool operator()(const boost::shared_ptr< const Note<Time> > a,
122                                        const boost::shared_ptr< const Note<Time> > b) const {
123                         return a->time() > b->time();
124                 }
125         };
126
127         struct LaterNoteEndComparator {
128                 typedef const Note<Time>* value_type;
129                 inline bool operator()(const boost::shared_ptr< const Note<Time> > a,
130                                        const boost::shared_ptr< const Note<Time> > b) const {
131                         return a->end_time() > b->end_time();
132                 }
133         };
134
135         typedef std::multiset<boost::shared_ptr< Note<Time> >, EarlierNoteComparator> Notes;
136         inline       Notes& notes()       { return _notes; }
137         inline const Notes& notes() const { return _notes; }
138
139         void set_notes (const Sequence<Time>::Notes& n);
140
141         typedef std::vector< boost::shared_ptr< Event<Time> > > SysExes;
142         inline       SysExes& sysexes()       { return _sysexes; }
143         inline const SysExes& sysexes() const { return _sysexes; }
144
145 private:
146         typedef std::priority_queue< boost::shared_ptr< Note<Time> >,
147                                      std::deque< boost::shared_ptr< Note<Time> > >,
148                                      LaterNoteEndComparator >
149                 ActiveNotes;
150 public:
151
152         /** Read iterator */
153         class const_iterator {
154         public:
155                 const_iterator();
156                 const_iterator(const Sequence<Time>& seq, Time t);
157                 ~const_iterator();
158
159                 inline bool valid() const { return !_is_end && _event; }
160                 //inline bool locked() const { return _locked; }
161
162                 void invalidate();
163
164                 const Event<Time>& operator*()  const { return *_event;  }
165                 const boost::shared_ptr< Event<Time> > operator->() const  { return _event; }
166                 const boost::shared_ptr< Event<Time> > get_event_pointer() { return _event; }
167
168                 const const_iterator& operator++(); // prefix only
169
170                 bool operator==(const const_iterator& other) const;
171                 bool operator!=(const const_iterator& other) const { return ! operator==(other); }
172
173                 const_iterator& operator=(const const_iterator& other);
174
175         private:
176                 friend class Sequence<Time>;
177
178                 typedef std::vector<ControlIterator> ControlIterators;
179                 enum MIDIMessageType { NIL, NOTE_ON, NOTE_OFF, CONTROL, SYSEX };
180
181                 const Sequence<Time>*            _seq;
182                 boost::shared_ptr< Event<Time> > _event;
183                 mutable ActiveNotes              _active_notes;
184                 MIDIMessageType                  _type;
185                 bool                             _is_end;
186                 typename Sequence::ReadLock      _lock;
187                 typename Notes::const_iterator   _note_iter;
188                 typename SysExes::const_iterator _sysex_iter;
189                 ControlIterators                 _control_iters;
190                 ControlIterators::iterator       _control_iter;
191         };
192
193         const_iterator        begin(Time t=0) const { return const_iterator(*this, t); }
194         const const_iterator& end()           const { return _end_iter; }
195
196         typename Notes::const_iterator note_lower_bound (Time t) const;
197
198         bool control_to_midi_event(boost::shared_ptr< Event<Time> >& ev,
199                                    const ControlIterator&            iter) const;
200
201         bool edited() const      { return _edited; }
202         void set_edited(bool yn) { _edited = yn; }
203
204         void add_note_unlocked(const boost::shared_ptr< Note<Time> > note);
205         void remove_note_unlocked(const boost::shared_ptr< const Note<Time> > note);
206
207         uint8_t lowest_note()  const { return _lowest_note; }
208         uint8_t highest_note() const { return _highest_note; }
209
210 protected:
211         bool                 _edited;
212         mutable Glib::RWLock _lock;
213
214 private:
215         friend class const_iterator;
216
217         void append_note_on_unlocked(uint8_t chan, Time time, uint8_t note, uint8_t velocity);
218         void append_note_off_unlocked(uint8_t chan, Time time, uint8_t note);
219         void append_control_unlocked(const Parameter& param, Time time, double value);
220         void append_sysex_unlocked(const MIDIEvent<Time>& ev);
221
222         const TypeMap& _type_map;
223
224         Notes   _notes;
225         SysExes _sysexes;
226
227         typedef std::set<boost::shared_ptr< Note<Time> >, NoteNumberComparator> WriteNotes;
228         WriteNotes _write_notes[16];
229         bool       _writing;
230
231         typedef std::vector< boost::shared_ptr<const ControlList> > ControlLists;
232         ControlLists _dirty_controls;
233
234         const   const_iterator _end_iter;
235         bool                   _percussive;
236
237         uint8_t _lowest_note;
238         uint8_t _highest_note;
239 };
240
241
242 } // namespace Evoral
243
244 #endif // EVORAL_SEQUENCE_HPP
245