Audio Plug-in Application Which Plays and Processes A Pre-determined Sound File(Newbie)

I want to create a plug-in which processes a pre-determined audio file instead of sound device (or microphone) inputs. The music starts when the application starts, but plays without the effect. I couldn’t understand how can I redirect this sound.

The code in PluginProcessor.cpp is like:

#include “PluginProcessor.h”
#include “PluginEditor.h”

juce::File& currentDir = juce::File::getCurrentWorkingDirectory();
juce::File& audioFile = currentDir.getChildFile(“audio.mp3”);

TilteqAudioProcessor::TilteqAudioProcessor()
#ifndef JucePlugin_PreferredChannelConfigurations
: AudioProcessor(BusesProperties()
#if ! JucePlugin_IsMidiEffect
#if ! JucePlugin_IsSynth
.withInput(“Input”, juce::AudioChannelSet::stereo(), true)
#endif
.withOutput(“Output”, juce::AudioChannelSet::stereo(), true)
#endif

),
parameters(*this, nullptr)

#endif
{
auto audioFormatManager = new juce::AudioFormatManager();
audioFormatManager->registerBasicFormats();

// Format reader
auto audioFormatReader = audioFormatManager->createReaderFor(audioFile);

// Reader source
auto audioFormatReaderSource = new juce::AudioFormatReaderSource(audioFormatReader, true);


// Transport source
auto audioTransportSource = new juce::AudioTransportSource();
audioTransportSource->setSource(audioFormatReaderSource, 0, nullptr, 44100.0, 2);
audioTransportSource->start();

// Source player
auto audioSourcePlayer = new juce::AudioSourcePlayer();
audioSourcePlayer->setSource(audioTransportSource);

// Device manager
auto audioDeviceManager = new juce::AudioDeviceManager();
audioDeviceManager->initialise(2, 2, 0, true);
audioDeviceManager->addAudioCallback(audioSourcePlayer);


parameters.createAndAddParameter(std::make_unique<juce::AudioParameterFloat>("freq", 
    "Frequency", juce::NormalisableRange<float>(500.0f, 2000.0f, 1.0f, 0.63f), 1000.0f, "Hz"));
parameters.createAndAddParameter(std::make_unique<juce::AudioParameterFloat>("gain", 
    "Gain", juce::NormalisableRange<float>(-6.0f, 6.0f, 0.25f), 0.0f, "dB"));
parameters.state = juce::ValueTree("savedParams");

}

void TilteqAudioProcessor::prepareToPlay(double sampleRate, int samplesPerBlock)
{
tiltEQ.prepare(sampleRate, samplesPerBlock);
tiltEQ.setParameters(parameters);

}

void TilteqAudioProcessor::processBlock(juce::AudioBuffer& buffer, juce::MidiBuffer&)
{

juce::ScopedNoDenormals noDenormals;


for (auto i = getTotalNumInputChannels(); i < getTotalNumOutputChannels(); ++i)
    buffer.clear(i, 0, buffer.getNumSamples());



for (int channel = 0; channel < getTotalNumInputChannels(); ++channel)
{
    auto* channelData = buffer.getWritePointer(channel);
    tiltEQ.process(buffer);
    
    
}

}

Try an audio buffer, with interpolation.

header:

double sampleInc = 0;
double position = 0;
double sampleLength = 0;
juce::AudioBuffer< float> sound;
juce::AudioFormatManager mFormatManager;

constructor or loaded file function:

auto mFormatReader = mFormatManager->createReaderFor(audioFile);
if(mFormatReader != 0){
sampleLength = mFormatReader->lengthInSamples;

sound.setSize(mFormatReader->numChannels, sampleLength);
mFormatReader->read(&sound, 0, sampleLength, 0, true, true);

prepareForPlay

sampleInc = sampleRate/sourceSampleRate;

processBlock

juce::ScopedNoDenormals noDenormals;
buffer.clear();
// get the loaded sound input pointers
const float* readL = sound.getReadPointer(0);
const float* readR;
int numSoundChannels = sound.getNumChannels();
if(numSoundChannels > 1)
readR = buffer.getReadPointer(1);

// get the output buffer pointers
float* outL = sound.getWritePointer(0);
float* outR = outL;
int numChannels = buffer.getNumChannels();
if(numChannels > 1)
outR = buffer.getWritePointer(1);

// process the samples
for(int sample = 0; sample < buffer.getNumSamples(); ++sample){
auto pos = (int) position;
auto alpha = (float) (position - pos);
auto invAlpha = 1.0f - alpha;

float inL = (readL[pos] * invAlpha + readL[pos + 1] * alpha);
float inR = inL;
if(numSoundChannels > 1)
inR = (readL[pos] * invAlpha + readL[pos + 1] * alpha);

position += sampleInc;
if(position > sampleLength -1)
position = 0;

outL[sample] = inL;
if(numChannels > 1)
outR[sample] = inR;
}

You could also do a dryBuffer for reading, and a wetBuffer for printing then export the wetBuffer to a file.