Skip to content

Commit ee6d511

Browse files
* Convert all displays to classes
- Eliminates a bunch of icky global variables :)
1 parent 38bc0cf commit ee6d511

13 files changed

+130
-75
lines changed

DisplayFilterFrequency.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Filter Frequency Display
1616
#include "Filter.h"
1717

1818
// show filter frequency
19-
void UpdateFilterFrequencyDisplay(HANDLE hOut, int const v)
19+
void DisplayFilterFrequency::Update(HANDLE hOut, int const v)
2020
{
2121
// get low-frequency filter value
2222
// (assume it is constant for the duration)

DisplayFilterFrequency.h

+5-1
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,8 @@ Copyright 2014 Kenneth D. Miller III
66
Filter Frequency Display
77
*/
88

9-
extern void UpdateFilterFrequencyDisplay(HANDLE hOut, int const v);
9+
class DisplayFilterFrequency
10+
{
11+
public:
12+
void Update(HANDLE hOut, int const v);
13+
};

DisplayKeyVolumeEnvelope.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ static WORD const env_attrib[EnvelopeState::COUNT] =
2727
BACKGROUND_INTENSITY, // EnvelopeState::RELEASE,
2828
};
2929

30-
void InitKeyVolumeEnvelopeDisplay(HANDLE hOut)
30+
void DisplayKeyVolumeEnvelope::Init(HANDLE hOut)
3131
{
3232
// show the note keys
3333
DWORD written;
@@ -41,7 +41,7 @@ void InitKeyVolumeEnvelopeDisplay(HANDLE hOut)
4141
}
4242

4343

