Created
December 20, 2020 22:38
-
-
Save Xenakios/8d94c4e85ff69c106962ed198076233d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <JuceHeader.h> | |
class ConvolutionSource : public juce::AudioSource | |
{ | |
public: | |
ConvolutionSource(juce::File inputfile) | |
{ | |
juce::AudioFormatManager fman; | |
fman.registerBasicFormats(); | |
auto reader = fman.createReaderFor(inputfile); | |
if (reader) | |
{ | |
m_readersource = std::make_unique<juce::AudioFormatReaderSource>(reader,true); | |
} | |
} | |
void prepareToPlay(int samplesPerBlockExpected, double sampleRate) override | |
{ | |
if (!m_readersource) | |
return; | |
m_readersource->prepareToPlay(samplesPerBlockExpected, sampleRate); | |
juce::dsp::ProcessSpec spec; | |
spec.maximumBlockSize = samplesPerBlockExpected; | |
spec.numChannels = 2; | |
spec.sampleRate = sampleRate; | |
m_convolution.prepare(spec); | |
juce::File irfile(juce::File("/Users/name/Downloads/11082020/IMreverbs/In The Silo.wav")); | |
m_convolution.loadImpulseResponse(irfile, juce::dsp::Convolution::Stereo::yes, | |
juce::dsp::Convolution::Trim::no,44100, | |
juce::dsp::Convolution::Normalise::no); | |
} | |
void releaseResources() override {} | |
void getNextAudioBlock(const juce::AudioSourceChannelInfo &bufferToFill) override | |
{ | |
if (!m_readersource) | |
return; | |
m_readersource->getNextAudioBlock(bufferToFill); | |
juce::dsp::AudioBlock<float> block(*bufferToFill.buffer); | |
m_convolution.process(juce::dsp::ProcessContextReplacing<float>(block)); | |
bufferToFill.buffer->applyGain(0.01f); | |
} | |
bool finishedPlaying() | |
{ | |
if (!m_readersource) | |
return true; | |
return m_readersource->getNextReadPosition()>=m_readersource->getTotalLength(); | |
} | |
private: | |
std::unique_ptr<juce::AudioFormatReaderSource> m_readersource; | |
juce::dsp::Convolution m_convolution; | |
}; | |
//============================================================================== | |
int main (int argc, char* argv[]) | |
{ | |
juce::ScopedJuceInitialiser_GUI mm; | |
juce::AudioDeviceManager aman; | |
aman.initialiseWithDefaultDevices(0, 2); | |
juce::AudioSourcePlayer aplayer; | |
ConvolutionSource convsource(juce::File("/Users/name/AudioProjects/sourcesamples/count.wav")); | |
aplayer.setSource(&convsource); | |
aman.addAudioCallback(&aplayer); | |
while (!convsource.finishedPlaying()) | |
{ | |
juce::Thread::sleep(10); | |
} | |
aman.removeAudioCallback(&aplayer); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment