PlayHandle.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * PlayHandle.h - base class PlayHandle - core of rendering engine
  3. *
  4. * Copyright (c) 2004-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 PLAY_HANDLE_H
  25. #define PLAY_HANDLE_H
  26. #include <QtCore/QList>
  27. #include <QtCore/QMutex>
  28. #include "lmms_export.h"
  29. #include "MemoryManager.h"
  30. #include "ThreadableJob.h"
  31. #include "lmms_basics.h"
  32. class QThread;
  33. class Track;
  34. class AudioPort;
  35. class LMMS_EXPORT PlayHandle : public ThreadableJob
  36. {
  37. public:
  38. enum Types
  39. {
  40. TypeNotePlayHandle = 0x01,
  41. TypeInstrumentPlayHandle = 0x02,
  42. TypeSamplePlayHandle = 0x04,
  43. TypePresetPreviewHandle = 0x08
  44. } ;
  45. typedef Types Type;
  46. enum
  47. {
  48. MaxNumber = 1024
  49. } ;
  50. PlayHandle( const Type type, f_cnt_t offset = 0 );
  51. PlayHandle & operator = ( PlayHandle & p )
  52. {
  53. m_type = p.m_type;
  54. m_offset = p.m_offset;
  55. m_affinity = p.m_affinity;
  56. m_usesBuffer = p.m_usesBuffer;
  57. m_audioPort = p.m_audioPort;
  58. return *this;
  59. }
  60. virtual ~PlayHandle();
  61. virtual bool affinityMatters() const
  62. {
  63. return false;
  64. }
  65. const QThread* affinity() const
  66. {
  67. return m_affinity;
  68. }
  69. Type type() const
  70. {
  71. return m_type;
  72. }
  73. // required for ThreadableJob
  74. void doProcessing() override;
  75. bool requiresProcessing() const override
  76. {
  77. return !isFinished();
  78. }
  79. void lock()
  80. {
  81. m_processingLock.lock();
  82. }
  83. void unlock()
  84. {
  85. m_processingLock.unlock();
  86. }
  87. bool tryLock()
  88. {
  89. return m_processingLock.tryLock();
  90. }
  91. virtual void play( sampleFrame* buffer ) = 0;
  92. virtual bool isFinished() const = 0;
  93. // returns the frameoffset at the start of the playhandle,
  94. // ie. how many empty frames should be inserted at the start of the first period
  95. f_cnt_t offset() const
  96. {
  97. return m_offset;
  98. }
  99. void setOffset( f_cnt_t _offset )
  100. {
  101. m_offset = _offset;
  102. }
  103. virtual bool isFromTrack( const Track * _track ) const = 0;
  104. bool usesBuffer() const
  105. {
  106. return m_usesBuffer;
  107. }
  108. void setUsesBuffer( const bool b )
  109. {
  110. m_usesBuffer = b;
  111. }
  112. AudioPort * audioPort()
  113. {
  114. return m_audioPort;
  115. }
  116. void setAudioPort( AudioPort * port )
  117. {
  118. m_audioPort = port;
  119. }
  120. void releaseBuffer();
  121. sampleFrame * buffer();
  122. private:
  123. Type m_type;
  124. f_cnt_t m_offset;
  125. QThread* m_affinity;
  126. QMutex m_processingLock;
  127. sampleFrame* m_playHandleBuffer;
  128. bool m_bufferReleased;
  129. bool m_usesBuffer;
  130. AudioPort * m_audioPort;
  131. } ;
  132. typedef QList<PlayHandle *> PlayHandleList;
  133. typedef QList<const PlayHandle *> ConstPlayHandleList;
  134. #endif