Skip to content

Commit

Permalink
patch types working
Browse files Browse the repository at this point in the history
  • Loading branch information
hsetlik committed Apr 11, 2021
1 parent a2935bc commit 5f44d68
Show file tree
Hide file tree
Showing 3 changed files with 171 additions and 53 deletions.
125 changes: 86 additions & 39 deletions Source/PatchManagerComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,91 @@

#include "PatchManagerComponent.h"

void PatchSelector::initialize()
{
int nextId = 1;
if(lib->bassGroup.patches.size() > 0)
{
addSectionHeading("Bass");
addSeparator();
for(auto f : lib->bassGroup.patches)
{
std::unique_ptr<juce::XmlElement> currentXml = juce::parseXML(f);
if(currentXml != nullptr)
{
if(currentXml->hasAttribute("HexFmPatchName"))
{
auto presetName = currentXml->getStringAttribute("HexFmPatchName");
patchNames.add(presetName);
addItem(presetName, nextId);
++nextId;
}
}
}
}

if(lib->leadGroup.patches.size() > 0)
{
addSectionHeading("Lead");
addSeparator();
for(auto f : lib->leadGroup.patches)
{
std::unique_ptr<juce::XmlElement> currentXml = juce::parseXML(f);
if(currentXml != nullptr)
{
if(currentXml->hasAttribute("HexFmPatchName"))
{
auto presetName = currentXml->getStringAttribute("HexFmPatchName");
patchNames.add(presetName);
addItem(presetName, nextId);
++nextId;
}
}
}
}

if(lib->chordGroup.patches.size() > 0)
{
addSectionHeading("Chord");
addSeparator();
for(auto f : lib->chordGroup.patches)
{
std::unique_ptr<juce::XmlElement> currentXml = juce::parseXML(f);
if(currentXml != nullptr)
{
if(currentXml->hasAttribute("HexFmPatchName"))
{
auto presetName = currentXml->getStringAttribute("HexFmPatchName");
patchNames.add(presetName);
addItem(presetName, nextId);
++nextId;
}
}
}
}

if(lib->padGroup.patches.size() > 0)
{
addSectionHeading("Pad");
addSeparator();
for(auto f : lib->padGroup.patches)
{
std::unique_ptr<juce::XmlElement> currentXml = juce::parseXML(f);
if(currentXml != nullptr)
{
if(currentXml->hasAttribute("HexFmPatchName"))
{
auto presetName = currentXml->getStringAttribute("HexFmPatchName");
patchNames.add(presetName);
addItem(presetName, nextId);
++nextId;
}
}
}
}
}


PatchLoader::PatchLoader(HexFmAudioProcessor* proc, juce::Component* patchDlg) :
processor(proc),
saveDialogComponent(patchDlg)
Expand All @@ -33,8 +118,6 @@ saveDialogComponent(patchDlg)
//auto fldr = presetFolder.getFileName().toUTF8();
//printf("Presets are at: %s\n", fldr);

getPresetsFromFolder();

addAndMakeVisible(&patchSelector);
addAndMakeVisible(&nextPatchButton);
addAndMakeVisible(&lastPatchButton);
Expand All @@ -61,38 +144,6 @@ void PatchLoader::resized()
saveButton.setBounds(44 * dY, dY, 6 * dY, 4 * dY);
}

void PatchLoader::getPresetsFromFolder()
{
patchSelector.clear();
patchNames.clear();
auto files = presetFolder.findChildFiles(juce::File::findFiles, true);
auto numFiles = files.size();
if(numFiles > 0)
{
for(int i = 0; i < numFiles; ++i)
{
auto currentFile = files.getUnchecked(i);
std::unique_ptr<juce::XmlElement> currentXml = juce::parseXML(currentFile);
if(currentXml != nullptr)
{
if(currentXml->hasAttribute("HexFmPatchName"))
{
auto presetName = currentXml->getStringAttribute("HexFmPatchName");
patchNames.add(presetName);
patchSelector.addItem(presetName, 1 + i);

}
}
}
if(patchNames.size() > 0)
{
patchSelector.setSelectedId(1);
loadPreset(patchSelector.getText());
printf("%d available patches\n", patchNames.size());
printf("default patch found\n");
}
}
}
void PatchLoader::savePreset(juce::String name, juce::String type)
{
auto fileName = juce::File::createLegalFileName(name);
Expand All @@ -110,11 +161,7 @@ void PatchLoader::savePreset(juce::String name, juce::String type)
xml->setAttribute("HexFmPatchName", name);
xml->setAttribute("HexFmPatchType", type);
xml->writeTo(file);
if(isNew)
{
patchNames.add(name);
patchSelector.addItem(name, patchSelector.getNumItems() + 1);
}
patchSelector.reInitList();
}
}
void PatchLoader::loadPreset(juce::String presetName)
Expand Down
39 changes: 27 additions & 12 deletions Source/PatchManagerComponent.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,33 @@
#include "PluginProcessor.h"
#include "PatchTypeData.h"

class PatchMenu : public juce::Component
class PatchSelector : public juce::ComboBox
{
public:
PatchMenu();
~PatchMenu(){}

PatchSelector() : lib(std::make_unique<PatchLibrary>())
{
initialize();
}
~PatchSelector(){}
void initialize();
void reInitList()
{
clear();
lib.reset(new PatchLibrary());
initialize();
}
int getIndexWithText(juce::String text)
{
for(int i = 0; i < getNumItems(); ++i)
{
if(getItemText(i) == text)
return i;
}
return getNumItems() - 1;
}
juce::StringArray patchNames;
private:
juce::ComboBox allPatchNames;
std::unique_ptr<PatchLibrary> lib;
};

class PatchLoader : public juce::Component, juce::Button::Listener, juce::ComboBox::Listener
Expand All @@ -29,12 +48,6 @@ class PatchLoader : public juce::Component, juce::Button::Listener, juce::ComboB
PatchLoader(HexFmAudioProcessor* proc, juce::Component* patchDlg);
~PatchLoader() {}
void resized() override;
void loadNames(juce::StringArray patchNames)
{
patchSelector.addItemList(patchNames, 1);
}
//TODO:
void getPresetsFromFolder();
void savePreset(juce::String name, juce::String type);
void loadPreset(juce::String name);
void comboBoxChanged(juce::ComboBox* box) override;
Expand Down Expand Up @@ -90,7 +103,9 @@ class PatchLoader : public juce::Component, juce::Button::Listener, juce::ComboB
saveDialogComponent->resized();
}
}
juce::ComboBox patchSelector;


PatchSelector patchSelector;

HexFmAudioProcessor* processor;
private:
Expand Down
60 changes: 58 additions & 2 deletions Source/PatchTypeData.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,30 @@ struct PatchGroup
void loadFilesForType(juce::File& folder)
{
auto allFiles = folder.findChildFiles(juce::File::TypesOfFileToFind::findFiles, true);

for(auto f : allFiles)
{
std::unique_ptr<juce::XmlElement> currentXml = juce::parseXML(f);
if(currentXml != nullptr)
{
if(currentXml->hasAttribute("HexFmPatchType"))
{
auto checkType = currentXml->getStringAttribute("HexFmPatchType");
if(checkType == PatchTypeStrings[(int)type])
patches.push_back(f);

}
}
}
}
juce::String nameOfLastPatch()
{
if(patches.size() > 0)
{
std::unique_ptr<juce::XmlElement> currentXml = juce::parseXML(patches[patches.size() - 1]);
if(currentXml->hasAttribute("HexFmPatchName"))
return currentXml->getStringAttribute("HexFmPatchName");
}
return "";
}
};

Expand All @@ -71,7 +94,6 @@ struct PatchLibrary
auto patchFolder = appFolder.getChildFile("HexFM_Patches");
if(patchFolder.exists() && patchFolder.isDirectory())
{
printf("patch folder exists\n");
patchFolder.setAsCurrentWorkingDirectory();
}
else
Expand All @@ -81,6 +103,40 @@ struct PatchLibrary
printf("patch folder created\n");
}
presetFolder = patchFolder;
bassGroup.loadFilesForType(presetFolder);
leadGroup.loadFilesForType(presetFolder);
chordGroup.loadFilesForType(presetFolder);
padGroup.loadFilesForType(presetFolder);
}
PatchGroup* getGroup(juce::String& typeStr)
{
PatchType type = PatchType::lead;
int idx = 0;
for(auto s : PatchTypeStrings)
{
if(s == typeStr)
type = (PatchType)idx;
++idx;
}
switch(type)
{
case PatchType::bass:
{
return &bassGroup;
}
case PatchType::lead:
{
return &leadGroup;
}
case PatchType::chord:
{
return &chordGroup;
}
case PatchType::pad:
{
return &padGroup;
}
}
}
PatchGroup bassGroup;
PatchGroup leadGroup;
Expand Down

0 comments on commit 5f44d68

Please sign in to comment.