The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

277 lines
11KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. The code included in this file is provided under the terms of the ISC license
  8. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  9. To use, copy, modify, and/or distribute this software for any purpose with or
  10. without fee is hereby granted provided that the above copyright notice and
  11. this permission notice appear in all copies.
  12. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  13. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  14. DISCLAIMED.
  15. ==============================================================================
  16. */
  17. namespace juce
  18. {
  19. //==============================================================================
  20. /**
  21. A subclass of AudioPlayHead can supply information about the position and
  22. status of a moving play head during audio playback.
  23. One of these can be supplied to an AudioProcessor object so that it can find
  24. out about the position of the audio that it is rendering.
  25. @see AudioProcessor::setPlayHead, AudioProcessor::getPlayHead
  26. @tags{Audio}
  27. */
  28. class JUCE_API AudioPlayHead
  29. {
  30. protected:
  31. //==============================================================================
  32. AudioPlayHead() = default;
  33. public:
  34. virtual ~AudioPlayHead() = default;
  35. //==============================================================================
  36. /** Frame rate types. */
  37. enum FrameRateType
  38. {
  39. fps23976 = 0,
  40. fps24 = 1,
  41. fps25 = 2,
  42. fps2997 = 3,
  43. fps30 = 4,
  44. fps2997drop = 5,
  45. fps30drop = 6,
  46. fps60 = 7,
  47. fps60drop = 8,
  48. fpsUnknown = 99
  49. };
  50. /** More descriptive frame rate type. */
  51. class JUCE_API FrameRate
  52. {
  53. public:
  54. /** Creates a frame rate with a base rate of 0. */
  55. FrameRate() = default;
  56. /** Creates a FrameRate instance from a FrameRateType. */
  57. FrameRate (FrameRateType type) : FrameRate (fromType (type)) {}
  58. /** Gets the FrameRateType that matches the state of this FrameRate.
  59. Returns fpsUnknown if this FrameRate cannot be represented by any of the
  60. other enum fields.
  61. */
  62. FrameRateType getType() const
  63. {
  64. switch (base)
  65. {
  66. case 24: return pulldown ? fps23976 : fps24;
  67. case 25: return fps25;
  68. case 30: return pulldown ? (drop ? fps2997drop : fps2997)
  69. : (drop ? fps30drop : fps30);
  70. case 60: return drop ? fps60drop : fps60;
  71. }
  72. return fpsUnknown;
  73. }
  74. /** Returns the plain rate, without taking pulldown into account. */
  75. int getBaseRate() const { return base; }
  76. /** Returns true if drop-frame timecode is in use. */
  77. bool isDrop() const { return drop; }
  78. /** Returns true if the effective framerate is actually equal to the base rate divided by 1.001 */
  79. bool isPullDown() const { return pulldown; }
  80. /** Returns the actual rate described by this object, taking pulldown into account. */
  81. double getEffectiveRate() const { return pulldown ? (double) base / 1.001 : (double) base; }
  82. /** Returns a copy of this object with the specified base rate. */
  83. FrameRate withBaseRate (int x) const { return with (&FrameRate::base, x); }
  84. /** Returns a copy of this object with drop frames enabled or disabled, as specified. */
  85. FrameRate withDrop (bool x = true) const { return with (&FrameRate::drop, x); }
  86. /** Returns a copy of this object with pulldown enabled or disabled, as specified. */
  87. FrameRate withPullDown (bool x = true) const { return with (&FrameRate::pulldown, x); }
  88. /** Returns true if this instance is equal to other. */
  89. bool operator== (const FrameRate& other) const
  90. {
  91. const auto tie = [] (const FrameRate& x) { return std::tie (x.base, x.drop, x.pulldown); };
  92. return tie (*this) == tie (other);
  93. }
  94. /** Returns true if this instance is not equal to other. */
  95. bool operator!= (const FrameRate& other) const { return ! (*this == other); }
  96. private:
  97. static FrameRate fromType (FrameRateType type)
  98. {
  99. switch (type)
  100. {
  101. case fps23976: return FrameRate().withBaseRate (24).withPullDown();
  102. case fps24: return FrameRate().withBaseRate (24);
  103. case fps25: return FrameRate().withBaseRate (25);
  104. case fps2997: return FrameRate().withBaseRate (30).withPullDown();
  105. case fps30: return FrameRate().withBaseRate (30);
  106. case fps2997drop: return FrameRate().withBaseRate (30).withDrop().withPullDown();
  107. case fps30drop: return FrameRate().withBaseRate (30).withDrop();
  108. case fps60: return FrameRate().withBaseRate (60);
  109. case fps60drop: return FrameRate().withBaseRate (60).withDrop();
  110. case fpsUnknown: break;
  111. }
  112. return {};
  113. }
  114. template <typename Member, typename Value>
  115. FrameRate with (Member&& member, Value&& value) const
  116. {
  117. auto copy = *this;
  118. copy.*member = std::forward<Value> (value);
  119. return copy;
  120. }
  121. int base = 0;
  122. bool drop = false, pulldown = false;
  123. };
  124. //==============================================================================
  125. /** This structure is filled-in by the AudioPlayHead::getCurrentPosition() method.
  126. */
  127. struct JUCE_API CurrentPositionInfo
  128. {
  129. /** The tempo in BPM */
  130. double bpm = 120.0;
  131. /** Time signature numerator, e.g. the 3 of a 3/4 time sig */
  132. int timeSigNumerator = 4;
  133. /** Time signature denominator, e.g. the 4 of a 3/4 time sig */
  134. int timeSigDenominator = 4;
  135. /** The current play position, in samples from the start of the timeline. */
  136. int64 timeInSamples = 0;
  137. /** The current play position, in seconds from the start of the timeline. */
  138. double timeInSeconds = 0;
  139. /** For timecode, the position of the start of the timeline, in seconds from 00:00:00:00. */
  140. double editOriginTime = 0;
  141. /** The current play position, in units of quarter-notes. */
  142. double ppqPosition = 0;
  143. /** The position of the start of the last bar, in units of quarter-notes.
  144. This is the time from the start of the timeline to the start of the current
  145. bar, in ppq units.
  146. Note - this value may be unavailable on some hosts, e.g. Pro-Tools. If
  147. it's not available, the value will be 0.
  148. */
  149. double ppqPositionOfLastBarStart = 0;
  150. /** The video frame rate, if applicable. */
  151. FrameRate frameRate = FrameRateType::fps23976;
  152. /** True if the transport is currently playing. */
  153. bool isPlaying = false;
  154. /** True if the transport is currently recording.
  155. (When isRecording is true, then isPlaying will also be true).
  156. */
  157. bool isRecording = false;
  158. /** The current cycle start position in units of quarter-notes.
  159. Note that not all hosts or plugin formats may provide this value.
  160. @see isLooping
  161. */
  162. double ppqLoopStart = 0;
  163. /** The current cycle end position in units of quarter-notes.
  164. Note that not all hosts or plugin formats may provide this value.
  165. @see isLooping
  166. */
  167. double ppqLoopEnd = 0;
  168. /** True if the transport is currently looping. */
  169. bool isLooping = false;
  170. //==============================================================================
  171. bool operator== (const CurrentPositionInfo& other) const noexcept
  172. {
  173. const auto tie = [] (const CurrentPositionInfo& i)
  174. {
  175. return std::tie (i.timeInSamples,
  176. i.ppqPosition,
  177. i.editOriginTime,
  178. i.ppqPositionOfLastBarStart,
  179. i.frameRate,
  180. i.isPlaying,
  181. i.isRecording,
  182. i.bpm,
  183. i.timeSigNumerator,
  184. i.timeSigDenominator,
  185. i.ppqLoopStart,
  186. i.ppqLoopEnd,
  187. i.isLooping);
  188. };
  189. return tie (*this) == tie (other);
  190. }
  191. bool operator!= (const CurrentPositionInfo& other) const noexcept
  192. {
  193. return ! operator== (other);
  194. }
  195. void resetToDefault()
  196. {
  197. *this = CurrentPositionInfo{};
  198. }
  199. };
  200. //==============================================================================
  201. /** Fills-in the given structure with details about the transport's
  202. position at the start of the current processing block. If this method returns
  203. false then the current play head position is not available and the given
  204. structure will be undefined.
  205. You can ONLY call this from your processBlock() method! Calling it at other
  206. times will produce undefined behaviour, as the host may not have any context
  207. in which a time would make sense, and some hosts will almost certainly have
  208. multithreading issues if it's not called on the audio thread.
  209. */
  210. virtual bool getCurrentPosition (CurrentPositionInfo& result) = 0;
  211. /** Returns true if this object can control the transport. */
  212. virtual bool canControlTransport() { return false; }
  213. /** Starts or stops the audio. */
  214. virtual void transportPlay (bool shouldStartPlaying) { ignoreUnused (shouldStartPlaying); }
  215. /** Starts or stops recording the audio. */
  216. virtual void transportRecord (bool shouldStartRecording) { ignoreUnused (shouldStartRecording); }
  217. /** Rewinds the audio. */
  218. virtual void transportRewind() {}
  219. };
  220. } // namespace juce