Skip to content

Commit

Permalink
Fix issue #30 Normalization
Browse files Browse the repository at this point in the history
  • Loading branch information
brummer10 committed Dec 16, 2024
1 parent 60396d6 commit 5de2620
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 5 deletions.
7 changes: 7 additions & 0 deletions Ratatouille/ModelerSelector.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class ModelerBase {
virtual inline void clearState() {}
virtual inline void init(unsigned int sample_rate) {}
virtual void connect(uint32_t port,void* data) {}
virtual inline void normalize(int count, float *buf) {}
virtual inline void compute(int count, float *input0, float *output0) {}
virtual bool loadModel() { return false;}
virtual void unloadModel() {}
Expand Down Expand Up @@ -76,11 +77,13 @@ class NeuralModel : public ModelerBase {

public:
std::string modelFile;
float nGain;

void setModelFile(std::string modelFile_) override { modelFile = modelFile_;}
inline void clearState() override;
inline void init(unsigned int sample_rate) override;
void connect(uint32_t port,void* data) override;
inline void normalize(int count, float *buf) override;
inline void compute(int count, float *input0, float *output0) override;
bool loadModel() override;
void unloadModel() override;
Expand Down Expand Up @@ -118,6 +121,7 @@ class RtNeuralModel : public ModelerBase {
inline void clearState() override;
inline void init(unsigned int sample_rate) override;
void connect(uint32_t port,void* data) override;
inline void normalize(int count, float *buf) override;
inline void compute(int count, float *input0, float *output0) override;
bool loadModel() override;
void unloadModel() override;
Expand Down Expand Up @@ -151,6 +155,9 @@ class ModelerSelector {
void connect(uint32_t port,void* data) {
return modeler->connect(port, data);}

inline void normalize(int count, float *buf) {
return modeler->normalize(count, buf); }

inline void compute(int count, float *input0, float *output0) {
return modeler->compute(count, input0, output0);}

Expand Down
18 changes: 17 additions & 1 deletion Ratatouille/NeuralModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ NeuralModel::NeuralModel(std::condition_variable *Sync)
: model(nullptr), smp(), SyncWait(Sync) {
nam::activations::Activation::enable_fast_tanh();
loudness = 0.0;
nGain = 1.0;
needResample = 0;
isInited = false;
ready.store(false, std::memory_order_release);
Expand All @@ -42,6 +43,16 @@ void NeuralModel::connect(uint32_t port,void* data)
{
}

inline void NeuralModel::normalize(int count, float *buf)
{
if (!model) return;
if (nGain != 1.0) {
for (int i0 = 0; i0 < count; i0 = i0 + 1) {
buf[i0] = float(double(buf[i0]) * nGain);
}
}
}

inline void NeuralModel::compute(int count, float *input0, float *output0)
{
if (!model) return;
Expand Down Expand Up @@ -105,7 +116,12 @@ bool NeuralModel::loadModel() {

if (model) {
// fprintf(stderr, "load model\n");
if (model->HasLoudness()) loudness = model->GetLoudness();
if (model->HasLoudness()) {
loudness = model->GetLoudness();
nGain = pow(10.0, (-18.0 - loudness) / 20.0);
} else {
nGain = 1.0;
}
modelSampleRate = static_cast<int>(model->GetExpectedSampleRate());
//model->SetLoudness(-15.0);
if (modelSampleRate <= 0) modelSampleRate = 48000;
Expand Down
5 changes: 4 additions & 1 deletion Ratatouille/Ratatouille.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/


#define CONTROLS 8
#define CONTROLS 10

#define GUI_ELEMENTS 0

Expand Down Expand Up @@ -385,6 +385,9 @@ void plugin_create_controller_widgets(X11_UI *ui, const char * plugin_uri) {
combobox_add_entry(ps->mb.fbutton, "None");
ps->mb.fbutton->func.value_changed_callback = file_menu_callback;

ui->widget[8] = add_lv2_toggle_button (ui->widget[8], ui->win, 12, "", ui, 70, 248, 25, 25);
ui->widget[9] = add_lv2_toggle_button (ui->widget[9], ui->win, 13, "", ui, 70, 288, 25, 25);

ps->ir.fbutton = add_lv2_button(ps->ir.fbutton, ui->win, "", ui, 545, 324, 22, 30);
ps->ir.fbutton->parent_struct = (void*)&ps->ir;
combobox_set_pop_position(ps->ir.fbutton, 0);
Expand Down
10 changes: 10 additions & 0 deletions Ratatouille/Ratatouille.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ class Xratatouille
float* _normB;
uint32_t normA;
uint32_t normB;
float* _normSlotA;
float* _normSlotB;
double fRec0[2];
double fRec3[2];
double fRec2[2];
Expand Down Expand Up @@ -373,6 +375,12 @@ void Xratatouille::connect_(uint32_t port,void* data)
case 11:
_inputGain1 = static_cast<float*>(data);
break;
case 12:
_normSlotA = static_cast<float*>(data);
break;
case 13:
_normSlotB = static_cast<float*>(data);
break;
default:
break;
}
Expand Down Expand Up @@ -601,6 +609,7 @@ inline const LV2_Atom* Xratatouille::read_set_file(const LV2_Atom_Object* obj) {
// process slotB in parallel thread
inline void Xratatouille::processSlotB() {
slotB.compute(bufsize, _bufb, _bufb);
if (*(_normSlotB)) slotB.normalize(bufsize, _bufb);
}

// process second convolver in parallel thread
Expand Down Expand Up @@ -736,6 +745,7 @@ void Xratatouille::run_dsp_(uint32_t n_samples)
// process slot A
if (_neuralA.load(std::memory_order_acquire)) {
slotA.compute(n_samples, bufa, bufa);
if (*(_normSlotA)) slotA.normalize(n_samples, bufa);
}

//wait for parallel processed slot B when needed
Expand Down
20 changes: 20 additions & 0 deletions Ratatouille/Ratatouille.ttl
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,26 @@ A Neural Model loader and mixer
lv2:default 0.000000 ;
lv2:minimum -20.000000 ;
lv2:maximum 20.000000 ;
], [
a lv2:InputPort ,
lv2:ControlPort ;
lv2:index 12 ;
lv2:portProperty lv2:toggled ;
lv2:symbol "NormalizeSlotA" ;
lv2:name "Normalize Slot A" ;
lv2:default 0.0 ;
lv2:minimum 0.0 ;
lv2:maximum 1.0 ;
], [
a lv2:InputPort ,
lv2:ControlPort ;
lv2:index 13 ;
lv2:portProperty lv2:toggled ;
lv2:symbol "NormalizeSlotB" ;
lv2:name "Normalize Slot B" ;
lv2:default 0.0 ;
lv2:minimum 0.0 ;
lv2:maximum 1.0 ;
] .

<urn:brummer:ratatouille_ui>
Expand Down
5 changes: 5 additions & 0 deletions Ratatouille/RtNeuralModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ void RtNeuralModel::connect(uint32_t port,void* data)
{
}

inline void RtNeuralModel::normalize(int count, float *buf)
{
// not implemented
}

inline void RtNeuralModel::compute(int count, float *input0, float *output0)
{
if (!model ) return;
Expand Down
6 changes: 3 additions & 3 deletions Ratatouille/makefile
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ endif
# check for c++ level
ifeq (,$(filter clean,$(MAKECMDGOALS)))
ifeq (,$(filter install,$(MAKECMDGOALS)))
$(info $(yellow) INFO: $(reset)build with $(blue)$(CXX)$(reset))
$(info $(yellow) INFO: $(reset)build with $(blue)$(CXX) $(CXX_v)$(reset))
ifeq ($(shell awk -v a="$(CXX_v)" -v b="11" 'BEGIN{print(a<b)}'), 1)
CXXFLAGS += -std=c++17
$(info $(yellow) INFO: $(reset)using $(blue)-std=c++17$(reset))
Expand Down Expand Up @@ -182,7 +182,7 @@ ifeq ($(TARGET), Linux)
-Wl,-z,noexecstack -Wl,--no-undefined -Wl,--gc-sections -Wl,--exclude-libs,ALL \
`$(PKGCONFIG) --cflags --libs sndfile`

CXXFLAGS += -MMD -flto=auto -fPIC -DPIC -Ofast -Wall -funroll-loops $(SSE_CFLAGS) \
CXXFLAGS += -MMD -flto=auto -fPIC -DPIC -O3 -Wall -funroll-loops $(SSE_CFLAGS) \
-Wno-sign-compare -Wno-reorder -Wno-infinite-recursion -DUSE_ATOM $(FFT_FLAG) \
-fomit-frame-pointer -fstack-protector -fvisibility=hidden \
-fdata-sections -I. -I./ -I./zita-resampler-1.1.0 -I$(CONV_DIR) \
Expand All @@ -193,7 +193,7 @@ ifeq ($(TARGET), Linux)
ifeq (,$(findstring clang, $(CXX)))
CXXFLAGS += -fstrength-reduce -fno-fat-lto-objects -Wno-deprecated-declarations
else
CXXFLAGS += -Wno-unused-private-field -fdenormal-fp-math=positive-zero
CXXFLAGS += -Wno-unused-private-field -fdenormal-fp-math=positive-zero -Wno-vla-cxx-extension
endif
else
CXXFLAGS += -Wno-unused-private-field -fdenormal-fp-math=positive-zero \
Expand Down

0 comments on commit 5de2620

Please sign in to comment.