OpenShot Audio Library | OpenShotAudio  0.6.0
juce_ARAAudioReaders.h
1 /*
2  ==============================================================================
3 
4  This file is part of the JUCE library.
5  Copyright (c) 2022 - Raw Material Software Limited
6 
7  JUCE is an open source library subject to commercial or open-source
8  licensing.
9 
10  By using JUCE, you agree to the terms of both the JUCE 7 End-User License
11  Agreement and JUCE Privacy Policy.
12 
13  End User License Agreement: www.juce.com/juce-7-licence
14  Privacy Policy: www.juce.com/juce-privacy-policy
15 
16  Or: You may also use this code under the terms of the GPL v3 (see
17  www.gnu.org/licenses).
18 
19  JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
20  EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
21  DISCLAIMED.
22 
23  ==============================================================================
24 */
25 
26 #pragma once
27 
28 namespace juce
29 {
30 
31 class AudioProcessor;
32 
33 /* All these readers follow a common pattern of "invalidation":
34 
35  Whenever the samples they are reading are altered, the readers become invalid and will stop
36  accessing the model graph. These alterations are model edits such as property changes, content
37  changes (if affecting sample scope), or the deletion of some model object involved in the read
38  process. Since these edits are performed on the document controller thread, reader validity can
39  immediately be checked after the edit has been concluded, and any reader that has become invalid
40  can be recreated.
41 
42  Note that encountering a failure in any individual read call does not invalidate the reader, so
43  that the entity using the reader can decide whether to retry or to back out. This includes trying
44  to read an audio source for which the host has currently disabled access: the failure will be
45  immediately visible, but the reader will remain valid. This ensures that for example a realtime
46  renderer can just keep reading and will be seeing proper samples again once sample access is
47  re-enabled.
48 
49  If desired, the code calling readSamples() can also implement proper signaling of any read error
50  to the document controller thread to trigger rebuilding the reader as needed. This will typically
51  be done when implementing audio source analysis: if there is an error upon reading the samples
52  that cannot be resolved within a reasonable timeout, then the analysis would be aborted. The
53  document controller code that monitors the analysis tasks can evaluate this and re-launch a new
54  analysis when appropriate (e.g. when access is re-enabled).
55 
56  When reading playback regions (directly or through a region sequence reader), the reader will
57  represent the regions as a single source object that covers the union of all affected regions.
58  The first sample produced by the reader thus will be the first sample of the earliest region.
59  This means that the location of this region has to be taken into account by the calling code if
60  it wants to relate the samples to the model or any other reader output.
61 */
62 
63 //==============================================================================
77 class JUCE_API ARAAudioSourceReader : public AudioFormatReader,
78  private ARAAudioSource::Listener
79 {
80 public:
82  explicit ARAAudioSourceReader (ARAAudioSource* audioSource);
83 
84  ~ARAAudioSourceReader() override;
85 
86  bool readSamples (int* const* destSamples,
87  int numDestChannels,
88  int startOffsetInDestBuffer,
89  int64 startSampleInFile,
90  int numSamples) override;
91 
95  bool isValid() const { return audioSourceBeingRead != nullptr; }
96 
100  void invalidate();
101 
102  void willUpdateAudioSourceProperties (ARAAudioSource* audioSource,
103  ARAAudioSource::PropertiesPtr newProperties) override;
104  void doUpdateAudioSourceContent (ARAAudioSource* audioSource,
105  ARAContentUpdateScopes scopeFlags) override;
106  void willEnableAudioSourceSamplesAccess (ARAAudioSource* audioSource, bool enable) override;
107  void didEnableAudioSourceSamplesAccess (ARAAudioSource* audioSource, bool enable) override;
108  void willDestroyAudioSource (ARAAudioSource* audioSource) override;
109 
110 private:
111  ARAAudioSource* audioSourceBeingRead;
112  std::unique_ptr<ARA::PlugIn::HostAudioReader> hostReader;
113  ReadWriteLock lock;
114  std::vector<void*> tmpPtrs;
115 
116  JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ARAAudioSourceReader)
117 };
118 
119 //==============================================================================
139  private ARAPlaybackRegion::Listener
140 {
141 public:
147  explicit ARAPlaybackRegionReader (ARAPlaybackRegion* playbackRegion);
148 
156  ARAPlaybackRegionReader (double sampleRate, int numChannels,
157  const std::vector<ARAPlaybackRegion*>& playbackRegions);
158 
159  ~ARAPlaybackRegionReader() override;
160 
162  bool isValid() const { return (playbackRenderer != nullptr); }
163 
167  void invalidate();
168 
169  bool readSamples (int* const* destSamples,
170  int numDestChannels,
171  int startOffsetInDestBuffer,
172  int64 startSampleInFile,
173  int numSamples) override;
174 
175  void willUpdatePlaybackRegionProperties (ARAPlaybackRegion* playbackRegion,
176  ARAPlaybackRegion::PropertiesPtr newProperties) override;
177  void didUpdatePlaybackRegionContent (ARAPlaybackRegion* playbackRegion,
178  ARAContentUpdateScopes scopeFlags) override;
179  void willDestroyPlaybackRegion (ARAPlaybackRegion* playbackRegion) override;
180 
182  int64 startInSamples = 0;
183 
184 private:
185  std::unique_ptr<ARAPlaybackRenderer> playbackRenderer;
186  AudioPlayHead::PositionInfo positionInfo;
187  ReadWriteLock lock;
188 
189  static constexpr int maximumBlockSize = 4 * 1024;
190 
191  JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ARAPlaybackRegionReader)
192 };
193 
194 } // namespace juce