Skip to content

Commit 2120690

Browse files
* Added Noise Cubic oscillator
- White noise with cubic interpolation
1 parent cf454c3 commit 2120690

File tree

5 files changed

+44
-9
lines changed

5 files changed

+44
-9
lines changed

Wave.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ WaveEvaluate const wave_evaluate[WAVE_COUNT] =
2727
OscillatorTriangle, // WAVE_TRIANGLE,
2828
OscillatorNoise, // WAVE_NOISE,
2929
OscillatorNoiseHold, // WAVE_NOISE_HOLD
30-
OscillatorNoiseSlope, // WAVE_NOISE_SLOPE
30+
OscillatorNoiseLinear, // WAVE_NOISE_LINEAR
31+
OscillatorNoiseCubic, // WAVE_NOISE_CUBIC
3132
OscillatorPoly, // WAVE_POLY4,
3233
OscillatorPoly, // WAVE_POLY5,
3334
OscillatorPoly, // WAVE_PERIOD93,
@@ -47,7 +48,8 @@ char const * const wave_name[WAVE_COUNT] =
4748
"Triangle", // WAVE_TRIANGLE,
4849
"Noise", // WAVE_NOISE,
4950
"Noise Hold", // WAVE_NOISE_HOLD
50-
"Noise Slope", // WAVE_NOISE_SLOPE
51+
"Noise Linear", // WAVE_NOISE_LINEAR
52+
"Noise Cubic", // WAVE_NOISE_CUBIC
5153
"Poly4", // WAVE_POLY4,
5254
"Poly5", // WAVE_POLY5,
5355
"Period-93", // WAVE_PERIOD93,
@@ -70,7 +72,8 @@ float const wave_adjust_frequency[WAVE_COUNT] =
7072
1.0f, // WAVE_TRIANGLE,
7173
1.0f, // WAVE_NOISE,
7274
1.0f, // WAVE_NOISE_HOLD,
73-
1.0f, // WAVE_NOISE_SLOPE,
75+
1.0f, // WAVE_NOISE_LINEAR,
76+
1.0f, // WAVE_NOISE_CUBIC,
7477
2.0f * 15.0f / 16.0f, // WAVE_POLY4,
7578
2.0f * 31.0f / 32.0f, // WAVE_POLY5,
7679
2.0f * 93.0f / 128.0f, // WAVE_PERIOD93,
@@ -91,7 +94,8 @@ int const wave_loop_cycle[WAVE_COUNT] =
9194
INT_MAX, // WAVE_TRIANGLE,
9295
INT_MAX, // WAVE_NOISE,
9396
ARRAY_SIZE(noise), // WAVE_NOISE_HOLD
94-
ARRAY_SIZE(noise), // WAVE_NOISE_SLOPE
97+
ARRAY_SIZE(noise), // WAVE_NOISE_LINEAR
98+
ARRAY_SIZE(noise), // WAVE_NOISE_CUBIC
9599
ARRAY_SIZE(poly4), // WAVE_POLY4,
96100
ARRAY_SIZE(poly5), // WAVE_POLY5,
97101
ARRAY_SIZE(period93), // WAVE_PERIOD93,

Wave.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ enum Wave
2020
WAVE_SAWTOOTH,
2121
WAVE_TRIANGLE,
2222
WAVE_NOISE,
23-
WAVE_NOISE_HOLD,
24-
WAVE_NOISE_SLOPE,
23+
WAVE_NOISE_HOLD, // noise with sample and hold
24+
WAVE_NOISE_LINEAR, // noise with linear interpolation
25+
WAVE_NOISE_CUBIC, // noise with cubic interpolation
2526

2627
WAVE_POLY4, // Atari POKEY AUDC 12
2728
WAVE_POLY5, // Atari TIA AUDC 7 or 9

WaveHold.cpp

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,28 @@ static float OscillatorLerp(OscillatorConfig const &config, OscillatorState &sta
108108
return value;
109109
}
110110

111+
// shared data oscillator
112+
static float OscillatorCubic(OscillatorConfig const &config, OscillatorState &state, float data[], int cycle, float step)
113+
{
114+
if (step > 0.5f * cycle)
115+
return 0;
116+
117+
// four consecutive wavetable values
118+
register float const value0 = data[state.index];
119+
register float const value1 = data[(state.index + 1) & (cycle - 1)];
120+
register float const value2 = data[(state.index + 2) & (cycle - 1)];
121+
register float const value3 = data[(state.index + 3) & (cycle - 1)];
122+
123+
// cubic interpolation
124+
// (Catmull-Rom spline)
125+
register float value =
126+
value1 +
127+
state.phase * 0.5 * (value2 - value0 +
128+
state.phase * (2.0 * value0 - 5.0 * value1 + 4.0 * value2 - value3 +
129+
state.phase * (3.0 * (value1 - value2) + value3 - value0)));
130+
131+
return value;
132+
}
111133

112134
// sample-and-hold noise
113135
float OscillatorNoiseHold(OscillatorConfig const &config, OscillatorState &state, float step)
@@ -116,7 +138,13 @@ float OscillatorNoiseHold(OscillatorConfig const &config, OscillatorState &state
116138
}
117139

118140
// linear interpolated noise
119-
float OscillatorNoiseSlope(OscillatorConfig const &config, OscillatorState &state, float step)
141+
float OscillatorNoiseLinear(OscillatorConfig const &config, OscillatorState &state, float step)
120142
{
121143
return OscillatorLerp(config, state, noise, ARRAY_SIZE(noise), step);
122144
}
145+
146+
// cubic interpolated noise
147+
float OscillatorNoiseCubic(OscillatorConfig const &config, OscillatorState &state, float step)
148+
{
149+
return OscillatorCubic(config, state, noise, ARRAY_SIZE(noise), step);
150+
}

WaveHold.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ extern void InitNoise();
88

99
// sample-and-hold noise
1010
extern float OscillatorNoiseHold(OscillatorConfig const &config, OscillatorState &state, float step);
11-
extern float OscillatorNoiseSlope(OscillatorConfig const &config, OscillatorState &state, float step);
11+
extern float OscillatorNoiseLinear(OscillatorConfig const &config, OscillatorState &state, float step);
12+
extern float OscillatorNoiseCubic(OscillatorConfig const &config, OscillatorState &state, float step);

WavePoly.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ static char const * const poly_data[WAVE_COUNT] =
3232
NULL, // WAVE_TRIANGLE,
3333
NULL, // WAVE_NOISE,
3434
NULL, // WAVE_NOISE_HOLD,
35-
NULL, // WAVE_NOISE_SLOPE,
35+
NULL, // WAVE_NOISE_LINEAR,
36+
NULL, // WAVE_NOISE_CUBIC,
3637
poly4, // WAVE_POLY4,
3738
poly5, // WAVE_POLY5,
3839
period93, // WAVE_PERIOD93,

0 commit comments

Comments
 (0)