Skip to content

Commit

Permalink
- loris_library code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
christoph-hart committed Apr 11, 2023
1 parent b6f8fba commit d449584
Show file tree
Hide file tree
Showing 11 changed files with 1,260 additions and 923 deletions.
112 changes: 112 additions & 0 deletions loris_library/Source/Helpers.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*
* This file is part of the HISE loris_library codebase (https://github.com/christophhart/loris-tools).
* Copyright (c) 2023 Christoph Hart
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "Helpers.h"
#include "LorisState.h"

namespace loris2hise
{

juce::var Options::toJSON() const
{
juce::DynamicObject::Ptr obj = new juce::DynamicObject();

obj->setProperty(OptionIds::timedomain,
getTimeDomainOptions()[(int)currentDomainType]);

obj->setProperty(OptionIds::freqfloor, freqfloor);
obj->setProperty(OptionIds::ampfloor, ampfloor);
obj->setProperty(OptionIds::sidelobes, sidelobes);
obj->setProperty(OptionIds::freqdrift, freqdrift);
obj->setProperty(OptionIds::hoptime, hoptime);
obj->setProperty(OptionIds::croptime, croptime);
obj->setProperty(OptionIds::bwregionwidth, bwregionwidth);
obj->setProperty(OptionIds::windowwidth, windowwidth);
obj->setProperty(OptionIds::enablecache, enablecache);

return juce::var(obj.get());
}

juce::StringArray Options::getTimeDomainOptions()
{
static const juce::StringArray options =
{
"seconds",
"samples",
"0to1"
};

return options;
}

void Options::initLorisParameters()
{
hoptime = analyzer_getHopTime();
croptime = analyzer_getCropTime();
freqfloor = analyzer_getFreqFloor();
freqdrift = analyzer_getFreqDrift();
ampfloor = analyzer_getAmpFloor();
sidelobes = analyzer_getSidelobeLevel();
bwregionwidth = analyzer_getBwRegionWidth();
windowwidth = analyzer_getWindowWidth();
enablecache = false;
}

bool Options::update(const juce::Identifier& id, const juce::var& value)
{
if (id == OptionIds::timedomain)
{
auto x = value.toString().trim().unquoted();

auto idx = getTimeDomainOptions().indexOf(x);

if (idx == -1)
throw juce::Result::fail("unknown time domain option: " + value.toString());

currentDomainType = (TimeDomainType)idx;

return true;
}

return true;

if (id == OptionIds::freqfloor) { freqfloor = (double)value; analyzer_setFreqFloor(freqfloor); return true; }
if (id == OptionIds::ampfloor) { ampfloor = (double)value; analyzer_setAmpFloor(ampfloor); return true; }
if (id == OptionIds::sidelobes) { sidelobes = (double)value; analyzer_setSidelobeLevel(sidelobes); return true; }
if (id == OptionIds::freqdrift) { freqdrift = (double)value; analyzer_setFreqDrift(freqdrift); return true; }
if (id == OptionIds::hoptime) { hoptime = (double)value; analyzer_setHopTime(hoptime); return true; }
if (id == OptionIds::croptime) { croptime = (double)value; analyzer_setCropTime(croptime); return true; }
if (id == OptionIds::bwregionwidth) { bwregionwidth = (double)value; analyzer_setBwRegionWidth(bwregionwidth); return true; }
if (id == OptionIds::enablecache) { enablecache = (bool)value; return true; }
if (id == OptionIds::windowwidth) { windowwidth = (double)value; }
if (id == OptionIds::windowwidth) { windowwidth = (double)value; }

throw juce::Result::fail("Invalid option: " + id.toString());
}

void Helpers::reportError(const char* msg)
{
if (LorisState::getCurrentInstance() != nullptr)
LorisState::getCurrentInstance()->reportError(msg);
}

void Helpers::logMessage(const char* msg)
{
if (LorisState::getCurrentInstance() != nullptr)
LorisState::getCurrentInstance()->messages.add(juce::String(msg));
}

}
130 changes: 130 additions & 0 deletions loris_library/Source/Helpers.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/*
* This file is part of the HISE loris_library codebase (https://github.com/christophhart/loris-tools).
* Copyright (c) 2023 Christoph Hart
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#pragma once

#include "src/loris.h"

#include "src/Breakpoint.h"
#include "src/PartialUtils.h"

#include <JuceHeader.h>

namespace loris2hise
{
using namespace juce;


enum class TimeDomainType
{
Seconds,
Samples,
Normalised,
Frequency,
numTimeDomainTypes
};

struct Options
{
var toJSON() const;

static StringArray getTimeDomainOptions();

void initLorisParameters();

bool update(const Identifier& id, const var& value);

TimeDomainType currentDomainType = TimeDomainType::Seconds;
double freqfloor;
double ampfloor = 90.0;
double sidelobes = 90.0;
double freqdrift;
double hoptime = 0.0129;
double croptime = 0.0129;
double bwregionwidth = 1.0;
bool enablecache = true;
double windowwidth = 1.0;
};

struct Helpers
{
struct FunctionPOD
{
// Constants
int channelIndex = 0;
int partialIndex = 0;
double sampleRate = 44100.0;
double rootFrequency = 0.0;
void* obj = nullptr;

// Variable properties
double time = 0.0;
double frequency = 0.0;
double phase = 0.0;
double gain = 1.0;
double bandwidth = 0.0;

};

struct CustomFunctionArgs
{
CustomFunctionArgs(void* obj_, const Breakpoint& b, int channelIndex_,
int partialIndex_,
double sampleRate_,
double time_,
double rootFrequency_) :
channelIndex(channelIndex_),
partialIndex(partialIndex_),
sampleRate(sampleRate_),
rootFrequency(rootFrequency_),
obj(obj_),
time(time_)
{
static_assert(sizeof(CustomFunctionArgs) == sizeof(FunctionPOD), "not the same size");

frequency = b.frequency();
phase = b.phase();
gain = b.amplitude();
bandwidth = b.bandwidth();
};

// Constants
const int channelIndex = 0;
const int partialIndex = 0;
const double sampleRate = 44100.0;
const double rootFrequency = 0.0;
void* obj = nullptr;

// Variable properties
double time = 0.0;
double frequency = 0.0;
double phase = 0.0;
double gain = 1.0;
double bandwidth = 0.0;


};

using CustomFunctionType = bool(*)(CustomFunctionArgs&);
using CustomFunction = std::function<bool(CustomFunctionArgs&)>;

static void reportError(const char* msg);

static void logMessage(const char* msg);
};

}
Loading

0 comments on commit d449584

Please sign in to comment.