ardour
MIDIEvent.hpp
Go to the documentation of this file.
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 #ifdef EVORAL_MIDI_XML
30 class XMLNode;
31 #endif
32 
33 namespace Evoral {
34 
41 template<typename Time>
42 class /*LIBEVORAL_API*/ MIDIEvent : public Event<Time> {
43 public:
44  MIDIEvent(EventType type=0, Time time=0, uint32_t size=0, uint8_t* buf=NULL, bool alloc=false)
45  : Event<Time>(type, time, size, buf, alloc)
46  {}
47 
48  MIDIEvent(const Event<Time>& copy, bool alloc)
49  : Event<Time>(copy, alloc)
50  {}
51 
52 #ifdef EVORAL_MIDI_XML
53 
54  MIDIEvent(const XMLNode& event);
55 
57  boost::shared_ptr<XMLNode> to_xml() const;
58 #endif
59 
60  inline uint8_t type() const { return (this->_buf[0] & 0xF0); }
61  inline void set_type(uint8_t type) { this->_buf[0] = (0x0F & this->_buf[0])
62  | (0xF0 & type); }
63 
64  inline uint8_t channel() const { return (this->_buf[0] & 0x0F); }
65  inline void set_channel(uint8_t channel) { this->_buf[0] = (0xF0 & this->_buf[0])
66  | (0x0F & channel); }
67 
68  inline bool is_note_on() const { return (type() == MIDI_CMD_NOTE_ON); }
69  inline bool is_note_off() const { return (type() == MIDI_CMD_NOTE_OFF); }
70  inline bool is_cc() const { return (type() == MIDI_CMD_CONTROL); }
71  inline bool is_pitch_bender() const { return (type() == MIDI_CMD_BENDER); }
72  inline bool is_pgm_change() const { return (type() == MIDI_CMD_PGM_CHANGE); }
73  inline bool is_note() const { return (is_note_on() || is_note_off()); }
74  inline bool is_aftertouch() const { return (type() == MIDI_CMD_NOTE_PRESSURE); }
75  inline bool is_channel_pressure() const { return (type() == MIDI_CMD_CHANNEL_PRESSURE); }
76  inline uint8_t note() const { return (this->_buf[1]); }
77  inline void set_note(uint8_t n) { this->_buf[1] = n; }
78  inline uint8_t velocity() const { return (this->_buf[2]); }
79  inline void set_velocity(uint8_t value) { this->_buf[2] = value; }
80  inline void scale_velocity(float factor) {
81  if (factor < 0) factor = 0;
82  this->_buf[2] = (uint8_t) lrintf (this->_buf[2]*factor);
83  if (this->_buf[2] > 127) this->_buf[2] = 127;
84  }
85  inline uint8_t cc_number() const { return (this->_buf[1]); }
86  inline void set_cc_number(uint8_t number) { this->_buf[1] = number; }
87  inline uint8_t cc_value() const { return (this->_buf[2]); }
88  inline void set_cc_value(uint8_t value) { this->_buf[2] = value; }
89  inline uint8_t pitch_bender_lsb() const { return (this->_buf[1]); }
90  inline uint8_t pitch_bender_msb() const { return (this->_buf[2]); }
91  inline uint16_t pitch_bender_value() const { return ( ((0x7F & this->_buf[2]) << 7)
92  | (0x7F & this->_buf[1]) ); }
93  inline uint8_t pgm_number() const { return (this->_buf[1]); }
94  inline void set_pgm_number(uint8_t number) { this->_buf[1] = number; }
95  inline uint8_t aftertouch() const { return (this->_buf[1]); }
96  inline uint8_t channel_pressure() const { return (this->_buf[1]); }
97  inline bool is_channel_event() const { return (0x80 <= type()) && (type() <= 0xE0); }
98  inline bool is_smf_meta_event() const { return this->_buf[0] == 0xFF; }
99  inline bool is_sysex() const { return this->_buf[0] == 0xF0
100  || this->_buf[0] == 0xF7; }
101  inline bool is_spp() const { return this->_buf[0] == 0xF2 && this->size() == 1; }
102  inline bool is_mtc_quarter() const { return this->_buf[0] == 0xF1 && this->size() == 1; }
103  inline bool is_mtc_full() const {
104  return this->size() == 10 && this->_buf[0] == 0xf0 && this->_buf[1] == 0x7f &&
105  this->_buf[3] == 0x01 && this->_buf[4] == 0x01;
106  }
107 
108  inline uint16_t value() const {
109  switch (type()) {
110  case MIDI_CMD_CONTROL:
111  return cc_value();
112  case MIDI_CMD_BENDER:
113  return pitch_bender_value();
115  return aftertouch();
117  return channel_pressure();
118  default:
119  return 0;
120  }
121  }
122 };
123 
124 } // namespace Evoral
125 
126 #endif // EVORAL_MIDI_EVENT_HPP
bool is_pgm_change() const
Definition: MIDIEvent.hpp:72
bool is_spp() const
Definition: MIDIEvent.hpp:101
#define MIDI_CMD_NOTE_OFF
Definition: midi_events.h:107
#define MIDI_CMD_NOTE_ON
Definition: midi_events.h:108
uint8_t velocity() const
Definition: MIDIEvent.hpp:78
bool is_note_off() const
Definition: MIDIEvent.hpp:69
uint8_t channel_pressure() const
Definition: MIDIEvent.hpp:96
uint8_t channel() const
Definition: MIDIEvent.hpp:64
uint8_t note() const
Definition: MIDIEvent.hpp:76
void set_type(uint8_t type)
Definition: MIDIEvent.hpp:61
bool is_smf_meta_event() const
Definition: MIDIEvent.hpp:98
#define MIDI_CMD_CHANNEL_PRESSURE
Definition: midi_events.h:112
bool is_note_on() const
Definition: MIDIEvent.hpp:68
uint8_t pitch_bender_lsb() const
Definition: MIDIEvent.hpp:89
bool is_channel_event() const
Definition: MIDIEvent.hpp:97
#define MIDI_CMD_PGM_CHANGE
Definition: midi_events.h:111
bool is_pitch_bender() const
Definition: MIDIEvent.hpp:71
uint16_t pitch_bender_value() const
Definition: MIDIEvent.hpp:91
#define MIDI_CMD_BENDER
Definition: midi_events.h:113
void set_note(uint8_t n)
Definition: MIDIEvent.hpp:77
bool is_aftertouch() const
Definition: MIDIEvent.hpp:74
void set_channel(uint8_t channel)
Definition: MIDIEvent.hpp:65
MIDIEvent(const Event< Time > &copy, bool alloc)
Definition: MIDIEvent.hpp:48
void set_pgm_number(uint8_t number)
Definition: MIDIEvent.hpp:94
#define MIDI_CMD_CONTROL
Definition: midi_events.h:110
uint8_t cc_value() const
Definition: MIDIEvent.hpp:87
uint32_t size() const
Definition: Event.hpp:134
bool is_channel_pressure() const
Definition: MIDIEvent.hpp:75
uint8_t pgm_number() const
Definition: MIDIEvent.hpp:93
bool is_mtc_quarter() const
Definition: MIDIEvent.hpp:102
Time time() const
Definition: Event.hpp:132
void scale_velocity(float factor)
Definition: MIDIEvent.hpp:80
uint8_t cc_number() const
Definition: MIDIEvent.hpp:85
uint32_t EventType
Definition: types.hpp:43
uint8_t type() const
Definition: MIDIEvent.hpp:60
bool is_sysex() const
Definition: MIDIEvent.hpp:99
bool is_cc() const
Definition: MIDIEvent.hpp:70
Definition: xml++.h:95
uint16_t value() const
Definition: MIDIEvent.hpp:108
bool is_note() const
Definition: MIDIEvent.hpp:73
uint8_t pitch_bender_msb() const
Definition: MIDIEvent.hpp:90
#define MIDI_CMD_NOTE_PRESSURE
Definition: midi_events.h:109
uint8_t * _buf
Definition: Event.hpp:151
void set_cc_number(uint8_t number)
Definition: MIDIEvent.hpp:86
uint8_t aftertouch() const
Definition: MIDIEvent.hpp:95
bool is_mtc_full() const
Definition: MIDIEvent.hpp:103
void set_cc_value(uint8_t value)
Definition: MIDIEvent.hpp:88
MIDIEvent(EventType type=0, Time time=0, uint32_t size=0, uint8_t *buf=NULL, bool alloc=false)
Definition: MIDIEvent.hpp:44
void set_velocity(uint8_t value)
Definition: MIDIEvent.hpp:79