-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnlowlevel.sin.h
More file actions
126 lines (112 loc) · 2.96 KB
/
nlowlevel.sin.h
File metadata and controls
126 lines (112 loc) · 2.96 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
/* defines nlowlevel::sin functions
seems to work!
description:
sin (
double *dest // destination array
,const int len // length of dest
,const double rrate // 1.0 / samples_per_second
,const double mag // magnitude of result
,const double freq | *freq // scalar or array of frequency values, in hertz
,[double &phase | // starting phase value, reuse next time
[const double *phase // array of phase values
,double &acc]] // set to 0.0 to start, reuse next time
);
*/
#pragma once
#include "constants.h"
#include "sintable/sintable.hpp" // magic
#include <math.h>
// assign one of these...
#define FASTSINEFUNC( X ) ( nlowlevel::sine1fast( X ) ) // parabolic approximation (faster than the standard library but audibly inaccurate)
#define TABLESINEFUNC( X ) ( sintable::lookup( X ) ) // linearly interpolated table lookup (fastest if there is good caching, also accurate)
#define STANDARDSINEFUNC( X ) ( ::sin( X * M_2PI ) ) // standard math library (slowest)
// .. to this SINEFUNC( X ) macro.
#define SINEFUNC( X ) ( TABLESINEFUNC(X) )
namespace nlowlevel {
namespace {
double sine1fast(double x) { // there might be a way to make this faster, but the compiler already does well here
x = (x - floor(x)) * 2.0 - 1.0; // wrap to [-1..1]
x *= -4.0 * (1.0 - abs(x));
const double P = 0.218; // 0.225, 0.218
const double Q = 0.792; // 0.775, 0.782
//return x * (P * (abs(x) - 1.0) + 1.0); // low precision
return x * (Q + P * abs(x)); // high precision
}
double sine(double x) {
return SINEFUNC(x);
}
}
void
init_sin (
void
) {
sintable::init();
}
void sin ( // a a
double *dest
,const int len
,const double rrate
,const double mag
,const double *freq
,const double *phase
,double &acc
) {
double freq1 = freq[ 0 ];
double phase1 = phase[ 0 ];
for (int i = 0; i < len; ++i) {
acc += freq1 * rrate;
freq1 = freq[ i + 1 ];
dest[i] = mag * sine(phase1 + acc);
phase1 = phase[ i + 1 ];
}
}
void sin ( // a s
double *dest
,const int len
,const double rrate
,const double mag
,const double *freq
,double &phase
) {
double freq0 = freq[ 0 ];
double phase0 = phase;
int i = 0;
for( ; i < len; ++i ) {
dest[ i ] = mag * sine( phase0 );
phase0 += freq0 * rrate;
freq0 = freq[ i + 1 ];
}
phase = phase0;
}
void sin ( // s a
double *dest
,const int len
,const double rrate
,const double mag
,const double freq
,const double *phase
,double &acc
) {
double phase1 = phase[ 0 ];
for (int i = 0; i < len; ++i) {
acc += freq * rrate;
dest[i] = mag * sine(phase1 + acc);
phase1 = phase[ i + 1 ];
}
}
void sin ( // s s
double *dest
,const int len
,const double rrate
,const double mag
,const double freq
,double &phase
) {
double phase0 = phase;
for (int i = 0; i < len; ++i) {
dest[i] = mag * sine(phase0);
phase0 += freq * rrate;
}
phase = phase0;
}
}