filmstro_ffmpeg
 All Classes Files Functions Friends Macros
filmstro_audiohelpers_OutputSourcePlayer.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 OutputSourcePlayer
29  \file filmstro_audiobasics_OutputSourcePlayer.h
30  \brief An AudioSourcePlayer which handles changes of the sample rate
31 
32  \author Daniel Walz / Filmstro Ltd.
33  \date December 19th 2016
34 
35  \description An AudioSourcePlayer which handles changes of the sample rate
36 
37  ==============================================================================
38  */
39 
40 
41 #ifndef filmstro_audiobasics_OutputSourcePlayer_h
42 #define filmstro_audiobasics_OutputSourcePlayer_h
43 
48 class OutputSourcePlayer : public juce::AudioSourcePlayer
49 {
50 public:
56  OutputSourcePlayer (double sampleRate, int channels=2)
57  : internalSampleRate (sampleRate),
58  deviceSampleRate (0.0),
59  numChannels (channels),
60  outputDevice (nullptr)
61  {
62 
63  }
64 
68  void setSource (juce::AudioSource* newSource)
69  {
70  const juce::ScopedLock sl (readLock);
71  juce::AudioSourcePlayer::setSource (newSource);
72  }
73 
77  void audioDeviceIOCallback (const float **inputChannelData, int totalNumInputChannels,
78  float **outputChannelData, int totalNumOutputChannels,
79  int numSamples) override
80  {
81  if (getCurrentSource()) {
82  const juce::ScopedLock sl (readLock);
83 
84  if (outputDevice != nullptr) {
85  if (fabs (deviceSampleRate - outputDevice->getCurrentSampleRate ()) > 1.0) {
86  deviceSampleRate = outputDevice->getCurrentSampleRate();
87  resampler->setResamplingRatio (deviceSampleRate != 0 ?
88  internalSampleRate / deviceSampleRate :
89  1.0);
90  resampler->flushBuffers ();
91  }
92  }
93  juce::AudioBuffer<float> buffer (outputChannelData,
94  totalNumOutputChannels,
95  numSamples);
96  juce::AudioSourceChannelInfo bufferToFill (&buffer, 0, numSamples);
97  resampler->getNextAudioBlock (bufferToFill);
98  }
99  else {
100  juce::AudioSourcePlayer::audioDeviceIOCallback (inputChannelData,
101  totalNumInputChannels,
102  outputChannelData,
103  totalNumOutputChannels,
104  numSamples);
105  }
106  }
107 
111  void audioDeviceAboutToStart (juce::AudioIODevice *device) override
112  {
113  outputDevice = device;
114  resampler = new juce::ResamplingAudioSource (getCurrentSource(), false);
115  resampler->prepareToPlay (device->getCurrentBufferSizeSamples(),
116  device->getCurrentSampleRate());
117  juce::AudioSourcePlayer::audioDeviceAboutToStart (device);
118  }
119 
123  void audioDeviceStopped () override
124  {
125  juce::AudioSourcePlayer::audioDeviceStopped();
126  resampler = nullptr;
127  outputDevice = nullptr;
128  }
129 
130 
131 
132 private:
133  double internalSampleRate;
134 
135  double deviceSampleRate;
136 
137  int numChannels;
138 
139  juce::AudioIODevice* outputDevice;
140 
141  juce::ScopedPointer<juce::ResamplingAudioSource> resampler;
142 
143  juce::CriticalSection readLock;
144 };
145 
146 
147 #endif /* filmstro_audiobasics_OutputSourcePlayer_h */
void setSource(juce::AudioSource *newSource)
Set or replace the source of the AudioSourcePlayer.
Definition: filmstro_audiohelpers_OutputSourcePlayer.h:68
void audioDeviceStopped() override
Callback when the AudioIODevice just stopped using the AudioDeviceIOCallback.
Definition: filmstro_audiohelpers_OutputSourcePlayer.h:123
void audioDeviceAboutToStart(juce::AudioIODevice *device) override
callback when the device wants to start using the AudioDeviceIOCallback
Definition: filmstro_audiohelpers_OutputSourcePlayer.h:111
void audioDeviceIOCallback(const float **inputChannelData, int totalNumInputChannels, float **outputChannelData, int totalNumOutputChannels, int numSamples) override
Callback from the AudioIODevice.
Definition: filmstro_audiohelpers_OutputSourcePlayer.h:77
This class provides an AudioSourcePlayer, which will adapt its resampler whenever the device changes ...
Definition: filmstro_audiohelpers_OutputSourcePlayer.h:48
OutputSourcePlayer(double sampleRate, int channels=2)
Constructs an OutputSourcePlayer.
Definition: filmstro_audiohelpers_OutputSourcePlayer.h:56