Merge pull request #1 from atsampson/master
[calfbox.git] / wavebank.c
blob6ca52d3f59a62a8f7a307b8434bee184acc65674
1 /*
2 Calf Box, an open source musical instrument.
3 Copyright (C) 2010-2011 Krzysztof Foltman
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #include "cmd.h"
20 #include "config.h"
21 #include "config-api.h"
22 #include "dspmath.h"
23 #include "errors.h"
24 #include "tarfile.h"
25 #include "wavebank.h"
26 #include <assert.h>
27 #include <errno.h>
28 #include <glib.h>
29 #include <math.h>
30 #include <stdlib.h>
31 #include <string.h>
33 #define STD_WAVEFORM_FRAMES 1024
34 #define STD_WAVEFORM_BITS 10
36 ///////////////////////////////////////////////////////////////////////////////
38 // Sine table
39 static complex float euler_table[STD_WAVEFORM_FRAMES];
41 // Bit reversal table
42 static int map_table[STD_WAVEFORM_FRAMES];
44 // Initialise tables using for FFT
45 static void init_tables(void)
47 int rev = 1 << (STD_WAVEFORM_BITS - 1);
48 for (int i = 0; i < STD_WAVEFORM_FRAMES; i++)
50 euler_table[i] = cos(i * 2 * M_PI / STD_WAVEFORM_FRAMES) + I * sin(i * 2 * M_PI / STD_WAVEFORM_FRAMES);
51 int ni = 0;
52 for (int j = 0; j < STD_WAVEFORM_BITS; j++)
54 if (i & (rev >> j))
55 ni = ni | (1 << j);
57 map_table[i] = ni;
61 // Trivial implementation of Cooley-Tukey, only works for even values of ANALYSIS_BUFFER_BITS
62 static void my_fft_main(complex float output[STD_WAVEFORM_FRAMES])
64 complex float temp[STD_WAVEFORM_FRAMES];
66 for (int i = 0; i < STD_WAVEFORM_BITS; i++)
68 complex float *src = (i & 1) ? temp : output;
69 complex float *dst = (i & 1) ? output : temp;
70 int invi = STD_WAVEFORM_BITS - i - 1;
71 int disp = 1 << i;
72 int mask = disp - 1;
74 for (int j = 0; j < STD_WAVEFORM_FRAMES / 2; j++)
76 int jj1 = (j & mask) + ((j & ~mask) << 1); // insert 0 at ith bit to get the left arm of the butterfly
77 int jj2 = jj1 + disp; // insert 1 at ith bit to get the right arm
78 assert((jj1 + disp) == (jj1 | disp));
80 // e^iw
81 complex float eiw1 = euler_table[(jj1 << invi) & (STD_WAVEFORM_FRAMES - 1)];
82 complex float eiw2 = euler_table[(jj2 << invi) & (STD_WAVEFORM_FRAMES - 1)];
84 // printf("%d -> %d, %d\n", j, jj, jj + disp);
85 butterfly(&dst[jj1], &dst[jj2], src[jj1], src[jj2], eiw1, eiw2);
90 static void my_fft_r2c(complex float output[STD_WAVEFORM_FRAMES], int16_t input[STD_WAVEFORM_FRAMES])
92 assert(!(STD_WAVEFORM_BITS&1));
93 // Copy + bit reversal addressing
94 for (int i = 0; i < STD_WAVEFORM_FRAMES; i++)
95 output[i] = input[map_table[i]] * (1.0 / STD_WAVEFORM_FRAMES);
97 my_fft_main(output);
101 static void my_ifft_c2r(int16_t output[STD_WAVEFORM_FRAMES], complex float input[STD_WAVEFORM_FRAMES])
103 complex float temp2[STD_WAVEFORM_FRAMES];
104 for (int i = 0; i < STD_WAVEFORM_FRAMES; i++)
105 temp2[i] = input[map_table[i]];
106 assert(!(STD_WAVEFORM_BITS&1));
108 my_fft_main(temp2);
109 // Copy + bit reversal addressing
110 float maxv = 0;
111 for (int i = 0; i < STD_WAVEFORM_FRAMES; i++)
113 float value = creal(temp2[i]);
114 if (value < -32768) value = -32768;
115 if (value > 32767) value = 32767;
116 if (fabs(value) > maxv)
117 maxv = fabs(value);
118 output[i] = (int16_t)value;
122 struct wave_bank
124 int64_t bytes, maxbytes, serial_no;
125 GHashTable *waveforms_by_name, *waveforms_by_id;
126 GSList *std_waveforms;
127 uint32_t streaming_prefetch_size;
130 static struct wave_bank bank;
132 GQuark cbox_waveform_error_quark(void)
134 return g_quark_from_string("cbox-waveform-error-quark");
137 float func_sine(float v, void *user_data)
139 return sin(2 * M_PI * v);
142 float func_sqr(float v, void *user_data)
144 return v < 0.5 ? -1 : 1;
147 float func_saw(float v, void *user_data)
149 return 2 * v - 1;
152 float func_tri(float v, void *user_data)
154 if (v <= 0.25f)
155 return v * 4;
156 if (v <= 0.75f)
157 return 1 - (v - 0.25f) * 4;
158 return -1 + 4 * (v - 0.75f);
161 void cbox_waveform_generate_levels(struct cbox_waveform *waveform, int levels, double ratio)
163 complex float output[STD_WAVEFORM_FRAMES], bandlimited[STD_WAVEFORM_FRAMES];
164 my_fft_r2c(output, waveform->data);
165 int N = STD_WAVEFORM_FRAMES;
167 waveform->levels = calloc(levels, sizeof(struct cbox_waveform_level));
168 double rate = 65536.0 * 65536.0; // / waveform->info.frames;
169 double orig_rate = 65536.0 * 65536.0; // / waveform->info.frames;
170 for (int i = 0; i < levels; i++)
172 int harmonics = N / 2 / (rate / orig_rate);
173 bandlimited[0] = 0;
175 if (harmonics > 0)
177 for (int j = 1; j <= harmonics; j++)
179 bandlimited[j] = output[j];
180 bandlimited[N - j] = output[N - j];
182 for (int j = harmonics; j <= N / 2; j++)
183 bandlimited[j] = bandlimited [N - j] = 0;
186 waveform->levels[i].data = calloc(N + MAX_INTERPOLATION_ORDER, sizeof(int16_t));
187 my_ifft_c2r(waveform->levels[i].data, bandlimited);
188 memcpy(waveform->levels[i].data + N, waveform->levels[i].data, MAX_INTERPOLATION_ORDER * sizeof(int16_t));
189 waveform->levels[i].max_rate = (uint64_t)(rate);
190 rate *= ratio;
192 waveform->level_count = levels;
195 void cbox_wavebank_add_std_waveform(const char *name, float (*getfunc)(float v, void *user_data), void *user_data, int levels)
197 int nsize = STD_WAVEFORM_FRAMES;
198 int16_t *wave = calloc(nsize, sizeof(int16_t));
199 for (int i = 0; i < nsize; i++)
201 float v = getfunc(i * 1.0 / nsize, user_data);
202 if (fabs(v) > 1)
203 v = (v < 0) ? -1 : 1;
204 // cannot use full scale here, because bandlimiting will introduce
205 // some degree of overshoot
206 wave[i] = (int16_t)(25000 * v);
208 struct cbox_waveform *waveform = calloc(1, sizeof(struct cbox_waveform));
209 waveform->data = wave;
210 waveform->info.channels = 1;
211 waveform->preloaded_frames = waveform->info.frames = nsize;
212 waveform->info.samplerate = (int)(nsize * 261.6255);
213 waveform->id = ++bank.serial_no;
214 waveform->bytes = waveform->info.channels * 2 * (waveform->info.frames + 1);
215 waveform->refcount = 1;
216 waveform->canonical_name = g_strdup(name);
217 waveform->display_name = g_strdup(name);
218 waveform->has_loop = TRUE;
219 waveform->loop_start = 0;
220 waveform->loop_end = nsize;
221 waveform->levels = NULL;
222 waveform->level_count = 0;
224 if (levels)
225 cbox_waveform_generate_levels(waveform, levels, 2);
227 g_hash_table_insert(bank.waveforms_by_name, waveform->canonical_name, waveform);
228 g_hash_table_insert(bank.waveforms_by_id, &waveform->id, waveform);
229 bank.std_waveforms = g_slist_prepend(bank.std_waveforms, waveform);
230 // These waveforms are not included in the bank size, I don't think it has
231 // much value for the user.
234 void cbox_wavebank_init()
236 init_tables();
238 bank.bytes = 0;
239 bank.maxbytes = 0;
240 bank.serial_no = 0;
241 bank.waveforms_by_name = g_hash_table_new(g_str_hash, g_str_equal);
242 bank.waveforms_by_id = g_hash_table_new(g_int_hash, g_int_equal);
243 bank.std_waveforms = NULL;
244 bank.streaming_prefetch_size = cbox_config_get_int("streaming", "prefetch_size", 65536);
246 cbox_wavebank_add_std_waveform("*sine", func_sine, NULL, 0);
247 cbox_wavebank_add_std_waveform("*saw", func_saw, NULL, 11);
248 cbox_wavebank_add_std_waveform("*sqr", func_sqr, NULL, 11);
249 cbox_wavebank_add_std_waveform("*tri", func_tri, NULL, 11);
252 struct cbox_waveform *cbox_wavebank_get_waveform(const char *context_name, struct cbox_tarfile *tarfile, const char *sample_dir, const char *filename, GError **error)
254 if (!filename)
256 g_set_error(error, CBOX_WAVEFORM_ERROR, CBOX_WAVEFORM_ERROR_FAILED, "%s: no filename specified", context_name);
257 return NULL;
260 // Built in waveforms don't go through path canonicalization
261 if (filename[0] == '*')
263 gpointer value = g_hash_table_lookup(bank.waveforms_by_name, filename);
264 if (value)
266 struct cbox_waveform *waveform = value;
267 cbox_waveform_ref(waveform);
268 return waveform;
272 gchar *value_copy = g_strdup(filename);
273 for (int i = 0; value_copy[i]; i++)
275 if (value_copy[i] == '\\')
276 value_copy[i] = '/';
278 gchar *pathname = value_copy[0] == '/' ? g_strdup(value_copy) : g_build_filename(sample_dir, value_copy, NULL);
279 g_free(value_copy);
281 char *canonical = NULL;
282 if (tarfile)
283 canonical = g_strdup_printf("sbtar:%s;%s", tarfile->file_pathname, pathname);
284 else
286 // make sure canonical is always allocated on the same (glib) heap
287 char *p = realpath(pathname, NULL);
288 if (p)
290 canonical = g_strdup(p);
291 free(p);
294 if (!canonical)
296 g_set_error(error, CBOX_WAVEFORM_ERROR, CBOX_WAVEFORM_ERROR_FAILED, "%s: cannot find a real path for '%s': %s", context_name, pathname, strerror(errno));
297 g_free(pathname);
298 return NULL;
300 gpointer value = g_hash_table_lookup(bank.waveforms_by_name, canonical);
301 if (value)
303 g_free(pathname);
304 g_free(canonical);
306 struct cbox_waveform *waveform = value;
307 cbox_waveform_ref(waveform);
308 return waveform;
311 struct cbox_waveform *waveform = calloc(1, sizeof(struct cbox_waveform));
312 SNDFILE *sndfile = NULL;
313 struct cbox_taritem *taritem = NULL;
314 if (tarfile)
316 taritem = cbox_tarfile_get_item_by_name(tarfile, pathname, TRUE);
317 if (taritem)
318 sndfile = cbox_tarfile_opensndfile(tarfile, taritem, &waveform->sndstream, &waveform->info);
320 else
321 sndfile = sf_open(pathname, SFM_READ, &waveform->info);
322 if (!sndfile)
324 g_set_error(error, G_FILE_ERROR, g_file_error_from_errno (errno), "%s: cannot open '%s'", context_name, pathname);
325 g_free(pathname);
326 g_free(canonical);
327 free(waveform);
328 return NULL;
331 SF_INSTRUMENT instrument;
332 int nshorts;
333 if (waveform->info.channels != 1 && waveform->info.channels != 2)
335 g_set_error(error, CBOX_WAVEFORM_ERROR, CBOX_WAVEFORM_ERROR_FAILED,
336 "%s: cannot open file '%s': unsupported channel count %d", context_name, pathname, (int)waveform->info.channels);
337 sf_close(sndfile);
338 free(canonical);
339 g_free(pathname);
340 return NULL;
342 g_free(pathname);
343 uint32_t preloaded_frames = waveform->info.frames;
344 // If sample is larger than 2x prefetch buffer size, then load only
345 // a prefetch buffer worth of data, and stream the rest.
346 if (preloaded_frames > 2 * bank.streaming_prefetch_size)
347 preloaded_frames = bank.streaming_prefetch_size;
348 waveform->id = ++bank.serial_no;
349 waveform->bytes = waveform->info.channels * 2 * preloaded_frames;
350 waveform->data = malloc(waveform->bytes);
351 waveform->refcount = 1;
352 waveform->canonical_name = canonical;
353 waveform->display_name = g_filename_display_name(canonical);
354 waveform->has_loop = FALSE;
355 waveform->levels = NULL;
356 waveform->level_count = 0;
357 waveform->preloaded_frames = preloaded_frames;
358 waveform->tarfile = tarfile;
359 waveform->taritem = taritem;
361 if (sf_command(sndfile, SFC_GET_INSTRUMENT, &instrument, sizeof(SF_INSTRUMENT)))
363 for (int i = 0; i < instrument.loop_count; i++)
365 if (instrument.loops[i].mode == SF_LOOP_FORWARD)
367 waveform->loop_start = instrument.loops[i].start;
368 waveform->loop_end = instrument.loops[i].end;
369 waveform->has_loop = TRUE;
370 break;
375 nshorts = waveform->info.channels * preloaded_frames;
376 for (uint32_t i = 0; i < nshorts; i++)
377 waveform->data[i] = 0;
378 sf_readf_short(sndfile, waveform->data, preloaded_frames);
379 sf_close(sndfile);
380 bank.bytes += waveform->bytes;
381 if (bank.bytes > bank.maxbytes)
382 bank.maxbytes = bank.bytes;
383 g_hash_table_insert(bank.waveforms_by_name, waveform->canonical_name, waveform);
384 g_hash_table_insert(bank.waveforms_by_id, &waveform->id, waveform);
386 return waveform;
389 int64_t cbox_wavebank_get_bytes()
391 return bank.bytes;
394 int64_t cbox_wavebank_get_maxbytes()
396 return bank.maxbytes;
399 int cbox_wavebank_get_count()
401 return g_hash_table_size(bank.waveforms_by_id);
404 struct cbox_waveform *cbox_wavebank_peek_waveform_by_id(int id)
406 return g_hash_table_lookup(bank.waveforms_by_id, &id);
409 void cbox_wavebank_foreach(void (*cb)(void *, struct cbox_waveform *), void *user_data)
411 GHashTableIter iter;
412 gpointer key, value;
414 g_hash_table_iter_init (&iter, bank.waveforms_by_id);
415 while (g_hash_table_iter_next (&iter, &key, &value))
417 (*cb)(user_data, value);
421 void cbox_wavebank_close()
423 if (bank.bytes > 0)
424 g_warning("Warning: %lld bytes in unfreed samples", (long long int)bank.bytes);
425 while(bank.std_waveforms)
427 cbox_waveform_unref((struct cbox_waveform *)bank.std_waveforms->data);
428 bank.std_waveforms = g_slist_delete_link(bank.std_waveforms, bank.std_waveforms);
430 g_hash_table_destroy(bank.waveforms_by_id);
431 g_hash_table_destroy(bank.waveforms_by_name);
432 bank.waveforms_by_id = NULL;
433 bank.waveforms_by_name = NULL;
436 /////////////////////////////////////////////////////////////////////////////////////////////////////
438 void cbox_waveform_ref(struct cbox_waveform *waveform)
440 ++waveform->refcount;
443 void cbox_waveform_unref(struct cbox_waveform *waveform)
445 if (--waveform->refcount > 0)
446 return;
448 g_hash_table_remove(bank.waveforms_by_name, waveform->canonical_name);
449 g_hash_table_remove(bank.waveforms_by_id, &waveform->id);
450 bank.bytes -= waveform->bytes;
452 g_free(waveform->display_name);
453 g_free(waveform->canonical_name);
454 for (int i = 0; i < waveform->level_count; i++)
455 free(waveform->levels[i].data);
456 free(waveform->levels);
457 free(waveform->data);
458 free(waveform);
462 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
464 struct waves_foreach_data
466 struct cbox_command_target *fb;
467 GError **error;
468 gboolean success;
471 void wave_list_cb(void *user_data, struct cbox_waveform *waveform)
473 struct waves_foreach_data *wfd = user_data;
475 wfd->success = wfd->success && cbox_execute_on(wfd->fb, NULL, "/waveform", "i", wfd->error, (int)waveform->id);
478 static gboolean waves_process_cmd(struct cbox_command_target *ct, struct cbox_command_target *fb, struct cbox_osc_command *cmd, GError **error)
480 if (!strcmp(cmd->command, "/status") && !strcmp(cmd->arg_types, ""))
482 if (!cbox_check_fb_channel(fb, cmd->command, error))
483 return FALSE;
485 // XXXKF this only supports 4GB - not a big deal for now yet?
486 return cbox_execute_on(fb, NULL, "/bytes", "i", error, (int)cbox_wavebank_get_bytes()) &&
487 cbox_execute_on(fb, NULL, "/max_bytes", "i", error, (int)cbox_wavebank_get_maxbytes()) &&
488 cbox_execute_on(fb, NULL, "/count", "i", error, (int)cbox_wavebank_get_count())
491 else if (!strcmp(cmd->command, "/list") && !strcmp(cmd->arg_types, ""))
493 if (!cbox_check_fb_channel(fb, cmd->command, error))
494 return FALSE;
496 struct waves_foreach_data wfd = { fb, error, TRUE };
497 cbox_wavebank_foreach(wave_list_cb, &wfd);
498 return wfd.success;
500 else if (!strcmp(cmd->command, "/info") && !strcmp(cmd->arg_types, "i"))
502 if (!cbox_check_fb_channel(fb, cmd->command, error))
503 return FALSE;
505 int id = CBOX_ARG_I(cmd, 0);
506 struct cbox_waveform *waveform = cbox_wavebank_peek_waveform_by_id(id);
507 if (waveform == NULL)
509 g_set_error(error, CBOX_MODULE_ERROR, CBOX_MODULE_ERROR_FAILED, "Waveform %d not found", id);
510 return FALSE;
512 assert(id == waveform->id);
513 if (!cbox_execute_on(fb, NULL, "/filename", "s", error, waveform->canonical_name)) // XXXKF convert to utf8
514 return FALSE;
515 if (!cbox_execute_on(fb, NULL, "/name", "s", error, waveform->display_name))
516 return FALSE;
517 if (!cbox_execute_on(fb, NULL, "/bytes", "i", error, (int)waveform->bytes))
518 return FALSE;
519 if (waveform->has_loop && !cbox_execute_on(fb, NULL, "/loop", "ii", error, (int)waveform->loop_start, (int)waveform->loop_end))
520 return FALSE;
521 return TRUE;
523 else
525 g_set_error(error, CBOX_MODULE_ERROR, CBOX_MODULE_ERROR_FAILED, "Unknown combination of target path and argument: '%s', '%s'", cmd->command, cmd->arg_types);
526 return FALSE;
530 struct cbox_command_target cbox_waves_cmd_target =
532 .process_cmd = waves_process_cmd,
533 .user_data = NULL