Factor out and extend MIDIXML implementation
[ardour.git] / libs / evoral / evoral / MIDIEvent.hpp
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 #ifndef EVORAL_MIDI_EVENT_HPP
20 #define EVORAL_MIDI_EVENT_HPP
21
22 #include <cmath>
23 #include <boost/shared_ptr.hpp>
24
25 #include "evoral/visibility.h"
26 #include "evoral/Event.hpp"
27 #include "evoral/midi_events.h"
28
29 namespace Evoral {
30
31 /** MIDI helper functions for an Event.
32  *
33  * This class contains no data, an Evoral::Event can be cast to a MIDIEvent
34  * but the application must make sure the Event actually contains
35  * valid MIDI data for these functions to make sense.
36  */
37 template<typename Time>
38 class /*LIBEVORAL_API*/ MIDIEvent : public Event<Time> {
39 public:
40         MIDIEvent(EventType type=0, Time time=0, uint32_t size=0, uint8_t* buf=NULL, bool alloc=false)
41                 : Event<Time>(type, time, size, buf, alloc)
42         {}
43
44         MIDIEvent(const Event<Time>& copy, bool alloc)
45                 : Event<Time>(copy, alloc)
46         {}
47
48         inline uint8_t  type()                  const { return (this->_buf[0] & 0xF0); }
49         inline void     set_type(uint8_t type)        { this->_buf[0] =   (0x0F & this->_buf[0])
50                                                                         | (0xF0 & type); }
51
52         inline uint8_t  channel()               const { return (this->_buf[0] & 0x0F); }
53         inline void     set_channel(uint8_t channel)  { this->_buf[0] =   (0xF0 & this->_buf[0])
54                                                                         | (0x0F & channel); }
55
56         inline bool     is_note_on()            const { return (type() == MIDI_CMD_NOTE_ON); }
57         inline bool     is_note_off()           const { return (type() == MIDI_CMD_NOTE_OFF); }
58         inline bool     is_cc()                 const { return (type() == MIDI_CMD_CONTROL); }
59         inline bool     is_pitch_bender()       const { return (type() == MIDI_CMD_BENDER); }
60         inline bool     is_pgm_change()         const { return (type() == MIDI_CMD_PGM_CHANGE); }
61         inline bool     is_note()               const { return (is_note_on() || is_note_off()); }
62         inline bool     is_poly_pressure()      const { return (type() == MIDI_CMD_NOTE_PRESSURE); }
63         inline bool     is_channel_pressure()   const { return (type() == MIDI_CMD_CHANNEL_PRESSURE); }
64         inline uint8_t  note()                  const { return (this->_buf[1]); }
65         inline void     set_note(uint8_t n)           { this->_buf[1] = n; }
66         inline uint8_t  velocity()              const { return (this->_buf[2]); }
67         inline void     set_velocity(uint8_t value)   { this->_buf[2] = value; }
68         inline void     scale_velocity(float factor)  {
69                 if (factor < 0) factor = 0;
70                 this->_buf[2] = (uint8_t) lrintf (this->_buf[2]*factor);
71                 if (this->_buf[2] > 127) this->_buf[2] = 127;
72         }
73         inline uint8_t  cc_number()             const { return (this->_buf[1]); }
74         inline void     set_cc_number(uint8_t number) { this->_buf[1] = number; }
75         inline uint8_t  cc_value()              const { return (this->_buf[2]); }
76         inline void     set_cc_value(uint8_t value)   { this->_buf[2] = value;  }
77         inline uint8_t  pitch_bender_lsb()      const { return (this->_buf[1]); }
78         inline uint8_t  pitch_bender_msb()      const { return (this->_buf[2]); }
79         inline uint16_t pitch_bender_value()    const { return ( ((0x7F & this->_buf[2]) << 7)
80                                                                 | (0x7F & this->_buf[1]) ); }
81         inline uint8_t  pgm_number()              const { return (this->_buf[1]); }
82         inline void     set_pgm_number(uint8_t number)  { this->_buf[1] = number; }
83         inline uint8_t  poly_note()               const { return (this->_buf[1]); }
84         inline uint8_t  poly_pressure()           const { return (this->_buf[2]); }
85         inline uint8_t  channel_pressure()        const { return (this->_buf[1]); }
86         inline bool     is_channel_event()        const { return (0x80 <= type()) && (type() <= 0xE0); }
87         inline bool     is_smf_meta_event()       const { return this->_buf[0] == 0xFF; }
88         inline bool     is_sysex()                const { return this->_buf[0] == 0xF0
89                                                               || this->_buf[0] == 0xF7; }
90         inline bool     is_spp()                  const { return this->_buf[0] == 0xF2 && this->size() == 1; }
91         inline bool     is_mtc_quarter()          const { return this->_buf[0] == 0xF1 && this->size() == 1; }
92         inline bool     is_mtc_full()             const {
93                 return this->size() == 10    && this->_buf[0] == 0xf0 && this->_buf[1] == 0x7f &&
94                        this->_buf[3] == 0x01 && this->_buf[4] == 0x01;
95         }
96
97         inline uint16_t value() const {
98                 switch (type()) {
99                 case MIDI_CMD_CONTROL:
100                         return cc_value();
101                 case MIDI_CMD_BENDER:
102                         return pitch_bender_value();
103                 case MIDI_CMD_NOTE_PRESSURE:
104                         return poly_pressure();
105                 case MIDI_CMD_CHANNEL_PRESSURE:
106                         return channel_pressure();
107                 default:
108                         return 0;
109                 }
110         }
111 };
112
113 } // namespace Evoral
114
115 #endif // EVORAL_MIDI_EVENT_HPP