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
|
#include <stdlib.h>
#include <math.h>
#include <errno.h>
#include <xas/synth.h>
static int16_t sample_sine(xas_synth *synth) {
int16_t ret;
static float tau = 2.0f * M_PI;
if (synth->state == XAS_SYNTH_ACTIVE) {
ret = (int16_t)roundf(synth->gain * (INT16_MAX >> 2) * sinf(synth->phase));
synth->phase += tau / (synth->format.sample_rate / synth->frequency);
if (synth->phase > tau) {
synth->phase -= tau;
}
} else {
ret = 0;
}
return ret;
}
static int16_t sample_square(xas_synth *synth) {
int16_t ret;
static float tau = 2.0f * M_PI;
if (synth->state == XAS_SYNTH_ACTIVE) {
ret = (synth->phase - M_PI) > 0.0?
(int16_t)roundf(synth->gain * (INT16_MAX / 4)):
(int16_t)roundf(synth->gain * (INT16_MIN / 4));
synth->phase += tau / (synth->format.sample_rate / synth->frequency);
if (synth->phase > tau) {
synth->phase -= tau;
}
} else {
ret = 0;
}
return ret;
}
static int16_t sample_triangle(xas_synth *synth) {
int16_t ret;
static float tau = 2.0f * M_PI;
if (synth->state == XAS_SYNTH_ACTIVE) {
float v = 2.0f * fabs(2.0f * fabs(synth->phase - floorf(synth->phase + 0.5))) - 1.0f;
ret = (int16_t)roundf(synth->gain * (INT16_MAX >> 2) * v);
synth->phase += tau / (synth->format.sample_rate / synth->frequency);
if (synth->phase > tau) {
synth->phase -= tau;
}
} else {
ret = 0;
}
return ret;
}
static int16_t sample_sawtooth(xas_synth *synth) {
int16_t ret;
static float tau = 2.0f * M_PI;
if (synth->state == XAS_SYNTH_ACTIVE) {
float v = 2.0f * (synth->phase / tau - 0.5f);
ret = (int16_t)roundf(synth->gain * (INT16_MAX >> 2) * v);
synth->phase += tau / (synth->format.sample_rate / synth->frequency);
if (synth->phase > tau) {
synth->phase -= tau;
}
} else {
ret = 0;
}
return ret;
}
static ssize_t synth_fill(xas_synth *synth,
int16_t *samples,
size_t count,
xas_audio_stream *stream) {
size_t i;
for (i=0; i<count; i++) {
samples[i] = synth->sample(synth);
}
return count;
}
static int synth_start(xas_synth *synth) {
xas_synth_start(synth);
return 0;
}
static int synth_stop(xas_synth *synth) {
xas_synth_stop(synth);
return 0;
}
static int set_gain(xas_synth *synth, float gain) {
xas_synth_set_gain(synth, gain);
return 0;
}
static int set_type(xas_synth *synth, enum xas_synth_type type) {
switch (type) {
case XAS_SYNTH_SINE: synth->sample = sample_sine; break;
case XAS_SYNTH_SQUARE: synth->sample = sample_square; break;
case XAS_SYNTH_TRIANGLE: synth->sample = sample_triangle; break;
case XAS_SYNTH_SAWTOOTH: synth->sample = sample_sawtooth; break;
default:
errno = EINVAL;
goto error_invalid_type;
}
synth->type = type;
return 0;
error_invalid_type:
return -1;
}
xas_synth *xas_synth_new(xas_audio_format format,
size_t buffer_size,
enum xas_synth_type type) {
xas_synth *synth;
if ((synth = malloc(sizeof(*synth))) == NULL) {
goto error_malloc_synth;
}
synth->obj.start = (xas_object_start_callback)synth_start;
synth->obj.stop = (xas_object_stop_callback)synth_stop;
synth->obj.set_gain = (xas_object_set_gain_callback)set_gain;
synth->obj.stream_new = (xas_object_stream_new_callback)xas_synth_stream_new;
synth->obj.destroy = (xas_object_destroy_callback)xas_synth_destroy;
synth->state = XAS_SYNTH_IDLE;
synth->phase = 0.0f;
synth->gain = XAS_SYNTH_DEFAULT_GAIN;
synth->frequency = XAS_SYNTH_DEFAULT_FREQUENCY;
synth->format.channels = XAS_AUDIO_MONO;
synth->format.sample_size = format.sample_size;
synth->format.sample_rate = format.sample_rate;
synth->buffer_size = buffer_size;
if (set_type(synth, type) < 0) {
goto error_set_type;
}
return synth;
error_set_type:
free(synth);
error_malloc_synth:
return NULL;
}
void xas_synth_destroy(xas_synth *synth) {
free(synth);
}
void xas_synth_set_gain(xas_synth *synth, float gain) {
synth->gain = gain;
}
void xas_synth_set_type(xas_synth *synth, enum xas_synth_type type) {
set_type(synth, type);
}
void xas_synth_set_frequency(xas_synth *synth, size_t frequency) {
synth->frequency = frequency;
}
void xas_synth_start(xas_synth *synth) {
synth->state = XAS_SYNTH_ACTIVE;
}
void xas_synth_stop(xas_synth *synth) {
synth->state = XAS_SYNTH_IDLE;
synth->phase = 0.0f;
}
xas_audio_stream *xas_synth_stream_new(xas_synth *synth) {
return xas_audio_stream_new_source((xas_audio_fill)synth_fill,
NULL,
synth->format,
synth->buffer_size,
synth);
}
|