juicysfplugin/Source/TablesComponent.cpp

194 lines
5.4 KiB
C++
Raw Normal View History

2018-02-27 08:25:20 +08:00
//
// Created by Alex Birch on 17/09/2017.
//
#include "TablesComponent.h"
using namespace std;
using namespace placeholders;
TablesComponent::TablesComponent(
AudioProcessorValueTreeState& valueTreeState,
FluidSynthModel& fluidSynthModel
)
: valueTreeState{valueTreeState}
, fluidSynthModel{fluidSynthModel}
, banksToPresets{fluidSynthModel.getBanks()}
, initialised{false}
2018-02-27 08:25:20 +08:00
{
fluid_preset_t* currentPreset = getCurrentPreset();
selectedBank = -1;
int selectedPreset = -1;
2018-02-27 08:25:20 +08:00
if (currentPreset != nullptr) {
selectedBank = fluid_preset_get_banknum(currentPreset);
selectedPreset = fluid_preset_get_num(currentPreset);
}
2018-02-27 08:25:20 +08:00
// auto rowToPresetMapper = [this](const vector<string> &row) {
// return stoi(row[0]);
// };
2018-02-27 08:25:20 +08:00
auto itemToBankMapper = [](const string &item) {
return stoi(item);
};
presetTable = new TableComponent(
valueTreeState,
// {"#", "Name"},
// mapPresets(
// banksToPresets,
// selectedBank
// ),
// [this](int preset){
// this->onPresetSelected(preset);
// },
// rowToPresetMapper,
// presetToIndexMapper(selectedPreset)
2018-02-27 08:25:20 +08:00
);
banks = new Pills(
"Banks",
mapBanks(banksToPresets),
[this](int bank){
this->onBankSelected(bank);
},
itemToBankMapper,
selectedBank
);
presetTable->setWantsKeyboardFocus(false);
addAndMakeVisible(presetTable);
addAndMakeVisible(banks);
initialised = true;
fluidSynthModel.addListener(this);
2018-02-27 08:25:20 +08:00
}
fluid_preset_t* TablesComponent::getCurrentPreset() {
shared_ptr<fluid_synth_t> synth {fluidSynthModel.getSynth()};
2018-02-27 08:25:20 +08:00
return fluid_synth_get_channel_preset(synth.get(), fluidSynthModel.getChannel());
2018-02-27 08:25:20 +08:00
}
Preset TablesComponent::getFirstPresetInBank(int bank) {
pair<BanksToPresets::const_iterator, BanksToPresets::const_iterator> iterators = banksToPresets.equal_range(bank);
BanksToPresets::const_iterator it = iterators.first;
return it->second;
}
void TablesComponent::onBankSelected(int bank) {
if (!initialised || bank == -1) {
2018-02-27 08:25:20 +08:00
return;
}
cout << "Bank " << bank << endl;
selectedBank = bank;
Preset firstPresetInBank = getFirstPresetInBank(bank);
presetTable->setRows(
mapPresets(
banksToPresets,
bank
),
presetToIndexMapper(firstPresetInBank.getPreset())
);
}
int TablesComponent::presetToIndexMapper(int preset) {
int ix = 0;
pair<BanksToPresets::const_iterator, BanksToPresets::const_iterator> iterators = this->banksToPresets.equal_range(this->selectedBank);
for (auto it = iterators.first; it != iterators.second; ++it, ix++) {
Preset b = it->second;
if (preset == b.getPreset()) {
return ix;
}
}
return 0;
}
// void TablesComponent::onPresetSelected(int preset) {
// if (!initialised || preset == -1) {
// return;
// }
// cout << "Preset " << preset << endl;
// // selectedPreset = preset;
// fluidSynthModel.changePreset(selectedBank, preset);
// }
2018-02-27 08:25:20 +08:00
TablesComponent::~TablesComponent() {
delete presetTable;
delete banks;
// fluidSynthModel.removeListener(this);
2018-02-27 08:25:20 +08:00
}
vector<string> TablesComponent::mapBanks(const BanksToPresets &banksToPresets) {
vector<string> rows;
const auto compareKey = [](const BanksToPresets::value_type& lhs, const BanksToPresets::value_type& rhs) {
return lhs.first < rhs.first;
};
for(auto i = banksToPresets.begin(); i != banksToPresets.end(); i = std::upper_bound(i, banksToPresets.end(), *i, compareKey)) {
rows.push_back(to_string(i->first));
}
return rows;
}
// vector<vector<string>> TablesComponent::mapPresets(const BanksToPresets &banksToPresets, int bank) {
// vector<vector<string>> rows;
2018-02-27 08:25:20 +08:00
// pair<BanksToPresets::const_iterator, BanksToPresets::const_iterator> iterators = banksToPresets.equal_range(bank);
// for (auto it = iterators.first; it != iterators.second; ++it) {
// Preset b = it->second;
// vector<string> row;
// row.push_back(to_string(b.getPreset()));
// row.push_back(b.getName());
2018-02-27 08:25:20 +08:00
// rows.push_back(row);
// }
2018-02-27 08:25:20 +08:00
// return rows;
// }
2018-02-27 08:25:20 +08:00
void TablesComponent::resized() {
Rectangle<int> r (getLocalBounds());
banks->setBounds (r.removeFromTop(27).reduced(5,0));
presetTable->setBounds (r);
}
bool TablesComponent::keyPressed(const KeyPress &key) {
if (key.getKeyCode() == KeyPress::leftKey
|| key.getKeyCode() == KeyPress::rightKey) {
banks->cycle(key.getKeyCode() == KeyPress::rightKey);
return true;
}
return presetTable->keyPressed(key);
}
// void TablesComponent::fontChanged(FluidSynthModel *, const String &) {
// banksToPresets = fluidSynthModel.getBanks();
2018-02-27 08:25:20 +08:00
// fluid_preset_t* currentPreset = getCurrentPreset();
2018-02-27 08:25:20 +08:00
// selectedBank = fluid_preset_get_banknum(currentPreset);
// int selectedPreset = fluid_preset_get_num(currentPreset);
2018-02-27 08:25:20 +08:00
// presetTable->setRows(
// mapPresets(
// banksToPresets,
// selectedBank
// ),
// presetToIndexMapper(selectedPreset)
// );
2018-02-27 08:25:20 +08:00
// banks->setItems(
// mapBanks(banksToPresets),
// selectedBank
// );
// }