Puls der Zeit
[ardour.git] / libs / evoral / evoral / MIDIXML.hpp
1 /* This file is part of Evoral.
2  * Copyright (C) 2008-2016 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 #ifndef EVORAL_MIDI_XML_HPP
20 #define EVORAL_MIDI_XML_HPP
21
22 #include "evoral/Event.hpp"
23 #include "pbd/xml++.h"
24
25 namespace Evoral {
26 namespace MIDIXML {
27
28 template<typename Time>
29 bool
30 xml_to_midi(const XMLNode& node, Evoral::Event<Time>& ev)
31 {
32         if (node.name() == "ControlChange") {
33                 ev.set_type(MIDI_CMD_CONTROL);
34                 ev.set_cc_number(atoi(node.property("Control")->value().c_str()));
35                 ev.set_cc_value(atoi(node.property("Value")->value().c_str()));
36                 return true;
37         } else if (node.name() == "ProgramChange") {
38                 ev.set_type(MIDI_CMD_PGM_CHANGE);
39                 ev.set_pgm_number(atoi(node.property("Number")->value().c_str()));
40                 return true;
41         }
42
43         return false;
44 }
45
46 template<typename Time>
47 boost::shared_ptr<XMLNode>
48 midi_to_xml(const Evoral::Event<Time>& ev)
49 {
50         XMLNode* result = 0;
51
52         switch (ev.type()) {
53         case MIDI_CMD_CONTROL:
54                 result = new XMLNode("ControlChange");
55                 result->add_property("Channel", long(ev.channel()));
56                 result->add_property("Control", long(ev.cc_number()));
57                 result->add_property("Value",   long(ev.cc_value()));
58                 break;
59
60         case MIDI_CMD_PGM_CHANGE:
61                 result = new XMLNode("ProgramChange");
62                 result->add_property("Channel", long(ev.channel()));
63                 result->add_property("Number",  long(ev.pgm_number()));
64                 break;
65
66         case MIDI_CMD_NOTE_ON:
67                 result = new XMLNode("NoteOn");
68                 result->add_property("Channel",  long(ev.channel()));
69                 result->add_property("Note",     long(ev.note()));
70                 result->add_property("Velocity", long(ev.velocity()));
71                 break;
72
73         case MIDI_CMD_NOTE_OFF:
74                 result = new XMLNode("NoteOff");
75                 result->add_property("Channel",  long(ev.channel()));
76                 result->add_property("Note",     long(ev.note()));
77                 result->add_property("Velocity", long(ev.velocity()));
78                 break;
79
80         case MIDI_CMD_BENDER:
81                 result = new XMLNode("PitchBendChange");
82                 result->add_property("Channel", long(ev.channel()));
83                 result->add_property("Value",   long(ev.pitch_bender_value()));
84                 break;
85
86         default:
87                 return boost::shared_ptr<XMLNode>();
88         }
89
90         return boost::shared_ptr<XMLNode>(result);
91 }
92
93 } // namespace MIDIXML
94 } // namespace Evoral
95
96 #endif // EVORAL_MIDI_XML_HPP