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"
30 #include "juce_AudioProcessorPlayer.h"
33 //==============================================================================
34 AudioProcessorPlayer::AudioProcessorPlayer()
35 : processor (nullptr),
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
;
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
,
84 // these should have been prepared by audioDeviceAboutToStart()...
85 jassert (sampleRate
> 0 && blockSize
> 0);
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
,
99 for (i
= 0; i
< numOutputChannels
; ++i
)
101 channels
[totalNumChans
] = outputChannelData
[i
];
102 memcpy (channels
[totalNumChans
], inputChannelData
[i
], sizeof (float) * numSamples
);
106 for (i
= numOutputChannels
; i
< numInputChannels
; ++i
)
108 channels
[totalNumChans
] = tempBuffer
.getSampleData (i
- numOutputChannels
, 0);
109 memcpy (channels
[totalNumChans
], inputChannelData
[i
], sizeof (float) * numSamples
);
115 for (i
= 0; i
< numInputChannels
; ++i
)
117 channels
[totalNumChans
] = outputChannelData
[i
];
118 memcpy (channels
[totalNumChans
], inputChannelData
[i
], sizeof (float) * numSamples
);
122 for (i
= numInputChannels
; i
< numOutputChannels
; ++i
)
124 channels
[totalNumChans
] = outputChannelData
[i
];
125 zeromem (channels
[totalNumChans
], sizeof (float) * numSamples
);
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
);
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)
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();
183 tempBuffer
.setSize (1, 1);
186 void AudioProcessorPlayer::handleIncomingMidiMessage (MidiInput
*, const MidiMessage
& message
)
188 messageCollector
.addMessageToQueue (message
);