filmstro_ffmpeg
 All Classes Files Functions Friends Macros
filmstro_audiohelpers_AudioProcessorPlayerSource.h
Go to the documentation of this file.
1 /*
2  ==============================================================================
3  Copyright (c) 2017, Filmstro Ltd. - Daniel Walz
4  All rights reserved.
5 
6  Redistribution and use in source and binary forms, with or without modification,
7  are permitted provided that the following conditions are met:
8  1. Redistributions of source code must retain the above copyright notice, this
9  list of conditions and the following disclaimer.
10  2. Redistributions in binary form must reproduce the above copyright notice,
11  this list of conditions and the following disclaimer in the documentation
12  and/or other materials provided with the distribution.
13  3. Neither the name of the copyright holder nor the names of its contributors
14  may be used to endorse or promote products derived from this software without
15  specific prior written permission.
16 
17  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
21  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
22  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
24  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
25  OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
26  OF THE POSSIBILITY OF SUCH DAMAGE.
27  ==============================================================================
28  \class AudioProcessorPlayerSource
29  \file filmstro_audiobasics_AudioProcessorPlayerSource.h
30  \brief An AudioSource to playback audioProcessors
31 
32  \author Daniel Walz / Filmstro Ltd.
33  \date December 15th 2016
34 
35  \description An AudioSource playing back an AudioProcessor
36 
37  ==============================================================================
38  */
39 
40 
41 #ifndef filmstro_audiobasics_AudioProcessorPlayerSource_h
42 #define filmstro_audiobasics_AudioProcessorPlayerSource_h
43 
44 class AudioProcessorPlayerSource : public juce::AudioSource
45 {
46 public:
47  AudioProcessorPlayerSource (juce::AudioProcessor* proc, const int channels = 2,
48  const bool deleteProcessor = true)
49  : processor (proc, deleteProcessor),
50  numChannels (channels),
51  fifo (channels, 4096)
52  {
53 
54  }
55 
57  {
58  }
59 
60  void prepareToPlay (int samplesPerBlockExpected, double sampleRate) override
61  {
62  buffer.setSize (numChannels, samplesPerBlockExpected);
63  if (processor) {
64  buffer.setSize (numChannels, samplesPerBlockExpected);
65  processor->prepareToPlay (sampleRate, samplesPerBlockExpected);
66  }
67  }
68 
69  void releaseResources () override
70  {
71  if (processor) {
72  processor->releaseResources ();
73  }
74  }
75 
76  void setProcessor (juce::AudioProcessor* proc, const bool deleteProcessor)
77  {
78  fifo.clear ();
79  processor.set (proc, deleteProcessor);
80  }
81 
82  juce::AudioProcessor* getCurrentProcessor () const
83  {
84  return processor;
85  }
86 
87  void getNextAudioBlock (const juce::AudioSourceChannelInfo &bufferToFill) override
88  {
89  if (processor) {
90  juce::MidiBuffer midiBuffer;
91  while (fifo.getNumReady() < bufferToFill.numSamples) {
92  processor->processBlock (buffer, midiBuffer);
93  fifo.addToFifo (buffer);
94  }
95  const int ready = fifo.getNumReady ();
96  if (ready >= bufferToFill.numSamples)
97  {
98  fifo.readFromFifo (bufferToFill);
99  }
100  else if (ready > 0) {
101  fifo.readFromFifo (bufferToFill, ready);
102  bufferToFill.buffer->clear (bufferToFill.startSample + ready, bufferToFill.numSamples - ready);
103  }
104  else {
105  bufferToFill.clearActiveBufferRegion ();
106  }
107  }
108  else {
109  bufferToFill.clearActiveBufferRegion();
110  }
111  }
112 
113 private:
114 
115  juce::OptionalScopedPointer<juce::AudioProcessor> processor;
116 
117  int numChannels;
118 
119  // buffer some audio data
121 
122  // processor buffer
123  juce::AudioBuffer<float> buffer;
124 };
125 
126 
127 
128 #endif /* filmstro_audiobasics_AudioProcessorPlayerSource_h */
void readFromFifo(FloatType **samples, int numSamples)
Definition: filmstro_audiohelpers_AudioBufferFIFO.h:102
juce::AudioProcessor * getCurrentProcessor() const
Definition: filmstro_audiohelpers_AudioProcessorPlayerSource.h:82
void getNextAudioBlock(const juce::AudioSourceChannelInfo &bufferToFill) override
Definition: filmstro_audiohelpers_AudioProcessorPlayerSource.h:87
void setProcessor(juce::AudioProcessor *proc, const bool deleteProcessor)
Definition: filmstro_audiohelpers_AudioProcessorPlayerSource.h:76
AudioProcessorPlayerSource(juce::AudioProcessor *proc, const int channels=2, const bool deleteProcessor=true)
Definition: filmstro_audiohelpers_AudioProcessorPlayerSource.h:47
Definition: filmstro_audiohelpers_AudioProcessorPlayerSource.h:44
void prepareToPlay(int samplesPerBlockExpected, double sampleRate) override
Definition: filmstro_audiohelpers_AudioProcessorPlayerSource.h:60
void clear()
Definition: filmstro_audiohelpers_AudioBufferFIFO.h:160
virtual ~AudioProcessorPlayerSource()
Definition: filmstro_audiohelpers_AudioProcessorPlayerSource.h:56
void addToFifo(const FloatType **samples, int numSamples)
Definition: filmstro_audiohelpers_AudioBufferFIFO.h:69
void releaseResources() override
Definition: filmstro_audiohelpers_AudioProcessorPlayerSource.h:69