1 /* This file is part of Evoral.
2 * Copyright (C) 2008 Dave Robillard <http://drobilla.net>
3 * Copyright (C) 2000-2008 Paul Davis
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
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.
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
19 #ifndef EVORAL_SEQUENCE_HPP
20 #define EVORAL_SEQUENCE_HPP
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"
37 template<typename Time> class EventSink;
38 template<typename Time> class Note;
39 template<typename Time> class Event;
41 /** An iterator over (the x axis of) a 2-d double coordinate space.
43 class ControlIterator {
45 ControlIterator(boost::shared_ptr<const ControlList> al, double ax, double ay)
51 boost::shared_ptr<const ControlList> list;
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 {
63 Sequence(const TypeMap& type_map);
64 Sequence(const Sequence<Time>& other);
67 struct WriteLockImpl {
68 WriteLockImpl(Glib::RWLock& s, Glib::Mutex& c)
69 : sequence_lock(new Glib::RWLock::WriterLock(s))
70 , control_lock(new Glib::Mutex::Lock(c))
76 Glib::RWLock::WriterLock* sequence_lock;
77 Glib::Mutex::Lock* control_lock;
81 typedef boost::shared_ptr<Glib::RWLock::ReaderLock> ReadLock;
82 typedef boost::shared_ptr<WriteLockImpl> WriteLock;
84 virtual ReadLock read_lock() const { return ReadLock(new Glib::RWLock::ReaderLock(_lock)); }
85 virtual WriteLock write_lock() { return WriteLock(new WriteLockImpl(_lock, _control_lock)); }
89 bool percussive() const { return _percussive; }
90 void set_percussive(bool p) { _percussive = p; }
93 bool writing() const { return _writing; }
94 void end_write(bool delete_stuck=false);
96 void append(const Event<Time>& ev);
98 inline size_t n_notes() const { return _notes.size(); }
99 inline bool empty() const { return _notes.size() == 0 && ControlSet::controls_empty(); }
101 inline static bool note_time_comparator(const boost::shared_ptr< const Note<Time> >& a,
102 const boost::shared_ptr< const Note<Time> >& b) {
103 return a->time() < b->time();
106 struct NoteNumberComparator {
107 inline bool operator()(const boost::shared_ptr< const Note<Time> > a,
108 const boost::shared_ptr< const Note<Time> > b) const {
109 return a->note() < b->note();
113 struct EarlierNoteComparator {
114 inline bool operator()(const boost::shared_ptr< const Note<Time> > a,
115 const boost::shared_ptr< const Note<Time> > b) const {
116 return a->time() < b->time();
120 struct LaterNoteComparator {
121 typedef const Note<Time>* value_type;
122 inline bool operator()(const boost::shared_ptr< const Note<Time> > a,
123 const boost::shared_ptr< const Note<Time> > b) const {
124 return a->time() > b->time();
128 struct LaterNoteEndComparator {
129 typedef const Note<Time>* value_type;
130 inline bool operator()(const boost::shared_ptr< const Note<Time> > a,
131 const boost::shared_ptr< const Note<Time> > b) const {
132 return a->end_time() > b->end_time();
136 typedef std::multiset<boost::shared_ptr< Note<Time> >, EarlierNoteComparator> Notes;
137 inline Notes& notes() { return _notes; }
138 inline const Notes& notes() const { return _notes; }
140 void set_notes (const Sequence<Time>::Notes& n);
142 typedef std::vector< boost::shared_ptr< Event<Time> > > SysExes;
143 inline SysExes& sysexes() { return _sysexes; }
144 inline const SysExes& sysexes() const { return _sysexes; }
147 typedef std::priority_queue< boost::shared_ptr< Note<Time> >,
148 std::deque< boost::shared_ptr< Note<Time> > >,
149 LaterNoteEndComparator >
154 class const_iterator {
157 const_iterator(const Sequence<Time>& seq, Time t);
160 inline bool valid() const { return !_is_end && _event; }
161 //inline bool locked() const { return _locked; }
165 const Event<Time>& operator*() const { return *_event; }
166 const boost::shared_ptr< Event<Time> > operator->() const { return _event; }
167 const boost::shared_ptr< Event<Time> > get_event_pointer() { return _event; }
169 const const_iterator& operator++(); // prefix only
171 bool operator==(const const_iterator& other) const;
172 bool operator!=(const const_iterator& other) const { return ! operator==(other); }
174 const_iterator& operator=(const const_iterator& other);
177 friend class Sequence<Time>;
179 typedef std::vector<ControlIterator> ControlIterators;
180 enum MIDIMessageType { NIL, NOTE_ON, NOTE_OFF, CONTROL, SYSEX };
182 const Sequence<Time>* _seq;
183 boost::shared_ptr< Event<Time> > _event;
184 mutable ActiveNotes _active_notes;
185 MIDIMessageType _type;
187 typename Sequence::ReadLock _lock;
188 typename Notes::const_iterator _note_iter;
189 typename SysExes::const_iterator _sysex_iter;
190 ControlIterators _control_iters;
191 ControlIterators::iterator _control_iter;
194 const_iterator begin(Time t=0) const { return const_iterator(*this, t); }
195 const const_iterator& end() const { return _end_iter; }
197 typename Notes::const_iterator note_lower_bound (Time t) const;
199 bool control_to_midi_event(boost::shared_ptr< Event<Time> >& ev,
200 const ControlIterator& iter) const;
202 bool edited() const { return _edited; }
203 void set_edited(bool yn) { _edited = yn; }
205 bool contains (const boost::shared_ptr< Note<Time> > ev) const;
206 bool add_note_unlocked(const boost::shared_ptr< Note<Time> > note);
207 void remove_note_unlocked(const boost::shared_ptr< const Note<Time> > note);
209 uint8_t lowest_note() const { return _lowest_note; }
210 uint8_t highest_note() const { return _highest_note; }
214 mutable Glib::RWLock _lock;
217 friend class const_iterator;
219 void append_note_on_unlocked(uint8_t chan, Time time, uint8_t note, uint8_t velocity);
220 void append_note_off_unlocked(uint8_t chan, Time time, uint8_t note);
221 void append_control_unlocked(const Parameter& param, Time time, double value);
222 void append_sysex_unlocked(const MIDIEvent<Time>& ev);
224 const TypeMap& _type_map;
229 typedef std::set<boost::shared_ptr< Note<Time> >, NoteNumberComparator> WriteNotes;
230 WriteNotes _write_notes[16];
233 typedef std::vector< boost::shared_ptr<const ControlList> > ControlLists;
234 ControlLists _dirty_controls;
236 const const_iterator _end_iter;
239 uint8_t _lowest_note;
240 uint8_t _highest_note;
244 } // namespace Evoral
246 #endif // EVORAL_SEQUENCE_HPP