PlayHandle.h 3.1 KB

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