void JuceDemoPluginAudioProcessor::applyDelay (AudioBuffer<FloatType>& buffer, AudioBuffer<FloatType>& delayBuffer) { const int numSamples = buffer.getNumSamples(); const float delayLevel = *delayParam; int delayPos = 0; for (int channel = 0; channel < getNumInputChannels(); ++channel) { FloatType* const channelData = buffer.getWritePointer (channel); FloatType* const delayData = delayBuffer.getWritePointer (jmin (channel, delayBuffer.getNumChannels() - 1)); delayPos = delayPosition; for (int i = 0; i < numSamples; ++i) { const FloatType in = channelData[i]; channelData[i] += delayData[delayPos]; delayData[delayPos] = (delayData[delayPos] + in) * delayLevel; if (++delayPos >= delayBuffer.getNumSamples()) delayPos = 0; } } delayPosition = delayPos; }
void Synthesiser::processNextBlock (AudioBuffer<floatType>& outputAudio, const MidiBuffer& midiData, int startSample, int numSamples) { // must set the sample rate before using this! jassert (sampleRate != 0); const int targetChannels = outputAudio.getNumChannels(); MidiBuffer::Iterator midiIterator (midiData); midiIterator.setNextSamplePosition (startSample); bool firstEvent = true; int midiEventPos; MidiMessage m; const ScopedLock sl (lock); while (numSamples > 0) { if (! midiIterator.getNextEvent (m, midiEventPos)) { if (targetChannels > 0) renderVoices (outputAudio, startSample, numSamples); return; } const int samplesToNextMidiMessage = midiEventPos - startSample; if (samplesToNextMidiMessage >= numSamples) { if (targetChannels > 0) renderVoices (outputAudio, startSample, numSamples); handleMidiEvent (m); break; } if (samplesToNextMidiMessage < ((firstEvent && ! subBlockSubdivisionIsStrict) ? 1 : minimumSubBlockSize)) { handleMidiEvent (m); continue; } firstEvent = false; if (targetChannels > 0) renderVoices (outputAudio, startSample, samplesToNextMidiMessage); handleMidiEvent (m); startSample += samplesToNextMidiMessage; numSamples -= samplesToNextMidiMessage; } while (midiIterator.getNextEvent (m, midiEventPos)) handleMidiEvent (m); }
void processBlock (AudioBuffer<FloatType>& outputBuffer, int startSample, int numSamples) { if (angleDelta != 0.0) { if (tailOff > 0) { while (--numSamples >= 0) { const FloatType currentSample = static_cast<FloatType> (std::sin (currentAngle) * level * tailOff); for (int i = outputBuffer.getNumChannels(); --i >= 0;) outputBuffer.addSample (i, startSample, currentSample); currentAngle += angleDelta; ++startSample; tailOff *= 0.99; if (tailOff <= 0.005) { clearCurrentNote(); angleDelta = 0.0; break; } } } else { while (--numSamples >= 0) { const FloatType currentSample = static_cast<FloatType> (std::sin (currentAngle) * level); for (int i = outputBuffer.getNumChannels(); --i >= 0;) outputBuffer.addSample (i, startSample, currentSample); currentAngle += angleDelta; ++startSample; } } } }
MemoryAudioSource::MemoryAudioSource (AudioBuffer<float>& bufferToUse, bool copyMemory, bool shouldLoop) : isLooping (shouldLoop) { if (copyMemory) buffer.makeCopyOf (bufferToUse); else buffer.setDataToReferTo (bufferToUse.getArrayOfWritePointers(), bufferToUse.getNumChannels(), bufferToUse.getNumSamples()); }
//============================================================================== void copyBufferToTemporaryLocation (const AudioBuffer<float>& buffer) { const SpinLock::ScopedLockType sl (processLock); auto numChannels = buffer.getNumChannels() > 1 ? 2 : 1; temporaryBuffer.setSize (numChannels, buffer.getNumSamples(), false, false, true); for (auto channel = 0; channel < numChannels; ++channel) temporaryBuffer.copyFrom (channel, 0, buffer, channel, 0, buffer.getNumSamples()); }
void perform (AudioBuffer<FloatType>& buffer, MidiBuffer& midiMessages, AudioPlayHead* audioPlayHead) { auto numSamples = buffer.getNumSamples(); auto maxSamples = renderingBuffer.getNumSamples(); if (numSamples > maxSamples) { // being asked to render more samples than our buffers have, so slice things up... tempMIDI.clear(); tempMIDI.addEvents (midiMessages, maxSamples, numSamples, -maxSamples); { AudioBuffer<FloatType> startAudio (buffer.getArrayOfWritePointers(), buffer.getNumChannels(), maxSamples); midiMessages.clear (maxSamples, numSamples); perform (startAudio, midiMessages, audioPlayHead); } AudioBuffer<FloatType> endAudio (buffer.getArrayOfWritePointers(), buffer.getNumChannels(), maxSamples, numSamples - maxSamples); perform (endAudio, tempMIDI, audioPlayHead); return; } currentAudioInputBuffer = &buffer; currentAudioOutputBuffer.setSize (jmax (1, buffer.getNumChannels()), numSamples); currentAudioOutputBuffer.clear(); currentMidiInputBuffer = &midiMessages; currentMidiOutputBuffer.clear(); { const Context context { renderingBuffer.getArrayOfWritePointers(), midiBuffers.begin(), audioPlayHead, numSamples }; for (auto* op : renderOps) op->perform (context); } for (int i = 0; i < buffer.getNumChannels(); ++i) buffer.copyFrom (i, 0, currentAudioOutputBuffer, i, 0, numSamples); midiMessages.clear(); midiMessages.addEvents (currentMidiOutputBuffer, 0, buffer.getNumSamples(), 0); currentAudioInputBuffer = nullptr; }
void SynthesiserVoice::renderNextBlock (AudioBuffer<double>& outputBuffer, int startSample, int numSamples) { AudioBuffer<double> subBuffer (outputBuffer.getArrayOfWritePointers(), outputBuffer.getNumChannels(), startSample, numSamples); tempBuffer.makeCopyOf (subBuffer, true); renderNextBlock (tempBuffer, 0, numSamples); subBuffer.makeCopyOf (tempBuffer, true); }
bool readFromInputDevice (AudioBuffer<float>& inputChannelBuffer, const int numSamples) { jassert (numChannelsRunning <= inputChannelBuffer.getNumChannels()); float* const* const data = inputChannelBuffer.getArrayOfWritePointers(); if (isInterleaved) { scratch.ensureSize ((size_t) ((int) sizeof (float) * numSamples * numChannelsRunning), false); scratch.fillWith (0); // (not clearing this data causes warnings in valgrind) auto num = snd_pcm_readi (handle, scratch.getData(), (snd_pcm_uframes_t) numSamples); if (num < 0) { if (num == -(EPIPE)) overrunCount++; if (JUCE_ALSA_FAILED (snd_pcm_recover (handle, (int) num, 1 /* silent */))) return false; } if (num < numSamples) JUCE_ALSA_LOG ("Did not read all samples: num: " << num << ", numSamples: " << numSamples); for (int i = 0; i < numChannelsRunning; ++i) converter->convertSamples (data[i], 0, scratch.getData(), i, numSamples); } else { auto num = snd_pcm_readn (handle, (void**) data, (snd_pcm_uframes_t) numSamples); if (num < 0) { if (num == -(EPIPE)) overrunCount++; if (JUCE_ALSA_FAILED (snd_pcm_recover (handle, (int) num, 1 /* silent */))) return false; } if (num < numSamples) JUCE_ALSA_LOG ("Did not read all samples: num: " << num << ", numSamples: " << numSamples); for (int i = 0; i < numChannelsRunning; ++i) converter->convertSamples (data[i], data[i], numSamples); } return true; }
bool AudioFormatWriter::writeFromAudioSampleBuffer (const AudioBuffer<float>& source, int startSample, int numSamples) { auto numSourceChannels = source.getNumChannels(); jassert (startSample >= 0 && startSample + numSamples <= source.getNumSamples() && numSourceChannels > 0); if (startSample == 0) return writeFromFloatArrays (source.getArrayOfReadPointers(), numSourceChannels, numSamples); const float* chans[256]; jassert ((int) numChannels < numElementsInArray (chans)); for (int i = 0; i < numSourceChannels; ++i) chans[i] = source.getReadPointer (i, startSample); chans[numSourceChannels] = nullptr; return writeFromFloatArrays (chans, numSourceChannels, numSamples); }
// copy data from internal audio buffer to external audio buffer void AverageLevelFiltered::copyTo( AudioBuffer<float> &destination, const int numberOfSamples) { jassert(fftSampleBuffer_.getNumChannels() == destination.getNumChannels()); jassert(fftSampleBuffer_.getNumSamples() >= numberOfSamples); jassert(destination.getNumSamples() >= numberOfSamples); int numberOfChannels = fftSampleBuffer_.getNumChannels(); // copy data to external buffer for (int channel = 0; channel < numberOfChannels; ++channel) { destination.copyFrom(channel, 0, fftSampleBuffer_, channel, 0, numberOfSamples); } }
// copy data from external audio buffer to internal audio buffer void AverageLevelFiltered::copyFrom( const AudioBuffer<float> &source, const int numberOfSamples) { jassert(fftSampleBuffer_.getNumChannels() == source.getNumChannels()); jassert(fftSampleBuffer_.getNumSamples() == numberOfSamples); int numberOfChannels = fftSampleBuffer_.getNumChannels(); // copy data to internal buffer for (int channel = 0; channel < numberOfChannels; ++channel) { fftSampleBuffer_.copyFrom(channel, 0, source, channel, 0, numberOfSamples); } // calculate loudness for all channels calculateLoudness(); }
//============================================================================== bool writeToOutputDevice (AudioBuffer<float>& outputChannelBuffer, const int numSamples) { jassert (numChannelsRunning <= outputChannelBuffer.getNumChannels()); float* const* const data = outputChannelBuffer.getArrayOfWritePointers(); snd_pcm_sframes_t numDone = 0; if (isInterleaved) { scratch.ensureSize ((size_t) ((int) sizeof (float) * numSamples * numChannelsRunning), false); for (int i = 0; i < numChannelsRunning; ++i) converter->convertSamples (scratch.getData(), i, data[i], 0, numSamples); numDone = snd_pcm_writei (handle, scratch.getData(), (snd_pcm_uframes_t) numSamples); } else { for (int i = 0; i < numChannelsRunning; ++i) converter->convertSamples (data[i], data[i], numSamples); numDone = snd_pcm_writen (handle, (void**) data, (snd_pcm_uframes_t) numSamples); } if (numDone < 0) { if (numDone == -(EPIPE)) underrunCount++; if (JUCE_ALSA_FAILED (snd_pcm_recover (handle, (int) numDone, 1 /* silent */))) return false; } if (numDone < numSamples) JUCE_ALSA_LOG ("Did not write all samples: numDone: " << numDone << ", numSamples: " << numSamples); return true; }