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.

222 lines
5.0KB

  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. unsigned char
  88. midievent::note ( void ) const
  89. {
  90. return _data.lsb;
  91. }
  92. void
  93. midievent::note ( char note )
  94. {
  95. _data.lsb = note & 0x7F;
  96. }
  97. unsigned char
  98. midievent::note_velocity ( void ) const
  99. {
  100. return _data.msb;
  101. }
  102. bool
  103. midievent::is_same_note ( midievent * e ) const
  104. {
  105. return channel() == e->channel() && note() == e->note();
  106. }
  107. /** get name from opcode */
  108. const char *
  109. midievent::name ( void ) const
  110. {
  111. return opcode_names[ (opcode() >> 4) - 8 ];
  112. }
  113. /** get opcode from name */
  114. int
  115. midievent::name ( const char *name ) const
  116. {
  117. for ( unsigned int i = elementsof( opcode_names ); i--; )
  118. if ( ! strcmp( name, opcode_names[ i ] ) )
  119. return (i + 8) << 4;
  120. return -1;
  121. }
  122. /** print event in hexadecimal */
  123. void
  124. midievent::print ( void ) const
  125. {
  126. printf( "[%06f] %02X %02X %02X\n",
  127. _timestamp,
  128. _data.status,
  129. _data.lsb,
  130. _data.msb );
  131. }
  132. /** print event in english/decimal */
  133. void
  134. midievent::pretty_print ( void ) const
  135. {
  136. printf(
  137. "[%06f] %-15s c: %2d d1: %3d d2: %3d\n",
  138. _timestamp,
  139. name(),
  140. channel(),
  141. _data.lsb,
  142. _data.msb );
  143. }
  144. /*********/
  145. /* Sysex */
  146. /*********/
  147. midievent::sysex::sysex ( void )
  148. {
  149. _data = NULL;
  150. _size = 0;
  151. _alloc = 0;
  152. }
  153. midievent::sysex::~sysex ( void )
  154. {
  155. if ( _data )
  156. free( _data );
  157. _data = NULL;
  158. }
  159. /** add bytes to sysex message */
  160. void
  161. midievent::sysex::append ( byte_t *data, size_t size )
  162. {
  163. if ( _size + size > _alloc )
  164. _data = (byte_t *)realloc( _data, _alloc += 256 );
  165. memcpy( data + _size, data, size );
  166. _size += size;
  167. }
  168. /** return SysEx data */
  169. const byte_t *
  170. midievent::sysex::data ( void ) const
  171. {
  172. return _data;
  173. }
  174. long
  175. midievent::sysex::size ( void ) const
  176. {
  177. return _size;
  178. }
  179. bool
  180. midievent::operator== ( const midievent &rhs ) const
  181. {
  182. return _timestamp == rhs._timestamp &&
  183. ! bcmp( (void*)&_data, (void*)&rhs._data, size() );
  184. }
  185. }