44-
void UpdateKeyVolumeEnvelopeDisplay(HANDLE hOut)
44+
void DisplayKeyVolumeEnvelope::Update(HANDLE hOut)
4545
{
4646
WORD note_env_attrib[SPECTRUM_WIDTH];
4747
WORD voice_env_attrib[VOICES];

DisplayKeyVolumeEnvelope.h

+6-2
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,9 @@ Per-Key Volume Envelope Display
88

99
#include "Envelope.h"
1010

11-
void InitKeyVolumeEnvelopeDisplay(HANDLE hOut);
12-
void UpdateKeyVolumeEnvelopeDisplay(HANDLE hOut);
11+
class DisplayKeyVolumeEnvelope
12+
{
13+
public:
14+
void Init(HANDLE hOut);
15+
void Update(HANDLE hOut);
16+
};

DisplayLowFrequencyOscillator.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ static CHAR_INFO const negative = { 0, BACKGROUND_RED | FOREGROUND_RED | FOREGRO
2121
static CHAR_INFO const positive = { 0, BACKGROUND_GREEN | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE };
2222
static WORD const plot[2] = { 221, 222 };
2323

24-
void UpdateLowFrequencyOscillatorDisplay(HANDLE hOut)
24+
void DisplayLowFrequencyOscillator::Update(HANDLE hOut)
2525
{
2626
// initialize buffer
2727
CHAR_INFO buf[18];

DisplayLowFrequencyOscillator.h

+5-1
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,8 @@ Copyright 2014 Kenneth D. Miller III
66
Low-Frequency Oscillator Display
77
*/
88

9-
extern void UpdateLowFrequencyOscillatorDisplay(HANDLE hOut);
9+
class DisplayLowFrequencyOscillator
10+
{
11+
public:
12+
void Update(HANDLE hOut);
13+
};

DisplayOscillatorFrequency.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Oscillator Frequency Display
1515
#include "OscillatorNote.h"
1616

1717
// show oscillator frequency
18-
void UpdateOscillatorFrequencyDisplay(HANDLE hOut, int const v, int const o)
18+
void DisplayOscillatorFrequency::Update(HANDLE hOut, int const v, int const o)
1919
{
2020
// oscillator key frequency (taking key follow and pitch wheel control into account)
2121
float const osc_key_freq = NoteFrequency(voice_note[v], osc_config[o].key_follow);

DisplayOscillatorFrequency.h

+5-1
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,8 @@ Copyright 2014 Kenneth D. Miller III
66
Oscillator Frequency Display
77
*/
88

9-
extern void UpdateOscillatorFrequencyDisplay(HANDLE hOut, int const v, int const o);
9+
class DisplayOscillatorFrequency
10+
{
11+
public:
12+
void Update(HANDLE hOut, int const v, int const o);
13+
};

DisplayOscillatorWaveform.cpp

+12-24
Original file line numberDiff line numberDiff line change
@@ -31,21 +31,14 @@ static CHAR_INFO const plot[2] = {
3131
static COORD const pos = { 0, 0 };
3232
static COORD const size = { WAVEFORM_WIDTH, WAVEFORM_HEIGHT };
3333

34-
// local filter for plot
35-
static FilterState filter;
36-
37-
// detect when to reset the filter
38-
static bool prev_active;
39-
static int prev_v;
40-
41-
// previous frame time
42-
static DWORD prevTime = timeGetTime();
43-
44-
// leftover cycles
45-
static float cyclesLeftOver;
34+
// initialization
35+
void DisplayOscillatorWaveform::Init()
36+
{
37+
prevTime = timeGetTime();
38+
}
4639

4740
// get oscillator value
48-
static float UpdateOscillatorOutput(NoteOscillatorConfig const config[], OscillatorState state[], float step[], float delta[])
41+
float DisplayOscillatorWaveform::UpdateOscillatorOutput(NoteOscillatorConfig const config[])
4942
{
5043
float value = 0.0f;
5144
for (int o = 0; o < NUM_OSCILLATORS; ++o)
@@ -61,26 +54,26 @@ static float UpdateOscillatorOutput(NoteOscillatorConfig const config[], Oscilla
6154
}
6255

6356
// get one waveform step
64-
static float UpdateWaveformStep(int oversample, NoteOscillatorConfig const config[], OscillatorState state[], float step[], float delta[], FilterState &filter)
57+
float DisplayOscillatorWaveform::UpdateWaveformStep(int oversample, NoteOscillatorConfig const config[])
6558
{
6659
if (flt_config.enable)
6760
{
6861
// sum the oscillator outputs
6962
float value = 0;
7063
for (int i = 0; i < oversample; ++i)
7164
{
72-
value += filter.Update(flt_config, UpdateOscillatorOutput(config, state, step, delta));
65+
value += filter.Update(flt_config, UpdateOscillatorOutput(config));
7366
}
7467
return value / oversample;
7568
}
7669
else
7770
{
78-
return UpdateOscillatorOutput(config, state, step, delta);
71+
return UpdateOscillatorOutput(config);
7972
}
8073
}
8174

8275
// waveform display settings
83-
void UpdateOscillatorWaveformDisplay(HANDLE hOut, BASS_INFO const &info, int const v)
76+
void DisplayOscillatorWaveform::Update(HANDLE hOut, BASS_INFO const &info, int const v)
8477
{
8578
// display region
8679
SMALL_RECT region = { 0, 49 - WAVEFORM_HEIGHT, WAVEFORM_WIDTH - 1, 48 };
@@ -123,12 +116,7 @@ void UpdateOscillatorWaveformDisplay(HANDLE hOut, BASS_INFO const &info, int con
123116
// base phase step for plot
124117
float const step_base = cycle / float(WAVEFORM_WIDTH * oversample);
125118

126-
// local oscillator state for plot
127-
OscillatorState state[NUM_OSCILLATORS];
128-
129119
// compute phase steps and deltas for each oscillator
130-
float step[NUM_OSCILLATORS];
131-
float delta[NUM_OSCILLATORS];
132120
for (int o = 0; o < NUM_OSCILLATORS; ++o)
133121
{
134122
// step and delta phase
@@ -205,7 +193,7 @@ void UpdateOscillatorWaveformDisplay(HANDLE hOut, BASS_INFO const &info, int con
205193
// run oscillators and filters forward
206194
for (int x = 0; x < steps; ++x)
207195
{
208-
UpdateWaveformStep(oversample, config, state, step, delta, filter);
196+
UpdateWaveformStep(oversample, config);
209197
}
210198
}
211199
}
@@ -231,7 +219,7 @@ void UpdateOscillatorWaveformDisplay(HANDLE hOut, BASS_INFO const &info, int con
231219
for (int x = 0; x < WAVEFORM_WIDTH; ++x)
232220
{
233221
// sum the oscillator outputs
234-
float const value = amp_env_amplitude * UpdateWaveformStep(oversample, config, state, step, delta, filter);
222+
float const value = amp_env_amplitude * UpdateWaveformStep(oversample, config);
235223

236224
// plot waveform column
237225
int grid_y = FloorInt(-(WAVEFORM_HEIGHT - 0.5f) * value);

DisplayOscillatorWaveform.h

+33-1
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,36 @@ Copyright 2014 Kenneth D. Miller III
66
Oscillator Waveform Display
77
*/
88

9-
void UpdateOscillatorWaveformDisplay(HANDLE hOut, BASS_INFO const &info, int const v);
9+
#include "OscillatorNote.h"
10+
#include "Filter.h"
11+
12+
class DisplayOscillatorWaveform
13+
{
14+
public:
15+
void Init();
16+
void Update(HANDLE hOut, BASS_INFO const &info, int const v);
17+
18+
private:
19+
float UpdateOscillatorOutput(NoteOscillatorConfig const config[]);
20+
float UpdateWaveformStep(int oversample, NoteOscillatorConfig const config[]);
21+
22+
// compute phase steps and deltas for each oscillator
23+
float step[NUM_OSCILLATORS];
24+
float delta[NUM_OSCILLATORS];
25+
26+
// local oscillator state for plot
27+
OscillatorState state[NUM_OSCILLATORS];
28+
29+
// local filter for plot
30+
FilterState filter;
31+
32+
// detect when to reset the filter
33+
bool prev_active;
34+
int prev_v;
35+
36+
// previous frame time
37+
DWORD prevTime;
38+
39+
// leftover cycles
40+
float cyclesLeftOver;
41+
};

DisplaySpectrumAnalyzer.cpp

+17-28
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,6 @@ Spectrum Analyzer Display
99
#include "DisplaySpectrumAnalyzer.h"
1010
#include "Math.h"
1111

12-
// fast fourier transform properties
13-
#define FFT_TYPE 5
14-
static int const SCALE = 128;
15-
static int const FFT_SIZE = 256 << FFT_TYPE;
16-
static int const FREQUENCY_BINS = FFT_SIZE / 2;
17-
18-
// sliding sample buffer stream
19-
static CRITICAL_SECTION cs;
20-
static HSTREAM sliding_stream;
21-
static HDSP collect_samples;
22-
23-
// sliding sample buffer for use by the FFT
24-
static float sliding_buffer[FFT_SIZE];
25-
2612
// frequency constants
2713
static float const semitone = 1.0594630943592952645618252949463f; //powf(2.0f, 1.0f / 12.0f);
2814
static float const quartertone = 0.97153194115360586874328941582127f; //1/sqrtf(semitone)
@@ -40,17 +26,17 @@ static CHAR_INFO const bar_empty = { 0, BACKGROUND_BLUE };
4026
static CHAR_INFO const bar_nyquist = { 0, BACKGROUND_RED };
4127

4228
// add data from the audio stream to the sliding sample buffer
43-
static void CALLBACK AppendDataToSlidingBuffer(HDSP handle, DWORD channel, float *buffer, DWORD length, void *user)
29+
void CALLBACK DisplaySpectrumAnalyzer::AppendDataToSlidingBuffer(HDSP handle, DWORD channel, float *buffer, DWORD length, DisplaySpectrumAnalyzer *user)
4430
{
4531
// enter critical section for the sliding buffer
46-
EnterCriticalSection(&cs);
32+
EnterCriticalSection(&user->cs);
4733

4834
// stereo sample count
4935
unsigned int count = length / (2 * sizeof(float));
5036
if (count < FFT_SIZE)
5137
{
5238
// shift sample data down
53-
memmove(sliding_buffer, sliding_buffer + count, (FFT_SIZE - count) * sizeof(float));
39+
memmove(user->sliding_buffer, user->sliding_buffer + count, (FFT_SIZE - count) * sizeof(float));
5440
}
5541
else if (count > FFT_SIZE)
5642
{
@@ -63,45 +49,48 @@ static void CALLBACK AppendDataToSlidingBuffer(HDSP handle, DWORD channel, float
6349
// converting to mono samples for analysis
6450
for (unsigned int i = 0; i < count; ++i)
6551
{
66-
sliding_buffer[FFT_SIZE - count + i] = 0.5f * (buffer[i + i] + buffer[i + i + 1]);
52+
user->sliding_buffer[FFT_SIZE - count + i] = 0.5f * (buffer[i + i] + buffer[i + i + 1]);
6753
}
6854

6955
// done
70-
LeaveCriticalSection(&cs);
56+
LeaveCriticalSection(&user->cs);
7157
}
7258

7359
// get data from the sliding sample buffer
74-
static DWORD CALLBACK GetDataFromSlidingBuffer(HSTREAM handle, void *buffer, DWORD length, void *user)
60+
DWORD CALLBACK DisplaySpectrumAnalyzer::GetDataFromSlidingBuffer(HSTREAM handle, void *buffer, DWORD length, DisplaySpectrumAnalyzer *user)
7561
{
7662
// enter critical section for the sliding buffer
77-
EnterCriticalSection(&cs);
63+
EnterCriticalSection(&user->cs);
7864

7965
// limit length to that of the sliding buffer
8066
length = Min(length, DWORD(FFT_SIZE * sizeof(float)));
8167

8268
// copy the latest data from the sliding buffer
83-
memcpy(buffer, (char *)sliding_buffer + FFT_SIZE * sizeof(float) - length, length);
69+
memcpy(buffer, (char *)user->sliding_buffer + FFT_SIZE * sizeof(float)-length, length);
8470

8571
// done
86-
LeaveCriticalSection(&cs);
72+
LeaveCriticalSection(&user->cs);
8773

8874
// return the requested amount of data
8975
return length;
9076
}
9177

92-
void InitSpectrumAnalyzer(DWORD stream, BASS_INFO const &info)
78+
void DisplaySpectrumAnalyzer::Init(DWORD stream, BASS_INFO const &info)
9379
{
80+
// clear sliding buffer
81+
memset(sliding_buffer, 0, sizeof(sliding_buffer));
82+
9483
// create a critical section
9584
InitializeCriticalSection(&cs);
9685

9786
// add a channel DSP to collect samples
98-
collect_samples = BASS_ChannelSetDSP(stream, (DSPPROC *)AppendDataToSlidingBuffer, NULL, -1);
87+
collect_samples = BASS_ChannelSetDSP(stream, (DSPPROC *)AppendDataToSlidingBuffer, this, -1);
9988

10089
// create a data stream to return data from the sliding buffer
101-
sliding_stream = BASS_StreamCreate(info.freq, 1, BASS_SAMPLE_FLOAT | BASS_STREAM_DECODE, GetDataFromSlidingBuffer, NULL);
90+
sliding_stream = BASS_StreamCreate(info.freq, 1, BASS_SAMPLE_FLOAT | BASS_STREAM_DECODE, (STREAMPROC *)GetDataFromSlidingBuffer, this);
10291
}
10392

104-
void CleanupSpectrumAnalyzer(DWORD stream)
93+
void DisplaySpectrumAnalyzer::Cleanup(DWORD stream)
10594
{
10695
// remove the channel DSP
10796
BASS_ChannelRemoveDSP(stream, collect_samples);
@@ -116,7 +105,7 @@ void CleanupSpectrumAnalyzer(DWORD stream)
116105
// SPECTRUM ANALYZER
117106
// horizontal axis shows semitone frequency bands
118107
// vertical axis shows logarithmic power
119-
void UpdateSpectrumAnalyzer(HANDLE hOut, DWORD stream, BASS_INFO const &info, float const freq_min)
108+
void DisplaySpectrumAnalyzer::Update(HANDLE hOut, DWORD stream, BASS_INFO const &info, float const freq_min)
120109
{
121110
// get complex FFT data from the sliding buffer
122111
float fft[FREQUENCY_BINS * 2][2];

DisplaySpectrumAnalyzer.h

+26-3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,29 @@ Spectrum Analyzer Display
99
#define SPECTRUM_WIDTH 80
1010
#define SPECTRUM_HEIGHT 10
1111

12-
void InitSpectrumAnalyzer(DWORD stream, BASS_INFO const &info);
13-
void CleanupSpectrumAnalyzer(DWORD stream);
14-
void UpdateSpectrumAnalyzer(HANDLE hOut, DWORD stream, BASS_INFO const &info, float const freq_min);
12+
// fast fourier transform properties
13+
#define FFT_TYPE 5
14+
static int const SCALE = 128;
15+
static int const FFT_SIZE = 256 << FFT_TYPE;
16+
static int const FREQUENCY_BINS = FFT_SIZE / 2;
17+
18+
class DisplaySpectrumAnalyzer
19+
{
20+
public:
21+
void Init(DWORD stream, BASS_INFO const &info);
22+
void Cleanup(DWORD stream);
23+
void Update(HANDLE hOut, DWORD stream, BASS_INFO const &info, float const freq_min);
24+
25+
private:
26+
27+
static void CALLBACK AppendDataToSlidingBuffer(HDSP handle, DWORD channel, float *buffer, DWORD length, DisplaySpectrumAnalyzer *user);
28+
static DWORD CALLBACK GetDataFromSlidingBuffer(HSTREAM handle, void *buffer, DWORD length, DisplaySpectrumAnalyzer *user);
29+
30+
// sliding sample buffer stream
31+
CRITICAL_SECTION cs;
32+
HSTREAM sliding_stream;
33+
HDSP collect_samples;
34+
35+
// sliding sample buffer for use by the FFT
36+
float sliding_buffer[FFT_SIZE];
37+
};

0 commit comments

Comments
 (0)