Skip to content

Commit a319f2f

Browse files
* Combine key frequency with step
- Reduces the number of multiplies inside oscillators
1 parent ad03baf commit a319f2f

7 files changed

+19
-22
lines changed

DisplayLowFrequencyOscillator.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ void UpdateLowFrequencyOscillatorDisplay(HANDLE hOut)
2525
buf[x] = positive;
2626

2727
// plot low-frequency oscillator value
28-
float const lfo = lfo_state.Update(lfo_config, 1.0f, 0.0f);
28+
float const lfo = lfo_state.Update(lfo_config, 0.0f);
2929
int grid_x = Clamp(FloorInt(18.0f * lfo + 18.0f), 0, 35);
3030
buf[grid_x / 2].Char.UnicodeChar = plot[grid_x & 1];
3131

DisplayOscillatorWaveform.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ static float UpdateWaveformStep(NoteOscillatorConfig config[], OscillatorState s
4343
if (config[o].sync_enable)
4444
config[o].sync_phase = config[o].frequency / config[0].frequency;
4545
if (config[o].sub_osc_mode)
46-
value += config[o].sub_osc_amplitude * SubOscillator(config[o], state[o], 1, delta[o]);
46+
value += config[o].sub_osc_amplitude * SubOscillator(config[o], state[o], delta[o]);
4747
value += state[o].Compute(config[o], delta[o]);
4848
state[o].Advance(config[o], step[o]);
4949
}
@@ -68,7 +68,7 @@ void UpdateOscillatorWaveformDisplay(HANDLE hOut, BASS_INFO const &info, int con
6868

6969
// get low-frequency oscillator value
7070
// (assume it is constant for the duration)
71-
float const lfo = lfo_state.Update(lfo_config, 1.0f, 0.0f);
71+
float const lfo = lfo_state.Update(lfo_config, 0.0f);
7272

7373
// how many cycles to plot?
7474
int cycle = osc_config[0].cycle;

Oscillator.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ void OscillatorState::Start()
2424
}
2525

2626
// update oscillator
27-
float OscillatorState::Update(OscillatorConfig const &config, float const frequency, float const step)
27+
float OscillatorState::Update(OscillatorConfig const &config, float const step)
2828
{
2929
if (!config.enable)
3030
return 0.0f;
3131

32-
float const delta = config.frequency * config.adjust * frequency * step;
32+
float const delta = config.frequency * config.adjust * step;
3333

3434
// compute oscillator value
3535
float const value = Compute(config, delta);

Oscillator.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ class OscillatorState
6868
void Start();
6969

7070
// update the oscillator by one step
71-
float Update(OscillatorConfig const &config, float frequency, float const step);
71+
float Update(OscillatorConfig const &config, float const step);
7272

7373
// compute the oscillator value
7474
float Compute(OscillatorConfig const &config, float delta);

SubOscillator.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ const char * const sub_osc_name[SUBOSC_COUNT] =
2020
};
2121

2222
// sub-oscillator
23-
float SubOscillator(NoteOscillatorConfig const &config, OscillatorState const &state, float frequency, float step)
23+
float SubOscillator(NoteOscillatorConfig const &config, OscillatorState const &state, float step)
2424
{
2525
float sub_value = (state.index & config.sub_osc_mode) ? -1.0f : 1.0f;
26-
float const w = frequency * step * POLYBLEP_WIDTH;
26+
float const w = step * POLYBLEP_WIDTH;
2727
if (state.phase < 0.5f)
2828
{
2929
float sub_prev = ((state.index - 1) & config.sub_osc_mode) ? -1.0f : 1.0f;

SubOscillator.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,4 @@ enum SubOscillatorMode
2323
extern const char * const sub_osc_name[SUBOSC_COUNT];
2424

2525
// compute sub-oscillator value
26-
extern float SubOscillator(NoteOscillatorConfig const &config, OscillatorState const &state, float frequency, float step);
26+
extern float SubOscillator(NoteOscillatorConfig const &config, OscillatorState const &state, float step);

synth.cpp

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ DWORD CALLBACK WriteStream(HSTREAM handle, short *buffer, DWORD length, void *us
7171
memset(buffer, 0, length);
7272

7373
// advance low-frequency oscillator
74-
float lfo = lfo_state.Update(lfo_config, 1.0f, float(count) / info.freq);
74+
float lfo = lfo_state.Update(lfo_config, float(count) / info.freq);
7575

7676
// compute shared oscillator values
7777
for (int o = 0; o < NUM_OSCILLATORS; ++o)
@@ -102,7 +102,7 @@ DWORD CALLBACK WriteStream(HSTREAM handle, short *buffer, DWORD length, void *us
102102
if ((c & (BLOCK_UPDATE_SAMPLES - 1)) == 0)
103103
{
104104
// get low-frequency oscillator value
105-
lfo = lfo_state.Update(lfo_config, 1.0f, block_step);
105+
lfo = lfo_state.Update(lfo_config, block_step);
106106

107107
// compute shared oscillator values
108108
for (int o = 0; o < NUM_OSCILLATORS; ++o)
@@ -146,18 +146,20 @@ DWORD CALLBACK WriteStream(HSTREAM handle, short *buffer, DWORD length, void *us
146146
// key velocity
147147
float const key_vel = voice_vel[v] / 64.0f;
148148

149-
// update oscillator
149+
// key frequency times step
150+
float const key_step = key_freq * step;
151+
152+
// update oscillators
150153
// (assume key follow)
151154
float osc_value = 0.0f;
152155
for (int o = 0; o < NUM_OSCILLATORS; ++o)
153156
{
154157
if (osc_config[o].sub_osc_mode && osc_config[o].sub_osc_amplitude)
155-
osc_value += osc_config[o].sub_osc_amplitude * SubOscillator(osc_config[o], osc_state[v][o], key_freq, step);
156-
osc_value += osc_state[v][o].Update(osc_config[o], key_freq, step);
158+
osc_value += osc_config[o].sub_osc_amplitude * SubOscillator(osc_config[o], osc_state[v][o], key_step);
159+
osc_value += osc_state[v][o].Update(osc_config[o], key_step);
157160
}
158161

159162
// update filter
160-
float flt_value;
161163
if (flt_config.enable)
162164
{
163165
if ((c & (BLOCK_UPDATE_SAMPLES - 1)) == 0)
@@ -174,16 +176,11 @@ DWORD CALLBACK WriteStream(HSTREAM handle, short *buffer, DWORD length, void *us
174176
}
175177

176178
// get filtered oscillator value
177-
flt_value = flt_state[v].Update(flt_config, osc_value);
178-
}
179-
else
180-
{
181-
// pass unfiltered value
182-
flt_value = osc_value;
179+
osc_value = flt_state[v].Update(flt_config, osc_value);
183180
}
184181

185182
// apply amplifier level and accumulate result
186-
sample += flt_value * amp_config.GetLevel(amp_env_amplitude, key_vel);
183+
sample += osc_value * amp_config.GetLevel(amp_env_amplitude, key_vel);
187184
}
188185

189186
// left and right channels are the same

0 commit comments

Comments
 (0)