Skip to content

Commit c6de2ea

Browse files
Fix broken code and add LFO
1 parent c99d9c9 commit c6de2ea

24 files changed

Lines changed: 457 additions & 674 deletions

File tree

CMakeLists.txt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,6 @@ if (NEO_BUILD_TESTS)
4848
add_neuron_test(parameter tests/core/parameter_test.cpp)
4949

5050
add_neuron_test(oscillator tests/dsp/generators/oscillator_test.cpp)
51-
add_neuron_test(adsr tests/dsp/modulators/adsr_test.cpp)
52-
add_neuron_test(saturator tests/dsp/processors/saturator_test.cpp)
53-
add_neuron_test(wavefolder tests/dsp/processors/wavefolder_test.cpp)
5451
add_neuron_test(filter tests/dsp/processors/filter_test.cpp)
5552

5653
add_neuron_test(arithmetic tests/utils/arithmetic_test.cpp)

Makefile

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,11 @@ oscillator \
1313

1414
MODULATOR_MOD_DIR = dsp/modulators
1515
MODULATOR_MODULES = \
16-
adsr \
16+
lfo \
1717

1818
PROCESSOR_MOD_DIR = dsp/processors
1919
PROCESSOR_MODULES = \
2020
filter \
21-
saturator \
22-
wavefolder \
2321

2422
######################################
2523
# source

include/neuron/core/base.h

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
#include "neuron/core/context.h"
44
#include "neuron/dsp/modulators/modulator.h"
55

6-
#ifdef NEO_PLUGIN_SUPPORT
6+
#if NEO_PLUGIN_SUPPORT
77
#include <atomic>
88
#endif
99

