-
Notifications
You must be signed in to change notification settings - Fork 0
/
sgen.c
283 lines (258 loc) · 7.03 KB
/
sgen.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
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
/*
* This file is part of dsp.
*
* Copyright (c) 2018-2024 Michael Barbour <[email protected]>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "sgen.h"
#include "util.h"
enum sgen_type {
SGEN_TYPE_DELTA = 1,
SGEN_TYPE_SINE,
};
struct sgen_generator {
int type;
char *channel_selector;
ssize_t pos, offset;
double freq0, freq1, v;
};
struct sgen_state {
ssize_t w;
int n;
struct sgen_generator *g;
};
void sgen_run_generator(struct sgen_generator *g, struct codec *c, sample_t *buf, ssize_t frames)
{
sample_t s;
double t;
ssize_t i, k, samples;
switch (g->type) {
case SGEN_TYPE_DELTA:
if (g->offset >= 0) {
if (g->offset < frames)
for (i = 0; i < c->channels; ++i)
if (GET_BIT(g->channel_selector, i))
buf[g->offset * c->channels + i] += 1.0;
g->offset -= frames;
}
break;
case SGEN_TYPE_SINE:
samples = frames * c->channels;
for (i = 0; i < samples; i += c->channels) {
t = (double) g->pos / c->fs;
if (g->v != 0.0)
s = sin(g->freq0 / g->v * (exp(t * g->v) - 1.0));
else
s = sin(g->freq0 * t);
for (k = 0; k < c->channels; ++k)
if (GET_BIT(g->channel_selector, k))
buf[i + k] += s;
++g->pos;
}
break;
}
}
ssize_t sgen_read(struct codec *c, sample_t *buf, ssize_t frames)
{
int i;
struct sgen_state *state = (struct sgen_state *) c->data;
if (c->frames > 0 && state->w + frames > c->frames)
frames = c->frames - state->w;
if (frames > 0) {
memset(buf, 0, frames * c->channels * sizeof(sample_t));
for (i = 0; i < state->n; ++i)
sgen_run_generator(&state->g[i], c, buf, frames);
state->w += frames;
}
return frames;
}
ssize_t sgen_write(struct codec *c, sample_t *buf, ssize_t frames)
{
return 0;
}
ssize_t sgen_seek(struct codec *c, ssize_t pos)
{
return -1;
}
ssize_t sgen_delay(struct codec *c)
{
return 0;
}
void sgen_drop(struct codec *c)
{
/* do nothing */
}
void sgen_pause(struct codec *c, int p)
{
/* do nothing */
}
void sgen_destroy(struct codec *c)
{
int i;
struct sgen_state *state = (struct sgen_state *) c->data;
for (i = 0; i < state->n; ++i)
free(state->g[i].channel_selector);
free(state->g);
free(state);
}
static void sgen_init_generator(struct sgen_generator *g, struct codec *c)
{
switch (g->type) {
case SGEN_TYPE_SINE:
g->freq0 = g->freq1 = 440.0;
break;
}
}
#define SGEN_PARSE_FREQ_PARAM(dest, fs, type, key, value, endptr) do { \
dest = parse_freq(value, &(endptr)); \
if (check_endptr(type, value, endptr, key)) \
return -1; \
if ((dest) <= 0.0 || (dest) >= (double) (fs) / 2.0) { \
LOG_FMT(LL_ERROR, "%s: error: %s out of range", type, key); \
return -1; \
} \
} while(0)
static int sgen_parse_param(struct sgen_generator *g, struct codec *c, const char *type, const char *key, char *value)
{
char *endptr, *value1;
switch (g->type) {
case SGEN_TYPE_DELTA:
if (strcmp(key, "offset") == 0) {
g->offset = parse_len(value, c->fs, &endptr);
if (check_endptr(type, value, endptr, key))
return -1;
if (g->offset < 0 || (c->frames > 0 && g->offset >= c->frames)) {
LOG_FMT(LL_ERROR, "%s: error: %s out of range", type, key);
return -1;
}
}
else
return 1;
break;
case SGEN_TYPE_SINE:
if (strcmp(key, "freq") == 0) {
value1 = isolate(value, '-');
SGEN_PARSE_FREQ_PARAM(g->freq0, c->fs, type, key, value, endptr);
g->freq1 = g->freq0;
if (*value1 != '\0')
SGEN_PARSE_FREQ_PARAM(g->freq1, c->fs, type, key, value1, endptr);
}
else
return 1;
break;
}
return 0;
}
static void sgen_prepare_generator(struct sgen_generator *g, struct codec *c)
{
switch (g->type) {
case SGEN_TYPE_SINE:
g->freq0 *= 2.0 * M_PI;
g->freq1 *= 2.0 * M_PI;
g->v = (c->frames > 0 && g->freq0 != g->freq1) ? log(g->freq1 / g->freq0) / ((double) c->frames / c->fs) : 0.0;
break;
}
}
struct codec * sgen_codec_init(const char *path, const char *type, const char *enc, int fs, int channels, int endian, int mode)
{
char *args = NULL, *arg, *gen_type, *len_str, *next_arg, *next_type, *value, *endptr;
int parse_ret;
struct codec *c;
struct sgen_state *state;
struct sgen_generator *g;
c = calloc(1, sizeof(struct codec));
c->path = path;
c->type = type;
c->enc = "sample_t";
c->fs = fs;
c->channels = channels;
c->prec = 53;
c->frames = -1;
c->read = sgen_read;
c->write = sgen_write;
c->seek = sgen_seek;
c->delay = sgen_delay;
c->drop = sgen_drop;
c->pause = sgen_pause;
c->destroy = sgen_destroy;
state = calloc(1, sizeof(struct sgen_state));
state->n = 0;
c->data = state;
args = arg = strdup(path);
len_str = isolate(arg, '+');
if (*len_str != '\0') {
c->frames = parse_len(len_str, fs, &endptr);
if (check_endptr(type, len_str, endptr, "length"))
goto fail;
if (c->frames <= 0) {
LOG_FMT(LL_ERROR, "%s: error: length cannot be <= 0", type);
goto fail;
}
/* LOG_FMT(LL_VERBOSE, "%s: info: length=%zd", type, c->frames); */
}
while (*arg != '\0') {
next_type = isolate(arg, '/');
next_arg = isolate(arg, ':');
value = isolate(arg, '@');
/* LOG_FMT(LL_VERBOSE, "%s: info: type=%s channel_selector=%s", type, arg, value); */
state->g = realloc(state->g, (state->n + 1) * sizeof(struct sgen_generator));
g = &state->g[state->n];
memset(g, 0, sizeof(struct sgen_generator));
g->channel_selector = NEW_SELECTOR(channels);
SET_SELECTOR(g->channel_selector, channels);
++state->n;
gen_type = arg;
if (strcmp(gen_type, "delta") == 0) g->type = SGEN_TYPE_DELTA;
else if (strcmp(gen_type, "sine") == 0) g->type = SGEN_TYPE_SINE;
else {
LOG_FMT(LL_ERROR, "%s: error: illegal type: %s", type, gen_type);
goto fail;
}
sgen_init_generator(g, c);
if (*value != '\0' && parse_selector(value, g->channel_selector, channels))
goto fail;
arg = next_arg;
while (*arg != '\0') {
next_arg = isolate(arg, ':');
value = isolate(arg, '=');
/* LOG_FMT(LL_VERBOSE, "%s: %s: arg: key=%s value=%s", type, gen_type, arg, value); */
parse_ret = sgen_parse_param(g, c, type, arg, value);
if (parse_ret == 1) {
LOG_FMT(LL_ERROR, "%s: %s: error: illegal parameter: %s", type, gen_type, arg);
goto fail;
}
else if (parse_ret == -1)
goto fail;
arg = next_arg;
}
sgen_prepare_generator(g, c);
arg = next_type;
}
done:
free(args);
return c;
fail:
sgen_destroy(c);
free(c);
c = NULL;
goto done;
}
void sgen_codec_print_encodings(const char *type)
{
fprintf(stdout, " sample_t");
}