-
Notifications
You must be signed in to change notification settings - Fork 0
/
sound.h
133 lines (117 loc) · 2.89 KB
/
sound.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
/*
* sound.h
* Jumpman
*
* Copyright 2008 Andi McClure. All rights reserved.
*
*/
// Very simple audio playback objects (square tone, noise channel, sample)
#ifndef JUMP_SOUND_H
#define JUMP_SOUND_H
struct noise {
int ticks;
int max;
virtual void to(double &tosample) = 0;
inline bool done() { return ticks <= 0; } // TODO: Make virtual or something
}; // TODO: Something?
// EXCUSES: The sound classes contain much code duplication because I want 'inline' to work.
// SOUNDS
struct sqtone : public noise {
int w;
double amp;
sqtone(int _max, int _w, double _amp) : amp(_amp), w(_w) {
max = _max;
ticks = max;
}
inline double sample() {
double value = 0;
if (ticks > 0) {
value += ((max - ticks) % (w*2) >= w ? 1 : -1)
* ((double)(ticks)/max)
* amp;
ticks--;
}
return value;
}
inline void to(double &tosample) { tosample += sample(); }
};
extern int tapping; // Ugh don't... don't do this
struct slidetone : public noise {
int w, slideafter, slideon, slideby, flipnext, flipat, flips;
double amp;
slidetone(int _max, int _w, int _slideafter, int _slideon, int _slideby, double _amp) : amp(_amp), w(_w), slideafter(_slideafter), slideon(_slideon), slideby(_slideby) {
max = _max;
ticks = max;
flipnext = 0;
flipat = -1;
flips = 0;
}
inline double sample() {
double value = 0;
if (ticks > 0) {
if (tapping >= flipnext) {
flipat *= -1;
flipnext = tapping + w;
if (0 >= slideafter && 0 == flips++ % slideon)
w += slideby;
}
slideafter--;
value += flipat
* ((double)(ticks)/max)
* amp;
if (0 >= slideafter)
ticks--;
else
slideafter--;
}
return value;
}
inline void to(double &tosample) { tosample += sample(); }
};
struct notone : public noise {
int nextstepat;
int w;
double amp;
double stair;
notone(int _max, int _w, double _amp) : amp(_amp), w(_w), stair(0) {
max = _max;
ticks = max;
nextstepat = ticks - w;
stairstep();
}
inline void stairstep() {
stair = (double)random()/RANDOM_MAX * 2 - 1;
}
inline double sample() {
double value = 0;
if (ticks > 0) {
if (ticks == nextstepat) {
nextstepat = ticks - w;
stairstep();
}
value += stair
* ((double)(ticks)/max)
* amp;
ticks--;
}
return value;
}
inline void to(double &tosample) { tosample += sample(); }
};
struct sampletone : public noise {
double *source;
sampletone(double *_source, int len) : source(_source) {
max = len;
ticks = max;
}
inline double sample() {
double value = 0;
if (ticks > 0) {
value = source[max-ticks];
ticks--;
}
return value;
}
inline void to(double &tosample) { tosample += sample(); }
};
#endif // JUMP_SOUND_H