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.

212 lines
4.4KB

  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. /* raw MIDI events + timestamps. Some support for SysEx */
  19. #include "common.h"
  20. #include "midievent.H"
  21. static const char *opcode_names[] =
  22. {
  23. "Note Off",
  24. "Note On",
  25. "Aftertouch",
  26. "Control Change",
  27. "Program Change",
  28. "Channel Pressure",
  29. "Pitch Wheel"
  30. };
  31. midievent::midievent ( void )
  32. {
  33. _sysex = NULL;
  34. _timestamp = 0;
  35. _data.status = NOTE_OFF;
  36. _data.msb = _data.lsb = 0;
  37. }
  38. midievent::~midievent ( void )
  39. {
  40. if ( _sysex )
  41. delete _sysex;
  42. _sysex = NULL;
  43. }
  44. int
  45. midievent::pitch ( void ) const
  46. {
  47. return ((_data.msb << 7) | _data.lsb) - 0x2000;
  48. }
  49. void
  50. midievent::pitch ( int n )
  51. {
  52. n += 0x2000;
  53. _data.lsb = n & 0x7F;
  54. _data.msb = (n >> 7) & 0x7F;
  55. }
  56. void
  57. midievent::data ( byte_t D1, byte_t D2 )
  58. {
  59. _data.lsb = D1 & 0x7F;
  60. _data.msb = D2 & 0x7F;
  61. }
  62. void
  63. midievent::data ( byte_t *D1, byte_t *D2 ) const
  64. {
  65. *D1 = _data.lsb;
  66. *D2 = _data.msb;
  67. }
  68. void
  69. midievent::raw ( byte_t *p, int l) const
  70. {
  71. memcpy( p, &_data, l );
  72. }
  73. int
  74. midievent::size ( void ) const
  75. {
  76. return midievent::event_size( opcode() );
  77. }
  78. void
  79. midievent::note_velocity ( int vel )
  80. {
  81. _data.msb = vel & 0x7F;
  82. }
  83. void
  84. midievent::note ( char note )
  85. {
  86. _data.lsb = note & 0x7F;
  87. }
  88. unsigned char
  89. midievent::note_velocity ( void ) const
  90. {
  91. return _data.msb;
  92. }
  93. bool
  94. midievent::is_same_note ( midievent * e ) const
  95. {
  96. return channel() == e->channel() && note() == e->note();
  97. }
  98. /** get name from opcode */
  99. const char *
  100. midievent::name ( void ) const
  101. {
  102. return opcode_names[ (opcode() >> 4) - 8 ];
  103. }
  104. /** get opcode from name */
  105. int
  106. midievent::name ( const char *name ) const
  107. {
  108. for ( unsigned int i = elementsof( opcode_names ); i--; )
  109. if ( ! strcmp( name, opcode_names[ i ] ) )
  110. return (i + 8) << 4;
  111. return -1;
  112. }
  113. /** print event in hexadecimal */
  114. void
  115. midievent::print ( void ) const
  116. {
  117. printf( "[%06ld] %02X %02X %02X\n",
  118. _timestamp,
  119. _data.status,
  120. _data.lsb,
  121. _data.msb );
  122. }
  123. /** print event in english/decimal */
  124. void
  125. midievent::pretty_print ( void ) const
  126. {
  127. printf(
  128. "[%06ld] %-15s c: %2d d1: %3d d2: %3d\n",
  129. _timestamp,
  130. name(),
  131. channel(),
  132. _data.lsb,
  133. _data.msb );
  134. }
  135. /*********/
  136. /* Sysex */
  137. /*********/
  138. midievent::sysex::sysex ( void )
  139. {
  140. _data = NULL;
  141. _size = 0;
  142. _alloc = 0;
  143. }
  144. midievent::sysex::~sysex ( void )
  145. {
  146. if ( _data )
  147. free( _data );
  148. _data = NULL;
  149. }
  150. /** add bytes to sysex message */
  151. void
  152. midievent::sysex::append ( byte_t *data, size_t size )
  153. {
  154. if ( _size + size > _alloc )
  155. _data = (byte_t *)realloc( _data, _alloc += 256 );
  156. memcpy( data + _size, data, size );
  157. _size += size;
  158. }
  159. /** return SysEx data */
  160. const byte_t *
  161. midievent::sysex::data ( void ) const
  162. {
  163. return _data;
  164. }
  165. long
  166. midievent::sysex::size ( void ) const
  167. {
  168. return _size;
  169. }
  170. bool
  171. midievent::operator== ( const midievent &rhs ) const
  172. {
  173. return _timestamp == rhs._timestamp &&
  174. ! bcmp( (void*)&_data, (void*)&rhs._data, size() );
  175. }