Assists music production by grouping standalone programs into sessions. Community version of "Non Session Manager".
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

216 lines
4.9KB

  1. /*******************************************************************************/
  2. /* Copyright (C) 2008 Jonathan Moore Liles */
  3. /* */
  4. /* This program is free software; you can redistribute it and/or modify it */
  5. /* under the terms of the GNU General Public License as published by the */
  6. /* Free Software Foundation; either version 2 of the License, or (at your */
  7. /* option) any later version. */
  8. /* */
  9. /* This program is distributed in the hope that it will be useful, but WITHOUT */
  10. /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
  11. /* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for */
  12. /* more details. */
  13. /* */
  14. /* You should have received a copy of the GNU General Public License along */
  15. /* with This program; see the file COPYING. If not,write to the Free Software */
  16. /* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
  17. /*******************************************************************************/
  18. #include "midievent.H"
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <stdio.h>
  22. #include "debug.h"
  23. namespace MIDI
  24. {
  25. static const char *opcode_names[] =
  26. {
  27. "Note Off",
  28. "Note On",
  29. "Aftertouch",
  30. "Control Change",
  31. "Program Change",
  32. "Channel Pressure",
  33. "Pitch Wheel"
  34. };
  35. midievent::midievent ( void )
  36. {
  37. _sysex = NULL;
  38. _timestamp = 0;
  39. _data.status = NOTE_OFF;
  40. _data.msb = _data.lsb = 0;
  41. }
  42. midievent::~midievent ( void )
  43. {
  44. if ( _sysex )
  45. delete _sysex;
  46. _sysex = NULL;
  47. }
  48. int
  49. midievent::pitch ( void ) const
  50. {
  51. return ((_data.msb << 7) | _data.lsb) - 0x2000;
  52. }
  53. void
  54. midievent::pitch ( int n )
  55. {
  56. n += 0x2000;
  57. _data.lsb = n & 0x7F;
  58. _data.msb = (n >> 7) & 0x7F;
  59. }
  60. void
  61. midievent::data ( byte_t D1, byte_t D2 )
  62. {
  63. _data.lsb = D1 & 0x7F;
  64. _data.msb = D2 & 0x7F;
  65. }
  66. void
  67. midievent::data ( byte_t *D1, byte_t *D2 ) const
  68. {
  69. *D1 = _data.lsb;
  70. *D2 = _data.msb;
  71. }
  72. void
  73. midievent::raw ( byte_t *p, int l) const
  74. {
  75. memcpy( p, &_data, l );
  76. }
  77. int
  78. midievent::size ( void ) const
  79. {
  80. return midievent::event_size( opcode() );
  81. }
  82. void
  83. midievent::note_velocity ( int vel )
  84. {
  85. _data.msb = vel & 0x7F;
  86. }
  87. void
  88. midievent::note ( char note )
  89. {
  90. _data.lsb = note & 0x7F;
  91. }
  92. unsigned char
  93. midievent::note_velocity ( void ) const
  94. {
  95. return _data.msb;
  96. }
  97. bool
  98. midievent::is_same_note ( midievent * e ) const
  99. {
  100. return channel() == e->channel() && note() == e->note();
  101. }
  102. /** get name from opcode */
  103. const char *
  104. midievent::name ( void ) const
  105. {
  106. return opcode_names[ (opcode() >> 4) - 8 ];
  107. }
  108. /** get opcode from name */
  109. int
  110. midievent::name ( const char *name ) const
  111. {
  112. for ( unsigned int i = elementsof( opcode_names ); i--; )
  113. if ( ! strcmp( name, opcode_names[ i ] ) )
  114. return (i + 8) << 4;
  115. return -1;
  116. }
  117. /** print event in hexadecimal */
  118. void
  119. midievent::print ( void ) const
  120. {
  121. printf( "[%06f] %02X %02X %02X\n",
  122. _timestamp,
  123. _data.status,
  124. _data.lsb,
  125. _data.msb );
  126. }
  127. /** print event in english/decimal */
  128. void
  129. midievent::pretty_print ( void ) const
  130. {
  131. printf(
  132. "[%06f] %-15s c: %2d d1: %3d d2: %3d\n",
  133. _timestamp,
  134. name(),
  135. channel(),
  136. _data.lsb,
  137. _data.msb );
  138. }
  139. /*********/
  140. /* Sysex */
  141. /*********/
  142. midievent::sysex::sysex ( void )
  143. {
  144. _data = NULL;
  145. _size = 0;
  146. _alloc = 0;
  147. }
  148. midievent::sysex::~sysex ( void )
  149. {
  150. if ( _data )
  151. free( _data );
  152. _data = NULL;
  153. }
  154. /** add bytes to sysex message */
  155. void
  156. midievent::sysex::append ( byte_t *data, size_t size )
  157. {
  158. if ( _size + size > _alloc )
  159. _data = (byte_t *)realloc( _data, _alloc += 256 );
  160. memcpy( data + _size, data, size );
  161. _size += size;
  162. }
  163. /** return SysEx data */
  164. const byte_t *
  165. midievent::sysex::data ( void ) const
  166. {
  167. return _data;
  168. }
  169. long
  170. midievent::sysex::size ( void ) const
  171. {
  172. return _size;
  173. }
  174. bool
  175. midievent::operator== ( const midievent &rhs ) const
  176. {
  177. return _timestamp == rhs._timestamp &&
  178. ! bcmp( (void*)&_data, (void*)&rhs._data, size() );
  179. }
  180. }