i’ve followed both the audio programmers videos on this topic, as well as scoured this forum. I’ve attempted to follow the tutorials on this website but I think they must be written for an older version of juce because they don’t make any sense.
I just want to load a sound and play it. How could it be this difficult?
here is my code, please enlighten me. I must be missing something obvious.
pluginprocessor.cpp
#include "PluginProcessor.h"
#include "PluginEditor.h"
//==============================================================================
MinimalsamplerAudioProcessor::MinimalsamplerAudioProcessor()
#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
)
#endif
{
init();
}
void MinimalsamplerAudioProcessor::init() {
formatmanager.registerBasicFormats();
juce::AudioFormatReader* reader = formatmanager.createReaderFor(juce::File(juce::String("audio.wav"))); //tried loading "audio.wav" in the same folder as the DAW .exe, as well as using a universal path, neither work :(
if (reader != nullptr) {
synth.clearSounds();
for (auto i = 0; i < 4; ++i) {
synth.addVoice(new juce::SamplerVoice());
}
juce::BigInteger range;
range.setRange(0, 1, true);
synth.addSound(new juce::SamplerSound("sample", *reader, range, 0, 0.0f, 0.0f, 999.0));
}
}
MinimalsamplerAudioProcessor::~MinimalsamplerAudioProcessor()
{
}
//==============================================================================
const juce::String MinimalsamplerAudioProcessor::getName() const
{
return JucePlugin_Name;
}
bool MinimalsamplerAudioProcessor::acceptsMidi() const
{
#if JucePlugin_WantsMidiInput
return true;
#else
return false;
#endif
}
bool MinimalsamplerAudioProcessor::producesMidi() const
{
#if JucePlugin_ProducesMidiOutput
return true;
#else
return false;
#endif
}
bool MinimalsamplerAudioProcessor::isMidiEffect() const
{
#if JucePlugin_IsMidiEffect
return true;
#else
return false;
#endif
}
double MinimalsamplerAudioProcessor::getTailLengthSeconds() const
{
return 0.0;
}
int MinimalsamplerAudioProcessor::getNumPrograms()
{
return 1; // NB: some hosts don't cope very well if you tell them there are 0 programs,
// so this should be at least 1, even if you're not really implementing programs.
}
int MinimalsamplerAudioProcessor::getCurrentProgram()
{
return 0;
}
void MinimalsamplerAudioProcessor::setCurrentProgram (int index)
{
}
const juce::String MinimalsamplerAudioProcessor::getProgramName (int index)
{
return {};
}
void MinimalsamplerAudioProcessor::changeProgramName (int index, const juce::String& newName)
{
}
//==============================================================================
void MinimalsamplerAudioProcessor::prepareToPlay (double sampleRate, int samplesPerBlock)
{
synth.setCurrentPlaybackSampleRate(sampleRate);
}
void MinimalsamplerAudioProcessor::releaseResources()
{
// When playback stops, you can use this as an opportunity to free up any
// spare memory, etc.
}
#ifndef JucePlugin_PreferredChannelConfigurations
bool MinimalsamplerAudioProcessor::isBusesLayoutSupported (const BusesLayout& layouts) const
{
#if JucePlugin_IsMidiEffect
juce::ignoreUnused (layouts);
return true;
#else
// This is the place where you check if the layout is supported.
// In this template code we only support mono or stereo.
// Some plugin hosts, such as certain GarageBand versions, will only
// load plugins that support stereo bus layouts.
if (layouts.getMainOutputChannelSet() != juce::AudioChannelSet::mono()
&& layouts.getMainOutputChannelSet() != juce::AudioChannelSet::stereo())
return false;
// This checks if the input layout matches the output layout
#if ! JucePlugin_IsSynth
if (layouts.getMainOutputChannelSet() != layouts.getMainInputChannelSet())
return false;
#endif
return true;
#endif
}
#endif
void MinimalsamplerAudioProcessor::processBlock (juce::AudioBuffer<float>& buffer, juce::MidiBuffer& midiMessages)
{
synth.renderNextBlock(buffer, midiMessages, 0, buffer.getNumSamples());
for (const auto metadata : midiMessages)
{
auto message = metadata.getMessage();
if (message.isNoteOn())
{
DBG("Note number " << message.getNoteNumber());
}
}
}
//==============================================================================
bool MinimalsamplerAudioProcessor::hasEditor() const
{
return true; // (change this to false if you choose to not supply an editor)
}
juce::AudioProcessorEditor* MinimalsamplerAudioProcessor::createEditor()
{
return new MinimalsamplerAudioProcessorEditor (*this);
}
//==============================================================================
void MinimalsamplerAudioProcessor::getStateInformation (juce::MemoryBlock& destData)
{
// You should use this method to store your parameters in the memory block.
// You could do that either as raw data, or use the XML or ValueTree classes
// as intermediaries to make it easy to save and load complex data.
}
void MinimalsamplerAudioProcessor::setStateInformation (const void* data, int sizeInBytes)
{
// You should use this method to restore your parameters from this memory block,
// whose contents will have been created by the getStateInformation() call.
}
//==============================================================================
// This creates new instances of the plugin..
juce::AudioProcessor* JUCE_CALLTYPE createPluginFilter()
{
return new MinimalsamplerAudioProcessor();
}
pluginprocessor.h
/*
==============================================================================
This file contains the basic framework code for a JUCE plugin processor.
==============================================================================
*/
#pragma once
#include <JuceHeader.h>
//==============================================================================
/**
*/
class MinimalsamplerAudioProcessor : public juce::AudioProcessor
#if JucePlugin_Enable_ARA
, public juce::AudioProcessorARAExtension
#endif
{
public:
//==============================================================================
MinimalsamplerAudioProcessor();
~MinimalsamplerAudioProcessor() override;
//==============================================================================
void prepareToPlay (double sampleRate, int samplesPerBlock) override;
void releaseResources() override;
#ifndef JucePlugin_PreferredChannelConfigurations
bool isBusesLayoutSupported (const BusesLayout& layouts) const override;
#endif
void processBlock (juce::AudioBuffer<float>&, juce::MidiBuffer&) override;
//==============================================================================
juce::AudioProcessorEditor* createEditor() override;
bool hasEditor() const override;
//==============================================================================
const juce::String getName() const override;
bool acceptsMidi() const override;
bool producesMidi() const override;
bool isMidiEffect() const override;
double getTailLengthSeconds() const override;
//==============================================================================
int getNumPrograms() override;
int getCurrentProgram() override;
void setCurrentProgram (int index) override;
const juce::String getProgramName (int index) override;
void changeProgramName (int index, const juce::String& newName) override;
//==============================================================================
void getStateInformation (juce::MemoryBlock& destData) override;
void setStateInformation (const void* data, int sizeInBytes) override;
void init();
juce::AudioFormatManager formatmanager;
juce::Synthesiser synth;
private:
//==============================================================================
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MinimalsamplerAudioProcessor)
};