File: avg.c

package info (click to toggle)
sox 12.16-6
  • links: PTS
  • area: main
  • in suites: potato
  • size: 1,180 kB
  • ctags: 1,466
  • sloc: ansic: 16,658; sh: 2,071; makefile: 126
file content (280 lines) | stat: -rw-r--r-- 5,918 bytes parent folder | download
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280

/*
 * July 5, 1991
 * Copyright 1991 Lance Norskog And Sundry Contributors
 * This source code is freely redistributable and may be used for
 * any purpose.  This copyright notice must be maintained. 
 * Lance Norskog And Sundry Contributors are not responsible for 
 * the consequences of using this software.
 *
 * Channel duplication code by Graeme W. Gill - 93/5/18
 */

/*
 * Sound Tools stereo/quad -> mono mixdown effect file.
 * and mono/stereo -> stereo/quad channel duplication.
 *
 * What's in a center channel?
 */

#include "st.h"

/* Private data for SKEL file */
typedef struct avgstuff {
	int	mix;			/* How are we mixing it? */
} *avg_t;

#define MIX_CENTER	0
#define MIX_LEFT	1
#define MIX_RIGHT	2

/*
 * Process options
 */
void avg_getopts(effp, n, argv) 
eff_t effp;
int n;
char **argv;
{
	avg_t avg = (avg_t) effp->priv;

	/* NOTE :- should probably have a pan option for both */
	/* 2 -> 1 and 1 -> 2 etc. conversion, rather than just */
	/* left and right. If 4 channels is to be fully supported, */
	/* front and back pan is also needed. (GWG) */
	/* (should  at least have MIX_FRONT and MIX_BACK) */
	avg->mix = MIX_CENTER;
	if (n) {
		if(!strcmp(argv[0], "-l"))
			avg->mix = MIX_LEFT;
		else if (!strcmp(argv[0], "-r"))
			avg->mix = MIX_RIGHT;
		else
			fail("Usage: avg [ -l | -r ]");
	}
}

/*
 * Start processing
 */
void
avg_start(effp)
eff_t effp;
{
	switch (effp->outinfo.channels) 
	{
	case 1: switch (effp->ininfo.channels) 
		{
		case 2: 
		case 4:
			return;
		default:
			break;
		}
		break;
	case 2: switch (effp->ininfo.channels) 
		{
		case 1:
		case 4:
			return;
		default:
			break;
		}
		break;
	case 4: switch (effp->ininfo.channels) 
		{
		case 1:
		case 2:
			return;
		default:
			break;
		}
	default:
		break;
	}	
	fail("Can't average %d channels into %d channels",
		effp->ininfo.channels, effp->outinfo.channels);
}

/*
 * Process either isamp or osamp samples, whichever is smaller.
 */

