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 @@
12004-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
12004-06-09 Erik de Castro Lopo <erikd AT mega-nerd DOT com>62004-06-09 Erik de Castro Lopo <erikd AT mega-nerd DOT com>
2 7
3 * tests/benchmark.c8 * tests/benchmark.c
examples/sndfile-resample.c+30−4
@@ -32,7 +32,8 @@ · 31 unmodified lines
32#define BUFFER_LEN 4096 /*-(1<<16)-*/32#define BUFFER_LEN 4096 /*-(1<<16)-*/
33 33
34static void usage_exit (const char *progname) ;34static void usage_exit (const char *progname) ;
35static sf_count_t sample_rate_convert (SNDFILE *infile, SNDFILE *outfile, int converter, double src_ratio, int channels) ;35static sf_count_t sample_rate_convert (SNDFILE *infile, SNDFILE *outfile, int converter, double src_ratio, int channels, double * gain) ;
36static double apply_gain (float * data, long frames, int channels, double max, double gain) ;
36 37
37int38int
38main (int argc, char *argv [])39main (int argc, char *argv [])
@@ -40,7 +41,7 @@ main (int argc, char *argv []) · 1 unmodified line
40 SF_INFO sfinfo ;41 SF_INFO sfinfo ;
41 42
42 sf_count_t count ;43 sf_count_t count ;
43 double src_ratio = -1.0 ;44 double src_ratio = -1.0, gain = 1.0 ;
44 int new_sample_rate = -1, k, converter, max_speed = SF_FALSE ;45 int new_sample_rate = -1, k, converter, max_speed = SF_FALSE ;
45 46
46 if (argc == 2 && strcmp (argv [1], "--version") == 0)47 if (argc == 2 && strcmp (argv [1], "--version") == 0)
@@ -150,7 +151,9 @@ main (int argc, char *argv []) · 103 unmodified lines
150 printf ("Output file : %s\n", argv [argc - 1]) ;151 printf ("Output file : %s\n", argv [argc - 1]) ;
151 printf ("Sample Rate : %d\n", sfinfo.samplerate) ;152 printf ("Sample Rate : %d\n", sfinfo.samplerate) ;
152 153
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) ;
154 157
155 printf ("Output Frames : %ld\n\n", (long) count) ;158 printf ("Output Frames : %ld\n\n", (long) count) ;
156 159
@@ -164,13 +167,14 @@ main (int argc, char *argv []) · 7 unmodified lines
164*/167*/
165 168
166static sf_count_t169static sf_count_t
167sample_rate_convert (SNDFILE *infile, SNDFILE *outfile, int converter, double src_ratio, int channels)170sample_rate_convert (SNDFILE *infile, SNDFILE *outfile, int converter, double src_ratio, int channels, double * gain)
168{ static float input [BUFFER_LEN] ;171{ static float input [BUFFER_LEN] ;
169 static float output [BUFFER_LEN] ;172 static float output [BUFFER_LEN] ;
170 173
171 SRC_STATE *src_state ;174 SRC_STATE *src_state ;
172 SRC_DATA src_data ;175 SRC_DATA src_data ;
173 int error ;176 int error ;
177 double max = 0.0 ;
174 sf_count_t output_count = 0 ;178 sf_count_t output_count = 0 ;
175 179
176 sf_seek (infile, 0, SEEK_SET) ;180 sf_seek (infile, 0, SEEK_SET) ;
@@ -214,6 +218,8 @@ sample_rate_convert (SNDFILE *infile, SNDFILE *outfile, int converter, double sr · 37 unmodified lines
214 if (src_data.end_of_input && src_data.output_frames_gen == 0)218 if (src_data.end_of_input && src_data.output_frames_gen == 0)
215 break ;219 break ;
216 220
221 max = apply_gain (src_data.data_out, src_data.output_frames_gen, channels, max, *gain) ;
222
217 /* Write output. */223 /* Write output. */
218 sf_writef_float (outfile, output, src_data.output_frames_gen) ;224 sf_writef_float (outfile, output, src_data.output_frames_gen) ;
219 output_count += src_data.output_frames_gen ;225 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
224 230
225 src_state = src_delete (src_state) ;231 src_state = src_delete (src_state) ;
226 232
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
227 return output_count ;239 return output_count ;
228} /* sample_rate_convert */240} /* sample_rate_convert */
229 241
242static double
243apply_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 */
230 256
231static void257static void
232usage_exit (const char *progname)258usage_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