2018-02-27 08:25:20 +08:00
|
|
|
//
|
|
|
|
// Created by Alex Birch on 10/09/2017.
|
|
|
|
//
|
|
|
|
|
|
|
|
#include <iostream>
|
2019-07-07 07:22:47 +08:00
|
|
|
#include <fluidsynth.h>
|
2018-02-27 08:25:20 +08:00
|
|
|
#include "FluidSynthModel.h"
|
2019-06-24 02:40:36 +08:00
|
|
|
#include "MidiConstants.h"
|
2019-07-07 07:22:47 +08:00
|
|
|
#include "Util.h"
|
2018-02-27 08:25:20 +08:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
2019-07-07 07:22:47 +08:00
|
|
|
FluidSynthModel::FluidSynthModel(
|
2019-07-11 06:13:58 +08:00
|
|
|
AudioProcessorValueTreeState& valueTreeState
|
|
|
|
// ValueTree& valueTree
|
2019-07-08 00:35:31 +08:00
|
|
|
// SharesParams& sharedParams
|
2019-07-07 07:22:47 +08:00
|
|
|
)
|
|
|
|
: valueTreeState{valueTreeState}
|
2019-07-11 06:13:58 +08:00
|
|
|
// , valueTree{valueTree}
|
2019-07-08 00:35:31 +08:00
|
|
|
// , sharedParams{sharedParams}
|
2019-07-07 07:22:47 +08:00
|
|
|
//, synth{nullptr}
|
|
|
|
, settings{nullptr, nullptr}
|
|
|
|
, currentSoundFontAbsPath{}
|
|
|
|
, currentSampleRate{44100}
|
|
|
|
, initialised{false}
|
|
|
|
, sfont_id{0}
|
|
|
|
, channel{0}/*,
|
|
|
|
mod(nullptr)*/
|
2019-07-08 00:35:31 +08:00
|
|
|
{
|
2019-07-11 06:52:15 +08:00
|
|
|
valueTreeState.state.addListener(this);
|
2019-07-08 00:35:31 +08:00
|
|
|
}
|
2018-04-10 07:51:21 +08:00
|
|
|
|
2019-07-08 00:35:31 +08:00
|
|
|
FluidSynthModel::~FluidSynthModel() {
|
2019-07-11 06:52:15 +08:00
|
|
|
valueTreeState.state.removeListener(this);
|
2019-07-07 07:22:47 +08:00
|
|
|
// if (initialised) {
|
2018-02-27 08:25:20 +08:00
|
|
|
// delete_fluid_audio_driver(driver);
|
2019-07-07 07:22:47 +08:00
|
|
|
// delete_fluid_synth(synth);
|
|
|
|
// delete_fluid_settings(settings);
|
2018-02-27 08:25:20 +08:00
|
|
|
// delete driver;
|
|
|
|
// delete settings;
|
2019-06-24 02:40:36 +08:00
|
|
|
// delete_fluid_mod(mod);
|
2019-07-07 07:22:47 +08:00
|
|
|
// }
|
2019-07-08 00:35:31 +08:00
|
|
|
}
|
2019-07-07 07:22:47 +08:00
|
|
|
|
|
|
|
int FluidSynthModel::handleMidiEvent(void* data, fluid_midi_event_t* event)
|
|
|
|
{
|
|
|
|
// DEBUG_PRINT(fluid_midi_event_get_type(event));
|
|
|
|
// printf("event type: %d\n", fluid_midi_event_get_type(event));
|
|
|
|
return 0;
|
2018-02-27 08:25:20 +08:00
|
|
|
}
|
|
|
|
|
2018-04-10 08:20:23 +08:00
|
|
|
void FluidSynthModel::initialise() {
|
2018-02-28 07:42:16 +08:00
|
|
|
// if (initialised) {
|
|
|
|
// delete_fluid_synth(synth);
|
|
|
|
// delete_fluid_settings(settings);
|
|
|
|
// }
|
2019-07-07 07:22:47 +08:00
|
|
|
settings = { new_fluid_settings(), delete_fluid_settings };
|
|
|
|
|
2018-04-18 05:10:01 +08:00
|
|
|
// deactivate all audio drivers in fluidsynth to avoid FL Studio deadlock when initialising CoreAudio
|
|
|
|
// after all: we only use fluidsynth to render blocks of audio. it doesn't output to audio driver.
|
2019-07-07 07:22:47 +08:00
|
|
|
const char *DRV[] {NULL};
|
2018-04-18 05:10:01 +08:00
|
|
|
fluid_audio_driver_register(DRV);
|
2019-07-07 07:22:47 +08:00
|
|
|
|
|
|
|
// handle_midi_event_func_t handler = [](void* data, fluid_midi_event_t* event) -> int {
|
|
|
|
//
|
|
|
|
// };
|
|
|
|
|
|
|
|
// midiDriver = unique_ptr<fluid_midi_driver_t, decltype(&delete_fluid_midi_driver)>(
|
|
|
|
// new_fluid_midi_driver(
|
|
|
|
// settings.get(),
|
|
|
|
// [](void* data, fluid_midi_event_t* event) -> int {
|
|
|
|
//
|
|
|
|
// },
|
|
|
|
// nullptr),
|
|
|
|
// delete_fluid_midi_driver);
|
2018-04-18 05:10:01 +08:00
|
|
|
|
2019-07-07 07:22:47 +08:00
|
|
|
|
2018-02-27 08:25:20 +08:00
|
|
|
// https://sourceforge.net/p/fluidsynth/wiki/FluidSettings/
|
2019-07-02 04:15:33 +08:00
|
|
|
#if JUCE_DEBUG
|
2019-07-07 07:22:47 +08:00
|
|
|
fluid_settings_setint(settings.get(), "synth.verbose", 1);
|
2019-07-02 04:15:33 +08:00
|
|
|
#endif
|
2018-02-27 08:25:20 +08:00
|
|
|
|
2019-07-07 07:22:47 +08:00
|
|
|
synth = { new_fluid_synth(settings.get()), delete_fluid_synth };
|
|
|
|
fluid_synth_set_sample_rate(synth.get(), currentSampleRate);
|
|
|
|
|
2019-07-08 00:35:31 +08:00
|
|
|
// valueTreeState.getParameter("soundFontPath")->getValue();
|
2019-07-07 07:22:47 +08:00
|
|
|
// RangedAudioParameter *param {valueTreeState.getParameter("release")};
|
|
|
|
// jassert(dynamic_cast<AudioParameterInt*> (param) != nullptr);
|
|
|
|
// AudioParameterInt* castParam {dynamic_cast<AudioParameterInt*> (param)};
|
|
|
|
// *castParam = m.getControllerValue();
|
2019-07-08 00:35:31 +08:00
|
|
|
|
|
|
|
// if (sharedParams.getSoundFontPath().isNotEmpty()) {
|
|
|
|
// loadFont(sharedParams.getSoundFontPath());
|
2019-07-07 07:22:47 +08:00
|
|
|
// changePreset(sharedParams->getBank(), sharedParams->getPreset());
|
2019-07-08 00:35:31 +08:00
|
|
|
// }
|
2018-02-27 08:25:20 +08:00
|
|
|
|
2019-07-07 07:22:47 +08:00
|
|
|
fluid_synth_set_gain(synth.get(), 2.0);
|
2019-07-01 02:25:02 +08:00
|
|
|
|
|
|
|
for(int i{SOUND_CTRL1}; i <= SOUND_CTRL10; i++)
|
|
|
|
{
|
2019-07-01 04:40:24 +08:00
|
|
|
setControllerValue(i, 0);
|
2019-07-01 02:25:02 +08:00
|
|
|
}
|
2018-02-27 08:25:20 +08:00
|
|
|
|
|
|
|
// fluid_synth_bank_select(synth, 0, 3);
|
|
|
|
|
|
|
|
// fluid_handle_inst
|
|
|
|
|
|
|
|
// driver = new_fluid_audio_driver(settings, synth);
|
|
|
|
|
|
|
|
// changePreset(128, 13);
|
2019-06-23 05:59:28 +08:00
|
|
|
|
2019-06-30 05:10:28 +08:00
|
|
|
// float env_amount(12000.0f);
|
|
|
|
|
|
|
|
// http://www.synthfont.com/SoundFont_NRPNs.PDF
|
|
|
|
float env_amount(20000.0f);
|
2019-07-01 02:25:02 +08:00
|
|
|
// float env_amount(24000.0f);
|
2019-06-23 05:59:28 +08:00
|
|
|
|
2019-07-01 02:25:02 +08:00
|
|
|
// note: fluid_chan.c#fluid_channel_init_ctrl()
|
|
|
|
// all SOUND_CTRL are inited with value of 64, not zero.
|
|
|
|
// "Just like panning, a value of 64 indicates no change for sound ctrls"
|
2019-06-24 02:40:36 +08:00
|
|
|
|
2019-07-07 07:22:47 +08:00
|
|
|
unique_ptr<fluid_mod_t, decltype(&delete_fluid_mod)> mod{new_fluid_mod(), delete_fluid_mod};
|
2019-07-01 02:25:02 +08:00
|
|
|
//
|
2019-07-07 07:22:47 +08:00
|
|
|
fluid_mod_set_source1(mod.get(),
|
2019-06-24 02:40:36 +08:00
|
|
|
static_cast<int>(SOUND_CTRL2), // MIDI CC 71 Timbre/Harmonic Intensity (filter resonance)
|
|
|
|
FLUID_MOD_CC
|
|
|
|
| FLUID_MOD_UNIPOLAR
|
|
|
|
| FLUID_MOD_CONCAVE
|
2019-06-30 05:10:28 +08:00
|
|
|
| FLUID_MOD_POSITIVE);
|
2019-07-07 07:22:47 +08:00
|
|
|
fluid_mod_set_source2(mod.get(), 0, 0);
|
|
|
|
fluid_mod_set_dest(mod.get(), GEN_FILTERQ);
|
|
|
|
fluid_mod_set_amount(mod.get(), FLUID_PEAK_ATTENUATION);
|
|
|
|
fluid_synth_add_default_mod(synth.get(), mod.get(), FLUID_SYNTH_ADD);
|
2019-06-24 02:40:36 +08:00
|
|
|
|
2019-07-07 07:22:47 +08:00
|
|
|
mod = {new_fluid_mod(), delete_fluid_mod};
|
|
|
|
fluid_mod_set_source1(mod.get(),
|
2019-06-24 02:40:36 +08:00
|
|
|
static_cast<int>(SOUND_CTRL3), // MIDI CC 72 Release time
|
|
|
|
FLUID_MOD_CC
|
2019-06-30 05:10:28 +08:00
|
|
|
| FLUID_MOD_UNIPOLAR
|
|
|
|
| FLUID_MOD_LINEAR
|
2019-06-24 02:40:36 +08:00
|
|
|
| FLUID_MOD_POSITIVE);
|
2019-07-07 07:22:47 +08:00
|
|
|
fluid_mod_set_source2(mod.get(), 0, 0);
|
|
|
|
fluid_mod_set_dest(mod.get(), GEN_VOLENVRELEASE);
|
|
|
|
// fluid_mod_set_amount(mod.get(), 15200.0f);
|
|
|
|
fluid_mod_set_amount(mod.get(), env_amount);
|
|
|
|
fluid_synth_add_default_mod(synth.get(), mod.get(), FLUID_SYNTH_ADD);
|
2019-06-24 02:40:36 +08:00
|
|
|
|
2019-07-07 07:22:47 +08:00
|
|
|
mod = {new_fluid_mod(), delete_fluid_mod};
|
|
|
|
fluid_mod_set_source1(mod.get(),
|
2019-06-24 02:40:36 +08:00
|
|
|
static_cast<int>(SOUND_CTRL4), // MIDI CC 73 Attack time
|
|
|
|
FLUID_MOD_CC
|
2019-06-30 05:10:28 +08:00
|
|
|
| FLUID_MOD_UNIPOLAR
|
|
|
|
| FLUID_MOD_LINEAR
|
2019-06-24 02:40:36 +08:00
|
|
|
| FLUID_MOD_POSITIVE);
|
2019-07-07 07:22:47 +08:00
|
|
|
fluid_mod_set_source2(mod.get(), 0, 0);
|
|
|
|
fluid_mod_set_dest(mod.get(), GEN_VOLENVATTACK);
|
|
|
|
fluid_mod_set_amount(mod.get(), env_amount);
|
|
|
|
fluid_synth_add_default_mod(synth.get(), mod.get(), FLUID_SYNTH_ADD);
|
2019-06-23 05:59:28 +08:00
|
|
|
|
2019-07-01 02:25:02 +08:00
|
|
|
// soundfont spec says that if cutoff is >20kHz and resonance Q is 0, then no filtering occurs
|
2019-07-07 07:22:47 +08:00
|
|
|
mod = {new_fluid_mod(), delete_fluid_mod};
|
|
|
|
fluid_mod_set_source1(mod.get(),
|
2019-06-24 02:40:36 +08:00
|
|
|
static_cast<int>(SOUND_CTRL5), // MIDI CC 74 Brightness (cutoff frequency, FILTERFC)
|
|
|
|
FLUID_MOD_CC
|
2019-06-30 05:10:28 +08:00
|
|
|
| FLUID_MOD_LINEAR
|
2019-06-24 02:40:36 +08:00
|
|
|
| FLUID_MOD_UNIPOLAR
|
|
|
|
| FLUID_MOD_POSITIVE);
|
2019-07-07 07:22:47 +08:00
|
|
|
fluid_mod_set_source2(mod.get(), 0, 0);
|
|
|
|
fluid_mod_set_dest(mod.get(), GEN_FILTERFC);
|
|
|
|
fluid_mod_set_amount(mod.get(), -2400.0f);
|
|
|
|
fluid_synth_add_default_mod(synth.get(), mod.get(), FLUID_SYNTH_ADD);
|
2019-06-23 05:59:28 +08:00
|
|
|
|
2019-07-07 07:22:47 +08:00
|
|
|
mod = {new_fluid_mod(), delete_fluid_mod};
|
|
|
|
fluid_mod_set_source1(mod.get(),
|
2019-06-24 02:40:36 +08:00
|
|
|
static_cast<int>(SOUND_CTRL6), // MIDI CC 75 Decay Time
|
|
|
|
FLUID_MOD_CC
|
2019-06-30 05:10:28 +08:00
|
|
|
| FLUID_MOD_UNIPOLAR
|
|
|
|
| FLUID_MOD_LINEAR
|
2019-06-24 02:40:36 +08:00
|
|
|
| FLUID_MOD_POSITIVE);
|
2019-07-07 07:22:47 +08:00
|
|
|
fluid_mod_set_source2(mod.get(), 0, 0);
|
|
|
|
fluid_mod_set_dest(mod.get(), GEN_VOLENVDECAY);
|
|
|
|
fluid_mod_set_amount(mod.get(), env_amount);
|
|
|
|
fluid_synth_add_default_mod(synth.get(), mod.get(), FLUID_SYNTH_ADD);
|
2019-07-01 02:25:02 +08:00
|
|
|
|
2019-07-07 07:22:47 +08:00
|
|
|
mod = {new_fluid_mod(), delete_fluid_mod};
|
|
|
|
fluid_mod_set_source1(mod.get(),
|
2019-07-01 02:25:02 +08:00
|
|
|
static_cast<int>(SOUND_CTRL10), // MIDI CC 79 undefined
|
|
|
|
FLUID_MOD_CC
|
|
|
|
| FLUID_MOD_UNIPOLAR
|
2019-07-01 04:40:24 +08:00
|
|
|
| FLUID_MOD_CONCAVE
|
2019-07-01 02:25:02 +08:00
|
|
|
| FLUID_MOD_POSITIVE);
|
2019-07-07 07:22:47 +08:00
|
|
|
fluid_mod_set_source2(mod.get(), 0, 0);
|
|
|
|
fluid_mod_set_dest(mod.get(), GEN_VOLENVSUSTAIN);
|
2019-07-01 02:25:02 +08:00
|
|
|
// fluice_voice.c#fluid_voice_update_param()
|
|
|
|
// clamps the range to between 0 and 1000, so we'll copy that
|
2019-07-07 07:22:47 +08:00
|
|
|
fluid_mod_set_amount(mod.get(), 1000.0f);
|
|
|
|
fluid_synth_add_default_mod(synth.get(), mod.get(), FLUID_SYNTH_ADD);
|
2019-07-08 00:35:31 +08:00
|
|
|
|
2019-07-11 06:52:15 +08:00
|
|
|
valueTreeState.state.getChildWithName("soundFont").sendPropertyChangeMessage("path");
|
2019-07-10 03:44:26 +08:00
|
|
|
// valueTree.sendPropertyChangeMessage("soundFontPath");
|
2018-06-23 04:32:16 +08:00
|
|
|
|
2019-07-07 07:22:47 +08:00
|
|
|
// initialised = true;
|
2018-02-27 08:25:20 +08:00
|
|
|
}
|
|
|
|
|
2019-07-08 00:35:31 +08:00
|
|
|
void FluidSynthModel::valueTreePropertyChanged(ValueTree& treeWhosePropertyHasChanged,
|
|
|
|
const Identifier& property) {
|
2019-07-11 06:13:58 +08:00
|
|
|
if (treeWhosePropertyHasChanged.getType() == StringRef("soundFont")) {
|
2019-07-10 03:44:26 +08:00
|
|
|
// if (&treeWhosePropertyHasChanged == &valueTree) {
|
2019-07-11 06:13:58 +08:00
|
|
|
if (property == StringRef("path")) {
|
|
|
|
String soundFontPath{treeWhosePropertyHasChanged.getProperty("path", "")};
|
2019-07-08 00:35:31 +08:00
|
|
|
if (soundFontPath.isNotEmpty()) {
|
|
|
|
loadFont(soundFontPath);
|
|
|
|
}
|
|
|
|
}
|
2019-07-11 06:13:58 +08:00
|
|
|
}
|
2019-07-08 00:35:31 +08:00
|
|
|
}
|
|
|
|
|
2019-07-01 04:40:24 +08:00
|
|
|
void FluidSynthModel::setControllerValue(int controller, int value) {
|
|
|
|
fluid_midi_event_t *midi_event(new_fluid_midi_event());
|
|
|
|
fluid_midi_event_set_type(midi_event, static_cast<int>(CONTROL_CHANGE));
|
|
|
|
fluid_midi_event_set_channel(midi_event, channel);
|
|
|
|
fluid_midi_event_set_control(midi_event, controller);
|
|
|
|
fluid_midi_event_set_value(midi_event, value);
|
2019-07-07 07:22:47 +08:00
|
|
|
fluid_synth_handle_midi_event(synth.get(), midi_event);
|
2019-07-01 04:40:24 +08:00
|
|
|
delete_fluid_midi_event(midi_event);
|
|
|
|
// fluid_channel_set_cc(channel, i, 0);
|
|
|
|
}
|
|
|
|
|
2018-02-27 08:25:20 +08:00
|
|
|
int FluidSynthModel::getChannel() {
|
|
|
|
return channel;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FluidSynthModel::changePreset(int bank, int preset) {
|
2018-04-13 07:40:27 +08:00
|
|
|
if (bank == -1 || preset == -1) {
|
2018-04-13 08:14:07 +08:00
|
|
|
unique_ptr<BankAndPreset> bankAndPreset = getFirstBankAndPreset();
|
|
|
|
bank = bankAndPreset->getBank();
|
|
|
|
preset = bankAndPreset->getPreset();
|
2018-04-13 07:40:27 +08:00
|
|
|
}
|
|
|
|
changePresetImpl(bank, preset);
|
2019-07-07 07:22:47 +08:00
|
|
|
// sharedParams->setPreset(preset);
|
|
|
|
// sharedParams->setBank(bank);
|
2018-04-13 07:40:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void FluidSynthModel::changePresetImpl(int bank, int preset) {
|
2019-07-07 07:22:47 +08:00
|
|
|
fluid_synth_program_select(synth.get(), channel, sfont_id, static_cast<unsigned int>(bank), static_cast<unsigned int>(preset));
|
2018-02-27 08:25:20 +08:00
|
|
|
}
|
|
|
|
|
2018-06-18 00:53:32 +08:00
|
|
|
fluid_preset_t* FluidSynthModel::getFirstPreset() {
|
2019-07-07 07:22:47 +08:00
|
|
|
fluid_sfont_t* sfont = fluid_synth_get_sfont_by_id(synth.get(), sfont_id);
|
2018-02-27 08:25:20 +08:00
|
|
|
|
|
|
|
jassert(sfont != nullptr);
|
2018-06-18 00:53:32 +08:00
|
|
|
fluid_sfont_iteration_start(sfont);
|
2018-02-27 08:25:20 +08:00
|
|
|
|
2018-06-18 00:53:32 +08:00
|
|
|
return fluid_sfont_iteration_next(sfont);
|
2018-02-27 08:25:20 +08:00
|
|
|
}
|
|
|
|
|
2018-04-13 08:14:07 +08:00
|
|
|
unique_ptr<BankAndPreset> FluidSynthModel::getFirstBankAndPreset() {
|
2018-06-18 00:53:32 +08:00
|
|
|
fluid_preset_t* preset = getFirstPreset();
|
2018-04-13 08:14:07 +08:00
|
|
|
|
2019-07-07 07:22:47 +08:00
|
|
|
int offset = fluid_synth_get_bank_offset(synth.get(), sfont_id);
|
2018-04-13 08:14:07 +08:00
|
|
|
|
2018-06-18 00:53:32 +08:00
|
|
|
return make_unique<BankAndPreset>(fluid_preset_get_banknum(preset) + offset, fluid_preset_get_num(preset));
|
2018-04-13 08:14:07 +08:00
|
|
|
};
|
|
|
|
|
2018-02-27 08:25:20 +08:00
|
|
|
void FluidSynthModel::selectFirstPreset() {
|
2018-06-18 00:53:32 +08:00
|
|
|
fluid_preset_t* preset = getFirstPreset();
|
2018-02-27 08:25:20 +08:00
|
|
|
|
2019-07-07 07:22:47 +08:00
|
|
|
int offset = fluid_synth_get_bank_offset(synth.get(), sfont_id);
|
2018-02-27 08:25:20 +08:00
|
|
|
|
2018-06-18 00:53:32 +08:00
|
|
|
changePreset(fluid_preset_get_banknum(preset) + offset, fluid_preset_get_num(preset));
|
2018-02-27 08:25:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
BanksToPresets FluidSynthModel::getBanks() {
|
|
|
|
BanksToPresets banksToPresets;
|
|
|
|
|
2019-07-07 07:22:47 +08:00
|
|
|
int soundfontCount = fluid_synth_sfcount(synth.get());
|
2018-03-06 06:33:20 +08:00
|
|
|
|
|
|
|
if (soundfontCount == 0) {
|
|
|
|
// no soundfont selected
|
|
|
|
return banksToPresets;
|
|
|
|
}
|
|
|
|
|
2019-07-07 07:22:47 +08:00
|
|
|
fluid_sfont_t* sfont = fluid_synth_get_sfont_by_id(synth.get(), sfont_id);
|
2018-03-06 06:33:20 +08:00
|
|
|
if(sfont == nullptr) {
|
|
|
|
// no soundfont found by that ID
|
|
|
|
// the above guard (soundfontCount) protects us for the
|
|
|
|
// main case we're expecting. this guard is just defensive programming.
|
|
|
|
return banksToPresets;
|
|
|
|
}
|
2018-02-27 08:25:20 +08:00
|
|
|
|
2019-07-07 07:22:47 +08:00
|
|
|
int offset = fluid_synth_get_bank_offset(synth.get(), sfont_id);
|
2018-02-27 08:25:20 +08:00
|
|
|
|
2018-06-18 00:53:32 +08:00
|
|
|
fluid_sfont_iteration_start(sfont);
|
2018-02-27 08:25:20 +08:00
|
|
|
|
2018-06-18 00:53:32 +08:00
|
|
|
for(fluid_preset_t* preset = fluid_sfont_iteration_next(sfont);
|
|
|
|
preset != nullptr;
|
|
|
|
preset = fluid_sfont_iteration_next(sfont)) {
|
2018-02-27 08:25:20 +08:00
|
|
|
banksToPresets.insert(BanksToPresets::value_type(
|
2018-06-18 00:53:32 +08:00
|
|
|
fluid_preset_get_banknum(preset) + offset,
|
2018-02-27 08:25:20 +08:00
|
|
|
*new Preset(
|
2018-06-18 00:53:32 +08:00
|
|
|
fluid_preset_get_num(preset),
|
|
|
|
fluid_preset_get_name(preset)
|
2018-02-27 08:25:20 +08:00
|
|
|
)
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
return banksToPresets;
|
|
|
|
}
|
|
|
|
|
2019-07-07 07:22:47 +08:00
|
|
|
shared_ptr<fluid_synth_t> FluidSynthModel::getSynth() {
|
2018-02-27 08:25:20 +08:00
|
|
|
// https://msdn.microsoft.com/en-us/library/hh279669.aspx
|
|
|
|
// You can pass a shared_ptr to another function in the following ways:
|
|
|
|
// Pass the shared_ptr by value. This invokes the copy constructor, increments the reference count, and makes the callee an owner.
|
|
|
|
return synth;
|
|
|
|
}
|
|
|
|
|
2018-04-13 08:14:07 +08:00
|
|
|
void FluidSynthModel::onFileNameChanged(const String &absPath, int bank, int preset) {
|
2018-04-11 06:29:32 +08:00
|
|
|
if (!shouldLoadFont(absPath)) {
|
|
|
|
return;
|
|
|
|
}
|
2018-02-27 08:25:20 +08:00
|
|
|
unloadAndLoadFont(absPath);
|
2018-04-13 08:14:07 +08:00
|
|
|
changePreset(bank, preset);
|
2019-07-11 06:13:58 +08:00
|
|
|
ValueTree valueTree{valueTreeState.state.getChildWithName("soundFont")};
|
|
|
|
valueTree.setPropertyExcludingListener(this, "path", absPath, nullptr);
|
2019-07-10 03:44:26 +08:00
|
|
|
// valueTree.setPropertyExcludingListener(this, "soundFontPath", absPath, nullptr);
|
2019-07-08 00:35:31 +08:00
|
|
|
// sharedParams.setSoundFontPath(absPath);
|
2018-04-10 07:51:21 +08:00
|
|
|
eventListeners.call(&FluidSynthModel::Listener::fontChanged, this, absPath);
|
2018-02-27 08:25:20 +08:00
|
|
|
}
|
|
|
|
|
2018-04-11 06:29:32 +08:00
|
|
|
void FluidSynthModel::unloadAndLoadFont(const String &absPath) {
|
2018-03-06 06:38:51 +08:00
|
|
|
// in the base case, there is no font loaded
|
2019-07-07 07:22:47 +08:00
|
|
|
if (fluid_synth_sfcount(synth.get()) > 0) {
|
|
|
|
fluid_synth_sfunload(synth.get(), sfont_id, 1);
|
2018-03-06 06:38:51 +08:00
|
|
|
}
|
2018-02-27 08:25:20 +08:00
|
|
|
loadFont(absPath);
|
|
|
|
}
|
|
|
|
|
2018-04-11 06:29:32 +08:00
|
|
|
void FluidSynthModel::loadFont(const String &absPath) {
|
|
|
|
currentSoundFontAbsPath = absPath;
|
2018-02-27 08:25:20 +08:00
|
|
|
sfont_id++;
|
2019-07-07 07:22:47 +08:00
|
|
|
fluid_synth_sfload(synth.get(), absPath.toStdString().c_str(), 1);
|
2018-02-27 08:25:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
FluidSynthModel::Listener::~Listener() {
|
|
|
|
}
|
|
|
|
|
2018-04-11 06:29:32 +08:00
|
|
|
bool FluidSynthModel::shouldLoadFont(const String &absPath) {
|
|
|
|
if (absPath.isEmpty()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (absPath == currentSoundFontAbsPath) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FluidSynthModel::Listener::fontChanged(FluidSynthModel * model, const String &absPath) {
|
2018-02-27 08:25:20 +08:00
|
|
|
}
|
|
|
|
|
2018-04-11 07:08:15 +08:00
|
|
|
const String& FluidSynthModel::getCurrentSoundFontAbsPath() {
|
|
|
|
return currentSoundFontAbsPath;
|
|
|
|
}
|
|
|
|
|
2018-02-27 08:25:20 +08:00
|
|
|
//==============================================================================
|
|
|
|
void FluidSynthModel::addListener (FluidSynthModel::Listener* const newListener)
|
|
|
|
{
|
|
|
|
eventListeners.add(newListener);
|
|
|
|
}
|
|
|
|
|
|
|
|
void FluidSynthModel::removeListener (FluidSynthModel::Listener* const listener)
|
|
|
|
{
|
|
|
|
eventListeners.remove(listener);
|
2018-04-16 04:32:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void FluidSynthModel::setSampleRate(float sampleRate) {
|
|
|
|
currentSampleRate = sampleRate;
|
2019-07-07 07:22:47 +08:00
|
|
|
// https://stackoverflow.com/a/40856043/5257399
|
|
|
|
// test if a smart pointer is null
|
|
|
|
if (!synth) {
|
2018-04-16 04:32:26 +08:00
|
|
|
// don't worry; we'll do this in initialise phase regardless
|
|
|
|
return;
|
|
|
|
}
|
2019-07-07 07:22:47 +08:00
|
|
|
fluid_synth_set_sample_rate(synth.get(), sampleRate);
|
2018-06-18 00:53:32 +08:00
|
|
|
}
|