-
Notifications
You must be signed in to change notification settings - Fork 0
/
synth.c
132 lines (121 loc) · 3.74 KB
/
synth.c
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
/**
@file synth.c
@author Alejandro Ambroa
@date 1 Oct 2017
@brief Simple software synthetizer based on book "BasicSynth" by Daniel Mitchell.
*/
#include <math.h>
#include <stdlib.h>
#include "synth.h"
#include "math_constants.h"
float oscillator(OSCILLATOR_TYPE type, float* ang, float incr)
{
double value = 0.0;
switch (type) {
case SIN:
value = sin(*ang);
*ang += incr;
if (*ang >= P_2PI)
*ang -= P_2PI;
break;
case TRIANGLE: {
double triValue = *ang * M_PI_2;
*ang += incr;
if (triValue < 0.0)
value = 1.0 + triValue;
else
value = 1.0 - triValue;
if (*ang >= M_PI)
*ang -= P_2PI;
} break;
case SAW:
value = (*ang / M_PI) - 1.0;
*ang += incr;
if (*ang >= P_2PI)
*ang -= P_2PI;
break;
case COS:
value = cos(*ang);
*ang += incr;
if (*ang >= P_2PI)
*ang -= P_2PI;
break;
case NONE:
value = 0;
}
return (float)value;
}
int synthetize(SYNTH* synthParams, sample_t** out_samples, int sample_freq)
{
int state = 0;
float value;
float oldValue = 0.0f;
float volume = synthParams->volume;
int samples_count = (int)((float)sample_freq * synthParams->totalTime);
sample_t* samples = (sample_t*)malloc(samples_count * sizeof(sample_t));
int attackTimeSamples = (int)(synthParams->attackTime * (float)sample_freq);
int decayTimeSamples = (int)(synthParams->decayTime * (float)sample_freq);
int releaseTimeSamples = (int)(synthParams->releaseTime * (float)sample_freq);
int envCount = attackTimeSamples;
float slope = volume / (float)attackTimeSamples;
float phaseIncrOsc1 = (P_2PI / (float)sample_freq) * synthParams->oscillator1_freq;
float phaseIncrOsc2 = (P_2PI / (float)sample_freq) * synthParams->oscillator2_freq;
float phase1 = 0.0f;
float phase2 = 0.0f;
for (int i = 0; i < samples_count; i++) {
value = oscillator(synthParams->oscillator1_type, &phase1, phaseIncrOsc1);
if (synthParams->oscillator2_type != NONE) {
value = value * oscillator(synthParams->oscillator2_type, &phase2, phaseIncrOsc2);
}
// filter
value = (synthParams->filterBeta1 * value) + (synthParams->filterBeta2 * oldValue);
oldValue = value;
// amplitude envelope
switch (state) {
case 0:
if (envCount > 0) {
volume += slope;
envCount--;
} else {
state = 1;
envCount = decayTimeSamples;
slope = (volume - synthParams->decayValue) / (float)decayTimeSamples;
}
case 1:
if (envCount > 0) {
envCount--;
volume -= slope;
} else {
state = 2;
envCount = releaseTimeSamples;
slope = volume / (float)releaseTimeSamples;
}
break;
case 2:
if (envCount > 0) {
envCount--;
volume -= slope;
} else {
state = -1;
volume = 0.0f;
}
break;
}
samples[i] = (sample_t)(volume * value);
}
// simple reverb
float delayTime = synthParams->delayTime;
float reverbSize = synthParams->reverbSize;
int delaySamples = (int)(delayTime * (float)sample_freq);
if (delaySamples > 0) {
for (int i = 0; i < samples_count - delaySamples; i++) {
samples[i + delaySamples] += samples[i] * (sample_t)reverbSize;
}
}
*out_samples = samples;
return samples_count;
}
void free_samples(sample_t* samples)
{
free(samples);
}