History

Modify sndfile-resample to restart conversion if clipping has occurred.

Erik de Castro Lopo committed 22 years ago

Commit e3755e21 parent 0bbb3382 files · +35 −4Browse files

Unified · Split

2 files
  1. ChangeLog +5 −0
  2. examples/sndfile-resample.c +30 −4
ChangeLog+5−0
@@ -1,3 +1,8 @@
1+2004-06-15 Erik de Castro Lopo <erikd AT mega-nerd DOT com>
2+
3+ * examples/sndfile-resample.c
4+ Modified to restart conversion if clipping has occurred.
5+
16 2004-06-09 Erik de Castro Lopo <erikd AT mega-nerd DOT com>
27
38 * tests/benchmark.c
examples/sndfile-resample.c+30−4
@@ -32,7 +32,8 @@ · 31 unmodified lines
3232 #define BUFFER_LEN 4096 /*-(1<<16)-*/
3333
3434 static void usage_exit (const char *progname) ;
35−static sf_count_t sample_rate_convert (SNDFILE *infile, SNDFILE *outfile, int converter, double src_ratio, int channels) ;
35+static sf_count_t sample_rate_convert (SNDFILE *infile, SNDFILE *outfile, int converter, double src_ratio, int channels, double * gain) ;
36+static double apply_gain (float * data, long frames, int channels, double max, double gain) ;
3637
3738 int
3839 main (int argc, char *argv [])
@@ -40,7 +41,7 @@ main (int argc, char *argv []) · 1 unmodified line
4041 SF_INFO sfinfo ;
4142
4243 sf_count_t count ;
43− double src_ratio = -1.0 ;
44+ double src_ratio = -1.0, gain = 1.0 ;
4445 int new_sample_rate = -1, k, converter, max_speed = SF_FALSE ;
4546
4647 if (argc == 2 && strcmp (argv [1], "--version") == 0)
@@ -150,7 +151,9 @@ main (int argc, char *argv []) · 103 unmodified lines
150151 printf ("Output file : %s\n", argv [argc - 1]) ;
151152 printf ("Sample Rate : %d\n", sfinfo.samplerate) ;
152153
153− count = sample_rate_convert (infile, outfile, converter, src_ratio, sfinfo.channels) ;
154+ do
155+ count = sample_rate_convert (infile, outfile, converter, src_ratio, sfinfo.channels, &gain) ;
156+ while (count < 0) ;
154157
155158 printf ("Output Frames : %ld\n\n", (long) count) ;
156159
@@ -164,13 +167,14 @@ main (int argc, char *argv []) · 7 unmodified lines
164167 */
165168
166169 static sf_count_t
167−sample_rate_convert (SNDFILE *infile, SNDFILE *outfile, int converter, double src_ratio, int channels)
170+sample_rate_convert (SNDFILE *infile, SNDFILE *outfile, int converter, double src_ratio, int channels, double * gain)
168171 { static float input [BUFFER_LEN] ;
169172 static float output [BUFFER_LEN] ;
170173
171174 SRC_STATE *src_state ;
172175 SRC_DATA src_data ;
173176 int error ;
177+ double max = 0.0 ;
174178 sf_count_t output_count = 0 ;
175179
176180 sf_seek (infile, 0, SEEK_SET) ;
@@ -214,6 +218,8 @@ sample_rate_convert (SNDFILE *infile, SNDFILE *outfile, int converter, double sr · 37 unmodified lines
214218 if (src_data.end_of_input && src_data.output_frames_gen == 0)
215219 break ;
216220
221+ max = apply_gain (src_data.data_out, src_data.output_frames_gen, channels, max, *gain) ;
222+
217223 /* Write output. */
218224 sf_writef_float (outfile, output, src_data.output_frames_gen) ;
219225 output_count += src_data.output_frames_gen ;
@@ -224,9 +230,29 @@ sample_rate_convert (SNDFILE *infile, SNDFILE *outfile, int converter, double sr · 4 unmodified lines
224230
225231 src_state = src_delete (src_state) ;
226232
233+ if (max > 1.0)
234+ { *gain = 1.0 / max ;
235+ printf ("\nOutput has clipped. Restarting conversion to prevent clipping.\n\n") ;
236+ return -1 ;
237+ } ;
238+
227239 return output_count ;
228240 } /* sample_rate_convert */
229241
242+static double
243+apply_gain (float * data, long frames, int channels, double max, double gain)
244+{
245+ long k ;
246+
247+ for (k = 0 ; k < frames * channels ; k++)
248+ { data [k] *= gain ;
249+
250+ if (fabs (data [k]) > max)
251+ max = fabs (data [k]) ;
252+ } ;
253+
254+ return max ;
255+} /* apply_gain */
230256
231257 static void
232258 usage_exit (const char *progname)

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t