Sync with http://svn.drobilla.net/lad/trunk/evoral r1891.
[ardour.git] / libs / evoral / evoral / MIDIEvent.hpp
1 /* This file is part of Evoral.
2  * Copyright (C) 2008 Dave 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 "evoral/Event.hpp"
23 #include "evoral/midi_events.h"
24 #ifdef EVORAL_MIDI_XML
25         #include <pbd/xml++.h>
26 #endif
27
28 namespace Evoral {
29
30 /** MIDI helper functions for an Event.
31  *
32  * This class contains no data, an event can be cast to a MIDIEvent
33  * but the application must make sure the event actually contains
34  * valid MIDI data for these functions to make sense.
35  */
36 struct MIDIEvent : public Event {
37         MIDIEvent(EventType type=0, EventTime t=0, uint32_t s=0, uint8_t* b=NULL, bool alloc=false)
38                 : Event(type, t, s, b, alloc)
39         {}
40         
41         MIDIEvent(const Event& copy, bool alloc)
42                 : Event(copy, alloc)
43         {}
44
45 #ifdef EVORAL_MIDI_XML
46         /** Event from XML ala http://www.midi.org/dtds/MIDIEvents10.dtd
47          */
48         MIDIEvent(const XMLNode& event);
49         
50         /** Event to XML ala http://www.midi.org/dtds/MIDIEvents10.dtd
51          */
52         boost::shared_ptr<XMLNode> to_xml() const;
53 #endif
54
55         inline uint8_t  type()                  const { return (_buffer[0] & 0xF0); }
56         inline void     set_type(uint8_t type)        { _buffer[0] =   (0x0F & _buffer[0])
57                                                                      | (0xF0 & type); }
58         inline uint8_t  channel()               const { return (_buffer[0] & 0x0F); }
59         inline void     set_channel(uint8_t channel)  { _buffer[0] =   (0xF0 & _buffer[0])
60                                                                      | (0x0F & channel); }
61         inline bool     is_note_on()            const { return (type() == MIDI_CMD_NOTE_ON); }
62         inline bool     is_note_off()           const { return (type() == MIDI_CMD_NOTE_OFF); }
63         inline bool     is_cc()                 const { return (type() == MIDI_CMD_CONTROL); }
64         inline bool     is_pitch_bender()       const { return (type() == MIDI_CMD_BENDER); }
65         inline bool     is_pgm_change()         const { return (type() == MIDI_CMD_PGM_CHANGE); }
66         inline bool     is_note()               const { return (is_note_on() || is_note_off()); }
67         inline bool     is_aftertouch()         const { return (type() == MIDI_CMD_NOTE_PRESSURE); }
68         inline bool     is_channel_pressure()   const { return (type() == MIDI_CMD_CHANNEL_PRESSURE); }
69         inline uint8_t  note()                  const { return (_buffer[1]); }
70         inline uint8_t  velocity()              const { return (_buffer[2]); }
71         inline uint8_t  cc_number()             const { return (_buffer[1]); }
72         inline void     set_cc_number(uint8_t number) { _buffer[1] = number; }
73         inline uint8_t  cc_value()              const { return (_buffer[2]); }
74         inline void     set_cc_value(uint8_t value)   { _buffer[2] = value; }
75         inline uint8_t  pitch_bender_lsb()      const { return (_buffer[1]); }
76         inline uint8_t  pitch_bender_msb()      const { return (_buffer[2]); }
77         inline uint16_t pitch_bender_value()    const { return ( ((0x7F & _buffer[2]) << 7)
78                                                                 | (0x7F & _buffer[1]) ); }
79         inline uint8_t  pgm_number()            const { return (_buffer[1]); }
80         inline void     set_pgm_number(uint8_t number){ _buffer[1] = number; }
81         inline uint8_t  aftertouch()            const { return (_buffer[1]); }
82         inline uint8_t  channel_pressure()      const { return (_buffer[1]); }
83         inline bool     is_channel_event()      const { return (0x80 <= type()) && (type() <= 0xE0);    }
84         inline bool     is_smf_meta_event()     const { return _buffer[0] == 0xFF; }
85         inline bool     is_sysex()              const { return    _buffer[0] == 0xF0
86                                                                   || _buffer[0] == 0xF7; }
87 };
88
89 } // namespace Evoral
90
91 #endif // EVORAL_MIDI_EVENT_HPP