Update NTK.
[nondaw.git] / sequencer / src / midievent.C
blob3ce2362b102375f5226e8568ca5f1c63859c4635
2 /*******************************************************************************/
3 /* Copyright (C) 2008 Jonathan Moore Liles                                     */
4 /*                                                                             */
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.                                                  */
9 /*                                                                             */
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   */
13 /* more details.                                                               */
14 /*                                                                             */
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 */
22 #include "common.h"
23 #include "midievent.H"
25 static const char *opcode_names[] =
27     "Note Off",
28     "Note On",
29     "Aftertouch",
30     "Control Change",
31     "Program Change",
32     "Channel Pressure",
33     "Pitch Wheel"
36 midievent::midievent ( void )
38     _sysex = NULL;
39     _timestamp = 0;
40     _data.status = NOTE_OFF;
41     _data.msb =  _data.lsb = 0;
44 midievent::~midievent ( void )
46     if ( _sysex )
47         delete _sysex;
49     _sysex = NULL;
52 int
53 midievent::pitch ( void ) const
55     return ((_data.msb << 7) | _data.lsb) - 0x2000;
58 void
59 midievent::pitch ( int n )
61     n += 0x2000;
63     _data.lsb = n & 0x7F;
64     _data.msb = (n >> 7) & 0x7F;
67 void
68 midievent::data ( byte_t D1, byte_t D2 )
70     _data.lsb = D1 & 0x7F;
71     _data.msb = D2 & 0x7F;
74 void
75 midievent::data ( byte_t *D1, byte_t *D2 ) const
77     *D1 = _data.lsb;
78     *D2 = _data.msb;
81 void
82 midievent::raw ( byte_t *p, int l) const
84     memcpy( p, &_data, l );
87 int
88 midievent::size ( void ) const
90     return midievent::event_size( opcode() );
93 void
94 midievent::note_velocity ( int vel )
96     _data.msb = vel & 0x7F;
99 void
100 midievent::note ( char note )
102     _data.lsb = note & 0x7F;
105 unsigned char
106 midievent::note_velocity ( void ) const
108     return _data.msb;
111 bool
112 midievent::is_same_note ( midievent * e ) const
114     return channel() == e->channel() && note() == e->note();
117 /** get name from opcode */
118 const char *
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 ] ) )
130             return (i + 8) << 4;
132     return -1;
135 /** print event in hexadecimal */
136 void
137 midievent::print ( void ) const
139     printf( "[%06ld] %02X %02X %02X\n",
140             _timestamp,
141             _data.status,
142             _data.lsb,
143             _data.msb );
146 /** print event in english/decimal */
147 void
148 midievent::pretty_print ( void ) const
150     printf(
151         "[%06ld] %-15s c: %2d d1: %3d d2: %3d\n",
152         _timestamp,
153         name(),
154         channel(),
155         _data.lsb,
156         _data.msb );
160 /*********/
161 /* Sysex */
162 /*********/
164 midievent::sysex::sysex ( void )
166     _data = NULL;
167     _size = 0;
168     _alloc = 0;
171 midievent::sysex::~sysex ( void )
173     if ( _data )
174         free( _data );
176     _data = NULL;
179 /** add bytes to sysex message  */
180 void
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 );
188     _size += size;
191 /** return SysEx data */
192 const byte_t *
193 midievent::sysex::data ( void ) const
195     return _data;
198 long
199 midievent::sysex::size ( void ) const
201     return _size;
206 bool
207 midievent::operator== ( const midievent &rhs ) const
209     return _timestamp == rhs._timestamp &&
210         ! bcmp( (void*)&_data, (void*)&rhs._data, size() );