-
Notifications
You must be signed in to change notification settings - Fork 31
/
remix.c
103 lines (93 loc) · 3.24 KB
/
remix.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
/*
* This file is part of dsp.
*
* Copyright (c) 2014-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 "remix.h"
#include "util.h"
struct remix_state {
char **channel_selectors;
};
sample_t * remix_effect_run(struct effect *e, ssize_t *frames, sample_t *ibuf, sample_t *obuf)
{
struct remix_state *state = (struct remix_state *) e->data;
for (ssize_t i = 0; i < *frames; ++i) {
for (int k = 0; k < e->ostream.channels; ++k) {
obuf[i * e->ostream.channels + k] = 0;
for (int j = 0; j < e->istream.channels; ++j) {
if (GET_BIT(state->channel_selectors[k], j))
obuf[i * e->ostream.channels + k] += ibuf[i * e->istream.channels + j];
}
}
}
return obuf;
}
void remix_effect_plot(struct effect *e, int i)
{
struct remix_state *state = (struct remix_state *) e->data;
for (int k = 0; k < e->ostream.channels; ++k) {
printf("H%d_%d(w)=0.0", k, i);
for (int j = 0; j < e->istream.channels; ++j) {
if (GET_BIT(state->channel_selectors[k], j))
printf("+Ht%d_%d(w*%d/2.0/pi)", j, i, e->ostream.fs);
}
putchar('\n');
}
}
void remix_effect_destroy(struct effect *e)
{
struct remix_state *state = (struct remix_state *) e->data;
for (int k = 0; k < e->ostream.channels; ++k)
free(state->channel_selectors[k]);
free(state->channel_selectors);
free(state);
}
struct effect * remix_effect_init(const struct effect_info *ei, const struct stream_info *istream, const char *channel_selector, const char *dir, int argc, const char *const *argv)
{
struct effect *e;
struct remix_state *state;
if (argc <= 1) {
LOG_FMT(LL_ERROR, "%s: usage: %s", argv[0], ei->usage);
return NULL;
}
const int out_channels = argc - 1;
state = calloc(1, sizeof(struct remix_state));
state->channel_selectors = calloc(out_channels, sizeof(char *));
for (int k = 0; k < out_channels; ++k) {
state->channel_selectors[k] = NEW_SELECTOR(istream->channels);
if (strcmp(argv[k + 1], ".") != 0 && parse_selector(argv[k + 1], state->channel_selectors[k], istream->channels))
goto fail;
}
e = calloc(1, sizeof(struct effect));
e->name = ei->name;
e->istream.fs = e->ostream.fs = istream->fs;
e->istream.channels = istream->channels;
e->ostream.channels = out_channels;
e->plot_info |= PLOT_INFO_MIX;
e->run = remix_effect_run;
e->plot = remix_effect_plot;
e->destroy = remix_effect_destroy;
e->data = state;
return e;
fail:
if (state->channel_selectors)
for (int k = 0; k < out_channels; ++k)
free(state->channel_selectors[k]);
free(state->channel_selectors);
free(state);
return NULL;
}