/* This file is part of Evoral.
* Copyright (C) 2008 Dave Robillard <http://drobilla.net>
* Copyright (C) 2000-2008 Paul Davis
- *
+ *
* Evoral is free software; you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the Free Software
* Foundation; either version 2 of the License, or (at your option) any later
* version.
- *
+ *
* Evoral is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for details.
- *
+ *
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#include <vector>
#include <queue>
-#include <deque>
-#include <map>
+#include <set>
+#include <list>
#include <utility>
#include <boost/shared_ptr.hpp>
#include <glibmm/thread.h>
namespace Evoral {
class TypeMap;
-template<typename T> class EventSink;
-template<typename T> class Note;
-template<typename T> class Event;
+template<typename Time> class EventSink;
+template<typename Time> class Note;
+template<typename Time> class Event;
/** An iterator over (the x axis of) a 2-d double coordinate space.
*/
/** This is a higher level view of events, with separate representations for
* notes (instead of just unassociated note on/off events) and controller data.
* Controller data is represented as a list of time-stamped float values. */
-template<typename T>
+template<typename Time>
class Sequence : virtual public ControlSet {
public:
- Sequence(const TypeMap& type_map, size_t size=0);
-
- bool read_locked() { return _read_iter.locked(); }
+ Sequence(const TypeMap& type_map);
+ Sequence(const Sequence<Time>& other);
- void write_lock();
- void write_unlock();
+protected:
+ struct WriteLockImpl {
+ WriteLockImpl(Glib::RWLock& s, Glib::Mutex& c)
+ : sequence_lock(new Glib::RWLock::WriterLock(s))
+ , control_lock(new Glib::Mutex::Lock(c))
+ { }
+ ~WriteLockImpl() {
+ delete sequence_lock;
+ delete control_lock;
+ }
+ Glib::RWLock::WriterLock* sequence_lock;
+ Glib::Mutex::Lock* control_lock;
+ };
+
+public:
+
+ typedef typename boost::shared_ptr<Evoral::Note<Time> > NotePtr;
+ typedef typename boost::shared_ptr<const Evoral::Note<Time> > constNotePtr;
- void read_lock() const;
- void read_unlock() const;
+ typedef boost::shared_ptr<Glib::RWLock::ReaderLock> ReadLock;
+ typedef boost::shared_ptr<WriteLockImpl> WriteLock;
+
+ virtual ReadLock read_lock() const { return ReadLock(new Glib::RWLock::ReaderLock(_lock)); }
+ virtual WriteLock write_lock() { return WriteLock(new WriteLockImpl(_lock, _control_lock)); }
void clear();
bool writing() const { return _writing; }
void end_write(bool delete_stuck=false);
- size_t read(EventSink<T>& dst,
- timestamp_t start,
- timedur_t length,
- timestamp_t stamp_offset) const;
-
- /** Resizes vector if necessary (NOT realtime safe) */
- void append(const Event<T>& ev);
-
- inline const boost::shared_ptr< const Note<T> > note_at(unsigned i) const { return _notes[i]; }
- inline const boost::shared_ptr< Note<T> > note_at(unsigned i) { return _notes[i]; }
+ void append(const Event<Time>& ev, Evoral::event_id_t evid);
inline size_t n_notes() const { return _notes.size(); }
- inline bool empty() const { return _notes.size() == 0 && ControlSet::empty(); }
+ inline bool empty() const { return _notes.size() == 0 && ControlSet::controls_empty(); }
- inline static bool note_time_comparator(const boost::shared_ptr< const Note<T> >& a,
- const boost::shared_ptr< const Note<T> >& b) {
+ inline static bool note_time_comparator(const boost::shared_ptr< const Note<Time> >& a,
+ const boost::shared_ptr< const Note<Time> >& b) {
return a->time() < b->time();
}
+ struct NoteNumberComparator {
+ inline bool operator()(const boost::shared_ptr< const Note<Time> > a,
+ const boost::shared_ptr< const Note<Time> > b) const {
+ return a->note() < b->note();
+ }
+ };
+
+ struct EarlierNoteComparator {
+ inline bool operator()(const boost::shared_ptr< const Note<Time> > a,
+ const boost::shared_ptr< const Note<Time> > b) const {
+ return a->time() < b->time();
+ }
+ };
+
+ struct LaterNoteComparator {
+ typedef const Note<Time>* value_type;
+ inline bool operator()(const boost::shared_ptr< const Note<Time> > a,
+ const boost::shared_ptr< const Note<Time> > b) const {
+ return a->time() > b->time();
+ }
+ };
+
struct LaterNoteEndComparator {
- typedef const Note<T>* value_type;
- inline bool operator()(const boost::shared_ptr< const Note<T> > a,
- const boost::shared_ptr< const Note<T> > b) const {
+ typedef const Note<Time>* value_type;
+ inline bool operator()(const boost::shared_ptr< const Note<Time> > a,
+ const boost::shared_ptr< const Note<Time> > b) const {
return a->end_time() > b->end_time();
}
};
- typedef std::vector< boost::shared_ptr< Note<T> > > Notes;
+ typedef std::multiset<NotePtr, EarlierNoteComparator> Notes;
inline Notes& notes() { return _notes; }
inline const Notes& notes() const { return _notes; }
- // useful for storing SysEx / Meta events
- typedef std::vector< boost::shared_ptr< Event<T> > > SysExes;
+ enum NoteOperator {
+ PitchEqual,
+ PitchLessThan,
+ PitchLessThanOrEqual,
+ PitchGreater,
+ PitchGreaterThanOrEqual,
+ VelocityEqual,
+ VelocityLessThan,
+ VelocityLessThanOrEqual,
+ VelocityGreater,
+ VelocityGreaterThanOrEqual,
+ };
+
+ void get_notes (Notes&, NoteOperator, uint8_t val, int chan_mask = 0) const;
+
+ void remove_overlapping_notes ();
+ void trim_overlapping_notes ();
+ void remove_duplicate_notes ();
+
+ enum OverlapPitchResolution {
+ LastOnFirstOff,
+ FirstOnFirstOff
+ };
+
+ bool overlapping_pitches_accepted() const { return _overlapping_pitches_accepted; }
+ void overlapping_pitches_accepted(bool yn) { _overlapping_pitches_accepted = yn; }
+ OverlapPitchResolution overlap_pitch_resolution() const { return _overlap_pitch_resolution; }
+ void set_overlap_pitch_resolution(OverlapPitchResolution opr);
+
+ void set_notes (const Sequence<Time>::Notes& n);
+
+ typedef std::vector< boost::shared_ptr< Event<Time> > > SysExes;
inline SysExes& sysexes() { return _sysexes; }
inline const SysExes& sysexes() const { return _sysexes; }
+private:
+ typedef std::priority_queue<NotePtr, std::deque<NotePtr>, LaterNoteEndComparator> ActiveNotes;
+public:
+
/** Read iterator */
class const_iterator {
public:
- const_iterator(const Sequence<T>& seq, T t);
+ const_iterator();
+ const_iterator(const Sequence<Time>& seq, Time t, bool, std::set<Evoral::Parameter> const &);
~const_iterator();
inline bool valid() const { return !_is_end && _event; }
- inline bool locked() const { return _locked; }
+ //inline bool locked() const { return _locked; }
- const Event<T>& operator*() const { return *_event; }
- const boost::shared_ptr< Event<T> > operator->() const { return _event; }
- const boost::shared_ptr< Event<T> > get_event_pointer() { return _event; }
+ void invalidate();
+
+ const Event<Time>& operator*() const { return *_event; }
+ const boost::shared_ptr< Event<Time> > operator->() const { return _event; }
+ const boost::shared_ptr< Event<Time> > get_event_pointer() { return _event; }
const const_iterator& operator++(); // prefix only
+
bool operator==(const const_iterator& other) const;
bool operator!=(const const_iterator& other) const { return ! operator==(other); }
-
+
const_iterator& operator=(const const_iterator& other);
private:
- friend class Sequence<T>;
-
- enum MIDIMessageType { NIL, NOTE_ON, NOTE_OFF, CONTROL, SYSEX };
-
- const Sequence<T>* _seq;
- boost::shared_ptr< Event<T> > _event;
-
- typedef std::priority_queue< boost::shared_ptr< Note<T> >,
- std::deque< boost::shared_ptr< Note<T> > >,
- LaterNoteEndComparator >
- ActiveNotes;
-
- mutable ActiveNotes _active_notes;
+ friend class Sequence<Time>;
typedef std::vector<ControlIterator> ControlIterators;
+ enum MIDIMessageType { NIL, NOTE_ON, NOTE_OFF, CONTROL, SYSEX };
+ const Sequence<Time>* _seq;
+ boost::shared_ptr< Event<Time> > _event;
+ mutable ActiveNotes _active_notes;
+ MIDIMessageType _type;
bool _is_end;
- bool _locked;
+ typename Sequence::ReadLock _lock;
typename Notes::const_iterator _note_iter;
typename SysExes::const_iterator _sysex_iter;
ControlIterators _control_iters;
ControlIterators::iterator _control_iter;
+ bool _force_discrete;
};
-
- const_iterator begin(T t=0) const { return const_iterator(*this, t); }
- const const_iterator& end() const { return _end_iter; }
-
- void read_seek(T t) { _read_iter = begin(t); }
- T read_time() const { return _read_iter.valid() ? _read_iter->time() : 0.0; }
-
- bool control_to_midi_event(boost::shared_ptr< Event<T> >& ev,
- const ControlIterator& iter) const;
-
+
+ const_iterator begin (Time t = 0, bool force_discrete = false, std::set<Evoral::Parameter> const & f = std::set<Evoral::Parameter> ()) const {
+ return const_iterator (*this, t, force_discrete, f);
+ }
+ const const_iterator& end() const { return _end_iter; }
+
+ typename Notes::const_iterator note_lower_bound (Time t) const;
+
+ bool control_to_midi_event(boost::shared_ptr< Event<Time> >& ev,
+ const ControlIterator& iter) const;
+
bool edited() const { return _edited; }
void set_edited(bool yn) { _edited = yn; }
-#ifndef NDEBUG
- bool is_sorted() const;
-#endif
-
- void add_note_unlocked(const boost::shared_ptr< Note<T> > note);
- void remove_note_unlocked(const boost::shared_ptr< const Note<T> > note);
-
+ bool overlaps (const NotePtr& ev,
+ const NotePtr& ignore_this_note) const;
+ bool contains (const NotePtr& ev) const;
+
+ bool add_note_unlocked (const NotePtr note, void* arg = 0);
+ void remove_note_unlocked(const constNotePtr note);
+
uint8_t lowest_note() const { return _lowest_note; }
uint8_t highest_note() const { return _highest_note; }
-
+
+
protected:
- mutable const_iterator _read_iter;
bool _edited;
+ bool _overlapping_pitches_accepted;
+ OverlapPitchResolution _overlap_pitch_resolution;
+ mutable Glib::RWLock _lock;
+ bool _writing;
+
+ virtual int resolve_overlaps_unlocked (const NotePtr, void* arg = 0) {
+ return 0;
+ }
+
+ typedef std::multiset<NotePtr, NoteNumberComparator> Pitches;
+ inline Pitches& pitches(uint8_t chan) { return _pitches[chan&0xf]; }
+ inline const Pitches& pitches(uint8_t chan) const { return _pitches[chan&0xf]; }
private:
friend class const_iterator;
-
- void append_note_on_unlocked(uint8_t chan, T time, uint8_t note, uint8_t velocity);
- void append_note_off_unlocked(uint8_t chan, T time, uint8_t note);
- void append_control_unlocked(const Parameter& param, T time, double value);
- void append_sysex_unlocked(const MIDIEvent<T>& ev);
- mutable Glib::RWLock _lock;
+ bool overlaps_unlocked (const NotePtr& ev, const NotePtr& ignore_this_note) const;
+ bool contains_unlocked (const NotePtr& ev) const;
+
+ void append_note_on_unlocked (NotePtr, Evoral::event_id_t);
+ void append_note_off_unlocked(NotePtr);
+ void append_control_unlocked(const Parameter& param, Time time, double value, Evoral::event_id_t);
+ void append_sysex_unlocked(const MIDIEvent<Time>& ev, Evoral::event_id_t);
+
+ void get_notes_by_pitch (Notes&, NoteOperator, uint8_t val, int chan_mask = 0) const;
+ void get_notes_by_velocity (Notes&, NoteOperator, uint8_t val, int chan_mask = 0) const;
+
+ void control_list_marked_dirty ();
const TypeMap& _type_map;
-
- Notes _notes;
-
+
+ Notes _notes; // notes indexed by time
+ Pitches _pitches[16]; // notes indexed by channel+pitch
SysExes _sysexes;
-
- typedef std::vector<size_t> WriteNotes;
+
+ typedef std::multiset<NotePtr, EarlierNoteComparator> WriteNotes;
WriteNotes _write_notes[16];
- bool _writing;
-
- typedef std::vector< boost::shared_ptr<const ControlList> > ControlLists;
- ControlLists _dirty_controls;
const const_iterator _end_iter;
- mutable FrameTime _next_read;
bool _percussive;
uint8_t _lowest_note;
uint8_t _highest_note;
-
- typedef std::priority_queue<
- boost::shared_ptr< Note<T> >, std::deque< boost::shared_ptr< Note<T> > >,
- LaterNoteEndComparator>
- ActiveNotes;
};