2 /*******************************************************************************/
3 /* Copyright (C) 2008 Jonathan Moore Liles */
5 /* This program is free software; you can redistribute it and/or modify it */
6 /* under the terms of the GNU General Public License as published by the */
7 /* Free Software Foundation; either version 2 of the License, or (at your */
8 /* option) any later version. */
10 /* This program is distributed in the hope that it will be useful, but WITHOUT */
11 /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
12 /* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for */
15 /* You should have received a copy of the GNU General Public License along */
16 /* with This program; see the file COPYING. If not,write to the Free Software */
17 /* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 /*******************************************************************************/
20 /* raw MIDI events + timestamps. Some support for SysEx */
23 #include "midievent.H"
25 static const char *opcode_names[] =
36 midievent::midievent ( void )
40 _data.status = NOTE_OFF;
41 _data.msb = _data.lsb = 0;
44 midievent::~midievent ( void )
53 midievent::pitch ( void ) const
55 return ((_data.msb << 7) | _data.lsb) - 0x2000;
59 midievent::pitch ( int n )
64 _data.msb = (n >> 7) & 0x7F;
68 midievent::data ( byte_t D1, byte_t D2 )
70 _data.lsb = D1 & 0x7F;
71 _data.msb = D2 & 0x7F;
75 midievent::data ( byte_t *D1, byte_t *D2 ) const
82 midievent::raw ( byte_t *p, int l) const
84 memcpy( p, &_data, l );
88 midievent::size ( void ) const
90 return midievent::event_size( opcode() );
94 midievent::note_velocity ( int vel )
96 _data.msb = vel & 0x7F;
100 midievent::note ( char note )
102 _data.lsb = note & 0x7F;
106 midievent::note_velocity ( void ) const
112 midievent::is_same_note ( midievent * e ) const
114 return channel() == e->channel() && note() == e->note();
117 /** get name from opcode */
119 midievent::name ( void ) const
121 return opcode_names[ (opcode() >> 4) - 8 ];
124 /** get opcode from name */
126 midievent::name ( const char *name ) const
128 for ( unsigned int i = elementsof( opcode_names ); i--; )
129 if ( ! strcmp( name, opcode_names[ i ] ) )
135 /** print event in hexadecimal */
137 midievent::print ( void ) const
139 printf( "[%06ld] %02X %02X %02X\n",
146 /** print event in english/decimal */
148 midievent::pretty_print ( void ) const
151 "[%06ld] %-15s c: %2d d1: %3d d2: %3d\n",
164 midievent::sysex::sysex ( void )
171 midievent::sysex::~sysex ( void )
179 /** add bytes to sysex message */
181 midievent::sysex::append ( byte_t *data, size_t size )
183 if ( _size + size > _alloc )
184 _data = (byte_t *)realloc( _data, _alloc += 256 );
186 memcpy( data + _size, data, size );
191 /** return SysEx data */
193 midievent::sysex::data ( void ) const
199 midievent::sysex::size ( void ) const
207 midievent::operator== ( const midievent &rhs ) const
209 return _timestamp == rhs._timestamp &&
210 ! bcmp( (void*)&_data, (void*)&rhs._data, size() );