remove some unused methods on FluidSynthModel. make FilePicker update displayed text upon load. read and write into XML just the bits that we need, and take advantage of attribute names' being indexed. take more care in setting sfont_id.

This commit is contained in:
Alex Birch 2019-07-27 23:04:20 +01:00
parent 5c310a1606
commit f87ec7c8e4
No known key found for this signature in database
GPG Key ID: 305EB1F98D44ACBA
5 changed files with 194 additions and 140 deletions

View File

@ -66,7 +66,8 @@ void FilePicker::valueTreePropertyChanged(ValueTree& treeWhosePropertyHasChanged
// if (&treeWhosePropertyHasChanged == &valueTree) { // if (&treeWhosePropertyHasChanged == &valueTree) {
if (property == StringRef("path")) { if (property == StringRef("path")) {
String soundFontPath{treeWhosePropertyHasChanged.getProperty("path", "")}; String soundFontPath{treeWhosePropertyHasChanged.getProperty("path", "")};
// DEBUG_PRINT(soundFontPath); DEBUG_PRINT(soundFontPath);
setDisplayedFilePath(soundFontPath);
// if (soundFontPath.isNotEmpty()) { // if (soundFontPath.isNotEmpty()) {
// loadFont(soundFontPath); // loadFont(soundFontPath);
// } // }
@ -75,9 +76,9 @@ void FilePicker::valueTreePropertyChanged(ValueTree& treeWhosePropertyHasChanged
} }
void FilePicker::setDisplayedFilePath(const String& path) { void FilePicker::setDisplayedFilePath(const String& path) {
// if (!shouldChangeDisplayedFilePath(path)) { if (!shouldChangeDisplayedFilePath(path)) {
// return; return;
// } }
// currentPath = path; // currentPath = path;
fileChooser.setCurrentFile(File(path), true, dontSendNotification); fileChooser.setCurrentFile(File(path), true, dontSendNotification);
} }

View File

@ -50,4 +50,4 @@ private:
bool shouldChangeDisplayedFilePath(const String &path); bool shouldChangeDisplayedFilePath(const String &path);
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (FilePicker) JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (FilePicker)
}; };

View File