void avg_flow(effp, ibuf, obuf, isamp, osamp)
eff_t effp;
LONG *ibuf, *obuf;
int *isamp, *osamp;
{
	avg_t avg = (avg_t) effp->priv;
	int len, done;
	
	switch (effp->outinfo.channels) {
		case 1: switch (effp->ininfo.channels) {
			case 2:
				/* average 2 channels into 1 */
				len = ((*isamp/2 > *osamp) ? *osamp : *isamp/2);
				switch(avg->mix) {
				    case MIX_CENTER:
					for(done = 0; done < len; done++) {
						*obuf++ = ibuf[0]/2 + ibuf[1]/2;
						ibuf += 2;
					}
					break;
				    case MIX_LEFT:
					for(done = 0; done < len; done++) {
						*obuf++ = ibuf[0];
						ibuf += 2;
					}
					break;
				    case MIX_RIGHT:
					for(done = 0; done < len; done++) {
						*obuf++ = ibuf[1];
						ibuf += 2;
					}
					break;
				}
				*isamp = len * 2;
				*osamp = len;
				break;
			case 4:
				/* average 4 channels into 1 */
				len = ((*isamp/4 > *osamp) ? *osamp : *isamp/4);
				switch(avg->mix) {
				    case MIX_CENTER:
					for(done = 0; done < len; done++) {
						*obuf++ = ibuf[0]/4 + ibuf[1]/4 +
							ibuf[2]/4 + ibuf[3]/4;
						ibuf += 4;
					}
					break;
				    case MIX_LEFT:
					for(done = 0; done < len; done++) {
						*obuf++ = ibuf[0]/2 + ibuf[2]/2;
						ibuf += 4;
					}
					break;
				    case MIX_RIGHT:
					for(done = 0; done < len; done++) {
						*obuf++ = ibuf[1]/2 + ibuf[3]/2;
						ibuf += 4;
					}
					break;
				}
				*isamp = len * 4;
				*osamp = len;
				break;
			}
			break;
		case 2: switch (effp->ininfo.channels) {
			case 1:
				/* duplicate 1 channel into 2 */
				len = ((*isamp > *osamp/2) ? *osamp/2 : *isamp);
				switch(avg->mix) {
				    case MIX_CENTER:
					for(done = 0; done < len; done++) {
						obuf[0] = obuf[1] = ibuf[0];
						ibuf += 1;
						obuf += 2;
					}
					break;
				    case MIX_LEFT:
					for(done = 0; done < len; done++) {
						obuf[0] = ibuf[0];
						obuf[1] = 0;
						ibuf += 1;
						obuf += 2;
					}
					break;
				    case MIX_RIGHT:
					for(done = 0; done < len; done++) {
						obuf[0] = 0;
						obuf[1] = ibuf[0];
						ibuf += 1;
						obuf += 2;
					}
					break;
				}
				*isamp = len;
				*osamp = len * 2;
				break;
			/*
			 * After careful inspection of CSOUND source code,
			 * I'm mildly sure the order is:
			 * 	front-left, front-right, rear-left, rear-right
			 */
			case 4:
				/* average 4 channels into 2 */
				len = ((*isamp/4 > *osamp/2) ? *osamp/2 : *isamp/4);
				for(done = 0; done < len; done++) {
					obuf[0] = ibuf[0]/2 + ibuf[2]/2;
					obuf[1] = ibuf[1]/2 + ibuf[3]/2;
					ibuf += 4;
					obuf += 2;
				}
				*isamp = len * 4;
				*osamp = len * 2;
				break;
			}
			break;
		case 4: switch (effp->ininfo.channels) {
			case 1:
				/* duplicate 1 channel into 4 */
				len = ((*isamp > *osamp/4) ? *osamp/4 : *isamp);
				switch(avg->mix) {
				    case MIX_CENTER:
					for(done = 0; done < len; done++) {
						obuf[0] = obuf[1] = 
						obuf[2] = obuf[3] = ibuf[0];
						ibuf += 1;
						obuf += 4;
					}
					break;
				    case MIX_LEFT:
					for(done = 0; done < len; done++) {
						obuf[0] = obuf[2] = ibuf[0];
						obuf[1] = obuf[3] = 0;
						ibuf += 1;
						obuf += 4;
					}
					break;
				    case MIX_RIGHT:
					for(done = 0; done < len; done++) {
						obuf[0] = obuf[2] = 0;
						obuf[1] = obuf[3] = ibuf[0];
						ibuf += 1;
						obuf += 4;
					}
					break;
				}
				*isamp = len;
				*osamp = len * 4;
				break;
			case 2:
				/* duplicate 2 channels into 4 */
				len = ((*isamp/2 > *osamp/4) ? *osamp/4 : *isamp/2);
				for(done = 0; done < len; done++) {
					obuf[0] = obuf[2] = ibuf[0];
					obuf[1] = obuf[3] = ibuf[1];
					ibuf += 2;
					obuf += 4;
				}
				*isamp = len * 2;
				*osamp = len * 4;
				break;
			}
			break;
	}	/* end switch out channels */
}

/*
 * Do anything required when you stop reading samples.  
 * Don't close input file! 
 *
 * Should have statistics on right, left, and output amplitudes.
 */
void avg_stop(effp)
eff_t effp;
{
	/* nothing to do */
}