Skip to content

Instantly share code, notes, and snippets.

@Xenakios
Created December 23, 2024 21:23
Show Gist options
  • Save Xenakios/246b6d78066469dc102ce83bf36ca40e to your computer and use it in GitHub Desktop.
Save Xenakios/246b6d78066469dc102ce83bf36ca40e to your computer and use it in GitHub Desktop.
inline juce::String renderFile(juce::File inputFile, juce::File outputFile)
{
if (!outputFile.hasFileExtension(("wav")))
return "output file must be .wav";
juce::AudioFormatManager fman;
fman.registerBasicFormats();
auto reader = std::unique_ptr<juce::AudioFormatReader>(fman.createReaderFor(inputFile));
if (reader == nullptr)
return "could not create input file reader";
juce::WavAudioFormat wavformat;
if (outputFile.existsAsFile())
outputFile.deleteFile();
auto ostream = outputFile.createOutputStream();
// writer is for .wav, 32 bit float samples and the same channel count and sample rate as the
// input file
auto writer = std::unique_ptr<juce::AudioFormatWriter>(wavformat.createWriterFor(
ostream.release(), reader->sampleRate, reader->numChannels, 32, {}, 0));
if (!writer)
return "could not create output file writer";
uint32_t processingBufferSize = 4096;
juce::dsp::Chorus<float> chorus;
juce::dsp::ProcessSpec spec{reader->sampleRate, processingBufferSize, reader->numChannels};
chorus.prepare(spec);
juce::AudioBuffer<float> processingBuffer(reader->numChannels, processingBufferSize);
processingBuffer.clear();
auto outsampleCounter = 0;
while (outsampleCounter < reader->lengthInSamples)
{
// process exact number of samples
int samplesToProcess =
std::min<int64_t>(processingBufferSize, reader->lengthInSamples - outsampleCounter);
processingBuffer.setSize(processingBuffer.getNumChannels(), samplesToProcess, false, true,
true);
reader->read(&processingBuffer, 0, samplesToProcess, outsampleCounter, true, true);
// do our actual processing of the buffer
juce::dsp::AudioBlock<float> block(processingBuffer);
juce::dsp::ProcessContextReplacing<float> ctx(block);
chorus.process(ctx);
// write to output file
writer->writeFromAudioSampleBuffer(processingBuffer, 0, samplesToProcess);
outsampleCounter += processingBufferSize;
}
return "rendered ok";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment