Skip to content

Commit

Permalink
organize util files
Browse files Browse the repository at this point in the history
- separate Constants.h
- implementations separated into cpp file
  • Loading branch information
EwanMe committed Apr 17, 2024
1 parent 2977b7d commit 4aa93f9
Show file tree
Hide file tree
Showing 11 changed files with 226 additions and 200 deletions.
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ file(GLOB_RECURSE SRC_FILES
"src/ui/CustomLookAndFeel.cpp"
"src/ui/MainComponent.cpp"
"src/ui/UserInterface.cpp"
"src/Main.cpp"
"src/util/Util.cpp"
"src/Main.cpp"
)

target_sources(anyMidi PRIVATE ${SRC_FILES})
Expand Down
2 changes: 1 addition & 1 deletion src/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#include "./core/AudioProcessor.h"
#include "./ui/CustomLookAndFeel.h"
#include "./ui/MainComponent.h"
#include "./util/Globals.h"
#include "./util/Constants.h"

// NOLINTBEGIN(readability-identifier-naming)
namespace ProjectInfo {
Expand Down
9 changes: 5 additions & 4 deletions src/core/AudioProcessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*/

#include "AudioProcessor.h"
#include "../util/Globals.h"
#include "../util/Util.h"

namespace {
double midiToFrequency(const int &note) {
Expand Down Expand Up @@ -53,9 +53,9 @@ anyMidi::AudioProcessor::AudioProcessor(double sampleRate,
numOutputChannels);
});
} else {
if (AUDIO_SETTINGS_FILE.existsAsFile()) {
if (anyMidi::getAudioSettingsFile().existsAsFile()) {
// Loads settings from file if it exists.
const auto storedSettings = juce::parseXML(AUDIO_SETTINGS_FILE);
const auto storedSettings = juce::parseXML(getAudioSettingsFile());
setAudioChannels(numInputChannels, numOutputChannels,
storedSettings.get());
} else {
Expand Down Expand Up @@ -108,7 +108,8 @@ anyMidi::AudioProcessor::~AudioProcessor() {

if (audioDeviceSettings != nullptr) {
// Writes user settings to XML file for storage.
AUDIO_SETTINGS_FILE.replaceWithText(audioDeviceSettings->toString());
anyMidi::getAudioSettingsFile().replaceWithText(
audioDeviceSettings->toString());
}

deviceManager_->removeAudioCallback(&audioSourcePlayer_);
Expand Down
2 changes: 1 addition & 1 deletion src/core/ForwardFFT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

#include "ForwardFFT.h"

#include "../util/Globals.h"
#include "../util/Util.h"

anyMidi::ForwardFFT::ForwardFFT(
const double sampleRate,
Expand Down
2 changes: 1 addition & 1 deletion src/ui/MainComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

#include "../ui/MainComponent.h"
#include "../core/AudioProcessor.h"
#include "../util/Globals.h"
#include "../util/Util.h"
#include "UserInterface.h"

anyMidi::MainComponent::MainComponent(const juce::ValueTree &v)
Expand Down
89 changes: 1 addition & 88 deletions src/ui/UserInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,96 +11,9 @@
#include <format>

#include "../core/AudioProcessor.h"
#include "../util/Globals.h"
#include "../util/Util.h"
#include "UserInterface.h"

namespace {

/**
* @brief Removes a named child from a value tree.
*
* @param parentTree Value tree to remove from
* @param name Name of child to remove
*/
void removeChildWithName(juce::ValueTree &parentTree,
const juce::Identifier &name) {
for (int i = 0; i < parentTree.getNumChildren(); ++i) {
auto child = parentTree.getChild(i);
if (child.hasType(name)) {
parentTree.removeChild(i, nullptr);
return;
}
}
}

/**
* @brief Writes value tree to file.
*
* @param tree Value tree to store
* @param file File to write to
* @param debug Whether to log the operations
*/
void writeSettingsToFile(const juce::ValueTree &tree, const juce::File &file,
bool debug = false) {
auto xml = tree.toXmlString();

// Save state as xml file to local dir.
if (file.replaceWithText(xml) && debug) {
anyMidi::log(tree, std::format("State written to {}",
file.getFullPathName().toStdString()));
} else if (debug) {
anyMidi::log(tree, "Failed to write state to file.");
}
}

/**
* @brief Writes root of value tree to timestamped file for debug. Removes all
* binary nodes, but restores the value tree to previous state after
* write.
*
* @param tree Value tree to store
*/
void storeDebugSettings(juce::ValueTree &tree) {
// Reference Counted Objects can't be serialized into XML.
// Device manager is stored and the node it lies in is replaced with a
// string while XML is generated. After, the device manager is added
// back into the tree as a RCO.
auto root = tree.getRoot();
auto audioProcNode = root.getChildWithName(anyMidi::AUDIO_PROC_ID);
auto *deviceManager = dynamic_cast<anyMidi::AudioDeviceManagerRCO *>(
audioProcNode.getProperty(anyMidi::DEVICE_MANAGER_ID).getObject());

if (deviceManager) {
audioProcNode.setProperty(
anyMidi::DEVICE_MANAGER_ID,
"Audio device manager exists, settings in separate file.", nullptr);
} else {
audioProcNode.setProperty(anyMidi::DEVICE_MANAGER_ID,
"No audio device manager found.", nullptr);
}

writeSettingsToFile(root, anyMidi::getTimestampedAppSettingsFile());

// Reset the altered node.
audioProcNode.setProperty(anyMidi::DEVICE_MANAGER_ID, deviceManager,
nullptr);
}

/**
* @brief Find GUI node and stores the value tree to file. Log and list of
* windowing functions needs not be persisted between restarts.
*
* @param tree The value tree to store
*/
void storeSettings(juce::ValueTree &tree) {
auto guiNode = tree.getRoot().getChildWithName(anyMidi::GUI_ID);
guiNode.removeProperty(anyMidi::LOG_ID, nullptr);
removeChildWithName(guiNode, anyMidi::ALL_WIN_ID);

writeSettingsToFile(guiNode, anyMidi::getAppSettingsFile());
}
} // anonymous namespace

anyMidi::TabbedComp::TabbedComp(const juce::ValueTree &v)
: TabbedComponent(juce::TabbedButtonBar::TabsAtTop), tree_{v},
audioSetupPage_{v}, appSettingsPage_{v}, debugPage_{v} {
Expand Down
2 changes: 1 addition & 1 deletion src/ui/UserInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#include <juce_gui_basics/juce_gui_basics.h>
#include <juce_gui_extra/juce_gui_extra.h>

#include "../util/Globals.h"
#include "../util/Util.h"
#include "CustomLookAndFeel.h"

namespace anyMidi {
Expand Down
28 changes: 28 additions & 0 deletions src/util/Constants.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#pragma once

#include <juce_data_structures/juce_data_structures.h>

namespace anyMidi {
static const juce::String AUDIO_SETTINGS_FILENAME = "audio_device_settings.xml";

static const juce::Identifier ROOT_ID{"AnyMidi"};

static const juce::Identifier AUDIO_PROC_ID{"AudioProcessor"};
static const juce::Identifier DEVICE_MANAGER_ID{"DeviceManager"};

static const juce::Identifier GUI_ID{"UserSettings"};
static const juce::Identifier ATTACK_THRESH_ID{"AttackThreshold"};
static const juce::Identifier RELEASE_THRESH_ID{"ReleaseThreshold"};
static const juce::Identifier PARTIALS_ID{"NumParitals"};
static const juce::Identifier LO_CUT_ID{"LowCutFrequenzy"};
static const juce::Identifier HI_CUT_ID{"HighCutFrequenzy"};
static const juce::Identifier LOG_ID{"Log"};

static const juce::Identifier ALL_WIN_ID{"AllWindowFunc"};
static const juce::Identifier CURRENT_WIN_ID{"CurrentWindowFunc"};
static const juce::Identifier WIN_NODE_ID{"Window"};
static const juce::Identifier WIN_NAME_ID{"WindowName"};

constexpr double msToSec{0.001};
constexpr double defaultSampleRate{48000};
} // namespace anyMidi
103 changes: 0 additions & 103 deletions src/util/Globals.h

This file was deleted.

Loading

0 comments on commit 4aa93f9

Please sign in to comment.