| @@ -32,7 +32,8 @@ · 31 unmodified lines |
| 32 | 32 | #define BUFFER_LEN 4096 /*-(1<<16)-*/ |
| 33 | 33 | |
| 34 | 34 | 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) ; |
| 36 | 37 | |
| 37 | 38 | int |
| 38 | 39 | main (int argc, char *argv []) |
| @@ -40,7 +41,7 @@ main (int argc, char *argv []) · 1 unmodified line |
| 40 | 41 | SF_INFO sfinfo ; |
| 41 | 42 | |
| 42 | 43 | sf_count_t count ; |
| 43 | | − double src_ratio = -1.0 ; |
| 44 | + double src_ratio = -1.0, gain = 1.0 ; |
| 44 | 45 | int new_sample_rate = -1, k, converter, max_speed = SF_FALSE ; |
| 45 | 46 | |
| 46 | 47 | if (argc == 2 && strcmp (argv [1], "--version") == 0) |
| @@ -150,7 +151,9 @@ main (int argc, char *argv []) · 103 unmodified lines |
| 150 | 151 | printf ("Output file : %s\n", argv [argc - 1]) ; |
| 151 | 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 | 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 | |
| 166 | 169 | 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) |
| 168 | 171 | { static float input [BUFFER_LEN] ; |
| 169 | 172 | static float output [BUFFER_LEN] ; |
| 170 | 173 | |
| 171 | 174 | SRC_STATE *src_state ; |
| 172 | 175 | SRC_DATA src_data ; |
| 173 | 176 | int error ; |
| 177 | + double max = 0.0 ; |
| 174 | 178 | sf_count_t output_count = 0 ; |
| 175 | 179 | |
| 176 | 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 | 218 | if (src_data.end_of_input && src_data.output_frames_gen == 0) |
| 215 | 219 | break ; |
| 216 | 220 | |
| 221 | + max = apply_gain (src_data.data_out, src_data.output_frames_gen, channels, max, *gain) ; |
| 222 | + |
| 217 | 223 | /* Write output. */ |
| 218 | 224 | sf_writef_float (outfile, output, src_data.output_frames_gen) ; |
| 219 | 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 | 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 | 239 | return output_count ; |
| 228 | 240 | } /* sample_rate_convert */ |
| 229 | 241 | |
| 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 */ |
| 230 | 256 | |
| 231 | 257 | static void |
| 232 | 258 | usage_exit (const char *progname) |