-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsynth.h
More file actions
299 lines (243 loc) · 7.79 KB
/
synth.h
File metadata and controls
299 lines (243 loc) · 7.79 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
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
288
289
290
291
292
293
294
295
296
297
298
299
#include <Arduino.h>
//#define PI 3.14159265f
inline void f_analogWrite(float f)
{
analogWrite(6, f*255);
}
//unmessed version of delayMicroseconds
inline void u_delayMicroseconds(unsigned long int us)
{
delayMicroseconds(us);
}
//unmessed version of delay
inline void u_delay(unsigned long int us)
{
delay(us*64);
}
//unmessed version of micros
inline unsigned long u_micros()
{
return micros()/64;
}
/****************************math********************************/
inline float fract(float a)
{
int i_part = a; //extract integer part
return a-i_part; //decimnal part is simply the float part minus the integer part
}
float rand(float n){return fract(sin(n) * 43758.5453123);}
inline float m_to_u(float a){return a/1000.0f;}//from milli to unit
/****************************math********************************/
/*inline float decaying_sine(float frequency, float decay_rate, unsigned long time) //frequenzy in Hz and time sould start from 0 us
{
float ftime = float(time)/1000.0f;
return sin(ftime/1000.0f*2.0*PI) / exp(-10.0*ftime*1000);
}*/
struct f_out
{
f_out(){}
f_out(float a, float b)
{
wave = a;
amplitude = b;
}
float wave, amplitude;
};
/***************************drums*******************************/
inline f_out kick(float ftime)
{
float amplitude = exp(-15.0*ftime);
return f_out(sin(6.2831*45.0*ftime)*amplitude, amplitude);
}
inline f_out snare(float ftime)
{
float amplitude = exp(-45.0*ftime)+exp(-55.0*ftime);
return f_out((sin(6.2831*230.0*ftime))*exp(-55.0*ftime)*0.8 + rand(ftime*100.0)*exp(-45.0*ftime), amplitude);
}
inline f_out ltom(float ftime)
{
float amplitude = exp(-35.0*ftime);
return f_out(sin(6.2831*130.0*ftime)*amplitude, amplitude);
}
inline f_out lmtom(float ftime)
{
float amplitude = exp(-35.0*ftime);
return f_out(sin(6.2831*150.0*ftime)*amplitude, amplitude);
}
inline f_out htom(float ftime)
{
float amplitude = exp(-35.0*ftime);
return f_out(sin(6.2831*190.0*ftime)*amplitude, amplitude);
}
/***************************drums*******************************/
inline float ramp(float tup, float td, float ampl, float ftime)
{
return max(ftime < tup ? ftime/tup : 1.0f - ((ftime-tup)/td), 0);
}
enum drum_types
{
/*SNARE = 38,
KICK = 35,
KICK1 = 36,*/
KICK = 36,
SNARE = 38,
//C_HIHAT = 42,
//O_HIHAT = 46,
LTOM = 45,
LMTOM = 47,
HTOM = 50
//C_CYMB = 49,
//R_CYMB = 51,
//R_BELL =53
};
void play_drum(float drum_ampl, float tup, float td, float base, float ramp_ampl, drum_types drumt)
{
tup = min(m_to_u(5), tup);
const float stop_threshold = 0.01f; //once the function amplitude reaches this value it will stop being played
const float drum_delay = m_to_u(5.0f);//after how many ms the drum sound as to start
//const float tup= m_to_u(10.0f), td=m_to_u(1.0f), base = 40.0f / 310.0f, ampl = ;
float f_amplitude = 1.0f; //the amplitude of the drum function
//writes initial value and waits
f_analogWrite(base);
u_delay(1);
digitalWrite(13, HIGH);
unsigned long start_time;
switch(drumt)
{
case KICK:
start_time = u_micros();
while(f_amplitude > stop_threshold)
{
//calculates time making shure that it starts from zero
float ftime = (u_micros()-start_time)/1000000.0f;
//drum sound
f_out drum_r;
if(ftime > drum_delay)
drum_r = kick(ftime - drum_delay);
else
drum_r = f_out(0.0f, 10.0f);
//ramp
float ramp_r = ramp(tup, td, ramp_ampl, ftime);
//combines the two
float combined = drum_ampl*drum_r.wave + ramp_ampl*ramp_r;
//adds bias(half the drum sound amplitude unless it is less than the base voltage)
combined += base + drum_ampl*drum_r.amplitude/2.0f;
f_analogWrite(combined);
f_amplitude = drum_r.amplitude;
}
break;
case SNARE:
start_time = u_micros();
while(f_amplitude > stop_threshold)
{
//calculates time making shure that it starts from zero
float ftime = (u_micros()-start_time)/1000000.0f;
//drum sound
f_out drum_r;
if(ftime > drum_delay)
drum_r = snare(ftime - drum_delay);
else
drum_r = f_out(0.0f, 10.0f);
//ramp
float ramp_r = ramp(tup, td, ramp_ampl, ftime);
//combines the two
float combined = drum_ampl*drum_r.wave + ramp_ampl*ramp_r;
//adds bias(half the drum sound amplitude unless it is less than the base voltage)
combined += base + drum_ampl*drum_r.amplitude/2.0f;
f_analogWrite(combined);
f_amplitude = drum_r.amplitude;
}
break;
case LTOM:
start_time = u_micros();
while(f_amplitude > stop_threshold)
{
//calculates time making shure that it starts from zero
float ftime = (u_micros()-start_time)/1000000.0f;
//drum sound
f_out drum_r;
if(ftime > drum_delay)
drum_r = ltom(ftime - drum_delay);
else
drum_r = f_out(0.0f, 10.0f);
//ramp
float ramp_r = ramp(tup, td, ramp_ampl, ftime);
//combines the two
float combined = drum_ampl*drum_r.wave + ramp_ampl*ramp_r;
//adds bias(half the drum sound amplitude unless it is less than the base voltage)
combined += base + drum_ampl*drum_r.amplitude/2.0f;
f_analogWrite(combined);
f_amplitude = drum_r.amplitude;
}
break;
case LMTOM:
start_time = u_micros();
while(f_amplitude > stop_threshold)
{
//calculates time making shure that it starts from zero
float ftime = (u_micros()-start_time)/1000000.0f;
//drum sound
f_out drum_r;
if(ftime > drum_delay)
drum_r = lmtom(ftime - drum_delay);
else
drum_r = f_out(0.0f, 10.0f);
//ramp
float ramp_r = ramp(tup, td, ramp_ampl, ftime);
//combines the two
float combined = drum_ampl*drum_r.wave + ramp_ampl*ramp_r;
//adds bias(half the drum sound amplitude unless it is less than the base voltage)
combined += base + drum_ampl*drum_r.amplitude/2.0f;
f_analogWrite(combined);
f_amplitude = drum_r.amplitude;
}
break;
case HTOM:
start_time = u_micros();
while(f_amplitude > stop_threshold)
{
//calculates time making shure that it starts from zero
float ftime = (u_micros()-start_time)/1000000.0f;
//drum sound
f_out drum_r;
if(ftime > drum_delay)
drum_r = htom(ftime - drum_delay);
else
drum_r = f_out(0.0f, 10.0f);
//ramp
float ramp_r = ramp(tup, td, ramp_ampl, ftime);
//combines the two
float combined = drum_ampl*drum_r.wave + ramp_ampl*ramp_r;
//adds bias(half the drum sound amplitude unless it is less than the base voltage)
combined += base + drum_ampl*drum_r.amplitude/2.0f;
f_analogWrite(combined);
f_amplitude = drum_r.amplitude;
}
break;
default:
start_time = u_micros();
while(f_amplitude > stop_threshold)
{
//calculates time making shure that it starts from zero
float ftime = (u_micros()-start_time)/1000000.0f;
//drum sound
f_out drum_r;
if(ftime > drum_delay)
drum_r = htom(ftime - drum_delay);
else
drum_r = f_out(0.0f, 10.0f);
//ramp
float ramp_r = ramp(tup, td, ramp_ampl, ftime);
//combines the two
float combined = drum_ampl*drum_r.wave + ramp_ampl*ramp_r;
//adds bias(half the drum sound amplitude unless it is less than the base voltage)
combined += base + drum_ampl*drum_r.amplitude/2.0f;
f_analogWrite(combined);
f_amplitude = drum_r.amplitude;
}
break;
}
digitalWrite(13, LOW);
u_delay(1);
f_analogWrite(0);
}