Welcome, guest | Sign In | My Account | Store | Cart

Wav audio file dynamic range compressor

C, 355 lines
  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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
#include <stdio.h>
/*
gcc -o autolvl4.out autolvl4.c;./autolvl4.out
*/

// Auto-adjust the gain to keep a fairly constant volume.

#define g_numsamples 11000 // Min 12000 ok 
#define g_max_clip_level 32767
#define d_max_ampl 5000.0 // 4000 -> -17dB, 5000 -> -15dB, 5500 -> -14dB

char g_input[] = "seesound.wav";
char g_output[100] = {0};
double gain_ceiling=5.0;

short signed int scale_data(short signed int data, double gain)
{
	short signed int n_result=0;
	double d_result=(double)data * gain;
	if (d_result < -32768.0) 
	{
		d_result = -32768.0;
	}
	if (d_result > 32767.0)
	{
		d_result = 32767.0;
	}
	n_result = (short signed int)d_result;
	return n_result;
}

double abs_scale_data(short signed int data, double gain)
{
	double d_result=(double)data * gain;
	if (d_result<0.0) d_result=-d_result;
	return d_result;
}


int main(void)
{
	FILE *fin;
	long i=0;
	long j=0;

	double sumL=0.0;
	double sumR=0.0;
	double count=0.0;

	g_output[0] = '0';
	g_output[1] = '_';
	for (i=0; i<99; i++)
	{
		g_output[i+2]=g_input[i];
	}

	double locgainl=0.0;
	double locgainr=0.0;
	long loccount=0;

	short signed int ndata[g_numsamples]={0};
	short signed int cheader[88]={0};
	char b_make_output=0;
	long num_blocks=0;

	long num_gains=0;
	double l_gains[40000]={0.0};
	double r_gains[40000]={0.0};

	double l_avg_gains[40000]={0.0};
	double r_avg_gains[40000]={0.0};

	fin=fopen(g_input,"rb");
	if (!fin) 
	{
		printf("Error opening input file\n");
		return 0;
	}
	else
	{
		printf("Opening %s\n", g_input);
	}
	fseek(fin,88,SEEK_SET);

/*_____________________________


		MAIN LOOP

_____________________________*/

	double dgains[2]={0.0};
	long int gainptr=0;
	while(1)
	{
		if (feof(fin)) break;
		num_blocks++;
		fread(ndata,sizeof(ndata),1,fin);
		i=0;
		long int j=0;
		locgainl=0.0;
		locgainr=0.0;
		loccount=0;
		for (i=0;i<g_numsamples;i++)
			if (ndata[i]<0) ndata[i]=-ndata[i];
		for (i=0;i<g_numsamples;i+=2)
		{
			sumL+=(double)ndata[i];
			sumR+=(double)ndata[i+1];
			locgainl+=(double)ndata[i];
			locgainr+=(double)ndata[i+1];
			count+=1.0;
			loccount++;
		}
		// calculate the average
		locgainl/=(double)loccount;
		locgainr/=(double)loccount;

		// scale the average to meet the gain.
		locgainl=d_max_ampl/locgainl;
		locgainr=d_max_ampl/locgainr;
		dgains[0]=locgainl;
		dgains[1]=locgainr;

		if (locgainl > gain_ceiling) locgainl = gain_ceiling;
		if (locgainr > gain_ceiling) locgainr = gain_ceiling;

		l_gains[gainptr]=locgainl;
		r_gains[gainptr]=locgainr;
		gainptr++;
	}
	num_gains=gainptr;

	sumL/=count;
	sumR/=count;

/*__________________________________________


		Make sure that no interval clips or
		exceeds the gain specification.

____________________________________________*/

	fseek(fin,0,SEEK_SET);
	gainptr=0;
	double locdatal=0.0;
	double locdatar=0.0;
	double maxlocdata=0.0;
	while(1)
	{
		if (feof(fin)) break;
		fread(ndata,sizeof(ndata),1,fin);
		i=0;
		long int j=0;
		maxlocdata=0;
		for (i=0;i<g_numsamples;i++)
			if (ndata[i]<0) ndata[i]=-ndata[i];
		for (i=0;i<g_numsamples;i+=2)
		{
			locdatal=abs_scale_data(ndata[i],   l_gains[gainptr]);
			locdatar=abs_scale_data(ndata[i+1], r_gains[gainptr]);
			if (locdatal > maxlocdata) maxlocdata = locdatal;
			if (locdatar > maxlocdata) maxlocdata = locdatar;
		}

		if (maxlocdata >= g_max_clip_level)
		{
			double factor=g_max_clip_level / maxlocdata;
			l_gains[gainptr] *= factor;
			r_gains[gainptr] *= factor;
		}
		gainptr++;
	}

	// l_avg_gains
	long llim=0;
	long ulim=0;

	for (i=0; i<num_gains; i++)
	{
		double dlavg=0.0;
		double dravg=0.0;
		long j=0;
		double dcount=0.0;
		llim=i-10;
		ulim=i+10;
		dlavg=0.0;
		dravg=0.0;
		if (llim < 0)
		{
			llim=0;
			ulim-=llim;
		}
		for (j=llim; j<ulim; j++)
		{
			dlavg+=l_gains[j];
			dravg+=r_gains[j];
			dcount+=1.0;
		}
		dlavg/=dcount;
		dravg/=dcount;
		l_avg_gains[i]=dlavg;
		r_avg_gains[i]=dravg;
	}


	for (i=0; i<num_gains; i++)
	{
		double dfactor=0.0;
		double dnewgain=0.0;

		if (l_gains[i] > l_avg_gains[i])
		{
			dfactor=l_gains[i]/l_avg_gains[i];
			dnewgain=(l_gains[i] * dfactor) + l_avg_gains[i];
			dnewgain /= (dfactor + 1);
			l_gains[i] = dnewgain;
		}

		if (r_gains[i] > r_avg_gains[i])
		{
			dfactor=r_gains[i]/r_avg_gains[i];
			dnewgain=(r_gains[i] * dfactor) + r_avg_gains[i];
			dnewgain /= (dfactor + 1);
			r_gains[i] = dnewgain;
		}
	}

	// Make it so that the gains can decrease rapidly, but not increase as fast.
	// If a future gain is a lot bigger than a previous gain, then scale it back.

	for (i=1; i<num_gains; i++)
	{
		if (l_gains[i]>l_gains[i-1])
		{
			l_gains[i]=(l_gains[i]+l_gains[i-1]*3.0)/4.0;
		}
		if (r_gains[i]>r_gains[i-1])
		{
			r_gains[i]=(r_gains[i]+r_gains[i-1]*3.0)/4.0;
		}
	}

	long block=0;
	double d_ldata=0.0;
	double d_rdata=0.0;
	double lgain=0.0;
	double rgain=0.0;

/*_____________________________________


		Do the dynamic compression here

_______________________________________*/

	i=0;
	printf("Auto-scale volume\n");
	lgain=d_max_ampl/sumL;
	rgain=d_max_ampl/sumR;

	double lgaininc=0.0;
	double rgaininc=0.0;
	double dlastgains[2] = {0.0};

	double lptgain=0.0;
	double rptgain=0.0;

	double dpercent_in_file=0.0;

	FILE *fout;
	fout=fopen(g_output,"wb");
	if (fout)
	{
		fseek(fin,0,SEEK_SET);
		fread(cheader,sizeof(cheader),1,fin);
		fwrite(cheader,sizeof(cheader),1,fout);

		gainptr=0;
		dgains[0]=l_gains[gainptr];
		dgains[1]=r_gains[gainptr++];

		for (block=0; block<num_blocks; block++)
		{
			fread(ndata,sizeof(ndata),1,fin);

			// leave the dynamics at the end alone.
			// could make this a percentage too.
			// when we are 97% of the way through the song,
			// just let it ride out and don't adjust the gain
			// from what it was.

			dpercent_in_file = (double)block / (double)num_blocks;

			if (dpercent_in_file < 0.97)
			{
				dlastgains[0]=dgains[0];
				dlastgains[1]=dgains[1];

				lptgain=dlastgains[0];
				rptgain=dlastgains[1];

				if (lptgain > gain_ceiling) lptgain = gain_ceiling;
				if (rptgain > gain_ceiling) rptgain = gain_ceiling;

				dgains[0]=l_gains[gainptr];
				dgains[1]=r_gains[gainptr++];

				lgaininc=(dgains[0]-dlastgains[0]) / (double)g_numsamples;
				rgaininc=(dgains[1]-dlastgains[1]) / (double)g_numsamples;
			}
			else 
			{
				lgaininc=0;
				rgaininc=0;
			}

			long i=0;
			for (i=0;i<g_numsamples;i+=2)
			{
				ndata[i]   = scale_data(ndata[i]  , lptgain);
				ndata[i+1] = scale_data(ndata[i+1], rptgain);

				lptgain += lgaininc;
				rptgain += rgaininc;
			}

			fwrite(ndata,sizeof(ndata),1,fout);
		}

		while(1)
		{
			if (feof(fin)) break;
			fread(ndata,sizeof(short signed int),1,fin);
			short int i=0;
			ndata[i]=scale_data(ndata[i], lgain);
			ndata[i+1]=scale_data(ndata[i+1], rgain);
			fwrite(ndata,sizeof(short signed int),1,fout);
		}

		fclose(fout);
	}
	else
	{
		printf("Error opening output file\n");
	}

	printf("lgain = %.2f\n", lgain);
	printf("rgain = %.2f\n", rgain);
	
	fclose(fin);

	return 0;
}

Wav audio file dynamic range compressor Much better. Prevents clipping of the waveform and gain adjustment keeps the output level very smooth. Deleted unused functions.

Created by Granning Stoline on Mon, 8 Apr 2013 (MIT)
C recipes (32)
Granning Stoline's recipes (3)

Required Modules

  • (none specified)

Other Information and Tasks