initial pass at session-renaming functionality
[ardour.git] / libs / evoral / evoral / Sequence.hpp
index 36f4138abddf4444d49ef2b7fca3045d0a4d4aa0..298ef48e52ec979e75c6a035fbdbdf79d9e188ac 100644 (file)
@@ -1,5 +1,5 @@
 /* This file is part of Evoral.
- * Copyright (C) 2008 Dave Robillard <http://drobilla.net>
+ * Copyright (C) 2008 David Robillard <http://drobilla.net>
  * Copyright (C) 2000-2008 Paul Davis
  *
  * Evoral is free software; you can redistribute it and/or modify it under the
@@ -22,6 +22,7 @@
 #include <vector>
 #include <queue>
 #include <set>
+#include <list>
 #include <utility>
 #include <boost/shared_ptr.hpp>
 #include <glibmm/thread.h>
@@ -30,6 +31,7 @@
 #include "evoral/Parameter.hpp"
 #include "evoral/ControlSet.hpp"
 #include "evoral/ControlList.hpp"
+#include "evoral/PatchChange.hpp"
 
 namespace Evoral {
 
@@ -61,7 +63,7 @@ template<typename Time>
 class Sequence : virtual public ControlSet {
 public:
        Sequence(const TypeMap& type_map);
-        Sequence(const Sequence<Time>& other);
+       Sequence(const Sequence<Time>& other);
 
 protected:
        struct WriteLockImpl {
@@ -78,6 +80,10 @@ protected:
        };
 
 public:
+
+       typedef typename boost::shared_ptr<Evoral::Note<Time> >  NotePtr;
+       typedef typename boost::shared_ptr<const Evoral::Note<Time> >  constNotePtr;
+
        typedef boost::shared_ptr<Glib::RWLock::ReaderLock> ReadLock;
        typedef boost::shared_ptr<WriteLockImpl>            WriteLock;
 
@@ -91,12 +97,12 @@ public:
 
        void start_write();
        bool writing() const { return _writing; }
-       void end_write(bool delete_stuck=false);
+        void end_write (Time when=0, bool delete_stuck=false, bool resolve=false);
 
-       void append(const Event<Time>& ev);
+       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::controls_empty(); }
+       inline bool   empty()   const { return _notes.empty() && _sysexes.empty() && _patch_changes.empty() && ControlSet::controls_empty(); }
 
        inline static bool note_time_comparator(const boost::shared_ptr< const Note<Time> >& a,
                                                const boost::shared_ptr< const Note<Time> >& b) {
@@ -133,27 +139,38 @@ public:
                }
        };
 
-       typedef std::multiset<boost::shared_ptr< Note<Time> >, EarlierNoteComparator> Notes;
+       typedef std::multiset<NotePtr, EarlierNoteComparator> Notes;
        inline       Notes& notes()       { return _notes; }
        inline const Notes& notes() const { return _notes; }
 
-        enum NoteOperator { 
-                PitchEqual,
-                PitchLessThan,
-                PitchLessThanOrEqual,
-                PitchGreater,
-                PitchGreaterThanOrEqual,
-                VelocityEqual,
-                VelocityLessThan,
-                VelocityLessThanOrEqual,
-                VelocityGreater,
-                VelocityGreaterThanOrEqual,
-        };
+       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 ();
 
-        void get_notes (Notes&, NoteOperator, uint8_t val, int chan_mask = 0) const;
+       enum OverlapPitchResolution { 
+               LastOnFirstOff,
+               FirstOnFirstOff
+       };
 
-        void remove_overlapping_notes ();
-        void remove_duplicate_notes ();
+       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);
 
@@ -161,18 +178,30 @@ public:
        inline       SysExes& sysexes()       { return _sysexes; }
        inline const SysExes& sysexes() const { return _sysexes; }
 
+       typedef boost::shared_ptr<PatchChange<Time> > PatchChangePtr;
+       typedef boost::shared_ptr<const PatchChange<Time> > constPatchChangePtr;
+       
+       struct EarlierPatchChangeComparator {
+               inline bool operator() (constPatchChangePtr a, constPatchChangePtr b) const {
+                       return a->time() < b->time();
+               }
+       };
+
+       typedef std::multiset<PatchChangePtr, EarlierPatchChangeComparator> PatchChanges;
+       inline       PatchChanges& patch_changes ()       { return _patch_changes; }
+       inline const PatchChanges& patch_changes () const { return _patch_changes; }
+
+        void dump (std::ostream&) const;
+
 private:
-       typedef std::priority_queue< boost::shared_ptr< Note<Time> >,
-                                    std::deque< boost::shared_ptr< Note<Time> > >,
-                                    LaterNoteEndComparator >
-               ActiveNotes;
+       typedef std::priority_queue<NotePtr, std::deque<NotePtr>, LaterNoteEndComparator> ActiveNotes;
 public:
 
        /** Read iterator */
        class const_iterator {
        public:
                const_iterator();
-               const_iterator(const Sequence<Time>& seq, Time t);
+               const_iterator(const Sequence<Time>& seq, Time t, bool, std::set<Evoral::Parameter> const &);
                ~const_iterator();
 
                inline bool valid() const { return !_is_end && _event; }
@@ -195,24 +224,37 @@ public:
                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;
-               typename Sequence::ReadLock      _lock;
-               typename Notes::const_iterator   _note_iter;
-               typename SysExes::const_iterator _sysex_iter;
-               ControlIterators                 _control_iters;
-               ControlIterators::iterator       _control_iter;
+               enum MIDIMessageType { NIL, NOTE_ON, NOTE_OFF, CONTROL, SYSEX, PATCH_CHANGE };
+
+               const Sequence<Time>*                 _seq;
+               boost::shared_ptr< Event<Time> >      _event;
+               mutable ActiveNotes                   _active_notes;
+               /** If the iterator is pointing at a patch change, this is the index of the
+                *  sub-message within that change.
+                */
+               int                                   _active_patch_change_message;
+               MIDIMessageType                       _type;
+               bool                                  _is_end;
+               typename Sequence::ReadLock           _lock;
+               typename Notes::const_iterator        _note_iter;
+               typename SysExes::const_iterator      _sysex_iter;
+               typename PatchChanges::const_iterator _patch_change_iter;
+               ControlIterators                      _control_iters;
+               ControlIterators::iterator            _control_iter;
+               bool                                  _force_discrete;
        };
 
