Note.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*
  2. * Note.h - declaration of class note which contains all informations about a
  3. * note + definitions of several constants and enums
  4. *
  5. * Copyright (c) 2004-2014 Tobias Doerffel <tobydox/at/users.sourceforge.net>
  6. *
  7. * This file is part of LMMS - https://lmms.io
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 2 of the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public
  20. * License along with this program (see COPYING); if not, write to the
  21. * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  22. * Boston, MA 02110-1301 USA.
  23. *
  24. */
  25. #ifndef NOTE_H
  26. #define NOTE_H
  27. #include <QtCore/QVector>
  28. #include "volume.h"
  29. #include "panning.h"
  30. #include "SerializingObject.h"
  31. #include "TimePos.h"
  32. class DetuningHelper;
  33. enum Keys
  34. {
  35. Key_C = 0,
  36. Key_CIS = 1, Key_DES = 1,
  37. Key_D = 2,
  38. Key_DIS = 3, Key_ES = 3,
  39. Key_E = 4, Key_FES = 4,
  40. Key_F = 5,
  41. Key_FIS = 6, Key_GES = 6,
  42. Key_G = 7,
  43. Key_GIS = 8, Key_AS = 8,
  44. Key_A = 9,
  45. Key_AIS = 10, Key_B = 10,
  46. Key_H = 11
  47. } ;
  48. enum Octaves
  49. {
  50. Octave_m1, // MIDI standard starts at C-1
  51. Octave_0,
  52. Octave_1,
  53. Octave_2,
  54. Octave_3,
  55. Octave_4, DefaultOctave = Octave_4,
  56. Octave_5,
  57. Octave_6,
  58. Octave_7,
  59. Octave_8,
  60. Octave_9, // incomplete octave, MIDI only goes up to G9
  61. NumOctaves
  62. };
  63. const int FirstOctave = -1;
  64. const int KeysPerOctave = 12;
  65. const int DefaultKey = DefaultOctave * KeysPerOctave + Key_A;
  66. //! Number of physical keys, limited to MIDI range (valid for both MIDI 1.0 and 2.0)
  67. const int NumKeys = 128;
  68. const int DefaultMiddleKey = Octave_4 * KeysPerOctave + Key_C;
  69. const int DefaultBaseKey = Octave_4 * KeysPerOctave + Key_A;
  70. const float DefaultBaseFreq = 440.f;
  71. const float MaxDetuning = 4 * 12.0f;
  72. class LMMS_EXPORT Note : public SerializingObject
  73. {
  74. public:
  75. Note( const TimePos & length = TimePos( 0 ),
  76. const TimePos & pos = TimePos( 0 ),
  77. int key = DefaultKey,
  78. volume_t volume = DefaultVolume,
  79. panning_t panning = DefaultPanning,
  80. DetuningHelper * detuning = nullptr );
  81. Note( const Note & note );
  82. virtual ~Note();
  83. // used by GUI
  84. inline void setSelected( const bool selected ) { m_selected = selected; }
  85. inline void setOldKey( const int oldKey ) { m_oldKey = oldKey; }
  86. inline void setOldPos( const TimePos & oldPos ) { m_oldPos = oldPos; }
  87. inline void setOldLength( const TimePos & oldLength )
  88. {
  89. m_oldLength = oldLength;
  90. }
  91. inline void setIsPlaying( const bool isPlaying )
  92. {
  93. m_isPlaying = isPlaying;
  94. }
  95. void setLength( const TimePos & length );
  96. void setPos( const TimePos & pos );
  97. void setKey( const int key );
  98. virtual void setVolume( volume_t volume );
  99. virtual void setPanning( panning_t panning );
  100. void quantizeLength( const int qGrid );
  101. void quantizePos( const int qGrid );
  102. static inline bool lessThan( const Note * lhs, const Note * rhs )
  103. {
  104. // function to compare two notes - must be called explictly when
  105. // using qSort
  106. if( (int)( *lhs ).pos() < (int)( *rhs ).pos() )
  107. {
  108. return true;
  109. }
  110. else if( (int)( *lhs ).pos() > (int)( *rhs ).pos() )
  111. {
  112. return false;
  113. }
  114. return ( (int)( *lhs ).key() > (int)( *rhs ).key() );
  115. }
  116. inline bool selected() const
  117. {
  118. return m_selected;
  119. }
  120. inline int oldKey() const
  121. {
  122. return m_oldKey;
  123. }
  124. inline TimePos oldPos() const
  125. {
  126. return m_oldPos;
  127. }
  128. inline TimePos oldLength() const
  129. {
  130. return m_oldLength;
  131. }
  132. inline bool isPlaying() const
  133. {
  134. return m_isPlaying;
  135. }
  136. inline TimePos endPos() const
  137. {
  138. const int l = length();
  139. return pos() + l;
  140. }
  141. inline const TimePos & length() const
  142. {
  143. return m_length;
  144. }
  145. inline const TimePos & pos() const
  146. {
  147. return m_pos;
  148. }
  149. inline TimePos pos( TimePos basePos ) const
  150. {
  151. const int bp = basePos;
  152. return m_pos - bp;
  153. }
  154. inline int key() const
  155. {
  156. return m_key;
  157. }
  158. inline volume_t getVolume() const
  159. {
  160. return m_volume;
  161. }
  162. int midiVelocity( int midiBaseVelocity ) const
  163. {
  164. return qMin( MidiMaxVelocity, getVolume() * midiBaseVelocity / DefaultVolume );
  165. }
  166. inline panning_t getPanning() const
  167. {
  168. return m_panning;
  169. }
  170. static QString classNodeName()
  171. {
  172. return "note";
  173. }
  174. inline QString nodeName() const override
  175. {
  176. return classNodeName();
  177. }
  178. static TimePos quantized( const TimePos & m, const int qGrid );
  179. DetuningHelper * detuning() const
  180. {
  181. return m_detuning;
  182. }
  183. bool hasDetuningInfo() const;
  184. bool withinRange(int tickStart, int tickEnd) const;
  185. void createDetuning();
  186. protected:
  187. void saveSettings( QDomDocument & doc, QDomElement & parent ) override;
  188. void loadSettings( const QDomElement & _this ) override;
  189. private:
  190. // for piano roll editing
  191. bool m_selected;
  192. int m_oldKey;
  193. TimePos m_oldPos;
  194. TimePos m_oldLength;
  195. bool m_isPlaying;
  196. int m_key;
  197. volume_t m_volume;
  198. panning_t m_panning;
  199. TimePos m_length;
  200. TimePos m_pos;
  201. DetuningHelper * m_detuning;
  202. };
  203. typedef QVector<Note *> NoteVector;
  204. #endif