@@ -9,20 +9,6 @@ Spectrum Analyzer Display
9
9
#include " DisplaySpectrumAnalyzer.h"
10
10
#include " Math.h"
11
11
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
-
26
12
// frequency constants
27
13
static float const semitone = 1 .0594630943592952645618252949463f ; // powf(2.0f, 1.0f / 12.0f);
28
14
static float const quartertone = 0 .97153194115360586874328941582127f ; // 1/sqrtf(semitone)
@@ -40,17 +26,17 @@ static CHAR_INFO const bar_empty = { 0, BACKGROUND_BLUE };
40
26
static CHAR_INFO const bar_nyquist = { 0 , BACKGROUND_RED };
41
27
42
28
// 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)
44
30
{
45
31
// enter critical section for the sliding buffer
46
- EnterCriticalSection (&cs);
32
+ EnterCriticalSection (&user-> cs );
47
33
48
34
// stereo sample count
49
35
unsigned int count = length / (2 * sizeof (float ));
50
36
if (count < FFT_SIZE)
51
37
{
52
38
// 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 ));
54
40
}
55
41
else if (count > FFT_SIZE)
56
42
{
@@ -63,45 +49,48 @@ static void CALLBACK AppendDataToSlidingBuffer(HDSP handle, DWORD channel, float
63
49
// converting to mono samples for analysis
64
50
for (unsigned int i = 0 ; i < count; ++i)
65
51
{
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 ]);
67
53
}
68
54
69
55
// done
70
- LeaveCriticalSection (&cs);
56
+ LeaveCriticalSection (&user-> cs );
71
57
}
72
58
73
59
// 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)
75
61
{
76
62
// enter critical section for the sliding buffer
77
- EnterCriticalSection (&cs);
63
+ EnterCriticalSection (&user-> cs );
78
64
79
65
// limit length to that of the sliding buffer
80
66
length = Min (length, DWORD (FFT_SIZE * sizeof (float )));
81
67
82
68
// 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);
84
70
85
71
// done
86
- LeaveCriticalSection (&cs);
72
+ LeaveCriticalSection (&user-> cs );
87
73
88
74
// return the requested amount of data
89
75
return length;
90
76
}
91
77
92
- void InitSpectrumAnalyzer (DWORD stream, BASS_INFO const &info)
78
+ void DisplaySpectrumAnalyzer::Init (DWORD stream, BASS_INFO const &info)
93
79
{
80
+ // clear sliding buffer
81
+ memset (sliding_buffer, 0 , sizeof (sliding_buffer));
82
+
94
83
// create a critical section
95
84
InitializeCriticalSection (&cs);
96
85
97
86
// 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 );
99
88
100
89
// 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 );
102
91
}
103
92
104
- void CleanupSpectrumAnalyzer (DWORD stream)
93
+ void DisplaySpectrumAnalyzer::Cleanup (DWORD stream)
105
94
{
106
95
// remove the channel DSP
107
96
BASS_ChannelRemoveDSP (stream, collect_samples);
@@ -116,7 +105,7 @@ void CleanupSpectrumAnalyzer(DWORD stream)
116
105
// SPECTRUM ANALYZER
117
106
// horizontal axis shows semitone frequency bands
118
107
// 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)
120
109
{
121
110
// get complex FFT data from the sliding buffer
122
111
float fft[FREQUENCY_BINS * 2 ][2 ];
0 commit comments