MidiEvent.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*
  2. * MidiEvent.h - MidiEvent class
  3. *
  4. * Copyright (c) 2005-2014 Tobias Doerffel <tobydox/at/users.sourceforge.net>
  5. *
  6. * This file is part of LMMS - https://lmms.io
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2 of the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public
  19. * License along with this program (see COPYING); if not, write to the
  20. * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  21. * Boston, MA 02110-1301 USA.
  22. *
  23. */
  24. #ifndef MIDI_EVENT_H
  25. #define MIDI_EVENT_H
  26. #include <cstdlib>
  27. #include "Midi.h"
  28. #include "panning_constants.h"
  29. #include "volume.h"
  30. class MidiEvent
  31. {
  32. public:
  33. MidiEvent( MidiEventTypes type = MidiActiveSensing,
  34. int8_t channel = 0,
  35. int16_t param1 = 0,
  36. int16_t param2 = 0,
  37. const void* sourcePort = NULL ) :
  38. m_type( type ),
  39. m_metaEvent( MidiMetaInvalid ),
  40. m_channel( channel ),
  41. m_sysExData( NULL ),
  42. m_sourcePort( sourcePort )
  43. {
  44. m_data.m_param[0] = param1;
  45. m_data.m_param[1] = param2;
  46. }
  47. MidiEvent( MidiEventTypes type, const char* sysExData, int dataLen ) :
  48. m_type( type ),
  49. m_metaEvent( MidiMetaInvalid ),
  50. m_channel( 0 ),
  51. m_sysExData( sysExData ),
  52. m_sourcePort( NULL )
  53. {
  54. m_data.m_sysExDataLen = dataLen;
  55. }
  56. MidiEvent( const MidiEvent& other ) :
  57. m_type( other.m_type ),
  58. m_metaEvent( other.m_metaEvent ),
  59. m_channel( other.m_channel ),
  60. m_data( other.m_data ),
  61. m_sysExData( other.m_sysExData ),
  62. m_sourcePort( other.m_sourcePort )
  63. {
  64. }
  65. MidiEventTypes type() const
  66. {
  67. return m_type;
  68. }
  69. void setType( MidiEventTypes type )
  70. {
  71. m_type = type;
  72. }
  73. void setMetaEvent( MidiMetaEventType metaEvent )
  74. {
  75. m_metaEvent = metaEvent;
  76. }
  77. MidiMetaEventType metaEvent() const
  78. {
  79. return m_metaEvent;
  80. }
  81. int8_t channel() const
  82. {
  83. return m_channel;
  84. }
  85. void setChannel( int8_t channel )
  86. {
  87. m_channel = channel;
  88. }
  89. int16_t param( int i ) const
  90. {
  91. return m_data.m_param[i];
  92. }
  93. void setParam( int i, uint16_t value )
  94. {
  95. m_data.m_param[i] = value;
  96. }
  97. int16_t key() const
  98. {
  99. return param( 0 );
  100. }
  101. void setKey( int16_t key )
  102. {
  103. m_data.m_param[0] = key;
  104. }
  105. uint8_t velocity() const
  106. {
  107. return m_data.m_param[1] & 0x7F;
  108. }
  109. void setVelocity( int16_t velocity )
  110. {
  111. m_data.m_param[1] = velocity;
  112. }
  113. panning_t panning() const
  114. {
  115. return (panning_t) ( PanningLeft +
  116. ( (float)( midiPanning() - MidiMinPanning ) ) /
  117. ( (float)( MidiMaxPanning - MidiMinPanning ) ) *
  118. ( (float)( PanningRight - PanningLeft ) ) );
  119. }
  120. int16_t midiPanning() const
  121. {
  122. return m_data.m_param[1];
  123. }
  124. volume_t volume( int midiBaseVelocity ) const
  125. {
  126. return (volume_t)( velocity() * DefaultVolume / midiBaseVelocity );
  127. }
  128. const void* sourcePort() const
  129. {
  130. return m_sourcePort;
  131. }
  132. uint8_t controllerNumber() const
  133. {
  134. return param( 0 ) & 0x7F;
  135. }
  136. void setControllerNumber( uint8_t num )
  137. {
  138. setParam( 0, num );
  139. }
  140. uint8_t controllerValue() const
  141. {
  142. return param( 1 );
  143. }
  144. void setControllerValue( uint8_t value )
  145. {
  146. setParam( 1, value );
  147. }
  148. uint8_t program() const
  149. {
  150. return param( 0 );
  151. }
  152. uint8_t channelPressure() const
  153. {
  154. return param( 0 );
  155. }
  156. int16_t pitchBend() const
  157. {
  158. return param( 0 );
  159. }
  160. void setPitchBend( uint16_t pitchBend )
  161. {
  162. setParam( 0, pitchBend );
  163. }
  164. private:
  165. MidiEventTypes m_type; // MIDI event type
  166. MidiMetaEventType m_metaEvent; // Meta event (mostly unused)
  167. int8_t m_channel; // MIDI channel
  168. union
  169. {
  170. int16_t m_param[2]; // first/second parameter (key/velocity)
  171. uint8_t m_bytes[4]; // raw bytes
  172. int32_t m_sysExDataLen; // len of m_sysExData
  173. } m_data;
  174. const char* m_sysExData;
  175. const void* m_sourcePort;
  176. } ;
  177. #endif