1010
namespace neuron {
1111

1212
/**
13-
* Describes a Neuron DSP component, capable of processing, or
14-
* generating signals.
13+
* Describes a "neuron", i.e. a DSP component capable of generating signals,
14+
* processing signals, or modulating parameters of other neurons.
1515
*/
1616
template<class N, typename P>
1717
class Neuron {
@@ -21,35 +21,59 @@ namespace neuron {
2121
*/
2222
~Neuron() = default;
2323

24-
void SetContext(const Context& context)
24+
/**
25+
* Sets the DSP context for this neuron, holding information
26+
* such as sample rate, block size, and number of channels.
27+
*/
28+
void SetContext(Context context)
2529
{
2630
static_cast<N*>(this)->SetContextImpl(context);
2731
}
2832

33+
/**
34+
* Attaches a modulator object to a given parameter of this neuron.
35+
*
36+
* CAUTION: Modulators MUST be evaluated in the audio callback before
37+
* any other neurons. If modulator A is modulating modulator B, then A
38+
* must be evaluated before B is. A and B must both be evaluated before any
39+
* of the neurons they modulate are evaluated.
40+
*/
2941
template<class M>
30-
void AttachModulator(const P parameter, Modulator<M>* modulator)
42+
void AttachModulator(P parameter, Modulator<M>* modulator)
3143
{
3244
static_cast<N*>(this)->AttachModulatorImpl(parameter, modulator);
3345
}
3446

35-
void DetachModulator(const P parameter)
47+
/**
48+
* Detaches a modulator object from a given parameter of this neuron.
49+
*/
50+
void DetachModulator(P parameter)
3651
{
3752
static_cast<N*>(this)->DetachModulatorImpl(parameter);
3853
}
3954

40-
void SetModulationDepth(const P parameter, float depth)
55+
/**
56+
* Sets the modulation depth of a given parameter of this neuron, which is a value
57+
* between -1.0f and 1.0f.
58+
*
59+
* NOTE: If no modulator has been attached then this will have no effect.
60+
*/
61+
void SetModulationDepth(P parameter, float depth)
4162
{
4263
static_cast<N*>(this)->SetModulationDepthImpl(parameter, depth);
4364
}
4465

45-
#ifdef NEO_PLUGIN_SUPPORT
66+
#if NEO_PLUGIN_SUPPORT
4667
/**
4768
* Attach a source via an atomic pointer to a given parameter.
4869
*/
49-
void AttachParameterToSource(const P parameter, std::atomic<float>* source)
70+
void AttachParameterToSource(P parameter, std::atomic<float>* source)
5071
{
5172
static_cast<N*>(this)->AttachParameterToSourceImpl(parameter, source);
5273
}
5374
#endif
75+
76+
protected:
77+
Context m_context = DEFAULT_CONTEXT;
5478
};
5579
}

include/neuron/core/context.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace neuron {
88
* that use the sample rate to calculate phase positions.
99
*/
1010
struct Context {
11-
double sampleRate;
11+
float sampleRate;
1212
int numChannels;
1313
int blockSize;
1414
};
@@ -17,6 +17,6 @@ namespace neuron {
1717
* The common default context, using a sample rate of 44.1kHz, stereo
1818
* channel configuration, and a buffer size of 16 samples.
1919
*/
20-
static Context DEFAULT_CONTEXT = { 44100.0, 2, 16 };
20+
static Context DEFAULT_CONTEXT = { 44100.0f, 2, 16 };
2121

2222
}

include/neuron/core/parameter.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
#pragma once
22

3-
#ifdef NEO_PLUGIN_SUPPORT
3+
#if NEO_PLUGIN_SUPPORT
44
#include <atomic>
55
#endif
66

77
namespace neuron {
88

9-
#ifdef NEO_PLUGIN_SUPPORT
9+
#if NEO_PLUGIN_SUPPORT
1010
/**
1111
* A read-only parameter used by a DSP component to allow more
1212
* control and flexibility in shaping its sound.

include/neuron/dsp/generators/generator.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
namespace neuron {
66

77
/**
8-
* Describes a DSP component that generates a signal without
8+
* Describes a neuron that generates a signal without
99
* processing an input signal.
1010
*/
1111
template<class G>
@@ -22,7 +22,7 @@ namespace neuron {
2222
*/
2323
void Generate(Buffer<Sample>& output)
2424
{
25-
return static_cast<G*>(this)->GenerateImpl();
25+
return static_cast<G*>(this)->GenerateImpl(output);
2626
}
2727
};
2828

include/neuron/dsp/generators/oscillator.h

Lines changed: 18 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,10 @@
66
#include "neuron/core/parameter.h"
77
#include "neuron/dsp/generators/generator.h"
88
#include "neuron/utils/waveform.h"
9+
#include "neuron/utils/wavetable.h"
910

1011
namespace neuron {
1112

12-
const size_t WAVETABLE_SIZE = 256;
13-
1413
enum OscillatorParameter {
1514
OSC_FREQUENCY,
1615
};
@@ -22,13 +21,9 @@ namespace neuron {
2221
class Oscillator : public Generator<Oscillator>, public Neuron<Oscillator, OscillatorParameter> {
2322
public:
2423
/**
25-
* Creates an oscillator generator.
26-
*
27-
* @param context The DSP context to be used by the oscillator.
28-
* @param frequency The initial frequency of the oscillator.
29-
* @return Oscillator
24+
* Creates an oscillator generator that produces the given waveform at the given frequency.
3025
*/
31-
explicit Oscillator(Context& context = DEFAULT_CONTEXT, float frequency = 440.0f, Waveform waveform = Waveform::SINE);
26+
explicit Oscillator(float frequency = 440.0f, Waveform waveform = Waveform::SINE);
3227

3328
/**
3429
* Frees any memory allocated by the oscillator.
@@ -38,29 +33,21 @@ namespace neuron {
3833
/**
3934
* Resets the phase of the oscillator, starting it at the beginning
4035
* waveform position.
41-
*
42-
* @param
4336
*/
4437
void Reset(float phase = 0.0f);
4538

4639
/**
4740
* Sets the frequency of the oscillator.
48-
*
49-
* @param frequency The new oscillator output frequency.
5041
*/
5142
void SetFrequency(float frequency);
5243

5344
/**
5445
* Sets the waveform of the oscillator.
55-
*
56-
* @param waveform The new oscillator output waveform.
5746
*/
5847
void SetWaveform(Waveform waveform);
5948

6049
/**
6150
* Attaches a follower oscillator to be synced to this one.
62-
*
63-
* @param oscillator The oscillator that will be synced to this one.
6451
*/
6552
void AttachFollower(Oscillator* oscillator);
6653

@@ -74,32 +61,33 @@ namespace neuron {
7461
void GenerateImpl(Buffer<Sample>& output);
7562

7663
friend class Neuron<Oscillator, OscillatorParameter>;
77-
void SetContextImpl(const Context& context);
64+
void SetContextImpl(Context context);
65+
7866
template<class M>
79-
void AttachModulatorImpl(OscillatorParameter parameter, Modulator<M>* modulator);
67+
void AttachModulatorImpl(OscillatorParameter parameter, Modulator<M>* modulator)
68+
{
69+
switch (parameter) {
70+
case OscillatorParameter::OSC_FREQUENCY:
71+
m_frequencyModulator = ModulationSource(modulator);
72+
break;
73+
default:
74+
break;
75+
}
76+
}
77+
8078
void DetachModulatorImpl(OscillatorParameter parameter);
8179
void SetModulationDepthImpl(OscillatorParameter parameter, float depth);
82-
#ifdef NEO_PLUGIN_SUPPORT
80+
#if NEO_PLUGIN_SUPPORT
8381
void AttachParameterToSourceImpl(OscillatorParameter parameter, std::atomic<float>* source);
8482
#endif
8583

8684
private:
87-
void PopulateWavetable();
88-
void IncrementPhase();
89-
Sample Lerp();
90-
91-
Context& m_context;
92-
93-
Sample m_wavetable[WAVETABLE_SIZE];
94-
Waveform m_waveform;
85+
Wavetable m_wavetable;
9586

9687
Parameter<float> p_frequency;
9788
Parameter<float> p_frequencyModulationDepth;
9889
ModulationSource m_frequencyModulator;
9990

100-
float m_phase = 0.0f;
101-
float m_phaseIncrement = 0.0f;
102-
10391
Oscillator* m_follower = nullptr;
10492
};
10593

include/neuron/dsp/modulators/adsr.h

Lines changed: 0 additions & 126 deletions
This file was deleted.

0 commit comments

Comments
 (0)