*** NEW CODING POLICY ***
[ardour.git] / libs / ardour / ardour / midi_model.h
1 /*
2     Copyright (C) 2007 Paul Davis
3     Author: Dave Robillard
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
19 */
20
21 #ifndef __ardour_midi_model_h__ 
22 #define __ardour_midi_model_h__
23
24 #include <queue>
25 #include <deque>
26 #include <utility>
27 #include <boost/utility.hpp>
28 #include <glibmm/thread.h>
29 #include "pbd/command.h"
30 #include "ardour/types.h"
31 #include "ardour/midi_buffer.h"
32 #include "ardour/midi_ring_buffer.h"
33 #include "ardour/automatable_sequence.h"
34 #include "ardour/types.h"
35 #include "evoral/Note.hpp"
36 #include "evoral/Sequence.hpp"
37
38 namespace ARDOUR {
39
40 class Session;
41 class MidiSource;
42
43 /** This is a higher level (than MidiBuffer) model of MIDI data, with separate
44  * representations for notes (instead of just unassociated note on/off events)
45  * and controller data.  Controller data is represented as part of the
46  * Automatable base (i.e. in a map of AutomationList, keyed by Parameter).
47  * Because of this MIDI controllers and automatable controllers/widgets/etc
48  * are easily interchangeable.
49  */
50 class MidiModel : public AutomatableSequence<double> {
51 public:
52         typedef double TimeType;
53
54         MidiModel(MidiSource* s, size_t size=0);
55         
56         NoteMode note_mode() const { return (percussive() ? Percussive : Sustained); }
57         void set_note_mode(NoteMode mode) { set_percussive(mode == Percussive); };
58
59         /** Add/Remove notes.
60          * Technically all note operations can be implemented as one of these, but
61          * a custom command can be more efficient.
62          */
63         class DeltaCommand : public Command {
64         public:
65                 DeltaCommand (boost::shared_ptr<MidiModel> m, const std::string& name);
66                 DeltaCommand (boost::shared_ptr<MidiModel> m, const XMLNode& node);
67
68                 const std::string& name() const { return _name; }
69                 
70                 void operator()();
71                 void undo();
72                 
73                 int set_state (const XMLNode&);
74                 XMLNode& get_state ();
75
76                 void add(const boost::shared_ptr< Evoral::Note<TimeType> > note);
77                 void remove(const boost::shared_ptr< Evoral::Note<TimeType> > note);
78
79         private:
80                 XMLNode &marshal_note(const boost::shared_ptr< Evoral::Note<TimeType> > note);
81                 boost::shared_ptr< Evoral::Note<TimeType> > unmarshal_note(XMLNode *xml_note);
82                 
83                 boost::shared_ptr<MidiModel> _model;
84                 const std::string            _name;
85                 
86                 typedef std::list< boost::shared_ptr< Evoral::Note<TimeType> > > NoteList;
87                 
88                 NoteList _added_notes;
89                 NoteList _removed_notes;
90         };
91
92         MidiModel::DeltaCommand* new_delta_command(const std::string name="midi edit");
93         void                     apply_command(Session& session, Command* cmd);
94
95         bool write_to(boost::shared_ptr<MidiSource> source);
96                 
97         // MidiModel doesn't use the normal AutomationList serialisation code
98         // since controller data is stored in the .mid
99         XMLNode& get_state();
100         int set_state(const XMLNode&) { return 0; }
101
102         sigc::signal<void> ContentsChanged;
103         
104         const MidiSource* midi_source() const { return _midi_source; }
105         void set_midi_source(MidiSource* source) { _midi_source = source; } 
106         
107 private:
108         friend class DeltaCommand;
109         
110         // We cannot use a boost::shared_ptr here to avoid a retain cycle
111         MidiSource* _midi_source;
112 };
113
114 } /* namespace ARDOUR */
115
116 #endif /* __ardour_midi_model_h__ */
117