Skip to content

Commit

Permalink
Merge pull request #15 from kometbomb/reverb_module
Browse files Browse the repository at this point in the history
Reverb module
  • Loading branch information
kometbomb authored Mar 11, 2018
2 parents c8c8586 + db28798 commit f92c341
Show file tree
Hide file tree
Showing 14 changed files with 400 additions and 49 deletions.
34 changes: 29 additions & 5 deletions MODULES.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,16 +188,19 @@ Add inputs together and output the result.

### Mul

Multiply inputs and output the result. Use as an amplifier.
Multiply the two inputs and output the result. Use as an amplifier. Connect e.g. an EG to input 1
and use it to multiple the two separate inputs to synchronize.

| Input | Description |
|-------|--------------|
| 0 | Input A |
| 1 | Input B |
| 1 | Multiplier |
| 2 | Input B |

| Output | Description |
|--------|--------------|
| 0 | The product of inputs |
| 0 | A multiplied by the multiplier |
| 1 | B multiplied by the multiplier |

### Abs

Expand Down Expand Up @@ -347,13 +350,34 @@ This module will follow the input signal at a constant speed.

### Delay

This module delays the signal.
This module delays the input signal. Optionally, the signal can be fed back in the
delay buffer via input #3 (connect from output 1 to input 3) which gets amplified
by input #2. Input gain controls the delay amplitude and also the feedback ratio
if the signal is fed back.

| Input | Description |
|-------|--------------|
| 0 | Signal in |
| 1 | Delay (in seconds) |
| 2 | Input gain |
| 3 | Feedback input |

| Output | Description |
|--------|--------------|
| 0 | Combined output |
| 1 | Wet output |

### Reverb

Adds reverberation to input signal. Reverb time is the feedback gain inside
the reverb, so it should be set -0.999..0.999 to avoid distortion.

| Input | Description |
|-------|--------------|
| 0 | Signal in |
| 1 | Reverb time |

| Output | Description |
|--------|--------------|
| 0 | Delayed output |
| 0 | Combined output |
| 1 | Wet output |
4 changes: 2 additions & 2 deletions src/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Context* _context;
#endif

Context::Context()
: ready(false), done(false), themeLoaded(false), song(), player(song), synth(), mixer(player, synth), editorState(), mainEditor(editorState, player, player.getPlayerState(), song, synth)
: ready(false), done(false), themeLoaded(false), song(), player(song), synth(player), mixer(player, synth), editorState(), mainEditor(editorState, player, player.getPlayerState(), song, synth)
{
Theme theme;

Expand All @@ -47,7 +47,7 @@ Context::Context()

themeLoaded = true;
previousTick = SDL_GetTicks();

song.addSectionListener("SYNT", &synth, SectionListener::Save|SectionListener::Load);
}

Expand Down
13 changes: 10 additions & 3 deletions src/ModularSynth.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "FileSection.h"
#include "ModuleFactory.h"
#include "TrackState.h"
#include "IPlayer.h"
#include "SDL.h"
#include <cstdio>
#include <cstdlib>
Expand All @@ -14,8 +15,8 @@
#define TUNING 440.0
#endif

