Fix the horrible mess that was anything related to sources and paths.
[ardour.git] / libs / ardour / ardour / midi_model.h
index ff5239818056f0e4e0589c1f0e2ab295ed96eb58..16d972c3ccc6ef44101e220f9d8a0f0e4ea019e3 100644 (file)
 #ifndef __ardour_midi_model_h__ 
 #define __ardour_midi_model_h__
 
+#include <queue>
+#include <deque>
+#include <utility>
 #include <boost/utility.hpp>
+#include <glibmm/thread.h>
+#include <pbd/command.h>
 #include <ardour/types.h>
 #include <ardour/midi_buffer.h>
+#include <ardour/midi_ring_buffer.h>
+#include <ardour/automatable_sequence.h>
+#include <ardour/types.h>
+#include <evoral/Note.hpp>
+#include <evoral/Sequence.hpp>
 
 namespace ARDOUR {
 
-/** This is a slightly higher level (than MidiBuffer) model of MIDI note data.
- * Currently it only represents note data, which is represented as complete
- * note events (ie with a start time and a duration) rather than separate
- * note on and off events (controller data is not here since it's represented
- * as an AutomationList)
+class Session;
+class MidiSource;
+
+/** This is a higher level (than MidiBuffer) model of MIDI data, with separate
+ * representations for notes (instead of just unassociated note on/off events)
+ * and controller data.  Controller data is represented as part of the
+ * Automatable base (i.e. in a map of AutomationList, keyed by Parameter).
+ * Because of this MIDI controllers and automatable controllers/widgets/etc
+ * are easily interchangeable.
  */
-class MidiModel : public boost::noncopyable {
+class MidiModel : public AutomatableSequence<double> {
 public:
-       struct Note {
-               Note(double s=0, double d=0, uint8_t n=0, uint8_t v=0)
-               : start(s), duration(d), note(n), velocity(v) {}
-
-               double start;
-               double duration;
-               uint8_t note;
-               uint8_t velocity;
-       };
+       typedef double TimeType;
 
-       MidiModel(size_t size=0);
+       MidiModel(MidiSource* s, size_t size=0);
+       
+       NoteMode note_mode() const { return (percussive() ? Percussive : Sustained); }
+       void set_note_mode(NoteMode mode) { set_percussive(mode == Percussive); };
+
+       /** Add/Remove notes.
+        * Technically all note operations can be implemented as one of these, but
+        * a custom command can be more efficient.
+        */
+       class DeltaCommand : public Command {
+       public:
+               DeltaCommand (boost::shared_ptr<MidiModel> m, const std::string& name);
+               DeltaCommand (boost::shared_ptr<MidiModel> m, const XMLNode& node);
+
+               const std::string& name() const { return _name; }
+               
+               void operator()();
+               void undo();
+               
+               int set_state (const XMLNode&);
+               XMLNode& get_state ();
+
+               void add(const boost::shared_ptr< Evoral::Note<TimeType> > note);
+               void remove(const boost::shared_ptr< Evoral::Note<TimeType> > note);
+
+       private:
+               XMLNode &marshal_note(const boost::shared_ptr< Evoral::Note<TimeType> > note);
+               boost::shared_ptr< Evoral::Note<TimeType> > unmarshal_note(XMLNode *xml_note);
+               
+               boost::shared_ptr<MidiModel> _model;
+               const std::string            _name;
+               
+               typedef std::list< boost::shared_ptr< Evoral::Note<TimeType> > > NoteList;
+               
+               NoteList _added_notes;
+               NoteList _removed_notes;
+       };
 
-       void clear() { _notes.clear(); }
+       MidiModel::DeltaCommand* new_delta_command(const std::string name="midi edit");
+       void                     apply_command(Session& session, Command* cmd);
 
-       void start_write();
-       void end_write(bool delete_stuck=false);
+       bool write_to(boost::shared_ptr<MidiSource> source);
+               
+       // MidiModel doesn't use the normal AutomationList serialisation code
+       // since controller data is stored in the .mid
+       XMLNode& get_state();
+       int set_state(const XMLNode&) { return 0; }
 
-       /** Resizes vector if necessary (NOT realtime safe) */
-       void append(const MidiBuffer& data);
-       
-       /** Resizes vector if necessary (NOT realtime safe) */
-       void append(double time, size_t size, Byte* in_buffer);
+       sigc::signal<void> ContentsChanged;
        
-       inline const Note& note_at(unsigned i) const { return _notes[i]; }
-
-       inline size_t n_notes() const { return _notes.size(); }
-
-       typedef std::vector<Note> Notes;
+       const MidiSource* midi_source() const { return _midi_source; }
+       void set_midi_source(MidiSource* source) { _midi_source = source; } 
        
-       struct NoteTimeComparator {
-               inline bool operator() (const Note& a, const Note& b) const { 
-                       return a.start < b.start;
-               }
-       };
-
-       inline       Notes& notes()       { return _notes; }
-       inline const Notes& notes() const { return _notes; }
-
 private:
-
-       void append_note_on(double time, uint8_t note, uint8_t velocity);
-       void append_note_off(double time, uint8_t note);
-
-       Notes _notes;
-       Notes _write_notes;
+       friend class DeltaCommand;
+       
+       // We cannot use a boost::shared_ptr here to avoid a retain cycle
+       MidiSource* _midi_source;
 };
 
 } /* namespace ARDOUR */