Skip to content

Commit

Permalink
Fully Working Tremolo
Browse files Browse the repository at this point in the history
  • Loading branch information
FedericoLonghin committed Sep 11, 2023
1 parent 87ae035 commit c5cd5c7
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 16 deletions.
5 changes: 3 additions & 2 deletions src/embedded/Synth_Engine/RequestMenager.ino
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,10 @@ void requestAction(String msg) {
break;
case 3:
if (control == 29) { // Space Motion
audioEngine.soundList[1].EFX_tremolo_enable = value;
audioEngine.soundList[1].Trem.setDepth(value ? 2 : 8);
audioEngine.soundList[1].Trem.enable = 1;
} else if (control == 30) { //Space Slow Fast
audioEngine.soundList[1].EFX_tremolo_speed = value ? 2 : 10;
audioEngine.soundList[1].Trem.setSpeed(value ? 3 : 10);
}
break;
}
Expand Down
70 changes: 70 additions & 0 deletions src/embedded/Synth_Engine/Sound.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,74 @@
#include "Arduino.h"
#include "Sound.h"

/*
* LFO
*/

LFO::LFO() {}

void LFO::begin(int _freq) {
this->freq_sp = _freq;
this->freq_pv = _freq;
generateTable();
}

void LFO::generateTable() {
for (int i = 0; i < Wavetable_Length; i++) {
WaveTable[i] = (sin(i * TWO_PI / (float)Wavetable_Length) + 1) / 2;
}
}

float LFO::getValAutoIncrement(bool _increment) {
byte addr = (byte)(((autoIncrementIndex * freq_pv) % Sample_Rate) * (float)(Wavetable_Length / (float)Sample_Rate));

if (addr == 0 && freq_ramp_enable_next_step) {
if (freq_pv < freq_sp) {
freq_pv += 1;
Serial.println("freq_pv++");
autoIncrementIndex = 0;
} else if (freq_pv > freq_sp) {
freq_pv -= 1;
Serial.println("freq_pv--");
autoIncrementIndex = 0;
}
freq_ramp_enable_next_step = false;
}

if (addr != 0) { freq_ramp_enable_next_step = true; }

if (_increment) { autoIncrementIndex++; }
if (autoIncrementIndex >= Sample_Rate) autoIncrementIndex = 0;
return WaveTable[addr];
}

void LFO::setSpeed(int _speed) {
freq_sp = _speed;
}

/*
* Tremolo
*/

void Tremolo::begin(int _speed, int _depth) {
this->speed_sp = _speed;
this->depth_sp = _depth;
this->depth_pv = _depth;
this->_tremLFO.begin(_speed);
}

void Tremolo::setSpeed(int _speed) {
_tremLFO.freq_sp = _speed;
}

void Tremolo::setDepth(float _depth) {
depth_sp = _depth;
}

float Tremolo::getVal(bool _increment) {
if (!enable) return 1;
if (depth_pv < depth_sp) depth_pv += 0.001;
else if (depth_pv > depth_sp) depth_pv -= 0.001;

return 1 + (_tremLFO.getValAutoIncrement(_increment) * (depth_pv / (float)10));
}
40 changes: 36 additions & 4 deletions src/embedded/Synth_Engine/Sound.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,45 @@
#include "Definitions.h"
#include "Envelope.h"


class LFO {
private:
int freq_pv;
bool freq_ramp_enable_next_step = false;
unsigned int autoIncrementIndex = 0;
public:
int freq_sp;
float WaveTable[Wavetable_Length];
LFO();
void begin(int _freq);
void generateTable();
float getValAutoIncrement(bool _increment);
void setSpeed(int _speed);
};

class Tremolo {
private:
int speed_sp; //[1,Inf[
float depth_sp; //[0,10]
float depth_pv;
LFO _tremLFO;

public:
bool enable = false;
void begin(int _speed, int _depth);
void setSpeed(int _speed);
void setDepth(float _depth);
float getVal(bool _increment);
};

class Sound {
public:
Envelope ADSR;
byte Wavetype;
bool EFX_tremolo_enable = false;
int EFX_tremolo_speed = 10;
float EFX_tremolo_depth = 5;

Tremolo Trem;
void setDefaultParam() {

Trem.begin(1, 5);
ADSR.Env_At = 100;
ADSR.Env_Al = 255;
ADSR.Env_Dt = 100;
Expand All @@ -25,4 +54,7 @@ class Sound {
};





#endif
16 changes: 7 additions & 9 deletions src/embedded/Synth_Engine/SynthEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ int uselessCounter = 0;
// generateAudioChunk()
byte sampleVal;
byte ampl;
float trem;
float oldtrem;
unsigned int totalWaveVal;
float divider = Wavetable_Length / (float)(Sample_Rate);

Expand Down Expand Up @@ -176,31 +178,27 @@ void SynthEngine::AudioCompositorHandler() {
uselessCounter++; //just for triggering WDT
}
}
float trem;
void SynthEngine::generateAudioChunk(int len, bool _section) {
int noteNum = getActiveNotesNumber();
for (int a = 0; a < len; a++) {

totalWaveVal = 0;
for (int f = 0; f < noteNum; f++) {
// FillBufferIndex = 0;
// for (int i = 0; i < Sample_Rate; i++) {
int ObjAddr = activeNoteList[f];
// for (int i = 0; i < 4000; i++) {

// Serial.printf("i:%d\t\ttrem:%f\n", i, trem);
// Serial.printf("i:%d\t\ttrem:%f\n", i, soundList[AudioObjectList[ObjAddr]->sound].Trem.getVal(FillBufferIndex));
// Serial.printf("i:%d\t\ttrem:%f\n", i, soundList[AudioObjectList[ObjAddr]->sound].Trem.getVal(i));
// FillBufferIndex++;
// }
// while (1) {}

// Serial.println(trem);
int ObjAddr = activeNoteList[f];
sampleVal = Wavetable_table[soundList[AudioObjectList[ObjAddr]->sound].Wavetype][(byte)((((AudioObjectList[ObjAddr]->frequency) * FillBufferIndex) % (Sample_Rate)) * divider)];
ampl = soundList[AudioObjectList[ObjAddr]->sound].ADSR.getAmplitude(AudioObjectList[ObjAddr]->ticksFromLastEvent++, AudioObjectList[ObjAddr]->isKeyPressed, AudioObjectList[ObjAddr]->releaseStartingPoint, &AudioObjectList[ObjAddr]->toBeDeleted);
if (soundList[AudioObjectList[ObjAddr]->sound].EFX_tremolo_enable) {
trem = soundList[AudioObjectList[ObjAddr]->sound].Trem.getVal(f == 0);

trem = 1 + (Wavetable_table[WAVETYPE_TRIANG][(byte)(((FillBufferIndex * soundList[AudioObjectList[ObjAddr]->sound].EFX_tremolo_speed) % Sample_Rate) * divider)] - 128) * (soundList[AudioObjectList[ObjAddr]->sound].EFX_tremolo_depth) / 1024.0;
} else {
trem = 1;
}
totalWaveVal += (sampleVal * ampl * trem);
}
totalWaveVal /= 1000;
Expand Down
2 changes: 1 addition & 1 deletion src/embedded/Synth_Engine/Synth_Engine.ino
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ void setup() {
audioEngine.soundList[2].setDefaultParam();
audioEngine.soundList[2].Wavetype = 2;
audioEngine.soundList[1].Wavetype = WAVETYPE_SIN_3;

audioEngine.soundList[1].Trem.enable = 1;
}

void loop() {
Expand Down

0 comments on commit c5cd5c7

Please sign in to comment.