-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathExercise1_ConceptRefactoring.h
More file actions
419 lines (344 loc) · 13.6 KB
/
Exercise1_ConceptRefactoring.h
File metadata and controls
419 lines (344 loc) · 13.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
#pragma once
#include "../ModernCPP/AudioConcepts.h"
#include <vector>
#include <cmath>
#include <type_traits>
namespace ModernAudio::Exercises {
// Exercise 1: Refactor DSP Code with C++20 Concepts
//
// OBJECTIVE: Take existing DSP code and refactor it using C++20 concepts
// for better type safety and error messages.
// ============================================================================
// LEGACY CODE (Before Refactoring)
// ============================================================================
template<typename T>
class LegacyFilter {
public:
void setFrequency(T freq) {
frequency_ = freq;
}
T process(T input) {
// Simple one-pole lowpass filter
output_ = output_ + cutoff_ * (input - output_);
return output_;
}
void setSampleRate(T sampleRate) {
sampleRate_ = sampleRate;
updateCutoff();
}
private:
T frequency_{1000.0};
T sampleRate_{44100.0};
T output_{0.0};
T cutoff_{0.1};
void updateCutoff() {
cutoff_ = 1.0 - std::exp(-2.0 * M_PI * frequency_ / sampleRate_);
}
};
// Problems with legacy code:
// 1. No type constraints - can be instantiated with inappropriate types
// 2. No compile-time validation
// 3. Poor error messages when used incorrectly
// 4. No interface guarantees
// ============================================================================
// MODERN REFACTORED VERSION (Using C++20 Concepts)
// ============================================================================
// Step 1: Define audio-specific concepts (if not using AudioConcepts.h)
template<typename T>
concept NumericType = std::is_arithmetic_v<T> && !std::is_same_v<T, bool>;
template<typename T>
concept FloatingPointSample = std::floating_point<T>;
template<typename T>
concept AudioSampleType = NumericType<T>;
// Step 2: Create concept-constrained filter
template<AudioSampleType T>
class ModernFilter {
public:
// Frequency must be floating point for precision
void setFrequency(T freq) requires FloatingPointSample<T> {
static_assert(std::is_floating_point_v<T>, "Frequency must be floating point");
if (freq <= T(0) || freq >= sampleRate_ / T(2)) {
throw std::invalid_argument("Frequency must be positive and below Nyquist");
}
frequency_ = freq;
updateCutoff();
}
// Process method with concept constraints
T process(T input) requires AudioSampleType<T> {
// Type-safe processing with compile-time optimizations
return applyFilter(input);
}
void setSampleRate(T sampleRate) requires FloatingPointSample<T> {
if (sampleRate <= T(0)) {
throw std::invalid_argument("Sample rate must be positive");
}
sampleRate_ = sampleRate;
updateCutoff();
}
void reset() {
output_ = T(0);
}
// Satisfy AudioProcessor concept requirements
void processBlock(float* buffer, int numSamples) {
for (int i = 0; i < numSamples; ++i) {
buffer[i] = static_cast<float>(process(static_cast<T>(buffer[i])));
}
}
int getLatency() const { return 0; }
// Additional concept-constrained methods
T getFrequency() const { return frequency_; }
T getSampleRate() const { return sampleRate_; }
private:
T frequency_{T(1000)};
T sampleRate_{T(44100)};
T output_{T(0)};
T cutoff_{T(0.1)};
T applyFilter(T input) {
// Implementation with compile-time optimizations
if constexpr (std::is_floating_point_v<T>) {
// Use high-precision calculation for floating point
output_ = output_ + cutoff_ * (input - output_);
} else {
// Use integer-safe calculation for integer types
auto temp = static_cast<double>(output_) +
static_cast<double>(cutoff_) *
(static_cast<double>(input) - static_cast<double>(output_));
output_ = static_cast<T>(temp);
}
return output_;
}
void updateCutoff() {
if constexpr (std::is_floating_point_v<T>) {
cutoff_ = T(1) - std::exp(-T(2) * T(M_PI) * frequency_ / sampleRate_);
} else {
// Approximation for integer types
double temp = 1.0 - std::exp(-2.0 * M_PI *
static_cast<double>(frequency_) / static_cast<double>(sampleRate_));
cutoff_ = static_cast<T>(temp * std::numeric_limits<T>::max());
}
}
};
// Step 3: Concept validation and testing
template<typename T>
constexpr bool validateModernFilter() {
// Compile-time validation
static_assert(AudioSampleType<T>, "T must be a valid audio sample type");
return true;
}
// Step 4: Usage examples and demonstrations
class Exercise1Demo {
public:
static void runDemo() {
std::cout << "=== Exercise 1: Concept Refactoring Demo ===\n";
// Valid usage examples
demonstrateValidUsage();
// Show improved error messages
demonstrateErrorMessages();
// Performance comparison
performanceComparison();
}
private:
static void demonstrateValidUsage() {
std::cout << "\n--- Valid Usage Examples ---\n";
// Float filter
ModernFilter<float> floatFilter;
floatFilter.setFrequency(1000.0f);
floatFilter.setSampleRate(44100.0f);
float input = 0.5f;
float output = floatFilter.process(input);
std::cout << "Float filter: " << input << " -> " << output << "\n";
// Double filter for high precision
ModernFilter<double> doubleFilter;
doubleFilter.setFrequency(1000.0);
doubleFilter.setSampleRate(96000.0);
double preciseInput = 0.123456789;
double preciseOutput = doubleFilter.process(preciseInput);
std::cout << "Double filter: " << preciseInput << " -> " << preciseOutput << "\n";
// Integer filter (with appropriate scaling)
ModernFilter<int16_t> intFilter;
intFilter.setSampleRate(44100.0f);
intFilter.setFrequency(500.0f);
int16_t intInput = 16384; // Half scale
int16_t intOutput = intFilter.process(intInput);
std::cout << "Int16 filter: " << intInput << " -> " << intOutput << "\n";
}
static void demonstrateErrorMessages() {
std::cout << "\n--- Error Handling Examples ---\n";
try {
ModernFilter<float> filter;
filter.setSampleRate(44100.0f);
filter.setFrequency(-100.0f); // Invalid negative frequency
} catch (const std::exception& e) {
std::cout << "Caught expected error: " << e.what() << "\n";
}
try {
ModernFilter<float> filter;
filter.setSampleRate(44100.0f);
filter.setFrequency(25000.0f); // Above Nyquist
} catch (const std::exception& e) {
std::cout << "Caught expected error: " << e.what() << "\n";
}
// Compile-time validation examples
static_assert(validateModernFilter<float>());
static_assert(validateModernFilter<double>());
static_assert(validateModernFilter<int16_t>());
// static_assert(validateModernFilter<std::string>()); // Would fail at compile time
std::cout << "Compile-time validations passed!\n";
}
static void performanceComparison() {
std::cout << "\n--- Performance Comparison ---\n";
const size_t numSamples = 100000;
std::vector<float> testData(numSamples);
// Generate test signal
for (size_t i = 0; i < numSamples; ++i) {
testData[i] = std::sin(2.0f * M_PI * 440.0f * i / 44100.0f);
}
// Test legacy filter
LegacyFilter<float> legacyFilter;
legacyFilter.setFrequency(1000.0f);
legacyFilter.setSampleRate(44100.0f);
auto start = std::chrono::high_resolution_clock::now();
for (float& sample : testData) {
sample = legacyFilter.process(sample);
}
auto legacyTime = std::chrono::high_resolution_clock::now() - start;
// Reset test data
for (size_t i = 0; i < numSamples; ++i) {
testData[i] = std::sin(2.0f * M_PI * 440.0f * i / 44100.0f);
}
// Test modern filter
ModernFilter<float> modernFilter;
modernFilter.setFrequency(1000.0f);
modernFilter.setSampleRate(44100.0f);
start = std::chrono::high_resolution_clock::now();
for (float& sample : testData) {
sample = modernFilter.process(sample);
}
auto modernTime = std::chrono::high_resolution_clock::now() - start;
auto legacyMicros = std::chrono::duration_cast<std::chrono::microseconds>(legacyTime).count();
auto modernMicros = std::chrono::duration_cast<std::chrono::microseconds>(modernTime).count();
std::cout << "Legacy filter time: " << legacyMicros << " μs\n";
std::cout << "Modern filter time: " << modernMicros << " μs\n";
std::cout << "Performance ratio: " << static_cast<double>(modernMicros) / legacyMicros << "x\n";
}
};
// Step 5: Advanced concept-constrained filter with multiple algorithms
template<AudioSampleType SampleType>
class AdvancedConceptFilter {
public:
enum class FilterMode { LowPass, HighPass, BandPass, Notch };
AdvancedConceptFilter(FilterMode mode = FilterMode::LowPass) : mode_(mode) {}
void setParameters(SampleType frequency, SampleType q, SampleType sampleRate)
requires FloatingPointSample<SampleType> {
frequency_ = frequency;
q_ = q;
sampleRate_ = sampleRate;
calculateCoefficients();
}
SampleType process(SampleType input) requires AudioSampleType<SampleType> {
if constexpr (std::is_floating_point_v<SampleType>) {
return processBiquad(input);
} else {
// Integer processing with overflow protection
return processIntegerSafe(input);
}
}
void setMode(FilterMode mode) {
mode_ = mode;
calculateCoefficients();
}
void reset() {
x1_ = x2_ = y1_ = y2_ = SampleType(0);
}
// Concept compliance
void processBlock(float* buffer, int numSamples) {
for (int i = 0; i < numSamples; ++i) {
buffer[i] = static_cast<float>(process(static_cast<SampleType>(buffer[i])));
}
}
int getLatency() const { return 0; }
private:
FilterMode mode_;
SampleType frequency_{SampleType(1000)};
SampleType q_{SampleType(0.707)};
SampleType sampleRate_{SampleType(44100)};
// Biquad coefficients
SampleType b0_, b1_, b2_, a1_, a2_;
// State variables
SampleType x1_{0}, x2_{0}, y1_{0}, y2_{0};
void calculateCoefficients() {
if constexpr (std::is_floating_point_v<SampleType>) {
const SampleType omega = SampleType(2) * SampleType(M_PI) * frequency_ / sampleRate_;
const SampleType sin_omega = std::sin(omega);
const SampleType cos_omega = std::cos(omega);
const SampleType alpha = sin_omega / (SampleType(2) * q_);
switch (mode_) {
case FilterMode::LowPass:
b0_ = (SampleType(1) - cos_omega) / SampleType(2);
b1_ = SampleType(1) - cos_omega;
b2_ = (SampleType(1) - cos_omega) / SampleType(2);
break;
case FilterMode::HighPass:
b0_ = (SampleType(1) + cos_omega) / SampleType(2);
b1_ = -(SampleType(1) + cos_omega);
b2_ = (SampleType(1) + cos_omega) / SampleType(2);
break;
case FilterMode::BandPass:
b0_ = alpha;
b1_ = SampleType(0);
b2_ = -alpha;
break;
case FilterMode::Notch:
b0_ = SampleType(1);
b1_ = -SampleType(2) * cos_omega;
b2_ = SampleType(1);
break;
}
SampleType a0 = SampleType(1) + alpha;
a1_ = -SampleType(2) * cos_omega;
a2_ = SampleType(1) - alpha;
// Normalize
b0_ /= a0;
b1_ /= a0;
b2_ /= a0;
a1_ /= a0;
a2_ /= a0;
} else {
// Simplified coefficients for integer types
b0_ = b2_ = SampleType(1);
b1_ = SampleType(2);
a1_ = SampleType(0);
a2_ = SampleType(0);
}
}
SampleType processBiquad(SampleType input) {
SampleType output = b0_ * input + b1_ * x1_ + b2_ * x2_ - a1_ * y1_ - a2_ * y2_;
// Update state
x2_ = x1_;
x1_ = input;
y2_ = y1_;
y1_ = output;
return output;
}
SampleType processIntegerSafe(SampleType input) {
// Integer processing with overflow protection
using WideType = std::conditional_t<sizeof(SampleType) <= 2, int32_t, int64_t>;
WideType output = static_cast<WideType>(b0_) * input +
static_cast<WideType>(b1_) * x1_ +
static_cast<WideType>(b2_) * x2_ -
static_cast<WideType>(a1_) * y1_ -
static_cast<WideType>(a2_) * y2_;
// Clamp to valid range
output = std::clamp(output,
static_cast<WideType>(std::numeric_limits<SampleType>::min()),
static_cast<WideType>(std::numeric_limits<SampleType>::max()));
SampleType result = static_cast<SampleType>(output);
// Update state
x2_ = x1_;
x1_ = input;
y2_ = y1_;
y1_ = result;
return result;
}
};
} // namespace ModernAudio::Exercises