Merge pull request #1 from atsampson/master
[calfbox.git] / jackinput.c
blob7b0469a79a9411ef3fa746465c95fbf473e37c86
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 #if USE_JACK
34 struct jack_input_module
36 struct cbox_module module;
38 int inputs[2];
39 int offset;
42 void jack_input_process_event(struct cbox_module *module, const uint8_t *data, uint32_t len)
44 // struct jack_input_module *m = module->user_data;
47 void jack_input_process_block(struct cbox_module *module, cbox_sample_t **inputs, cbox_sample_t **outputs)
49 struct jack_input_module *m = module->user_data;
51 for (int i = 0; i < 2; i++)
53 float *src = module->rt->io->input_buffers[m->inputs[i]] + m->offset;
54 for (int j = 0; j < CBOX_BLOCK_SIZE; j++)
55 outputs[i][j] = src[j];
57 m->offset = (m->offset + CBOX_BLOCK_SIZE) % app.io.io_env.buffer_size;
60 static gboolean validate_input_index(int input, const char *cfg_section, const char *type, GError **error)
62 if (input < 1 || input > app.io.io_env.input_count)
64 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.io_env.input_count);
65 return FALSE;
67 return TRUE;
70 MODULE_CREATE_FUNCTION(jack_input)
72 static int inited = 0;
73 if (!inited)
75 inited = 1;
78 int left_input = cbox_config_get_int(cfg_section, "left_input", 1);
79 int right_input = cbox_config_get_int(cfg_section, "right_input", 2);
80 if (!validate_input_index(left_input, cfg_section, "left_input", error))
81 return NULL;
82 if (!validate_input_index(right_input, cfg_section, "right_input", error))
83 return NULL;
85 struct jack_input_module *m = malloc(sizeof(struct jack_input_module));
86 CALL_MODULE_INIT_SIMPLE(m, 0, 2);
87 m->module.process_event = jack_input_process_event;
88 m->module.process_block = jack_input_process_block;
90 m->inputs[0] = left_input - 1;
91 m->inputs[1] = right_input - 1;
92 m->offset = 0;
94 return &m->module;
98 struct cbox_module_keyrange_metadata jack_input_keyranges[] = {
101 struct cbox_module_livecontroller_metadata jack_input_controllers[] = {
104 DEFINE_MODULE(jack_input, 0, 2)
106 #endif