-       const_iterator        begin(Time t=0) const { return const_iterator(*this, t); }
-       const const_iterator& end()           const { return _end_iter; }
+       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;
+       typename PatchChanges::const_iterator patch_change_lower_bound (Time t) const;
 
        bool control_to_midi_event(boost::shared_ptr< Event<Time> >& ev,
                                   const ControlIterator&            iter) const;
@@ -220,36 +262,67 @@ public:
        bool edited() const      { return _edited; }
        void set_edited(bool yn) { _edited = yn; }
 
-        bool contains (const boost::shared_ptr< Note<Time> > ev) const;
-       bool add_note_unlocked(const boost::shared_ptr< Note<Time> > note);
-       void remove_note_unlocked(const boost::shared_ptr< const Note<Time> > 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);
+
+       void add_patch_change_unlocked (const PatchChangePtr);
+       void remove_patch_change_unlocked (const constPatchChangePtr);
 
        uint8_t lowest_note()  const { return _lowest_note; }
        uint8_t highest_note() const { return _highest_note; }
 
+
 protected:
-       bool                 _edited;
-       mutable Glib::RWLock _lock;
+       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, Time time, uint8_t note, uint8_t velocity);
-       void append_note_off_unlocked(uint8_t chan, Time time, uint8_t note);
-       void append_control_unlocked(const Parameter& param, Time time, double value);
-       void append_sysex_unlocked(const MIDIEvent<Time>& ev);
+       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 append_patch_change_unlocked (const PatchChange<Time>&, 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;
-       SysExes _sysexes;
+       Notes        _notes;       // notes indexed by time
+       Pitches      _pitches[16]; // notes indexed by channel+pitch
+       SysExes      _sysexes;
+       PatchChanges _patch_changes;
 
-       typedef std::set<boost::shared_ptr< Note<Time> >, NoteNumberComparator> 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;
+       /** Current bank number on each channel so that we know what
+        *  to put in PatchChange events when program changes are
+        *  seen.
+        */
+       int _bank[16];
 
        const   const_iterator _end_iter;
        bool                   _percussive;
@@ -261,5 +334,7 @@ private:
 
 } // namespace Evoral
 
+// template<typename Time> std::ostream& operator<<(std::ostream& o, const Evoral::Sequence<Time>& s) { s.dump (o); return o; }
+
 #endif // EVORAL_SEQUENCE_HPP