MidiEvent.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. enum class Source { Internal, External };
  34. MidiEvent(MidiEventTypes type = MidiActiveSensing,
  35. int8_t channel = 0,
  36. int16_t param1 = 0,
  37. int16_t param2 = 0,
  38. const void* sourcePort = nullptr,
  39. Source source = Source::External) :
  40. m_type( type ),
  41. m_metaEvent( MidiMetaInvalid ),
  42. m_channel( channel ),
  43. m_sysExData( nullptr ),
  44. m_sourcePort(sourcePort),
  45. m_source(source)
  46. {
  47. m_data.m_param[0] = param1;
  48. m_data.m_param[1] = param2;
  49. }
  50. MidiEvent(MidiEventTypes type, const char* sysExData, std::size_t dataLen, Source source = Source::External) :
  51. m_type( type ),
  52. m_metaEvent( MidiMetaInvalid ),
  53. m_channel( 0 ),
  54. m_sysExData( sysExData ),
  55. m_sourcePort(nullptr),
  56. m_source(source)
  57. {
  58. m_data.m_sysExDataLen = dataLen;
  59. }
  60. MidiEvent( const MidiEvent& other ) :
  61. m_type( other.m_type ),
  62. m_metaEvent( other.m_metaEvent ),
  63. m_channel( other.m_channel ),
  64. m_data( other.m_data ),
  65. m_sysExData( other.m_sysExData ),
  66. m_sourcePort(other.m_sourcePort),
  67. m_source(other.m_source)
  68. {
  69. }
  70. MidiEventTypes type() const
  71. {
  72. return m_type;
  73. }
  74. void setType( MidiEventTypes type )
  75. {
  76. m_type = type;
  77. }
  78. void setMetaEvent( MidiMetaEventType metaEvent )
  79. {
  80. m_metaEvent = metaEvent;
  81. }
  82. MidiMetaEventType metaEvent() const
  83. {
  84. return m_metaEvent;
  85. }
  86. int8_t channel() const
  87. {
  88. return m_channel;
  89. }
  90. void setChannel( int8_t channel )
  91. {
  92. m_channel = channel;
  93. }
  94. int16_t param( int i ) const
  95. {
  96. return m_data.m_param[i];
  97. }
  98. void setParam( int i, uint16_t value )
  99. {
  100. m_data.m_param[i] = value;
  101. }
  102. int16_t key() const
  103. {
  104. return param( 0 );
  105. }
  106. void setKey( int16_t key )
  107. {
  108. m_data.m_param[0] = key;
  109. }
  110. uint8_t velocity() const
  111. {
  112. return m_data.m_param[1] & 0x7F;
  113. }
  114. void setVelocity( int16_t velocity )
  115. {
  116. m_data.m_param[1] = velocity;
  117. }
  118. panning_t panning() const
  119. {
  120. return (panning_t) ( PanningLeft +
  121. ( (float)( midiPanning() - MidiMinPanning ) ) /
  122. ( (float)( MidiMaxPanning - MidiMinPanning ) ) *
  123. ( (float)( PanningRight - PanningLeft ) ) );
  124. }
  125. int16_t midiPanning() const
  126. {
  127. return m_data.m_param[1];
  128. }
  129. volume_t volume( int midiBaseVelocity ) const
  130. {
  131. return (volume_t)( velocity() * DefaultVolume / midiBaseVelocity );
  132. }
  133. const void* sourcePort() const
  134. {
  135. return m_sourcePort;
  136. }
  137. uint8_t controllerNumber() const
  138. {
  139. return param( 0 ) & 0x7F;
  140. }
  141. void setControllerNumber( uint8_t num )
  142. {
  143. setParam( 0, num );
  144. }
  145. uint8_t controllerValue() const
  146. {
  147. return param( 1 );
  148. }
  149. void setControllerValue( uint8_t value )
  150. {
  151. setParam( 1, value );
  152. }
  153. uint8_t program() const
  154. {
  155. return param( 0 );
  156. }
  157. uint8_t channelPressure() const
  158. {
  159. return param( 0 );
  160. }
  161. int16_t pitchBend() const
  162. {
  163. return param( 0 );
  164. }
  165. void setPitchBend( uint16_t pitchBend )
  166. {
  167. setParam( 0, pitchBend );
  168. }
  169. Source source() const
  170. {
  171. return m_source;
  172. }
  173. void setSource(Source value)
  174. {
  175. m_source = value;
  176. }
  177. private:
  178. MidiEventTypes m_type; // MIDI event type
  179. MidiMetaEventType m_metaEvent; // Meta event (mostly unused)
  180. int8_t m_channel; // MIDI channel
  181. union
  182. {
  183. int16_t m_param[2]; // first/second parameter (key/velocity)
  184. uint8_t m_bytes[4]; // raw bytes
  185. int32_t m_sysExDataLen; // len of m_sysExData
  186. } m_data;
  187. const char* m_sysExData;
  188. const void* m_sourcePort;
  189. // Stores the source of the MidiEvent: Internal or External (hardware controllers).
  190. Source m_source;
  191. } ;
  192. #endif