Add TAL-Reverb-II plugin to test
[juce-lv2.git] / juce / source / src / audio / processors / juce_AudioProcessorPlayer.cpp
blob77767d2b9329690cfd039805340602cf393888b7
1 /*
2 ==============================================================================
4 This file is part of the JUCE library - "Jules' Utility Class Extensions"
5 Copyright 2004-11 by Raw Material Software Ltd.
7 ------------------------------------------------------------------------------
9 JUCE can be redistributed and/or modified under the terms of the GNU General
10 Public License (Version 2), as published by the Free Software Foundation.
11 A copy of the license is included in the JUCE distribution, or can be found
12 online at www.gnu.org/licenses.
14 JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
18 ------------------------------------------------------------------------------
20 To release a closed-source product which uses JUCE, commercial licenses are
21 available: visit www.rawmaterialsoftware.com/juce for more information.
23 ==============================================================================
26 #include "../../core/juce_StandardHeader.h"
28 BEGIN_JUCE_NAMESPACE
30 #include "juce_AudioProcessorPlayer.h"
33 //==============================================================================
34 AudioProcessorPlayer::AudioProcessorPlayer()
35 : processor (nullptr),
36 sampleRate (0),
37 blockSize (0),
38 isPrepared (false),
39 numInputChans (0),
40 numOutputChans (0),
41 tempBuffer (1, 1)
45 AudioProcessorPlayer::~AudioProcessorPlayer()
47 setProcessor (nullptr);
50 //==============================================================================
51 void AudioProcessorPlayer::setProcessor (AudioProcessor* const processorToPlay)
53 if (processor != processorToPlay)
55 if (processorToPlay != nullptr && sampleRate > 0 && blockSize > 0)
57 processorToPlay->setPlayConfigDetails (numInputChans, numOutputChans,
58 sampleRate, blockSize);
60 processorToPlay->prepareToPlay (sampleRate, blockSize);
63 AudioProcessor* oldOne;
66 const ScopedLock sl (lock);
67 oldOne = isPrepared ? processor : nullptr;
68 processor = processorToPlay;
69 isPrepared = true;
72 if (oldOne != nullptr)
73 oldOne->releaseResources();
77 //==============================================================================
78 void AudioProcessorPlayer::audioDeviceIOCallback (const float** const inputChannelData,
79 const int numInputChannels,
80 float** const outputChannelData,
81 const int numOutputChannels,
82 const int numSamples)
84 // these should have been prepared by audioDeviceAboutToStart()...
85 jassert (sampleRate > 0 && blockSize > 0);
87 incomingMidi.clear();
88 messageCollector.removeNextBlockOfMessages (incomingMidi, numSamples);
89 int i, totalNumChans = 0;
91 if (numInputChannels > numOutputChannels)
93 // if there aren't enough output channels for the number of
94 // inputs, we need to create some temporary extra ones (can't
95 // use the input data in case it gets written to)
96 tempBuffer.setSize (numInputChannels - numOutputChannels, numSamples,
97 false, false, true);
99 for (i = 0; i < numOutputChannels; ++i)
101 channels[totalNumChans] = outputChannelData[i];
102 memcpy (channels[totalNumChans], inputChannelData[i], sizeof (float) * numSamples);
103 ++totalNumChans;
106 for (i = numOutputChannels; i < numInputChannels; ++i)
108 channels[totalNumChans] = tempBuffer.getSampleData (i - numOutputChannels, 0);
109 memcpy (channels[totalNumChans], inputChannelData[i], sizeof (float) * numSamples);
110 ++totalNumChans;
113 else
115 for (i = 0; i < numInputChannels; ++i)
117 channels[totalNumChans] = outputChannelData[i];
118 memcpy (channels[totalNumChans], inputChannelData[i], sizeof (float) * numSamples);
119 ++totalNumChans;
122 for (i = numInputChannels; i < numOutputChannels; ++i)
124 channels[totalNumChans] = outputChannelData[i];
125 zeromem (channels[totalNumChans], sizeof (float) * numSamples);
126 ++totalNumChans;
130 AudioSampleBuffer buffer (channels, totalNumChans, numSamples);
132 const ScopedLock sl (lock);
134 if (processor != nullptr)
136 const ScopedLock sl2 (processor->getCallbackLock());
138 if (processor->isSuspended())
140 for (i = 0; i < numOutputChannels; ++i)
141 zeromem (outputChannelData[i], sizeof (float) * numSamples);
143 else
145 processor->processBlock (buffer, incomingMidi);
150 void AudioProcessorPlayer::audioDeviceAboutToStart (AudioIODevice* device)
152 const ScopedLock sl (lock);
154 sampleRate = device->getCurrentSampleRate();
155 blockSize = device->getCurrentBufferSizeSamples();
156 numInputChans = device->getActiveInputChannels().countNumberOfSetBits();
157 numOutputChans = device->getActiveOutputChannels().countNumberOfSetBits();
159 messageCollector.reset (sampleRate);
160 zeromem (channels, sizeof (channels));
162 if (processor != nullptr)
164 if (isPrepared)
165 processor->releaseResources();
167 AudioProcessor* const oldProcessor = processor;
168 setProcessor (nullptr);
169 setProcessor (oldProcessor);
173 void AudioProcessorPlayer::audioDeviceStopped()
175 const ScopedLock sl (lock);
177 if (processor != nullptr && isPrepared)
178 processor->releaseResources();
180 sampleRate = 0.0;
181 blockSize = 0;
182 isPrepared = false;
183 tempBuffer.setSize (1, 1);
186 void AudioProcessorPlayer::handleIncomingMidiMessage (MidiInput*, const MidiMessage& message)
188 messageCollector.addMessageToQueue (message);
192 END_JUCE_NAMESPACE