-
Notifications
You must be signed in to change notification settings - Fork 0
/
Generator.cs
154 lines (133 loc) · 5.75 KB
/
Generator.cs
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
using UnityEngine;
namespace PxPre
{
namespace Phonics
{
public static class Generator
{
public const float pi = 3.14159265359f;
public const float tau = pi * 2.0f;
public static void ZeroBuffer(float [] rf, int start, int len)
{
System.Array.Clear(rf, start, len);
}
public static float SetSine(float [] rf, int start, int len, float time, float freq, float amp, int sampsSec)
{
// Multiply tau for 1 hertz.
// Multiply by frequency because that's requested as a paramter
float sincr = 1.0f / sampsSec;
float s = time;
for(int i = start; i < start + len; ++i)
{
rf[i] = Mathf.Sin(s * freq * tau) * amp;
s += sincr;
}
return time + (float)len / (float)sampsSec;
}
public static float SetSine(float[] rf, int start, int len, float phase, float time, float freq, int sampsSec, float env0, float env1, float env2, float env3)
{
float fincr = 1.0f / sampsSec;
float sincr = fincr * freq * tau;
float s = time + phase;
float f = s;
for (int i = start; i < start + len; ++i)
{
float f2 = f * f;
float f3 = f2 * f;
float amp = env0 + f * env1 + f2 * env2 + f3 * env3;
rf[i] = Mathf.Sin(s) * amp;
s += sincr;
f += fincr;
}
return time + (float)len / (float)sampsSec;
}
public static float AddSine(float [] rf, int start, int len, float phase, float time, float freq, float amp, int sampsSec)
{
float sincr = 1.0f / sampsSec * freq * tau;
float s = time + phase;
for (int i = start; i < start + len; ++i)
{
rf[i] += Mathf.Sin(s) * amp;
s += sincr;
}
return time + (float)len / (float)sampsSec;
}
public static float SetThSquare(float [] rf, int start, int len, float phase, float time, float freq, float amp, int sampsSec)
{
float s = (time + phase) * freq * 2.0f;
float sincr = (1.0f / sampsSec * freq) * 2.0f;
for (int i = start; i < start + len; ++i)
{
int cross = (int)s;
rf[i] = ((cross & 1) != 0) ? -amp : amp;
s += sincr;
}
return time + (float)len / (float)sampsSec;
}
public static float SetThSquare(float[] rf, int start, int len, float phase, float time, float freq, int sampsSec, float env0, float env1, float env2, float env3)
{
float s = (time + phase) * freq * 2.0f;
float f = s;
float fincr = 1.0f / sampsSec;
float sincr = fincr * freq * 2.0f;
for (int i = start; i < start + len; ++i)
{
float f2 = f * f;
float f3 = f2 * f;
float amp = env0 + f * env1 + f2 * env2 + f3 * env3;
int cross = (int)s;
rf[i] = ((cross & 1) != 0) ? -amp : amp;
s += sincr;
f += fincr;
}
return time + (float)len / (float)sampsSec;
}
public static float AddThSquare(float [] rf, int start, int len, float phase, float time, float freq, float amp, int sampsSec)
{
float sincr = 1.0f / sampsSec * freq * tau;
float s = time + phase;
for (int i = start; i < start + len; ++i)
{
int cross = (int)(s * freq * 2.0f);
rf[i] += ((cross & 1) != 0) ? -amp : amp;
s += sincr;
}
return time + (float)len / (float)sampsSec;
}
public static float SetTri(float [] rf, int start, int len, float phase, float time, float freq, float amp, int sampsSec)
{
float tamp = amp * 2.0f;
float sincr = (1.0f / sampsSec * freq) * 2.0f;
float s = (time + phase) * sampsSec * freq * 2.0f;
for (int i = start; i < start + len; ++i)
{
int cross = (int)s;
float lambda = s - cross;
if((cross & 1) != 0)
rf[i] = -amp + tamp * lambda;
else
rf[i] = amp - tamp * lambda;
s += sincr;
}
return time + (float)len / (float)sampsSec;
}
public static float AddTri(float [] rf, int start, int len, float phase, float time, float freq, float amp, int sampsSec)
{
float tamp = amp * 2.0f;
float sincr = (1.0f / sampsSec * freq) * 2.0f;
float s = (time + phase) * sampsSec * freq * 2.0f;
for (int i = start; i < start + len; ++i)
{
int cross = (int)s;
float lambda = s - cross;
if ((cross & 1) != 0)
rf[i] += -amp + tamp * lambda;
else
rf[i] += amp - tamp * lambda;
s += sincr;
}
return time + (float)len / (float)sampsSec;
}
}
}
}