Managed to get Synthesiser to work. Several notes can be played at the same time.
I am publishing it in the hope that it can help someone figure it out. To test, I put the sound on the first and two keys on the keyboard in Init() method:
void NewProjectAudioProcessor::Init()
{
formatManager.registerBasicFormats();
AudioFormatReader* audioReader = formatManager.createReaderFor(juce::File(juce::String("Samples/test1.wav"))); //in DAW .exe folder or plugin .vst3 folder
AudioFormatReader* audioReader2 = formatManager.createReaderFor(juce::File(juce::String("Samples/test2.wav"))); //in DAW .exe folder or plugin .vst3 folder
if (audioReader != nullptr)
{
synth.clearSounds();
for (auto i = 0; i < 4; ++i)
synth.addVoice(new SamplerVoice());
BigInteger usedNotes;
usedNotes.setRange(0, 1, true);
synth.addSound(new SamplerSound("demo sound", *audioReader, usedNotes, 0, 0.0f, 0.0f, 999.0));
BigInteger usedNotes2;
usedNotes2.setRange(1, 1, true);
synth.addSound(new SamplerSound("demo sound", *audioReader2, usedNotes2, 1, 0.0f, 0.0f, 999.0));
}
}
#include "PluginProcessor.h"
#include "PluginEditor.h"
//==============================================================================
NewProjectAudioProcessor::NewProjectAudioProcessor()
#ifndef JucePlugin_PreferredChannelConfigurations
: AudioProcessor (BusesProperties()
#if ! JucePlugin_IsMidiEffect
#if ! JucePlugin_IsSynth
.withInput ("Input", AudioChannelSet::stereo(), true)
#endif
.withOutput ("Output", AudioChannelSet::stereo(), true)
#endif
)
#endif
{
Init();
}
void NewProjectAudioProcessor::Init()
{
formatManager.registerBasicFormats();
AudioFormatReader* audioReader = formatManager.createReaderFor(juce::File(juce::String("Samples/test1.wav"))); //in DAW .exe folder or plugin .vst3 folder
AudioFormatReader* audioReader2 = formatManager.createReaderFor(juce::File(juce::String("Samples/test2.wav"))); //in DAW .exe folder or plugin .vst3 folder
if (audioReader != nullptr)
{
synth.clearSounds();
for (auto i = 0; i < 4; ++i)
synth.addVoice(new SamplerVoice());
BigInteger usedNotes;
usedNotes.setRange(0, 1, true);
synth.addSound(new SamplerSound("demo sound", *audioReader, usedNotes, 0, 0.0f, 0.0f, 999.0));
BigInteger usedNotes2;
usedNotes2.setRange(1, 1, true);
synth.addSound(new SamplerSound("demo sound", *audioReader2, usedNotes2, 1, 0.0f, 0.0f, 999.0));
}
}
NewProjectAudioProcessor::~NewProjectAudioProcessor()
{
}
//==============================================================================
const String NewProjectAudioProcessor::getName() const
{
return JucePlugin_Name;
}
bool NewProjectAudioProcessor::acceptsMidi() const
{
#if JucePlugin_WantsMidiInput
return true;
#else
return false;
#endif
}
bool NewProjectAudioProcessor::producesMidi() const
{
#if JucePlugin_ProducesMidiOutput
return true;
#else
return false;
#endif
}
bool NewProjectAudioProcessor::isMidiEffect() const
{
#if JucePlugin_IsMidiEffect
return true;
#else
return false;
#endif
}
double NewProjectAudioProcessor::getTailLengthSeconds() const
{
return 0.0;
}
int NewProjectAudioProcessor::getNumPrograms()
{
return 1;
}
int NewProjectAudioProcessor::getCurrentProgram()
{
return 0;
}
void NewProjectAudioProcessor::setCurrentProgram (int index)
{
}
const String NewProjectAudioProcessor::getProgramName (int index)
{
return {};
}
void NewProjectAudioProcessor::changeProgramName (int index, const String& newName)
{
}
//==============================================================================
void NewProjectAudioProcessor::prepareToPlay (double sampleRate, int samplesPerBlock)
{
synth.setCurrentPlaybackSampleRate(sampleRate);
}
void NewProjectAudioProcessor::releaseResources()
{
}
#ifndef JucePlugin_PreferredChannelConfigurations
bool NewProjectAudioProcessor::isBusesLayoutSupported (const BusesLayout& layouts) const
{
#if JucePlugin_IsMidiEffect
juce::ignoreUnused (layouts);
return true;
#else
if (layouts.getMainOutputChannelSet() != juce::AudioChannelSet::mono()
&& layouts.getMainOutputChannelSet() != juce::AudioChannelSet::stereo())
return false;
#if ! JucePlugin_IsSynth
if (layouts.getMainOutputChannelSet() != layouts.getMainInputChannelSet())
return false;
#endif
return true;
#endif
}
#endif
void NewProjectAudioProcessor::processBlock (AudioBuffer<float>& buffer, 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());
}
}
}
void NewProjectAudioProcessor::PlaySound()
{
}
//==============================================================================
bool NewProjectAudioProcessor::hasEditor() const
{
return true;
}
AudioProcessorEditor* NewProjectAudioProcessor::createEditor()
{
return new NewProjectAudioProcessorEditor (*this);
}
//==============================================================================
void NewProjectAudioProcessor::getStateInformation (MemoryBlock& destData)
{
}
void NewProjectAudioProcessor::setStateInformation (const void* data, int sizeInBytes)
{
}
//==============================================================================
// This creates new instances of the plugin..
AudioProcessor* JUCE_CALLTYPE createPluginFilter()
{
return new NewProjectAudioProcessor();
}
#pragma once
#include <JuceHeader.h>
using namespace juce;
//==============================================================================
/**
*/
class NewProjectAudioProcessor : public AudioProcessor
{
public:
//==============================================================================
NewProjectAudioProcessor();
~NewProjectAudioProcessor() override;
//==============================================================================
void prepareToPlay (double sampleRate, int samplesPerBlock) override;
void releaseResources() override;
#ifndef JucePlugin_PreferredChannelConfigurations
bool isBusesLayoutSupported (const BusesLayout& layouts) const override;
#endif
void processBlock (AudioBuffer<float>&, MidiBuffer&) override;
//==============================================================================
AudioProcessorEditor* createEditor() override;
bool hasEditor() const override;
//==============================================================================
const 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 String getProgramName (int index) override;
void changeProgramName (int index, const String& newName) override;
//==============================================================================
void getStateInformation (MemoryBlock& destData) override;
void setStateInformation (const void* data, int sizeInBytes) override;
void Init();
void PlaySound();
AudioFormatManager formatManager;
Synthesiser synth;
private:
//==============================================================================
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (NewProjectAudioProcessor)
};