summaryrefslogtreecommitdiff
path: root/plugins/dumb/dumb-kode54/src/sigtypes/combine.c
blob: 497f085cf23ce54b5756f4ec81255c9e2eaa248e (plain)
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*  _______         ____    __         ___    ___
 * \    _  \       \    /  \  /       \   \  /   /       '   '  '
 *  |  | \  \       |  |    ||         |   \/   |         .      .
 *  |  |  |  |      |  |    ||         ||\  /|  |
 *  |  |  |  |      |  |    ||         || \/ |  |         '  '  '
 *  |  |  |  |      |  |    ||         ||    |  |         .      .
 *  |  |_/  /        \  \__//          ||    |  |
 * /_______/ynamic    \____/niversal  /__\  /____\usic   /|  .  . ibliotheque
 *                                                      /  \
 *                                                     / .  \
 * combine.c - The combining (COMB) signal type.      / / \  \
 *                                                   | <  /   \_
 * By entheh.                                        |  \/ /\   /
 *                                                    \_  /  > /
 * This takes multiple monaural signals and             | \ / /
 * combines them into a single multichannel             |  ' /
 * signal. It assumes the correct number of              \__/
 * channels is passed. An ASSERT() is in place
 * to check the number of channels when you
 * compile with -DDEBUGMODE. As an exception, if one channel is passed the
 * signals are all mixed together.
 */

#include <stdlib.h>

#include "dumb.h"



#define SIGTYPE_COMBINING DUMB_ID('C','O','M','B')



typedef struct COMBINING_SIGNAL
{
	int n_signals;
	int sig[ZERO_SIZE];
}
COMBINING_SIGNAL;



typedef struct COMBINING_SAMPINFO
{
	int n_signals;
	int downmix;
	DUH_SIGNAL_SAMPINFO *csampinfo[ZERO_SIZE];
}
COMBINING_SAMPINFO;



static void *combining_load_signal(DUH *duh, DUMBFILE *file)
{
	COMBINING_SIGNAL *signal;
	int n_signals;

	(void)duh;

	n_signals = dumbfile_getc(file);

	/* No point in combining only one signal! */
	if (dumbfile_error(file) || n_signals <= 1)
		return NULL;

	signal = malloc(sizeof(*signal) + n_signals * sizeof(*signal->sig));
	if (!signal)
		return NULL;

	signal->n_signals = n_signals;

	{
		int n;
		for (n = 0; n < signal->n_signals; n++) {
			signal->sig[n] = dumbfile_igetl(file);
			if (dumbfile_error(file)) {
				free(signal);
				return NULL;
			}
		}
	}

	return signal;
}



static void *combining_start_samples(DUH *duh, void *signal, int n_channels, long pos)
{
#define signal ((COMBINING_SIGNAL *)signal)

	COMBINING_SAMPINFO *sampinfo;

	sampinfo = malloc(sizeof(*sampinfo) + signal->n_signals * sizeof(*sampinfo->csampinfo));
	if (!sampinfo)
		return NULL;

	sampinfo->n_signals = signal->n_signals;
	if (n_channels == 1)
		sampinfo->downmix = 1;
	else if (n_channels == signal->n_signals)
		sampinfo->downmix = 0;
	else {
		TRACE("Combining signal discrepancy: %d signals, %d channels.\n", signal->n_signals, n_channels);
		free(sampinfo);
		return NULL;
	}

	{
		int worthwhile = 0;

		{
			int n;
			for (n = 0; n < signal->n_signals; n++) {
				sampinfo->csampinfo[n] = duh_signal_start_samples(duh, signal->sig[n], 1, pos);
				if (sampinfo->csampinfo[n])
					worthwhile = 1;
			}
		}

		if (!worthwhile) {
			free(sampinfo);
			return NULL;
		}
	}

	return sampinfo;

#undef signal
}



static long combining_render_samples(
	void *sampinfo,
	float volume, float delta,
	long size, sample_t **samples
)
{
#define sampinfo ((COMBINING_SAMPINFO *)sampinfo)

	long max_size;

	int n;

	if (sampinfo->downmix)
		volume /= sampinfo->n_signals;

	max_size = duh_signal_render_samples(sampinfo->csampinfo[0], volume, delta, size, samples);

	if (sampinfo->downmix) {

		long s;
		long sz;

		sample_t *sampbuf = malloc(size * sizeof(sample_t));

		if (!sampbuf)
			return 0;

		for (n = 1; n < sampinfo->n_signals; n++) {
			sz = duh_signal_render_samples(sampinfo->csampinfo[n], volume, delta, size, &sampbuf);
			if (sz > max_size) {
				for (s = max_size; s < sz; s++)
					samples[0][s] = sampbuf[s];
				sz = max_size;
				max_size = s;
			}
			for (s = 0; s < sz; s++)
				samples[0][s] += sampbuf[s];
		}

		free(sampbuf);

	} else {

		long *sz = malloc(size * sizeof(*sz));
		long s;

		if (!sz)
			return 0;

		sz[0] = max_size;

		for (n = 1; n < sampinfo->n_signals; n++) {
			sz[n] = duh_signal_render_samples(sampinfo->csampinfo[n], volume, delta, size, samples + n);
			if (sz[n] > max_size)
				max_size = sz[n];
		}

		for (n = 0; n < sampinfo->n_signals; n++)
			for (s = sz[n]; s < max_size; s++)
				samples[n][s] = 0;

		free(sz);

	}

	return max_size;

#undef sampinfo
}



static void combining_end_samples(void *sampinfo)
{
#define sampinfo ((COMBINING_SAMPINFO *)sampinfo)

	int n;

	for (n = 0; n < sampinfo->n_signals; n++)
		duh_signal_end_samples(sampinfo->csampinfo[n]);

	free(sampinfo);

#undef sampinfo
}



static void combining_unload_signal(void *signal)
{
	free(signal);
}



static DUH_SIGTYPE_DESC sigtype_combining = {
	SIGTYPE_COMBINING,
	&combining_load_signal,
	&combining_start_samples,
	NULL,
	&combining_render_samples,
	&combining_end_samples,
	&combining_unload_signal
};


void dumb_register_sigtype_combining(void)
{
	dumb_register_sigtype(&sigtype_combining);
}