Skip to content

Commit

Permalink
Code clean-up and fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Ichthyostega committed Sep 16, 2024
1 parent a6fac59 commit 11da28e
Show file tree
Hide file tree
Showing 104 changed files with 1,586 additions and 1,797 deletions.
20 changes: 11 additions & 9 deletions Util/midiListGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,19 @@
#include <cstring>
#include <regex>

using std::string;

std::string asLongString(double n, size_t digits)

string asLongString(double n, size_t digits)
{
std::ostringstream oss;
oss.precision(digits);
oss.width(digits);
oss << n;
std::string value = oss.str();
string value = oss.str();
value = std::regex_replace(value, std::regex("^ +"), "");

if (value.find('.') == std::string::npos)
if (value.find('.') == string::npos)
value += '.';
while (value.length() <= digits)
value += '0';
Expand All @@ -54,16 +56,16 @@ int main(void)
std::cout.precision(10);
std::cout << "twelfth root of two = " << multiplier << std::endl;

static std::string names [] = {
static string names [] = {
"A", "#", "B", "C", "#", "D", "#", "E", "F", "#", "G", "#"
};
int stringcount = 0;
int octave = 0;
double result = 27.5;
int precision = 6;
std::string currentNote;
std::string fullString;
std::vector <std::string> ourlist;
string currentNote;
string fullString;
std::vector <string> ourlist;
for (int i = 21; i < 109; ++i) // practical MIDI note range
{
currentNote = names[stringcount];
Expand All @@ -81,7 +83,7 @@ int main(void)
result *= multiplier;
}
size_t idx = ourlist.size();
ofstream midiList;
std::ofstream midiList;
midiList.open("midiList.txt");
if (!midiList.is_open())
{
Expand All @@ -91,7 +93,7 @@ int main(void)
while (idx > 0)
{
--idx;
midiList << ourlist[idx] << endl;
midiList << ourlist[idx] << std::endl;
}
midiList.close();
return 0;
Expand Down
27 changes: 13 additions & 14 deletions src/CLI/CmdInterpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1471,7 +1471,7 @@ int CmdInterpreter::effects(Parser& input, unsigned char controlType)
else
{
value = string2int(input) - 1;
if (value >= Runtime.NumAvailableParts || value < 0)
if (value >= int(Runtime.NumAvailableParts) || value < 0)
return REPLY::range_msg;
}
effSend = value;
Expand Down Expand Up @@ -5339,8 +5339,7 @@ int CmdInterpreter::waveform(Parser& input, unsigned char controlType)

int CmdInterpreter::commandPart(Parser& input, unsigned char controlType)
{
Config &Runtime = synth->getRuntime();
int tmp = -1;
Config& Runtime = synth->getRuntime();
section = npart;
if (bitTest(context, LEVEL::AllFX))
return effects(input, controlType);
Expand Down Expand Up @@ -5382,22 +5381,22 @@ int CmdInterpreter::commandPart(Parser& input, unsigned char controlType)

if (input.isdigit())
{
tmp = string2int127(input);
uint num = string2int127(input);
input.skipChars();
if (tmp > 0)
if (num > 0)
{
tmp -= 1;
num -= 1;
if (!inKitEditor)
{
if (tmp >= Runtime.NumAvailableParts)
if (num >= Runtime.NumAvailableParts)
{
Runtime.Log("Part number too high");
return REPLY::done_msg;
}

//if (npart != tmp) // TODO sort this properly!
//if (npart != num) // TODO sort this properly!
{
npart = tmp;
npart = num;
section = npart;
if (controlType == TOPLEVEL::type::Write)
{
Expand All @@ -5416,9 +5415,9 @@ int CmdInterpreter::commandPart(Parser& input, unsigned char controlType)
{
if (controlType == TOPLEVEL::type::Write)
{
if (tmp >= NUM_KIT_ITEMS)
if (num >= NUM_KIT_ITEMS)
return REPLY::range_msg;
kitNumber = tmp;
kitNumber = num;
voiceNumber = 0;// to avoid confusion
}
Runtime.Log("Kit item number " + to_string(kitNumber + 1));
Expand Down Expand Up @@ -5502,7 +5501,7 @@ int CmdInterpreter::commandPart(Parser& input, unsigned char controlType)
sendDirect(synth, 0, inst, controlType, MAIN::control::loadInstrumentFromBank, TOPLEVEL::section::main, npart, bank, root);
return REPLY::done_msg;
}
tmp = string2int(input) - 1;
int tmp = string2int(input) - 1;
if (tmp < 0 || tmp >= MAX_INSTRUMENTS_IN_BANK)
return REPLY::range_msg;
sendDirect(synth, 0, tmp, controlType, MAIN::control::loadInstrumentFromBank, TOPLEVEL::section::main, npart);
Expand Down Expand Up @@ -5541,7 +5540,7 @@ int CmdInterpreter::commandPart(Parser& input, unsigned char controlType)
if (!readControl(synth, 0, PART::control::enable, npart))
return REPLY::inactive_msg;

tmp = -1;
int tmp = -1;
if (input.matchnMove(3, "normal"))
tmp = PART::kitType::Off;
else if (input.matchnMove(2, "multi"))
Expand Down Expand Up @@ -6463,7 +6462,7 @@ Reply CmdInterpreter::cmdIfaceProcessCommand(Parser& input)
input.reset_to_mark();
else
{
int tmp = string2int(input);
uint tmp = string2int(input);
if (tmp < 1 || tmp > Runtime.NumAvailableParts)
return REPLY::range_msg;

Expand Down
2 changes: 1 addition & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ set (Effects_sources
)

set (Misc_sources
Misc/Bank.cpp Misc/BuildScheduler.cpp Misc/CmdOptions.cpp Misc/DataBlockBuff.cpp
Misc/Bank.cpp Misc/BuildScheduler.cpp Misc/CmdOptions.cpp
Misc/Config.cpp Misc/InstanceManager.cpp Misc/Microtonal.cpp Misc/Part.cpp
Misc/SynthEngine.cpp Misc/WavFile.cpp Misc/XMLwrapper.cpp
)
Expand Down
2 changes: 1 addition & 1 deletion src/DSP/FormantFilter.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class FormantFilter : public Filter_
void setfreq(float frequency);
void setfreq_and_q(float frequency, float q_);
void setq(float q_);
void cleanup(void);
void cleanup();

private:
void setpos(float input);
Expand Down
2 changes: 1 addition & 1 deletion src/DSP/SVFilter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ void SVFilter::cleanup()
}


void SVFilter::computefiltercoefs(void)
void SVFilter::computefiltercoefs()
{
// calculations done in doubles for better portability of results
par.f = double(freq) / synth->samplerate * 4.0;
Expand Down
6 changes: 3 additions & 3 deletions src/DSP/SVFilter.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class SVFilter : public Filter_
SVFilter(SynthEngine* _synth, uchar Ftype, float Ffreq, float Fq, uchar Fstages);
SVFilter(SVFilter const& orig);
Filter_* clone() { return new SVFilter(*this); };
void filterout(float *smp);
void filterout(float* smp);
void setfreq(float frequency);
void setfreq_and_q(float frequency, float q_);
void setq(float q_);
Expand All @@ -57,8 +57,8 @@ class SVFilter : public Filter_
float f, q, q_sqrt;
} par, ipar;

void singlefilterout(float *smp, fstage &x, parameters &par);
void computefiltercoefs(void);
void singlefilterout(float* smp, fstage& x, parameters& par);
void computefiltercoefs();
int type; // The type of the filter (LPF1,HPF1,LPF2,HPF2...)
int stages; // how many times the filter is applied (0->1,1->2,etc.)
float freq; // Frequency given in Hz
Expand Down
51 changes: 22 additions & 29 deletions src/DSP/Unison.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,19 @@
using func::power;


Unison::Unison(int update_period_samples_, float max_delay_sec_, SynthEngine *_synth) :
unison_size(0),
base_freq(1.0f),
max_delay(std::max(10, int(_synth->samplerate_f * max_delay_sec_) + 1)),
delay_k(0),
first_time(false),
voice{},
delay_buffer{new float[max_delay]{0}}, // zero-init
update_period_samples(update_period_samples_),
update_period_sample_k(0),
unison_amplitude_samples(0.0f),
unison_bandwidth_cents(10.0f),
synth(_synth)
Unison::Unison(int update_period_samples_, float max_delay_sec_, SynthEngine* _synth)
: unison_size{0}
, base_freq{1.0f}
, max_delay{std::max(10, int(_synth->samplerate_f * max_delay_sec_) + 1)}
, delay_k{0}
, first_time{false}
, voice{}
, delay_buffer{new float[max_delay]{0}} // zero-init
, update_period_samples{update_period_samples_}
, update_period_sample_k{0}
, unison_amplitude_samples{0.0f}
, unison_bandwidth_cents{10.0f}
, synth{_synth}
{
setSize(1);
}
Expand Down Expand Up @@ -83,20 +83,18 @@ void Unison::setBandwidth(float bandwidth)
bandwidth = 0.0f;
if (bandwidth > 1200.0f)
bandwidth = 1200.0f;
// printf("band %f\n", bandwidth);
//#warning
// : todo: if bandwidth is too small the audio will be self canceled (because of the sign change of the outputs)
// : todo: if bandwidth is too small the audio will be self cancelled (because of the sign change of the outputs)
unison_bandwidth_cents = bandwidth;
updateParameters();
}


void Unison::updateParameters(void)
void Unison::updateParameters()
{
if (!voice)
return;
float increments_per_second = synth->samplerate_f / (float)update_period_samples;
// printf("#%g, %g\n",increments_per_second,base_freq);
float increments_per_second = synth->samplerate_f / float(update_period_samples);
for (int i = 0; i < unison_size; ++i)
{
float base = powf(UNISON_FREQ_SPAN, synth->numRandom() * 2.0f - 1.0f);
Expand All @@ -106,11 +104,9 @@ void Unison::updateParameters(void)
if (synth->numRandom() < 0.5f)
m = -m;
voice[i].step = m;
// printf("%g %g\n",uv[i].relative_amplitude,period);
}

float max_speed = power<2>(unison_bandwidth_cents / 1200.0f);
// printf("speed %f\n", max_speed);
unison_amplitude_samples = 0.125f * (max_speed - 1.0f) * synth->samplerate_f / base_freq;

//#warning
Expand All @@ -121,7 +117,7 @@ void Unison::updateParameters(void)
}


void Unison::process(int bufsize, float *inbuf, float *outbuf)
void Unison::process(int bufsize, float* inbuf, float* outbuf)
{
if (!voice)
return;
Expand All @@ -130,7 +126,7 @@ void Unison::process(int bufsize, float *inbuf, float *outbuf)

float volume = 1.0f / sqrtf(unison_size);
float xpos_step = 1.0f / update_period_samples;
float xpos = (float)update_period_sample_k * xpos_step;
float xpos = float(update_period_sample_k) * xpos_step;
for (int i = 0; i < bufsize; ++i)
{
if (update_period_sample_k++ >= update_period_samples)
Expand All @@ -146,7 +142,7 @@ void Unison::process(int bufsize, float *inbuf, float *outbuf)
for (int k = 0; k < unison_size; ++k)
{
float vpos = voice[k].realpos1 * (1.0f - xpos) + voice[k].realpos2 * xpos;
float pos = (float)(delay_k + max_delay) - vpos - 1.0f;
float pos = float(delay_k + max_delay) - vpos - 1.0f;
int posi = int(pos);
int posi_next = posi + 1;
if (posi >= max_delay)
Expand All @@ -158,10 +154,7 @@ void Unison::process(int bufsize, float *inbuf, float *outbuf)
sign = -sign;
}
outbuf[i] = out * volume;
// printf("%d %g\n",i,outbuf[i]);
delay_buffer[delay_k] = in;
//if ((++delay_k) >= max_delay)
// delay_k = 0;
delay_k = (++delay_k < max_delay) ? delay_k : 0;
}
}
Expand All @@ -175,7 +168,7 @@ void Unison::updateUnisonData()
float newval;
float pos;
float step;
float vibratto_val;
float vibratoFactor;
for (int k = 0; k < unison_size; ++k)
{
pos = voice[k].position;
Expand All @@ -191,14 +184,14 @@ void Unison::updateUnisonData()
pos = 1.0f;
step = -step;
}
vibratto_val = (pos - 0.333333333f * pos * pos * pos) * 1.5f; //make the vibratto lfo smoother
vibratoFactor = (pos - 1/3.0f * pos*pos*pos) * 1.5f; //make the vibrato LFO smoother

// #warning
// I will use relative amplitude, so the delay might be bigger than the whole buffer
// #warning
// I have to enlarge (reallocate) the buffer to make place for the whole delay

newval = 1.0f + 0.5f * (vibratto_val + 1.0f) * unison_amplitude_samples * voice[k].relative_amplitude;
newval = 1.0f + 0.5f * (vibratoFactor + 1.0f) * unison_amplitude_samples * voice[k].relative_amplitude;

if (first_time)
voice[k].realpos1 = voice[k].realpos2 = newval;
Expand Down
24 changes: 11 additions & 13 deletions src/DSP/Unison.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
/*
ZynAddSubFX - a software synthesizer
Unison.h - Unison effect (multivoice chorus)
Original author: Nasca Octavian Paul
Original ZynAddSubFX author Nasca Octavian Paul
Copyright (C) 2002-2009 Nasca Octavian Paul
Copyright 2009-2011, Alan Calvert
Expand Down Expand Up @@ -37,18 +36,18 @@ class SynthEngine;
class Unison
{
public:
Unison(int update_period_samples_, float max_delay_sec_, SynthEngine *_synth);
Unison(int update_period_samples_, float max_delay_sec_, SynthEngine*);
~Unison() = default;

void setSize(int new_size);
void setBaseFrequency(float freq);
void setBandwidth(float bandwidth_cents);

void process(int bufsize, float *inbuf, float *outbuf = NULL);
void process(int bufsize, float* inbuf, float* outbuf = nullptr);

private:
void updateParameters(void);
void updateUnisonData(void);
void updateParameters();
void updateUnisonData();

struct UnisonVoice {
float step; // base LFO
Expand Down Expand Up @@ -77,13 +76,12 @@ class Unison
std::unique_ptr<UnisonVoice[]> voice;
std::unique_ptr<float[]> delay_buffer;

int update_period_samples;
int update_period_sample_k;
float unison_amplitude_samples;
float unison_bandwidth_cents;
int update_period_samples;
int update_period_sample_k;
float unison_amplitude_samples;
float unison_bandwidth_cents;

SynthEngine *synth;
};

#endif

#endif /*UNISON_H*/
2 changes: 1 addition & 1 deletion src/Effects/Alienwah.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ void Alienwah::out(float *smpsl, float *smpsr)


// Cleanup the effect
void Alienwah::cleanup(void)
void Alienwah::cleanup()
{
Effect::cleanup();
for (int i = 0; i < Pdelay; ++i)
Expand Down
2 changes: 1 addition & 1 deletion src/Effects/Distorsion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ Distorsion::~Distorsion()


// Cleanup the effect
void Distorsion::cleanup(void)
void Distorsion::cleanup()
{
Effect::cleanup();
level.pushToTarget();
Expand Down
Loading

0 comments on commit 11da28e

Please sign in to comment.