ModularSynth::ModularSynth()
:mNumConnections(0), mFrequency(0), mVolume(0), mNoteTrigger(false)
ModularSynth::ModularSynth(IPlayer& player)
: mPlayer(player), mNumConnections(0), mFrequency(0), mVolume(0), mNoteTrigger(false)
{
for (int i = 0 ; i < maxModules ; ++i)
mModules[i] = NULL;
Expand Down Expand Up @@ -446,7 +447,7 @@ void ModularSynth::copy(const ModularSynth& source)

ModularSynth* ModularSynth::clone() const
{
ModularSynth *newSynth = new ModularSynth();
ModularSynth *newSynth = new ModularSynth(mPlayer);

newSynth->copy(*this);

Expand All @@ -465,3 +466,9 @@ int ModularSynth::getEffectValue(int effect) const
{
return mEffectValues[effect];
}


int ModularSynth::getSongRate() const
{
return mPlayer.getPlayerState().songRate;
}
5 changes: 4 additions & 1 deletion src/ModularSynth.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

struct SynthModule;
struct FileSection;
struct IPlayer;

class ModularSynth: public IOscillator, public Lockable
{
Expand All @@ -15,6 +16,7 @@ class ModularSynth: public IOscillator, public Lockable
static const int outputResolution = 4096;

private:
IPlayer& mPlayer;
SynthModule *mModules[maxModules];
SynthConnection mConnections[maxConnections];
int mNumConnections;
Expand All @@ -25,7 +27,7 @@ class ModularSynth: public IOscillator, public Lockable
void cycle();

public:
ModularSynth();
ModularSynth(IPlayer& player);
virtual ~ModularSynth();
ModularSynth* clone() const;
void copy(const ModularSynth& source);
Expand Down Expand Up @@ -53,6 +55,7 @@ class ModularSynth: public IOscillator, public Lockable
bool getNoteTrigger() const;
void setMasterOutput(int channel, float output);
int getEffectValue(int effect) const;
int getSongRate() const;

// IOscillator virtual methods
virtual void triggerNote();
Expand Down
4 changes: 4 additions & 0 deletions src/ModuleFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "modules/MixerModule.h"
#include "modules/ClampModule.h"
#include "modules/DelayModule.h"
#include "modules/ReverbModule.h"
#include "modules/ShapeModule.h"
#include "modules/BitsModule.h"
#include "modules/DistortionModule.h"
Expand All @@ -24,6 +25,7 @@
#include "modules/FrequencyInModule.h"
#include "modules/TransposeModule.h"
#include "modules/TriggerNoteModule.h"
#include "modules/TickModule.h"
#include "modules/LinearModule.h"
#include "modules/OscilloscopeModule.h"
#include "modules/VUMeterModule.h"
Expand All @@ -42,6 +44,7 @@ ModuleFactory::ModuleFactory()
REGISTER(MulModule);
REGISTER(AbsModule);
REGISTER(RMSModule);
REGISTER(TickModule);
REGISTER(ConstModule);
REGISTER(EffectModule);
REGISTER(SplitModule);
Expand All @@ -52,6 +55,7 @@ ModuleFactory::ModuleFactory()
REGISTER(DistortionModule);
REGISTER(BitsModule);
REGISTER(DelayModule);
REGISTER(ReverbModule);
REGISTER(FilterModule);
REGISTER(GlideModule);
REGISTER(AccumulatorModule);
Expand Down
24 changes: 12 additions & 12 deletions src/Synth.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,29 @@
#include "SDL.h"
#include <cstring>

Synth::Synth()
Synth::Synth(IPlayer& player)
: ISynth()
{
/*
/*
Initialize the audio tracks.
*/

for (int i = 0 ; i < SequenceRow::maxTracks ; ++i)
{
mOscillator[i] = new ModularSynth();
mOscillator[i] = new ModularSynth(player);
}
}


Synth::~Synth()
{
/*
NOTE: ~ISynth() will delete the Oscillator objects we initialized
above! No need to cleanup yourself.
*/
}

Expand All @@ -38,15 +38,15 @@ bool Synth::onFileSectionLoad(const FileSection& section, int& offset)
if (strcmp(section.getName(), "SYNT") == 0)
{
int synthCount = section.readByte(offset);

if (synthCount == FileSection::invalidRead || synthCount > SequenceRow::maxTracks)
return false;

for (int i = 0 ; i < SequenceRow::maxTracks ; ++i)
{
static_cast<ModularSynth*>(mOscillator[i])->readSynth(section, offset);
}

return true;
}
else
Expand All @@ -61,7 +61,7 @@ void Synth::onFileSectionSave(FileSection& section)
if (strcmp(section.getName(), "SYNT") == 0)
{
section.writeByte(SequenceRow::maxTracks);

for (int i = 0 ; i < SequenceRow::maxTracks ; ++i)
{
static_cast<ModularSynth*>(mOscillator[i])->writeSynth(section);
Expand Down
8 changes: 5 additions & 3 deletions src/Synth.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@
#include "ISynth.h"
#include "SectionListener.h"

struct IPlayer;

class Synth: public ISynth, public SectionListener
{
public:
Synth();
Synth(IPlayer& player);
virtual ~Synth();

virtual void reset();

virtual bool onFileSectionLoad(const FileSection& section, int& offset);
virtual void onFileSectionSave(FileSection& section);
};
62 changes: 45 additions & 17 deletions src/modules/DelayModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include "SDL.h"

DelayModule::DelayModule(ModularSynth& synth)
:SynthModule(synth, moduleId, 2, 1, 0), mHead(0), mBuffer(NULL)
:SynthModule(synth, moduleId, 4, 2, 0), mHead(0), mBuffer(NULL)
{
}

Expand All @@ -21,29 +21,55 @@ void DelayModule::cycle()
{
if (mBuffer == NULL)
return;

mBuffer[mHead] = getInput(0);


int currentReadHead = std::max(0, std::min(mMaxBufferSize - 1, static_cast<int>(getInput(1) * mSampleRate)));
int a = std::min(currentReadHead, mPrevReadHead);
int b = std::max(currentReadHead, mPrevReadHead);
float avg = 0;

// Feed input #3 and
mBuffer[mHead] = getInput(2) * (getInput(0) + getInput(3));

int readHead = (mHead - a + mMaxBufferSize) % mMaxBufferSize;

for (int i = a ; i <= b ; ++i)
{
avg += mBuffer[readHead];

if (++readHead >= mMaxBufferSize)
{
readHead = 0;
}
}

// Wet out
float wetOut = avg / (b - a + 1);
setOutput(1, wetOut);

// Combined out
setOutput(0, wetOut + getInput(0));

++mHead;
if (mHead >= std::max(0, std::min(mMaxBufferSize, static_cast<int>(getInput(1) * mSampleRate))))

if (mHead >= mMaxBufferSize)
mHead = 0;
setOutput(0, mBuffer[mHead]);

mPrevReadHead = currentReadHead;
}



const char * DelayModule::getInputName(int input) const
const char * DelayModule::getInputName(int input) const
{
static const char *names[] = {"Input", "Delay"};
static const char *names[] = {"Input", "Delay", "Feedback ratio", "Feedback in"};
return names[input];
}


const char * DelayModule::getOutputName(int output) const
const char * DelayModule::getOutputName(int output) const
{
return "Output";
static const char *names[] = {"Combined out", "Wet out"};
return names[output];
}


Expand All @@ -61,14 +87,16 @@ SynthModule * DelayModule::createModule(ModularSynth& synth)
void DelayModule::setSampleRate(int newRate)
{
SynthModule::setSampleRate(newRate);

if (mBuffer != NULL)
delete[] mBuffer;
mMaxBufferSize = maxBufferSizeMs * newRate / 1000;

mMaxBufferSize = std::max(1, maxBufferSizeMs * newRate / 1000);

mBuffer = new float[mMaxBufferSize];
SDL_memset(mBuffer, 0, mMaxBufferSize * sizeof(mBuffer[0]));


mPrevReadHead = static_cast<int>(getInput(1) * mSampleRate);

mHead = 0;
}
2 changes: 1 addition & 1 deletion src/modules/DelayModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class DelayModule: public SynthModule
{
DelayModule(ModularSynth& synth);
int mMaxBufferSize;
int mHead;
int mHead, mPrevReadHead;
float *mBuffer;

public:
Expand Down
Loading

0 comments on commit f92c341

Please sign in to comment.