Line data Source code
1 : #pragma once
2 :
3 : #include <cstdint>
4 : #include <cmath>
5 : #include <algorithm>
6 : #include <string>
7 : #include <vector>
8 : #include <array>
9 : #include <memory>
10 : #include <atomic>
11 : #include <functional>
12 : #include <mutex>
13 :
14 : namespace Amplitron {
15 :
16 : constexpr int DEFAULT_SAMPLE_RATE = 48000;
17 : constexpr int DEFAULT_BUFFER_SIZE = 64; // ~1.3ms latency at 48kHz
18 : constexpr int MAX_BUFFER_SIZE = 512;
19 : constexpr int MIN_BUFFER_SIZE = 32;
20 : constexpr float PI = 3.14159265358979323846f;
21 : constexpr float TWO_PI = 2.0f * PI;
22 :
23 1056847 : inline float clamp(float value, float min_val, float max_val) {
24 1034326 : return std::max(min_val, std::min(max_val, value));
25 : }
26 :
27 181619 : inline float db_to_linear(float db) {
28 193410 : return std::pow(10.0f, db / 20.0f);
29 : }
30 :
31 238088 : inline float linear_to_db(float linear) {
32 238092 : return 20.0f * std::log10(std::max(linear, 1e-10f));
33 : }
34 :
35 : // Fast tanh approximation using Padé approximant.
36 : // ~3× faster than std::tanh(), perceptually identical for musical signals.
37 6144 : inline float fast_tanh(float x) {
38 6144 : float x2 = x * x;
39 6144 : return x * (27.0f + x2) / (27.0f + 9.0f * x2);
40 : }
41 :
42 6158 : inline float soft_clip(float x) {
43 6158 : if (x > 1.0f) return 2.0f / 3.0f;
44 3983 : if (x < -1.0f) return -2.0f / 3.0f;
45 2205 : return x - (x * x * x) / 3.0f;
46 2055 : }
47 :
48 26118 : inline float hard_clip(float x, float threshold) {
49 26120 : return clamp(x, -threshold, threshold);
50 : }
51 :
52 : // Simple one-pole low-pass filter for parameter smoothing
53 2 : struct SmoothParam {
54 2 : float current = 0.0f;
55 2 : float target = 0.0f;
56 2 : float coeff = 0.995f;
57 :
58 : void set(float val) { target = val; }
59 600 : float next() {
60 600 : current += (target - current) * (1.0f - coeff);
61 600 : return current;
62 : }
63 3 : void snap() { current = target; }
64 : };
65 :
66 : } // namespace Amplitron
|