Fix crash bug in sampler_api_example.py
[calfbox.git] / jackinput.c
blob7c1f2530e4dd87fa44c56a46205474e1ab1e8771
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 "app.h"
20 #include "config.h"
21 #include "config-api.h"
22 #include "dspmath.h"
23 #include "module.h"
24 #include <glib.h>
25 #include <malloc.h>
26 #include <math.h>
27 #include <memory.h>
28 #include <sndfile.h>
29 #include <stdio.h>
30 #include <stdlib.h>
32 struct jack_input_module
34 struct cbox_module module;
36 int inputs[2];
37 int offset;
40 void jack_input_process_event(struct cbox_module *module, const uint8_t *data, uint32_t len)
42 // struct jack_input_module *m = module->user_data;
45 void jack_input_process_block(struct cbox_module *module, cbox_sample_t **inputs, cbox_sample_t **outputs)
47 struct jack_input_module *m = module->user_data;
49 for (int i = 0; i < 2; i++)
51 float *src = module->rt->io->input_buffers[m->inputs[i]] + m->offset;
52 for (int j = 0; j < CBOX_BLOCK_SIZE; j++)
53 outputs[i][j] = src[j];
55 m->offset = (m->offset + CBOX_BLOCK_SIZE) % app.io.buffer_size;
58 static gboolean validate_input_index(int input, const char *cfg_section, const char *type, GError **error)
60 if (input < 1 || input > app.io.input_count)
62 g_set_error(error, CBOX_MODULE_ERROR, CBOX_MODULE_ERROR_OUT_OF_RANGE, "%s: invalid value for %s (%d), allowed values are 1..%d", cfg_section, type, input, app.io.input_count);
63 return FALSE;
65 return TRUE;
68 MODULE_CREATE_FUNCTION(jack_input)
70 static int inited = 0;
71 if (!inited)
73 inited = 1;
76 int left_input = cbox_config_get_int(cfg_section, "left_input", 1);
77 int right_input = cbox_config_get_int(cfg_section, "right_input", 2);
78 if (!validate_input_index(left_input, cfg_section, "left_input", error))
79 return NULL;
80 if (!validate_input_index(right_input, cfg_section, "right_input", error))
81 return NULL;
83 struct jack_input_module *m = malloc(sizeof(struct jack_input_module));
84 CALL_MODULE_INIT_SIMPLE(m, 0, 2);
85 m->module.process_event = jack_input_process_event;
86 m->module.process_block = jack_input_process_block;
88 m->inputs[0] = left_input - 1;
89 m->inputs[1] = right_input - 1;
90 m->offset = 0;
92 return &m->module;
96 struct cbox_module_keyrange_metadata jack_input_keyranges[] = {
99 struct cbox_module_livecontroller_metadata jack_input_controllers[] = {
102 DEFINE_MODULE(jack_input, 0, 2)