NotePlayHandle.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. /*
  2. * NotePlayHandle.h - declaration of class NotePlayHandle which manages
  3. * playback of a single note by an instrument
  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_PLAY_HANDLE_H
  26. #define NOTE_PLAY_HANDLE_H
  27. #include <memory>
  28. #include "BasicFilters.h"
  29. #include "Note.h"
  30. #include "PlayHandle.h"
  31. #include "Track.h"
  32. #include "MemoryPool.h"
  33. class QReadWriteLock;
  34. class InstrumentTrack;
  35. class NotePlayHandle;
  36. typedef QList<NotePlayHandle *> NotePlayHandleList;
  37. typedef QList<const NotePlayHandle *> ConstNotePlayHandleList;
  38. class LMMS_EXPORT NotePlayHandle : public PlayHandle, public Note
  39. {
  40. public:
  41. void * m_pluginData;
  42. std::unique_ptr<BasicFilters<>> m_filter;
  43. // specifies origin of NotePlayHandle
  44. enum Origins
  45. {
  46. OriginPattern, /*! playback of a note from a pattern */
  47. OriginMidiInput, /*! playback of a MIDI note input event */
  48. OriginNoteStacking, /*! created by note stacking instrument function */
  49. OriginArpeggio, /*! created by arpeggio instrument function */
  50. OriginCount
  51. };
  52. typedef Origins Origin;
  53. NotePlayHandle( InstrumentTrack* instrumentTrack,
  54. const f_cnt_t offset,
  55. const f_cnt_t frames,
  56. const Note& noteToPlay,
  57. NotePlayHandle* parent = NULL,
  58. int midiEventChannel = -1,
  59. Origin origin = OriginPattern );
  60. virtual ~NotePlayHandle();
  61. void * operator new ( size_t size, void * p )
  62. {
  63. return p;
  64. }
  65. void setVolume( volume_t volume ) override;
  66. void setPanning( panning_t panning ) override;
  67. int midiKey() const;
  68. int midiChannel() const
  69. {
  70. return m_midiChannel;
  71. }
  72. /*! convenience function that returns offset for the first period and zero otherwise,
  73. used by instruments to handle the offset: instruments have to check this property and
  74. add the correct number of empty frames in the beginning of the period */
  75. f_cnt_t noteOffset() const
  76. {
  77. return m_totalFramesPlayed == 0
  78. ? offset()
  79. : 0;
  80. }
  81. const float& frequency() const
  82. {
  83. return m_frequency;
  84. }
  85. /*! Returns frequency without pitch wheel influence */
  86. float unpitchedFrequency() const
  87. {
  88. return m_unpitchedFrequency;
  89. }
  90. /*! Renders one chunk using the attached instrument into the buffer */
  91. void play( sampleFrame* buffer ) override;
  92. /*! Returns whether playback of note is finished and thus handle can be deleted */
  93. bool isFinished() const override
  94. {
  95. return m_released && framesLeft() <= 0;
  96. }
  97. /*! Returns number of frames left for playback */
  98. f_cnt_t framesLeft() const;
  99. /*! Returns how many frames have to be rendered in current period */
  100. fpp_t framesLeftForCurrentPeriod() const;
  101. /*! Returns whether the play handle plays on a certain track */
  102. bool isFromTrack( const Track* _track ) const override;
  103. /*! Releases the note (and plays release frames */
  104. void noteOff( const f_cnt_t offset = 0 );
  105. /*! Returns number of frames to be played until the note is going to be released */
  106. f_cnt_t framesBeforeRelease() const
  107. {
  108. return m_framesBeforeRelease;
  109. }
  110. /*! Returns how many frames were played since release */
  111. f_cnt_t releaseFramesDone() const
  112. {
  113. return m_releaseFramesDone;
  114. }
  115. /*! Returns the number of frames to be played after release according to
  116. the release times in the envelopes */
  117. f_cnt_t actualReleaseFramesToDo() const;
  118. /*! Returns total numbers of frames to play (including release frames) */
  119. f_cnt_t frames() const
  120. {
  121. return m_frames;
  122. }
  123. /*! Sets the total number of frames to play (including release frames) */
  124. void setFrames( const f_cnt_t _frames );
  125. /*! Returns whether note was released */
  126. bool isReleased() const
  127. {
  128. return m_released;
  129. }
  130. bool isReleaseStarted() const
  131. {
  132. return m_releaseStarted;
  133. }
  134. /*! Returns total numbers of frames played so far */
  135. f_cnt_t totalFramesPlayed() const
  136. {
  137. return m_totalFramesPlayed;
  138. }
  139. /*! Returns volume level at given frame (envelope/LFO) */
  140. float volumeLevel( const f_cnt_t frame );
  141. /*! Returns instrument track which is being played by this handle (const version) */
  142. const InstrumentTrack* instrumentTrack() const
  143. {
  144. return m_instrumentTrack;
  145. }
  146. /*! Returns instrument track which is being played by this handle */
  147. InstrumentTrack* instrumentTrack()
  148. {
  149. return m_instrumentTrack;
  150. }
  151. /*! Returns whether note has a parent, e.g. is not part of an arpeggio or a chord */
  152. bool hasParent() const
  153. {
  154. return m_hasParent;
  155. }
  156. /*! Returns origin of note */
  157. Origin origin() const
  158. {
  159. return m_origin;
  160. }
  161. /*! Returns whether note has children */
  162. bool isMasterNote() const
  163. {
  164. return m_subNotes.size() > 0 || m_hadChildren;
  165. }
  166. void setMasterNote()
  167. {
  168. m_hadChildren = true;
  169. setUsesBuffer( false );
  170. }
  171. /*! Returns whether note is muted */
  172. bool isMuted() const
  173. {
  174. return m_muted;
  175. }
  176. /*! Mutes playback of note */
  177. void mute();
  178. /*! Returns index of NotePlayHandle in vector of note-play-handles
  179. belonging to this instrument track - used by arpeggiator.
  180. Ignores child note-play-handles, returns -1 when called on one */
  181. int index() const;
  182. /*! Returns list of note-play-handles belonging to given instrument track.
  183. If allPlayHandles = true, also released note-play-handles and children
  184. are returned */
  185. static ConstNotePlayHandleList nphsOfInstrumentTrack( const InstrumentTrack* Track, bool allPlayHandles = false );
  186. /*! Returns whether given NotePlayHandle instance is equal to *this */
  187. bool operator==( const NotePlayHandle & _nph ) const;
  188. /*! Returns whether NotePlayHandle belongs to BB track and BB track is muted */
  189. bool isBbTrackMuted()
  190. {
  191. return m_bbTrack && m_bbTrack->isMuted();
  192. }
  193. /*! Sets attached BB track */
  194. void setBBTrack( Track* t )
  195. {
  196. m_bbTrack = t;
  197. }
  198. /*! Process note detuning automation */
  199. void processMidiTime( const MidiTime& time );
  200. /*! Updates total length (m_frames) depending on a new tempo */
  201. void resize( const bpm_t newTempo );
  202. /*! Set song-global offset (relative to containing pattern) in order to properly perform the note detuning */
  203. void setSongGlobalParentOffset( const MidiTime& offset )
  204. {
  205. m_songGlobalParentOffset = offset;
  206. }
  207. /*! Returns song-global offset */
  208. const MidiTime& songGlobalParentOffset() const
  209. {
  210. return m_songGlobalParentOffset;
  211. }
  212. void setFrequencyUpdate()
  213. {
  214. m_frequencyNeedsUpdate = true;
  215. }
  216. private:
  217. class BaseDetuning
  218. {
  219. MM_OPERATORS
  220. public:
  221. BaseDetuning( DetuningHelper* detuning );
  222. void setValue( float val )
  223. {
  224. m_value = val;
  225. }
  226. float value() const
  227. {
  228. return m_value;
  229. }
  230. private:
  231. float m_value;
  232. } ;
  233. void updateFrequency();
  234. InstrumentTrack* m_instrumentTrack; // needed for calling
  235. // InstrumentTrack::playNote
  236. f_cnt_t m_frames; // total frames to play
  237. f_cnt_t m_totalFramesPlayed; // total frame-counter - used for
  238. // figuring out whether a whole note
  239. // has been played
  240. f_cnt_t m_framesBeforeRelease; // number of frames after which note
  241. // is released
  242. f_cnt_t m_releaseFramesToDo; // total numbers of frames to be
  243. // played after release
  244. f_cnt_t m_releaseFramesDone; // number of frames done after
  245. // release of note
  246. NotePlayHandleList m_subNotes; // used for chords and arpeggios
  247. volatile bool m_released; // indicates whether note is released
  248. bool m_releaseStarted;
  249. bool m_hasMidiNote;
  250. bool m_hasParent; // indicates whether note has parent
  251. NotePlayHandle * m_parent; // parent note
  252. bool m_hadChildren;
  253. bool m_muted; // indicates whether note is muted
  254. Track* m_bbTrack; // related BB track
  255. // tempo reaction
  256. bpm_t m_origTempo; // original tempo
  257. f_cnt_t m_origFrames; // original m_frames
  258. int m_origBaseNote;
  259. float m_frequency;
  260. float m_unpitchedFrequency;
  261. BaseDetuning* m_baseDetuning;
  262. MidiTime m_songGlobalParentOffset;
  263. int m_midiChannel;
  264. Origin m_origin;
  265. bool m_frequencyNeedsUpdate; // used to update pitch
  266. } ;
  267. extern MemoryPool<NotePlayHandle> NotePlayHandlePool;
  268. #endif