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
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"
38 template<typename Time> class EventSink;
39 template<typename Time> class Note;
40 template<typename Time> class Event;
42 /** An iterator over (the x axis of) a 2-d double coordinate space.
44 class ControlIterator {
46 ControlIterator(boost::shared_ptr<const ControlList> al, double ax, double ay)
52 boost::shared_ptr<const ControlList> list;
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 {
64 Sequence(const TypeMap& type_map, size_t size=0);
69 void read_lock() const;
70 void read_unlock() const;
74 bool percussive() const { return _percussive; }
75 void set_percussive(bool p) { _percussive = p; }
78 bool writing() const { return _writing; }
79 void end_write(bool delete_stuck=false);
81 /** Resizes vector if necessary (NOT realtime safe) */
82 void append(const Event<Time>& ev);
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]; }
87 inline size_t n_notes() const { return _notes.size(); }
88 inline bool empty() const { return _notes.size() == 0 && ControlSet::controls_empty(); }
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();
95 struct LaterNoteComparator {
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->time() > b->time();
103 struct LaterNoteEndComparator {
104 typedef const Note<Time>* value_type;
105 inline bool operator()(const boost::shared_ptr< const Note<Time> > a,
106 const boost::shared_ptr< const Note<Time> > b) const {
107 return a->end_time() > b->end_time();
111 typedef std::vector< boost::shared_ptr< Note<Time> > > Notes;
112 inline Notes& notes() { return _notes; }
113 inline const Notes& notes() const { return _notes; }
115 void set_notes (const std::vector<boost::shared_ptr<Note<Time> > >&);
117 typedef std::vector< boost::shared_ptr< Event<Time> > > SysExes;
118 inline SysExes& sysexes() { return _sysexes; }
119 inline const SysExes& sysexes() const { return _sysexes; }
122 typedef std::priority_queue< boost::shared_ptr< Note<Time> >,
123 std::deque< boost::shared_ptr< Note<Time> > >,
124 LaterNoteEndComparator >
129 class const_iterator {
132 const_iterator(const Sequence<Time>& seq, Time t);
135 inline bool valid() const { return !_is_end && _event; }
136 inline bool locked() const { return _locked; }
140 const Event<Time>& operator*() const { return *_event; }
141 const boost::shared_ptr< Event<Time> > operator->() const { return _event; }
142 const boost::shared_ptr< Event<Time> > get_event_pointer() { return _event; }
144 const const_iterator& operator++(); // prefix only
146 bool operator==(const const_iterator& other) const;
147 bool operator!=(const const_iterator& other) const { return ! operator==(other); }
149 const_iterator& operator=(const const_iterator& other);
152 friend class Sequence<Time>;
154 typedef std::vector<ControlIterator> ControlIterators;
155 enum MIDIMessageType { NIL, NOTE_ON, NOTE_OFF, CONTROL, SYSEX };
157 const Sequence<Time>* _seq;
158 boost::shared_ptr< Event<Time> > _event;
159 mutable ActiveNotes _active_notes;
160 MIDIMessageType _type;
163 typename Notes::const_iterator _note_iter;
164 typename SysExes::const_iterator _sysex_iter;
165 ControlIterators _control_iters;
166 ControlIterators::iterator _control_iter;
169 const_iterator begin(Time t=0) const { return const_iterator(*this, t); }
170 const const_iterator& end() const { return _end_iter; }
172 bool control_to_midi_event(boost::shared_ptr< Event<Time> >& ev,
173 const ControlIterator& iter) const;
175 bool edited() const { return _edited; }
176 void set_edited(bool yn) { _edited = yn; }
178 void add_note_unlocked(const boost::shared_ptr< Note<Time> > note);
179 void remove_note_unlocked(const boost::shared_ptr< const Note<Time> > note);
181 uint8_t lowest_note() const { return _lowest_note; }
182 uint8_t highest_note() const { return _highest_note; }
188 friend class const_iterator;
190 void append_note_on_unlocked(uint8_t chan, Time time, uint8_t note, uint8_t velocity);
191 void append_note_off_unlocked(uint8_t chan, Time time, uint8_t note);
192 void append_control_unlocked(const Parameter& param, Time time, double value);
193 void append_sysex_unlocked(const MIDIEvent<Time>& ev);
195 mutable Glib::RWLock _lock;
197 const TypeMap& _type_map;
202 typedef std::vector<size_t> WriteNotes;
203 WriteNotes _write_notes[16];
206 typedef std::vector< boost::shared_ptr<const ControlList> > ControlLists;
207 ControlLists _dirty_controls;
209 const const_iterator _end_iter;
212 uint8_t _lowest_note;
213 uint8_t _highest_note;
217 } // namespace Evoral
219 #endif // EVORAL_SEQUENCE_HPP