Note.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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 "MidiTime.h"
  31. #include "SerializingObject.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_0,
  51. Octave_1,
  52. Octave_2,
  53. Octave_3,
  54. Octave_4, DefaultOctave = Octave_4,
  55. Octave_5,
  56. Octave_6,
  57. Octave_7,
  58. Octave_8,
  59. NumOctaves
  60. } ;
  61. const int WhiteKeysPerOctave = 7;
  62. const int BlackKeysPerOctave = 5;
  63. const int KeysPerOctave = WhiteKeysPerOctave + BlackKeysPerOctave;
  64. const int NumKeys = NumOctaves * KeysPerOctave;
  65. const int DefaultKey = DefaultOctave*KeysPerOctave + Key_A;
  66. const float MaxDetuning = 4 * 12.0f;
  67. class LMMS_EXPORT Note : public SerializingObject
  68. {
  69. public:
  70. Note( const MidiTime & length = MidiTime( 0 ),
  71. const MidiTime & pos = MidiTime( 0 ),
  72. int key = DefaultKey,
  73. volume_t volume = DefaultVolume,
  74. panning_t panning = DefaultPanning,
  75. DetuningHelper * detuning = NULL );
  76. Note( const Note & note );
  77. virtual ~Note();
  78. // used by GUI
  79. inline void setSelected( const bool selected ) { m_selected = selected; }
  80. inline void setOldKey( const int oldKey ) { m_oldKey = oldKey; }
  81. inline void setOldPos( const MidiTime & oldPos ) { m_oldPos = oldPos; }
  82. inline void setOldLength( const MidiTime & oldLength )
  83. {
  84. m_oldLength = oldLength;
  85. }
  86. inline void setIsPlaying( const bool isPlaying )
  87. {
  88. m_isPlaying = isPlaying;
  89. }
  90. void setLength( const MidiTime & length );
  91. void setPos( const MidiTime & pos );
  92. void setKey( const int key );
  93. virtual void setVolume( volume_t volume );
  94. virtual void setPanning( panning_t panning );
  95. void quantizeLength( const int qGrid );
  96. void quantizePos( const int qGrid );
  97. static inline bool lessThan( const Note * lhs, const Note * rhs )
  98. {
  99. // function to compare two notes - must be called explictly when
  100. // using qSort
  101. if( (int)( *lhs ).pos() < (int)( *rhs ).pos() )
  102. {
  103. return true;
  104. }
  105. else if( (int)( *lhs ).pos() > (int)( *rhs ).pos() )
  106. {
  107. return false;
  108. }
  109. return ( (int)( *lhs ).key() > (int)( *rhs ).key() );
  110. }
  111. inline bool selected() const
  112. {
  113. return m_selected;
  114. }
  115. inline int oldKey() const
  116. {
  117. return m_oldKey;
  118. }
  119. inline MidiTime oldPos() const
  120. {
  121. return m_oldPos;
  122. }
  123. inline MidiTime oldLength() const
  124. {
  125. return m_oldLength;
  126. }
  127. inline bool isPlaying() const
  128. {
  129. return m_isPlaying;
  130. }
  131. inline MidiTime endPos() const
  132. {
  133. const int l = length();
  134. return pos() + l;
  135. }
  136. inline const MidiTime & length() const
  137. {
  138. return m_length;
  139. }
  140. inline const MidiTime & pos() const
  141. {
  142. return m_pos;
  143. }
  144. inline MidiTime pos( MidiTime basePos ) const
  145. {
  146. const int bp = basePos;
  147. return m_pos - bp;
  148. }
  149. inline int key() const
  150. {
  151. return m_key;
  152. }
  153. inline volume_t getVolume() const
  154. {
  155. return m_volume;
  156. }
  157. int midiVelocity( int midiBaseVelocity ) const
  158. {
  159. return qMin( MidiMaxVelocity, getVolume() * midiBaseVelocity / DefaultVolume );
  160. }
  161. inline panning_t getPanning() const
  162. {
  163. return m_panning;
  164. }
  165. static QString classNodeName()
  166. {
  167. return "note";
  168. }
  169. inline QString nodeName() const override
  170. {
  171. return classNodeName();
  172. }
  173. static MidiTime quantized( const MidiTime & m, const int qGrid );
  174. DetuningHelper * detuning() const
  175. {
  176. return m_detuning;
  177. }
  178. bool hasDetuningInfo() const;
  179. bool withinRange(int tickStart, int tickEnd) const;
  180. void createDetuning();
  181. protected:
  182. void saveSettings( QDomDocument & doc, QDomElement & parent ) override;
  183. void loadSettings( const QDomElement & _this ) override;
  184. private:
  185. // for piano roll editing
  186. bool m_selected;
  187. int m_oldKey;
  188. MidiTime m_oldPos;
  189. MidiTime m_oldLength;
  190. bool m_isPlaying;
  191. int m_key;
  192. volume_t m_volume;
  193. panning_t m_panning;
  194. MidiTime m_length;
  195. MidiTime m_pos;
  196. DetuningHelper * m_detuning;
  197. };
  198. typedef QVector<Note *> NoteVector;
  199. #endif