-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSound_old.h
287 lines (227 loc) · 5.17 KB
/
Sound_old.h
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
#pragma once
#ifndef SOUND_H
#define SOUND_H
#ifndef SOUND_FREQUENCY
#define SOUND_FREQUENCY 44100.0
#endif
#include <SDL.h>
#include <vector>
#include <queue>
#include "AbstractedAccess.h"
// t: 0-1, progress of one wave
// Returns wave
typedef float(*synth_func)(float t);
template<class T>
using Pipe = EasyPointer<Source<T>>;
class soundMaker : public Source<float> {
public:
virtual float getSound() {
return 0;
};
float Get() {
return getSound();
}
virtual void reset();
};
class filter : public soundMaker {
public:
Pipe<float> source;
filter(Pipe<float> sound_src);
void linkSource(Pipe<float> sound_src);
virtual float getSound();
float Get() {
return getSound();;
}
virtual void reset();
};
class blendFilter : public soundMaker {
public:
std::vector<Pipe<float>> sources;
void addSource(Pipe<float> sound_src);
virtual float getSound();
virtual void reset();
};
class blendAdd : public blendFilter {
public:
float getSound() {
float sound = 0;
for (int i = 0; i < sources.size(); i++) sound += sources[i]->Get();
return sound;
};
};
class blendMult : public blendFilter {
public:
float getSound();
};
class dualFilter : public soundMaker {
public:
Pipe<float> source1;
Pipe<float> source2;
dualFilter(Pipe<float> src1, Pipe<float> src2);
virtual float getSound();
virtual void reset();
};
class dualAdd : public dualFilter {
public:
dualAdd(Pipe<float> s1, Pipe<float> s2);
float getSound();
};
class dualMultiply : public dualFilter {
public:
dualMultiply(Pipe<float> s1, Pipe<float> s2);
float getSound();
};
class dualDivide : public dualFilter {
public:
dualDivide(Pipe<float> s1, Pipe<float> s2);
float getSound();
};
class propertyWave : public Source<float> {
private:
float time;
public:
Pipe<float> min;
Pipe<float> max;
Pipe<float> frequency;
// f, o, min, max
propertyWave(Pipe<float> freq, float offset, Pipe<float> min, Pipe<float> max);
propertyWave(float freq, float offset, float min, float max);
float Get();
void reset();
};
class volumeFilter : public filter {
private:
float internalVolume;
public:
Pipe<float> targetVolume;
volumeFilter(Pipe<float> src, Pipe<float> vol);
float getSound();
void reset();
};
class Modulator : public Source<float> {
private:
float time;
public:
std::vector<float> vals;
Pipe<float> frequency;
Modulator(Pipe<float> f);
Modulator(float f);
float Get();
void reset();
};
class synthSound : public soundMaker {
private:
float time;
public:
synth_func sound;
Pipe<float> frequency;
synthSound(synth_func synth, Pipe<float>& f);
virtual float getSound();
void reset();
};
extern const synth_func sineFunc;
extern const synth_func squareFunc;
extern const synth_func triangleFunc;
extern const synth_func sawtoothFunc;
class sineSound : public synthSound {
public:
sineSound(Pipe<float> f);
};
class squareSound : public synthSound {
public:
squareSound(Pipe<float> f);
};
class triangleSound : public synthSound {
public:
triangleSound(EasyPointer <Source<float>> f);
};
class sawtoothSound : public synthSound {
public:
sawtoothSound(EasyPointer <Source<float>> f);
};
class noiseSound : public soundMaker {
private:
float progress = 0;
float amplitude = 0;
void setRandomAmplitude();
public:
Pipe<float> frequency;
noiseSound(Pipe<float> f);
float getSound();
void reset();
};
class pulseSound : public soundMaker {
private:
float time = 0;
public:
Pipe<float> frequency;
Pipe<float> duty;
pulseSound(Pipe<float> f, Pipe<float> d);
float getSound();
void reset();
};
class fadeFilter : public filter {
private:
float volume = 0;
float lastSample = 0;
bool stopped = true;
std::deque<float> resumeBuffer;
SDL_mutex* bufferLock;
// "Fade" is the trail left after audio is stopped, a rudimentary fadeout
float BlendWithFade(float v);
public:
Pipe<float> finishThreshold;
Pipe<float> decayRate;
fadeFilter(Pipe<float> s, Pipe<float> threshold, Pipe<float> decay);
fadeFilter(Pipe<float> s, float threshold, Pipe<float> decay) : fadeFilter(s, new Val<float>(threshold), decay) {}
fadeFilter(Pipe<float> s, Pipe<float> threshold, float decay) : fadeFilter(s, threshold, new Val<float>(decay)) {}
fadeFilter(Pipe<float> s, float threshold, float decay) : fadeFilter(s, new Val<float>(threshold), new Val<float>(decay)) {}
~fadeFilter();
bool isStopped();
bool finished();
float getSound();
void stop();
void start();
void restart();
void reset();
};
class LowPassFilter : filter {
protected:
float cutoff_freq;
float dt;
float RC;
float alpha;
float lastOutput = 0;
public:
LowPassFilter(Pipe<float> src, float cutoff);
float getSound();
void SetCutoff(float cutoff);
};
class HighPassFilter : filter {
protected:
float cutoff_freq;
float dt;
float RC;
float alpha;
float lastRawOutput = 0;
float lastOutput = 0;
public:
HighPassFilter(Pipe<float> src, float cutoff);
float getSound();
void SetCutoff(float cutoff);
};
class EchoFilter : public filter {
protected:
float* bufferStart = NULL;
float* bufferEnd = NULL;
float* bufferHead = NULL;
int len = 0;
Pipe<float> decayRate;
Pipe<float> echoDuration;
void SetLen(int l);
public:
EchoFilter(Pipe<float> src, Pipe<float> duration, Pipe<float> decay);
~EchoFilter();
float getSound();
};
#endif