add midi-bypass to re-configurable-i/o instruments
[ardour.git] / libs / evoral / src / MIDIEvent.cpp
1 /* This file is part of Evoral.
2  * Copyright (C) 2008 David Robillard <http://drobilla.net>
3  * Copyright (C) 2000-2008 Paul Davis
4  *
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
8  * version.
9  *
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.
13  *
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
17  */
18
19 #include <string>
20 #include "evoral/Beats.hpp"
21 #include "evoral/MIDIEvent.hpp"
22 #ifdef EVORAL_MIDI_XML
23         #include "pbd/xml++.h"
24 #endif
25
26 using namespace std;
27
28 namespace Evoral {
29
30 #ifdef EVORAL_MIDI_XML
31
32 template<typename Time>
33 MIDIEvent<Time>::MIDIEvent(const XMLNode& event)
34   : Event<Time>()
35 {
36         string name = event.name();
37
38         if (name == "ControlChange") {
39                 this->_buf = (uint8_t*) ::malloc(3);
40                 this->_owns_buf = true;
41                 set_type(MIDI_CMD_CONTROL);
42
43                 set_cc_number(atoi(event.property("Control")->value().c_str()));
44                 set_cc_value (atoi(event.property("Value")->value().c_str()));
45         } else if (name == "ProgramChange") {
46                 this->_buf = (uint8_t*) ::malloc(2);
47                 this->_owns_buf = true;
48                 set_type(MIDI_CMD_PGM_CHANGE);
49
50                 set_pgm_number(atoi(event.property("Number")->value().c_str()));
51         }
52 }
53
54
55 template<typename Time>
56 boost::shared_ptr<XMLNode>
57 MIDIEvent<Time>::to_xml() const
58 {
59         XMLNode *result = 0;
60
61         switch (type()) {
62         case MIDI_CMD_CONTROL:
63                 result = new XMLNode("ControlChange");
64                 result->add_property("Channel", long(channel()));
65                 result->add_property("Control", long(cc_number()));
66                 result->add_property("Value",   long(cc_value()));
67                 break;
68
69         case MIDI_CMD_PGM_CHANGE:
70                 result = new XMLNode("ProgramChange");
71                 result->add_property("Channel", long(channel()));
72                 result->add_property("Number",  long(pgm_number()));
73                 break;
74
75         case MIDI_CMD_NOTE_ON:
76                 result = new XMLNode("NoteOn");
77                 result->add_property("Channel", long(channel()));
78                 result->add_property("Note",  long(note()));
79                 result->add_property("Velocity",  long(velocity()));
80                 break;
81
82         case MIDI_CMD_NOTE_OFF:
83                 result = new XMLNode("NoteOff");
84                 result->add_property("Channel", long(channel()));
85                 result->add_property("Note",  long(note()));
86                 result->add_property("Velocity",  long(velocity()));
87                 break;
88
89         case MIDI_CMD_BENDER:
90                 result = new XMLNode("PitchBendChange");
91                 result->add_property("Channel", long(channel()));
92                 result->add_property("Value",  long(pitch_bender_value()));
93                 break;
94
95         default:
96                 // The implementation is continued as needed
97                 result = new XMLNode("NotImplemented");
98                 break;
99         }
100
101         return boost::shared_ptr<XMLNode>(result);
102 }
103
104 #endif // EVORAL_MIDI_XML
105
106 template class MIDIEvent<Evoral::Beats>;
107
108 } // namespace Evoral
109