|
| 1 | +/** libsox wrapper |
| 2 | +2026, Simon Zolin */ |
| 3 | + |
| 4 | +#include "sox-phi.h" |
| 5 | +#include <stdlib.h> |
| 6 | +#include <string.h> |
| 7 | +#include <sox.h> |
| 8 | + |
| 9 | +struct sox_ctx { |
| 10 | + sox_effects_chain_t *chain; |
| 11 | + sox_signalinfo_t signal; |
| 12 | + |
| 13 | + size_t input_len4; |
| 14 | + const sox_sample_t *input; |
| 15 | + |
| 16 | + size_t output_len4; |
| 17 | + const sox_sample_t *output; |
| 18 | +}; |
| 19 | + |
| 20 | +void phi_sox_destroy(sox_ctx *c) |
| 21 | +{ |
| 22 | + if (!c) return; |
| 23 | + |
| 24 | + if (c->chain) |
| 25 | + sox_delete_effects_chain(c->chain); |
| 26 | + free(c); |
| 27 | +} |
| 28 | + |
| 29 | +static int input_drain(sox_effect_t *e, sox_sample_t *obuf, size_t *osamp) |
| 30 | +{ |
| 31 | + struct sox_ctx *c = *(void**)e->priv; |
| 32 | + if (*osamp > c->input_len4) |
| 33 | + *osamp = c->input_len4; |
| 34 | + memcpy(obuf, c->input, *osamp * 4); |
| 35 | + c->input_len4 -= *osamp; |
| 36 | + c->input += *osamp; |
| 37 | + return SOX_EOF; |
| 38 | +} |
| 39 | + |
| 40 | +static const sox_effect_handler_t input_handler = { |
| 41 | + "input", NULL, SOX_EFF_MCHAN, NULL, NULL, NULL, input_drain, NULL, NULL, |
| 42 | + .priv_size = sizeof(void*) |
| 43 | +}; |
| 44 | + |
| 45 | +static int output_flow(sox_effect_t *e, const sox_sample_t *ibuf, sox_sample_t *obuf, size_t *isamp, size_t *osamp) |
| 46 | +{ |
| 47 | + struct sox_ctx *c = *(void**)e->priv; |
| 48 | + c->output = ibuf; |
| 49 | + c->output_len4 = *isamp; |
| 50 | + *osamp = 0; |
| 51 | + return (c->output_len4) ? SOX_EOF : 0; |
| 52 | +} |
| 53 | + |
| 54 | +static const sox_effect_handler_t output_handler = { |
| 55 | + "output", NULL, SOX_EFF_MCHAN, NULL, NULL, output_flow, NULL, NULL, NULL, |
| 56 | + .priv_size = sizeof(void*) |
| 57 | +}; |
| 58 | + |
| 59 | +int phi_sox_create(sox_ctx **pc, struct sox_conf *conf) |
| 60 | +{ |
| 61 | + sox_ctx *c = calloc(1, sizeof(struct sox_ctx)); |
| 62 | + |
| 63 | + sox_signalinfo_t signal = { |
| 64 | + .rate = conf->rate, |
| 65 | + .channels = conf->channels, |
| 66 | + }; |
| 67 | + c->signal = signal; |
| 68 | + |
| 69 | + sox_encodinginfo_t encoding = {}; |
| 70 | + c->chain = sox_create_effects_chain(&encoding, &encoding); |
| 71 | + |
| 72 | + sox_effect_t *e = sox_create_effect(&input_handler); |
| 73 | + *(void**)e->priv = c; |
| 74 | + if (sox_add_effect(c->chain, e, &signal, &signal)) |
| 75 | + goto err; |
| 76 | + free(e); e = NULL; |
| 77 | + |
| 78 | + *pc = c; |
| 79 | + return 0; |
| 80 | + |
| 81 | +err: |
| 82 | + free(e); |
| 83 | + phi_sox_destroy(c); |
| 84 | + return 1; |
| 85 | +} |
| 86 | + |
| 87 | +int phi_sox_filter(sox_ctx *c, const char *name, const char* argv[], unsigned argc) |
| 88 | +{ |
| 89 | + sox_effect_t *e = NULL; |
| 90 | + |
| 91 | + if (!name) { |
| 92 | + e = sox_create_effect(&output_handler); |
| 93 | + *(void**)e->priv = c; |
| 94 | + goto done; |
| 95 | + } |
| 96 | + |
| 97 | + const sox_effect_handler_t *eh; |
| 98 | + if (!(eh = sox_find_effect(name))) |
| 99 | + goto err; |
| 100 | + e = sox_create_effect(eh); |
| 101 | + |
| 102 | + if (argc |
| 103 | + && sox_effect_options(e, argc, (char**)argv)) |
| 104 | + goto err; |
| 105 | + |
| 106 | +done: |
| 107 | + if (sox_add_effect(c->chain, e, &c->signal, &c->signal)) |
| 108 | + goto err; |
| 109 | + free(e); e = NULL; |
| 110 | + return 0; |
| 111 | + |
| 112 | +err: |
| 113 | + free(e); |
| 114 | + return 1; |
| 115 | +} |
| 116 | + |
| 117 | +int phi_sox_process(sox_ctx *c, const int *input, size_t *len, int **output) |
| 118 | +{ |
| 119 | + c->input = input; |
| 120 | + c->input_len4 = *len / 4; |
| 121 | + c->output_len4 = 0; |
| 122 | + sox_flow_effects(c->chain, NULL, NULL); |
| 123 | + *len -= c->input_len4 * 4; |
| 124 | + *output = (int*)c->output; |
| 125 | + return c->output_len4 * 4; |
| 126 | +} |
| 127 | + |
| 128 | + |
| 129 | +static void output_message(unsigned level, const char *filename, const char *fmt, va_list ap) {} |
| 130 | + |
| 131 | +static sox_globals_t s_sox_globals = { |
| 132 | + 2, /* unsigned verbosity */ |
| 133 | + output_message, /* sox_output_message_handler */ |
| 134 | + sox_false, /* sox_bool repeatable */ |
| 135 | + 8192, /* size_t bufsiz */ |
| 136 | + 0, /* size_t input_bufsiz */ |
| 137 | + 0, /* int32_t ranqd1 */ |
| 138 | + NULL, /* char const * stdin_in_use_by */ |
| 139 | + NULL, /* char const * stdout_in_use_by */ |
| 140 | + NULL, /* char const * subsystem */ |
| 141 | + NULL, /* char * tmp_path */ |
| 142 | + sox_false, /* sox_bool use_magic */ |
| 143 | + sox_false, /* sox_bool use_threads */ |
| 144 | + 10 /* size_t log2_dft_min_size */ |
| 145 | +}; |
| 146 | + |
| 147 | +sox_globals_t* sox_get_globals() { return &s_sox_globals; } |
| 148 | + |
| 149 | +static sox_effects_globals_t s_sox_effects_globals = {sox_plot_off, &s_sox_globals}; |
| 150 | + |
| 151 | +sox_effects_globals_t* sox_get_effects_globals() |
| 152 | +{ |
| 153 | + return &s_sox_effects_globals; |
| 154 | +} |
| 155 | + |
| 156 | +#define SOX_MESSAGE_FUNCTION(name,level) \ |
| 157 | +void name(char const * fmt, ...) { \ |
| 158 | + va_list ap; \ |
| 159 | + va_start(ap, fmt); \ |
| 160 | + if (sox_globals.output_message_handler) \ |
| 161 | + (*sox_globals.output_message_handler)(level,sox_globals.subsystem,fmt,ap); \ |
| 162 | + va_end(ap); \ |
| 163 | +} |
| 164 | + |
| 165 | +SOX_MESSAGE_FUNCTION(lsx_fail_impl , 1) |
| 166 | +SOX_MESSAGE_FUNCTION(lsx_warn_impl , 2) |
| 167 | +SOX_MESSAGE_FUNCTION(lsx_report_impl, 3) |
| 168 | +SOX_MESSAGE_FUNCTION(lsx_debug_impl , 4) |
| 169 | +SOX_MESSAGE_FUNCTION(lsx_debug_more_impl , 5) |
| 170 | +SOX_MESSAGE_FUNCTION(lsx_debug_most_impl , 6) |
| 171 | + |
| 172 | +void init_fft_cache() {} |
| 173 | +void clear_fft_cache() {} |
0 commit comments