@ -20,10 +20,10 @@ FluidSynthModel::FluidSynthModel(
// , sharedParams{sharedParams} // , sharedParams{sharedParams}
//, synth{nullptr} //, synth{nullptr}
, settings{nullptr, nullptr} , settings{nullptr, nullptr}
, currentSoundFontAbsPath{} //, currentSoundFontAbsPath{}
, currentSampleRate{44100} , currentSampleRate{44100}
, initialised{false} //, initialised{false}
, sfont_id{0} , sfont_id{-1}
, channel{0}/*, , channel{0}/*,
mod(nullptr)*/ mod(nullptr)*/
{ {
@ -97,12 +97,15 @@ void FluidSynthModel::initialise() {
// loadFont(sharedParams.getSoundFontPath()); // loadFont(sharedParams.getSoundFontPath());
// changePreset(sharedParams->getBank(), sharedParams->getPreset()); // changePreset(sharedParams->getBank(), sharedParams->getPreset());
// } // }
ValueTree soundFont{valueTreeState.state.getChildWithName("soundFont")};
String path{soundFont.getProperty("path", "")};
loadFont(path);
fluid_synth_set_gain(synth.get(), 2.0); fluid_synth_set_gain(synth.get(), 2.0);
for(int i{SOUND_CTRL1}; i <= SOUND_CTRL10; i++) fluid_midi_control_change controllers[]{SOUND_CTRL2, SOUND_CTRL3, SOUND_CTRL4, SOUND_CTRL5, SOUND_CTRL6, SOUND_CTRL10};
{ for(fluid_midi_control_change controller : controllers) {
setControllerValue(i, 0); setControllerValue(static_cast<int>(controller), 0);
} }
// fluid_synth_bank_select(synth, 0, 3); // fluid_synth_bank_select(synth, 0, 3);
@ -116,7 +119,7 @@ void FluidSynthModel::initialise() {
// float env_amount(12000.0f); // float env_amount(12000.0f);
// http://www.synthfont.com/SoundFont_NRPNs.PDF // http://www.synthfont.com/SoundFont_NRPNs.PDF
float env_amount(20000.0f); float env_amount{20000.0f};
// float env_amount(24000.0f); // float env_amount(24000.0f);
// note: fluid_chan.c#fluid_channel_init_ctrl() // note: fluid_chan.c#fluid_channel_init_ctrl()
@ -200,7 +203,7 @@ void FluidSynthModel::initialise() {
fluid_mod_set_amount(mod.get(), 1000.0f); fluid_mod_set_amount(mod.get(), 1000.0f);
fluid_synth_add_default_mod(synth.get(), mod.get(), FLUID_SYNTH_ADD); fluid_synth_add_default_mod(synth.get(), mod.get(), FLUID_SYNTH_ADD);
valueTreeState.state.getChildWithName("soundFont").sendPropertyChangeMessage("path"); // valueTreeState.state.getChildWithName("soundFont").sendPropertyChangeMessage("path");
// valueTree.sendPropertyChangeMessage("soundFontPath"); // valueTree.sendPropertyChangeMessage("soundFontPath");
// initialised = true; // initialised = true;
@ -273,7 +276,7 @@ void FluidSynthModel::valueTreePropertyChanged(ValueTree& treeWhosePropertyHasCh
if (property == StringRef("path")) { if (property == StringRef("path")) {
String soundFontPath{treeWhosePropertyHasChanged.getProperty("path", "")}; String soundFontPath{treeWhosePropertyHasChanged.getProperty("path", "")};
if (soundFontPath.isNotEmpty()) { if (soundFontPath.isNotEmpty()) {
loadFont(soundFontPath); unloadAndLoadFont(soundFontPath);
} }
} }
} }
@ -294,82 +297,82 @@ int FluidSynthModel::getChannel() {
return channel; return channel;
} }
void FluidSynthModel::changePreset(int bank, int preset) { //void FluidSynthModel::changePreset(int bank, int preset) {
if (bank == -1 || preset == -1) { // if (bank == -1 || preset == -1) {
unique_ptr<BankAndPreset> bankAndPreset = getFirstBankAndPreset(); // unique_ptr<BankAndPreset> bankAndPreset = getFirstBankAndPreset();
bank = bankAndPreset->getBank(); // bank = bankAndPreset->getBank();
preset = bankAndPreset->getPreset(); // preset = bankAndPreset->getPreset();
} // }
changePresetImpl(bank, preset); // changePresetImpl(bank, preset);
// sharedParams->setPreset(preset); //// sharedParams->setPreset(preset);
// sharedParams->setBank(bank); //// sharedParams->setBank(bank);
} //}
void FluidSynthModel::changePresetImpl(int bank, int preset) { //void FluidSynthModel::changePresetImpl(int bank, int preset) {
fluid_synth_program_select(synth.get(), channel, sfont_id, static_cast<unsigned int>(bank), static_cast<unsigned int>(preset)); // fluid_synth_program_select(synth.get(), channel, sfont_id, static_cast<unsigned int>(bank), static_cast<unsigned int>(preset));
} //}
fluid_preset_t* FluidSynthModel::getFirstPreset() { //fluid_preset_t* FluidSynthModel::getFirstPreset() {
fluid_sfont_t* sfont = fluid_synth_get_sfont_by_id(synth.get(), sfont_id); // fluid_sfont_t* sfont = fluid_synth_get_sfont_by_id(synth.get(), sfont_id);
//
// jassert(sfont != nullptr);
// fluid_sfont_iteration_start(sfont);
//
// return fluid_sfont_iteration_next(sfont);
//}
//
//unique_ptr<BankAndPreset> FluidSynthModel::getFirstBankAndPreset() {
// fluid_preset_t* preset = getFirstPreset();
//
// int offset = fluid_synth_get_bank_offset(synth.get(), sfont_id);
//
// return make_unique<BankAndPreset>(fluid_preset_get_banknum(preset) + offset, fluid_preset_get_num(preset));
//};
//
//void FluidSynthModel::selectFirstPreset() {
// fluid_preset_t* preset = getFirstPreset();
//
// int offset = fluid_synth_get_bank_offset(synth.get(), sfont_id);
//
// changePreset(fluid_preset_get_banknum(preset) + offset, fluid_preset_get_num(preset));
//}
jassert(sfont != nullptr); //BanksToPresets FluidSynthModel::getBanks() {
fluid_sfont_iteration_start(sfont); // BanksToPresets banksToPresets;
//
return fluid_sfont_iteration_next(sfont); // int soundfontCount = fluid_synth_sfcount(synth.get());
} //
// if (soundfontCount == 0) {
unique_ptr<BankAndPreset> FluidSynthModel::getFirstBankAndPreset() { // // no soundfont selected
fluid_preset_t* preset = getFirstPreset(); // return banksToPresets;
// }
int offset = fluid_synth_get_bank_offset(synth.get(), sfont_id); //
// fluid_sfont_t* sfont = fluid_synth_get_sfont_by_id(synth.get(), sfont_id);
return make_unique<BankAndPreset>(fluid_preset_get_banknum(preset) + offset, fluid_preset_get_num(preset)); // if(sfont == nullptr) {
}; // // no soundfont found by that ID
// // the above guard (soundfontCount) protects us for the
void FluidSynthModel::selectFirstPreset() { // // main case we're expecting. this guard is just defensive programming.
fluid_preset_t* preset = getFirstPreset(); // return banksToPresets;
// }
int offset = fluid_synth_get_bank_offset(synth.get(), sfont_id); //
// int offset = fluid_synth_get_bank_offset(synth.get(), sfont_id);
changePreset(fluid_preset_get_banknum(preset) + offset, fluid_preset_get_num(preset)); //
} // fluid_sfont_iteration_start(sfont);
//
BanksToPresets FluidSynthModel::getBanks() { // for(fluid_preset_t* preset = fluid_sfont_iteration_next(sfont);
BanksToPresets banksToPresets; // preset != nullptr;
// preset = fluid_sfont_iteration_next(sfont)) {
int soundfontCount = fluid_synth_sfcount(synth.get()); // banksToPresets.insert(BanksToPresets::value_type(
// fluid_preset_get_banknum(preset) + offset,
if (soundfontCount == 0) { // *new Preset(
// no soundfont selected // fluid_preset_get_num(preset),
return banksToPresets; // fluid_preset_get_name(preset)
} // )
// ));
fluid_sfont_t* sfont = fluid_synth_get_sfont_by_id(synth.get(), sfont_id); // }
if(sfont == nullptr) { //
// no soundfont found by that ID // return banksToPresets;
// the above guard (soundfontCount) protects us for the //}
// main case we're expecting. this guard is just defensive programming.
return banksToPresets;
}
int offset = fluid_synth_get_bank_offset(synth.get(), sfont_id);
fluid_sfont_iteration_start(sfont);
for(fluid_preset_t* preset = fluid_sfont_iteration_next(sfont);
preset != nullptr;
preset = fluid_sfont_iteration_next(sfont)) {
banksToPresets.insert(BanksToPresets::value_type(
fluid_preset_get_banknum(preset) + offset,
*new Preset(
fluid_preset_get_num(preset),
fluid_preset_get_name(preset)
)
));
}
return banksToPresets;
}
shared_ptr<fluid_synth_t> FluidSynthModel::getSynth() { shared_ptr<fluid_synth_t> FluidSynthModel::getSynth() {
// https://msdn.microsoft.com/en-us/library/hh279669.aspx // https://msdn.microsoft.com/en-us/library/hh279669.aspx
@ -378,38 +381,50 @@ shared_ptr<fluid_synth_t> FluidSynthModel::getSynth() {
return synth; return synth;
} }
void FluidSynthModel::onFileNameChanged(const String &absPath, int bank, int preset) { //void FluidSynthModel::onFileNameChanged(const String &absPath, int bank, int preset) {
if (!shouldLoadFont(absPath)) { // if (!shouldLoadFont(absPath)) {
return; // return;
} // }
unloadAndLoadFont(absPath); // unloadAndLoadFont(absPath);
changePreset(bank, preset); // changePreset(bank, preset);
ValueTree valueTree{valueTreeState.state.getChildWithName("soundFont")}; // ValueTree valueTree{valueTreeState.state.getChildWithName("soundFont")};
valueTree.setPropertyExcludingListener(this, "path", absPath, nullptr); // valueTree.setPropertyExcludingListener(this, "path", absPath, nullptr);
// valueTree.setPropertyExcludingListener(this, "soundFontPath", absPath, nullptr); // // valueTree.setPropertyExcludingListener(this, "soundFontPath", absPath, nullptr);
// sharedParams.setSoundFontPath(absPath); //// sharedParams.setSoundFontPath(absPath);
// eventListeners.call(&FluidSynthModel::Listener::fontChanged, this, absPath); // // eventListeners.call(&FluidSynthModel::Listener::fontChanged, this, absPath);
} //}
void FluidSynthModel::unloadAndLoadFont(const String &absPath) { void FluidSynthModel::unloadAndLoadFont(const String &absPath) {
// in the base case, there is no font loaded // in the base case, there is no font loaded
if (fluid_synth_sfcount(synth.get()) > 0) { if (fluid_synth_sfcount(synth.get()) > 0) {
// if -1 is returned, that indicates failure
// not really sure how to handle "fail to unload"
fluid_synth_sfunload(synth.get(), sfont_id, 1); fluid_synth_sfunload(synth.get(), sfont_id, 1);
sfont_id = -1;
} }
loadFont(absPath); loadFont(absPath);
} }
void FluidSynthModel::loadFont(const String &absPath) { void FluidSynthModel::loadFont(const String &absPath) {
currentSoundFontAbsPath = absPath; // currentSoundFontAbsPath = absPath;
sfont_id++; // sfont_id++;
fluid_synth_sfload(synth.get(), absPath.toStdString().c_str(), 1); // fluid_synth_sfunload(synth.get(), sfont_id, 1);
if (!absPath.isEmpty()) {
sfont_id = fluid_synth_sfload(synth.get(), absPath.toStdString().c_str(), 1);
// if -1 is returned, that indicates failure
}
// refresh regardless of success, if only to clear the table
refreshBanks(); refreshBanks();
} }
void FluidSynthModel::refreshBanks() { void FluidSynthModel::refreshBanks() {
fluid_sfont_t* sfont {fluid_synth_get_sfont_by_id(synth.get(), sfont_id)};
ValueTree banks{"banks"}; ValueTree banks{"banks"};
if (sfont != nullptr) { fluid_sfont_t* sfont{
sfont_id == -1
? nullptr
: fluid_synth_get_sfont_by_id(synth.get(), sfont_id)
};
if (sfont) {
int greatestEncounteredBank{-1}; int greatestEncounteredBank{-1};
ValueTree bank; ValueTree bank;
@ -440,8 +455,8 @@ void FluidSynthModel::refreshBanks() {
valueTreeState.state.getChildWithName("banks").sendPropertyChangeMessage("synthetic"); valueTreeState.state.getChildWithName("banks").sendPropertyChangeMessage("synthetic");
#if JUCE_DEBUG #if JUCE_DEBUG
unique_ptr<XmlElement> xml{valueTreeState.state.createXml()}; // unique_ptr<XmlElement> xml{valueTreeState.state.createXml()};
Logger::outputDebugString(xml->createDocument("",false,false)); // Logger::outputDebugString(xml->createDocument("",false,false));
#endif #endif
} }
@ -557,22 +572,22 @@ void FluidSynthModel::refreshBanks() {
// FluidSynthModel::Listener::~Listener() { // FluidSynthModel::Listener::~Listener() {
// } // }
bool FluidSynthModel::shouldLoadFont(const String &absPath) { //bool FluidSynthModel::shouldLoadFont(const String &absPath) {
if (absPath.isEmpty()) { // if (absPath.isEmpty()) {
return false; // return false;
} // }
if (absPath == currentSoundFontAbsPath) { //// if (absPath == currentSoundFontAbsPath) {
return false; //// return false;
} //// }
return true; // return true;
} //}
// void FluidSynthModel::Listener::fontChanged(FluidSynthModel * model, const String &absPath) { // void FluidSynthModel::Listener::fontChanged(FluidSynthModel * model, const String &absPath) {
// } // }
const String& FluidSynthModel::getCurrentSoundFontAbsPath() { //const String& FluidSynthModel::getCurrentSoundFontAbsPath() {
return currentSoundFontAbsPath; // return currentSoundFontAbsPath;
} //}
//============================================================================== //==============================================================================
// void FluidSynthModel::addListener (FluidSynthModel::Listener* const newListener) // void FluidSynthModel::addListener (FluidSynthModel::Listener* const newListener)

View File

@ -31,12 +31,12 @@ public:
shared_ptr<fluid_synth_t> getSynth(); shared_ptr<fluid_synth_t> getSynth();
void initialise(); void initialise();
BanksToPresets getBanks(); // BanksToPresets getBanks();
void changePreset(int bank, int preset); // void changePreset(int bank, int preset);
int getChannel(); int getChannel();
void onFileNameChanged(const String &absPath, int bank, int preset); // void onFileNameChanged(const String &absPath, int bank, int preset);
void setControllerValue(int controller, int value); void setControllerValue(int controller, int value);
//============================================================================== //==============================================================================
@ -68,7 +68,7 @@ public:
void setSampleRate(float sampleRate); void setSampleRate(float sampleRate);
const String& getCurrentSoundFontAbsPath(); // const String& getCurrentSoundFontAbsPath();
virtual void parameterChanged (const String& parameterID, float newValue) override; virtual void parameterChanged (const String& parameterID, float newValue) override;
@ -123,7 +123,7 @@ private:
shared_ptr<fluid_synth_t> synth; shared_ptr<fluid_synth_t> synth;
// unique_ptr<fluid_midi_driver_t, decltype(&delete_fluid_midi_driver)> midiDriver; // unique_ptr<fluid_midi_driver_t, decltype(&delete_fluid_midi_driver)> midiDriver;
String currentSoundFontAbsPath; // String currentSoundFontAbsPath;
float currentSampleRate; float currentSampleRate;
@ -133,12 +133,12 @@ private:
void unloadAndLoadFont(const String &absPath); void unloadAndLoadFont(const String &absPath);
void loadFont(const String &absPath); void loadFont(const String &absPath);
bool shouldLoadFont(const String &absPath); // bool shouldLoadFont(const String &absPath);
void changePresetImpl(int bank, int preset); void changePresetImpl(int bank, int preset);
bool initialised; // bool initialised;
unsigned int sfont_id; int sfont_id;
unsigned int channel; unsigned int channel;
// fluid_mod_t* mod; // fluid_mod_t* mod;

View File

@ -218,7 +218,7 @@ void JuicySFAudioProcessor::processBlock (AudioBuffer<float>& buffer, MidiBuffer
int time; int time;
MidiMessage m; MidiMessage m;
// TODO: factor into a MidiCollector // TODO: factor into a MidiMessageCollector
for (MidiBuffer::Iterator i (midiMessages); i.getNextEvent (m, time);) { for (MidiBuffer::Iterator i (midiMessages); i.getNextEvent (m, time);) {
DEBUG_PRINT ( m.getDescription() ); DEBUG_PRINT ( m.getDescription() );
@ -385,10 +385,10 @@ void JuicySFAudioProcessor::getStateInformation (MemoryBlock& destData)
// as intermediaries to make it easy to save and load complex data. // as intermediaries to make it easy to save and load complex data.
// Create an outer XML element.. // Create an outer XML element..
// XmlElement xml{"MYPLUGINSETTINGS"}; XmlElement xml{"MYPLUGINSETTINGS"};
// sharedParams->setAttributesOnXml(xml); // sharedParams->setAttributesOnXml(xml);
auto state{valueTreeState.copyState()}; // auto state{valueTreeState.copyState()};
unique_ptr<XmlElement> xml{state.createXml()}; // unique_ptr<XmlElement> xml{state.createXml()};
// sharedParams.setAttributesOnXml(xml); // sharedParams.setAttributesOnXml(xml);
// list<StateChangeSubscriber*>::iterator p; // list<StateChangeSubscriber*>::iterator p;
@ -397,14 +397,44 @@ void JuicySFAudioProcessor::getStateInformation (MemoryBlock& destData)
// } // }
// Store the values of all our parameters, using their param ID as the XML attribute // Store the values of all our parameters, using their param ID as the XML attribute
// for (auto* param : getParameters()) XmlElement* params{xml.createNewChildElement("params")};
// if (auto* p = dynamic_cast<AudioProcessorParameterWithID*> (param)) for (auto* param : getParameters()) {
// xml->setAttribute (p->paramID, p->getValue()); if (auto* p = dynamic_cast<AudioProcessorParameterWithID*> (param)) {
// xml.setAttribute(p->paramID, p->getValue());
// XmlElement* param{params->createNewChildElement("PARAM")};
// param->setAttribute(p->paramID, p->getValue());
params->setAttribute(p->paramID, p->getValue());
}
}
{
ValueTree tree{valueTreeState.state.getChildWithName("uiState")};
XmlElement* newElement{xml.createNewChildElement("uiState")};
// Value value{tree.getPropertyAsValue("width", nullptr)};
{
double value{tree.getProperty("width", GuiConstants::minWidth)};
newElement->setAttribute("width", value);
}
{
double value{tree.getProperty("height", GuiConstants::minHeight)};
newElement->setAttribute("height", value);
}
}
{
ValueTree tree{valueTreeState.state.getChildWithName("soundFont")};
XmlElement* newElement{xml.createNewChildElement("soundFont")};
{
String value{tree.getProperty("path", "")};
newElement->setAttribute("path", value);
}
}
DEBUG_PRINT(xml.createDocument("",false,false));
// then use this helper function to stuff it into the binary blob and return it.. // then use this helper function to stuff it into the binary blob and return it..
if (xml.get() != nullptr) { // if (xml.get() != nullptr) {
copyXmlToBinary(*xml, destData); // copyXmlToBinary(*xml, destData);
} // }
copyXmlToBinary(xml, destData);
} }
void JuicySFAudioProcessor::setStateInformation (const void* data, int sizeInBytes) void JuicySFAudioProcessor::setStateInformation (const void* data, int sizeInBytes)
@ -433,16 +463,24 @@ void JuicySFAudioProcessor::setStateInformation (const void* data, int sizeInByt
// if (xmlState->hasTagName ("MYPLUGINSETTINGS")) { // if (xmlState->hasTagName ("MYPLUGINSETTINGS")) {
if (xmlState->hasTagName(valueTreeState.state.getType())) { if (xmlState->hasTagName(valueTreeState.state.getType())) {
// valueTreeState.replaceState(ValueTree::fromXml(*xmlState)); // valueTreeState.replaceState(ValueTree::fromXml(*xmlState));
for (auto* param : getParameters()) // for (auto* param : getParameters())
if (auto* p = dynamic_cast<AudioProcessorParameterWithID*>(param)) // if (auto* p = dynamic_cast<AudioProcessorParameterWithID*>(param))
p->setValue(static_cast<float>(xmlState->getDoubleAttribute(p->paramID, p->getValue()))); // p->setValue(static_cast<float>(xmlState->getDoubleAttribute(p->paramID, p->getValue())));
XmlElement* params{xmlState->getChildByName("params")};
if (params) {
for (auto* param : getParameters())
if (auto* p = dynamic_cast<AudioProcessorParameterWithID*>(param))
// XmlElement* xmlParam{params->getChildByAttribute("id", p->paramID)};
// p->setValue(static_cast<float>(xmlState->getDoubleAttribute(p->paramID, p->getValue())));
p->setValue(static_cast<float>(params->getDoubleAttribute(p->paramID, p->getValue())));
}
{ {
// Value value{valueTreeState.state.getPropertyAsValue("soundFontPath", nullptr)}; // Value value{valueTreeState.state.getPropertyAsValue("soundFontPath", nullptr)};
// value = xmlState->getStringAttribute("soundFontPath", value.getValue()); // value = xmlState->getStringAttribute("soundFontPath", value.getValue());
ValueTree tree{valueTreeState.state.getChildWithName("soundFont")};
XmlElement* xmlElement{xmlState->getChildByName("soundFont")}; XmlElement* xmlElement{xmlState->getChildByName("soundFont")};
if (xmlElement) { if (xmlElement) {
ValueTree tree{valueTreeState.state.getChildWithName("soundFont")};
Value value{tree.getPropertyAsValue("path", nullptr)}; Value value{tree.getPropertyAsValue("path", nullptr)};
value = xmlState->getStringAttribute("path", value.getValue()); value = xmlState->getStringAttribute("path", value.getValue());
} }