X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~mdw/git/disorder/blobdiff_plain/0e8c21de2a464fc913e0dbb6f7f148a84db95d55..6ebc4527c6a103d0532c08744fb916f951018413:/lib/resample.c diff --git a/lib/resample.c b/lib/resample.c index 9617db9..39514ed 100644 --- a/lib/resample.c +++ b/lib/resample.c @@ -58,7 +58,6 @@ void resample_init(struct resampler *rs, int output_bits, int output_channels, int output_rate, int output_signed, int output_endian) { - int error_; memset(rs, 0, sizeof *rs); assert(input_bits == 8 || input_bits == 16); assert(output_bits == 8 || output_bits == 16); @@ -80,11 +79,12 @@ void resample_init(struct resampler *rs, rs->input_bytes_per_frame = rs->input_channels * rs->input_bytes_per_sample; if(rs->input_rate != rs->output_rate) { #if HAVE_SAMPLERATE_H + int error_; rs->state = src_new(SRC_SINC_BEST_QUALITY, rs->output_channels, &error_); if(!rs->state) - fatal(0, "calling src_new: %s", src_strerror(error_)); + disorder_fatal(0, "calling src_new: %s", src_strerror(error_)); #else - fatal(0, "need to resample audio data but libsamplerate not available"); + disorder_fatal(0, "need to resample audio data but libsamplerate not available"); #endif } } @@ -96,6 +96,8 @@ void resample_close(struct resampler *rs) { #if HAVE_SAMPLERATE_H if(rs->state) src_delete(rs->state); +#else + rs = 0; /* quieten compiler */ #endif } @@ -105,17 +107,17 @@ void resample_close(struct resampler *rs) { * @param where Where to store result * @return Number of bytes consumed */ -static size_t resample_get_sample(struct resampler *rs, +static size_t resample_get_sample(const struct resampler *rs, const uint8_t *bytes, float *where) { switch(rs->input_bits + rs->input_signed + rs->input_endian) { case 8+ENDIAN_BIG: case 8+ENDIAN_LITTLE: - *where = (bytes[0] - 128)/ 128; + *where = (bytes[0] - 128)/ 128.0; return 1; case 8+SIGNED+ENDIAN_BIG: case 8+SIGNED+ENDIAN_LITTLE: - *where = (int8_t)bytes[0] / 128; + *where = (int8_t)bytes[0] / 128.0; return 1; case 16+ENDIAN_BIG: *where = (bytes[0] * 256 + bytes[1] - 32768)/ 32768.0; @@ -156,11 +158,11 @@ static inline int clip(int n, int min, int max) { * * The value is clipped naively if it will not fit. */ -static size_t resample_put_sample(struct resampler *rs, +static size_t resample_put_sample(const struct resampler *rs, float sample, uint8_t *bytes) { unsigned value; - switch(rs->input_bits + rs->input_signed + rs->input_endian) { + switch(rs->output_bits + rs->output_signed + rs->output_endian) { case 8+ENDIAN_BIG: case 8+ENDIAN_LITTLE: *bytes = clip(sample * 128.0 + 128, 0, 255); @@ -210,12 +212,10 @@ static size_t resample_put_sample(struct resampler *rs, * the input either mono or stereo, so the result isn't actually going to be * too bad. */ -static void resample_prepare_input(struct resampler *rs, +static void resample_prepare_input(const struct resampler *rs, const uint8_t *bytes, - size_t nbytes, + size_t nframes, float *floats) { - size_t nframes = nbytes / (rs->input_bytes_per_frame); - while(nframes > 0) { int n; @@ -233,6 +233,7 @@ static void resample_prepare_input(struct resampler *rs, ++floats; } } + --nframes; } } @@ -242,24 +243,28 @@ static void resample_prepare_input(struct resampler *rs, * @param nbytes Number of bytes to convert * @param eof Set an end of input stream * @param converted Called with converted data (possibly more than once) + * @param cd Passed to @p cd * @return Number of bytes consumed */ -size_t resample_convert(struct resampler *rs, +size_t resample_convert(const struct resampler *rs, const uint8_t *bytes, size_t nbytes, int eof, void (*converted)(uint8_t *bytes, - size_t nbytes)) { + size_t nbytes, + void *cd), + void *cd) { size_t nframesin = nbytes / (rs->input_bytes_per_frame); size_t nsamplesout; float *input = xcalloc(nframesin * rs->output_channels, sizeof (float)); float *output = 0; - resample_prepare_input(rs, bytes, nbytes, input); + resample_prepare_input(rs, bytes, nframesin, input); #if HAVE_SAMPLERATE_H if(rs->state) { /* A sample-rate conversion must be performed */ SRC_DATA data; + memset(&data, 0, sizeof data); /* Compute how many frames are expected to come out. */ size_t maxframesout = nframesin * rs->output_rate / rs->input_rate + 1; output = xcalloc(maxframesout * rs->output_channels, sizeof(float)); @@ -268,12 +273,17 @@ size_t resample_convert(struct resampler *rs, data.input_frames = nframesin; data.output_frames = maxframesout; data.end_of_input = eof; - data.src_ratio = rs->output_rate / rs->input_rate; + data.src_ratio = (double)rs->output_rate / rs->input_rate; + D(("nframesin=%zu maxframesout=%zu eof=%d ratio=%d.%06d", + nframesin, maxframesout, eof, + (int)data.src_ratio, + ((int)(data.src_ratio * 1000000) % 1000000))); int error_ = src_process(rs->state, &data); if(error_) - fatal(0, "calling src_process: %s", src_strerror(error_)); + disorder_fatal(0, "calling src_process: %s", src_strerror(error_)); nframesin = data.input_frames_used; nsamplesout = data.output_frames_gen * rs->output_channels; + D(("new nframesin=%zu nsamplesout=%zu", nframesin, nsamplesout)); } #endif if(!output) { @@ -290,12 +300,14 @@ size_t resample_convert(struct resampler *rs, bufused += resample_put_sample(rs, *op++, buffer + bufused); --nsamplesout; } - converted(buffer, bufused); + converted(buffer, bufused, cd); } if(output != input) xfree(output); xfree(input); + eof = 0; /* quieten compiler */ /* Report how many input bytes were actually consumed */ + //fprintf(stderr, "converted %zu frames\n", nframesin); return nframesin * rs->input_bytes_per_frame; }