Skip to content

Commit

Permalink
Add built-in SoundFont player instrument (#236)
Browse files Browse the repository at this point in the history
  • Loading branch information
peterrudenko committed Oct 30, 2022
1 parent e4325e2 commit 04c67e4
Show file tree
Hide file tree
Showing 14 changed files with 634 additions and 32 deletions.
14 changes: 14 additions & 0 deletions Source/Core/Audio/BuiltIn/BuiltInSynthsPluginFormat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "BuiltInSynthsPluginFormat.h"
#include "DefaultSynthAudioPlugin.h"
#include "MetronomeSynthAudioPlugin.h"
#include "SoundFontSynthAudioPlugin.h"

const String BuiltInSynthsPluginFormat::formatName = "BuiltIn";
const String BuiltInSynthsPluginFormat::formatIdentifier = "BuiltIn";
Expand All @@ -30,6 +31,9 @@ BuiltInSynthsPluginFormat::BuiltInSynthsPluginFormat()

MetronomeSynthAudioPlugin metronomeAudioPlugin;
metronomeAudioPlugin.fillInPluginDescription(this->metronomeInstrument);

SoundFontSynthAudioPlugin soundFontSynthAudioPlugin;
soundFontSynthAudioPlugin.fillInPluginDescription(this->soundFontPlayerInstrument);
}

String BuiltInSynthsPluginFormat::getName() const
Expand All @@ -47,6 +51,10 @@ void BuiltInSynthsPluginFormat::findAllTypesForFile(OwnedArray<PluginDescription
{
description.add(new PluginDescription(this->metronomeInstrument));
}
else if (id == SoundFontSynthAudioPlugin::instrumentId)
{
description.add(new PluginDescription(this->soundFontPlayerInstrument));
}
}

bool BuiltInSynthsPluginFormat::fileMightContainThisPluginType(const String &fileOrIdentifier)
Expand All @@ -70,5 +78,11 @@ void BuiltInSynthsPluginFormat::createPluginInstance(const PluginDescription &de
return;
}

if (desc.name == this->soundFontPlayerInstrument.name)
{
callback(make<SoundFontSynthAudioPlugin>(), {});
return;
}

callback(nullptr, {});
}
1 change: 1 addition & 0 deletions Source/Core/Audio/BuiltIn/BuiltInSynthsPluginFormat.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,6 @@ class BuiltInSynthsPluginFormat final : public AudioPluginFormat

PluginDescription defaultInstrument;
PluginDescription metronomeInstrument;
PluginDescription soundFontPlayerInstrument;

};
95 changes: 95 additions & 0 deletions Source/Core/Audio/BuiltIn/SoundFont/SoundFontSynth.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -690,6 +690,49 @@ void SoundFontVoice::killNote()
// SoundFontSynth
//===----------------------------------------------------------------------===//

void SoundFontSynth::initSynth(const Parameters &parameters)
{
const File file(parameters.filePath);
if (!file.existsAsFile())
{
return;
}

ScopedLock locker(this->lock);

this->allNotesOff(0, false);

this->clearVoices();
for (int i = SoundFontSynth::numVoices; i --> 0 ;)
{
this->addVoice(new SoundFontVoice());
}

this->clearSounds();

AudioFormatManager audioFormatManager;
audioFormatManager.registerBasicFormats();

if (file.getFullPathName().endsWithIgnoreCase("sf2"))
{
auto sound = make<SoundFont2Sound>(file);
sound->loadRegions();
sound->loadSamples(audioFormatManager);
this->sounds.add(sound.release());
}
else if (file.getFullPathName().endsWithIgnoreCase("sbk"))
{
auto sound = make<SoundFontSound>(file);
sound->loadRegions();
sound->loadSamples(audioFormatManager);
this->sounds.add(sound.release());
}

// new file has been loaded so we need to set the program anyway
jassert(parameters.programIndex < this->getNumPrograms());
this->setCurrentProgram(parameters.programIndex);
}

void SoundFontSynth::noteOn(int midiChannel, int midiNoteNumber, float velocity)
{
int i;
Expand Down Expand Up @@ -837,3 +880,55 @@ void SoundFontSynth::changeProgramName(int index, const String &newName)
{
jassertfalse;
}

//===----------------------------------------------------------------------===//
// Synth parameters
//===----------------------------------------------------------------------===//

SoundFontSynth::Parameters SoundFontSynth::Parameters::withSoundFontFile(const String &newFilePath) const noexcept
{
Parameters other(*this);
other.filePath = newFilePath;
return other;
}

SoundFontSynth::Parameters SoundFontSynth::Parameters::withProgramIndex(int newProgramIndex) const noexcept
{
Parameters other(*this);
other.programIndex = newProgramIndex;
return other;
}

SerializedData SoundFontSynth::Parameters::serialize() const
{
using namespace Serialization::Audio;

SerializedData data(SoundFont::soundFontConfig);
data.setProperty(SoundFont::filePath, this->filePath);
data.setProperty(SoundFont::programIndex, this->programIndex);

return data;
}

void SoundFontSynth::Parameters::deserialize(const SerializedData &data)
{
this->reset();
using namespace Serialization::Audio;

const auto root = data.hasType(SoundFont::soundFontConfig) ?
data : data.getChildWithName(SoundFont::soundFontConfig);

if (!root.isValid())
{
return;
}

this->filePath = root.getProperty(SoundFont::filePath);
this->programIndex = root.getProperty(SoundFont::programIndex);
}

void SoundFontSynth::Parameters::reset()
{
this->filePath.clear();
this->programIndex = 0;
}
20 changes: 20 additions & 0 deletions Source/Core/Audio/BuiltIn/SoundFont/SoundFontSynth.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,24 @@ class SoundFontSynth final : public Synthesiser
void noteOn(int midiChannel, int midiNoteNumber, float velocity) override;
void noteOff(int midiChannel, int midiNoteNumber, float velocity, bool allowTailOff) override;

//===------------------------------------------------------------------===//
// Synth parameters
//===------------------------------------------------------------------===//

struct Parameters final : Serializable
{
String filePath;
int programIndex = 0;

Parameters withSoundFontFile(const String &newFilePath) const noexcept;
Parameters withProgramIndex(int newProgramIndex) const noexcept;

SerializedData serialize() const override;
void deserialize(const SerializedData &data) override;
void reset() override;
};

void initSynth(const Parameters &parameters);

//===------------------------------------------------------------------===//
// Presets
Expand All @@ -43,6 +61,8 @@ class SoundFontSynth final : public Synthesiser

private:

static constexpr auto numVoices = 16;

int noteVelocities[128];

JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(SoundFontSynth)
Expand Down
Loading

0 comments on commit 04c67e4

Please sign in to comment.