chiark / gitweb /
plugins/tracklength-gstreamer.c: Rewrite to use `GstDiscoverer'.
[disorder] / lib / resample.c
index de2358cdbfbdf7339f7b464a1efb4cdfa1434c26..0363d992b887892a4d6277f1ff88714f22d225dd 100644 (file)
  * @param input_channels Number of input channels
  * @param input_signed Whether input samples are signed or unsigned
  * @param input_rate Frames/second in input
+ * @param input_endian Input endianness (@c ENDIAN_BIG or @c ENDIAN_LITTLE)
  * @param output_bits Bits/sample in output
  * @param output_channels Number of output channels
  * @param output_rate Frames/second in output
  * @param output_signed Whether output samples are signed or unsigned
+ * @param output_endian Output endianness (@c ENDIAN_BIG or @c ENDIAN_LITTLE)
  *
  * For formats with more than two channels it's assume that the first
  * two channels are left and right.  No particular meaning is attached
@@ -58,7 +60,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 +81,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 +98,8 @@ void resample_close(struct resampler *rs) {
 #if HAVE_SAMPLERATE_H
   if(rs->state)
     src_delete(rs->state);
+#else
+  if(rs){}                              /* quieten compiler */
 #endif
 }
 
@@ -197,7 +201,7 @@ static size_t resample_put_sample(const struct resampler *rs,
 /** @brief Convert input samples to floats
  * @param rs Resampler state
  * @param bytes Input bytes
- * @param nbytes Number of input bytes
+ * @param nframes Number of input frames
  * @param floats Where to store converted data
  *
  * @p floats must be big enough.  As well as converting to floats this
@@ -212,10 +216,8 @@ static size_t resample_put_sample(const 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;
 
@@ -259,11 +261,12 @@ size_t resample_convert(const struct resampler *rs,
   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));
@@ -272,12 +275,17 @@ size_t resample_convert(const 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) {
@@ -299,7 +307,9 @@ size_t resample_convert(const struct resampler *rs,
   if(output != input)
     xfree(output);
   xfree(input);
+  if(eof){}                             /* quieten compiler */
   /* Report how many input bytes were actually consumed */
+  //fprintf(stderr, "converted %zu frames\n", nframesin);
   return nframesin * rs->input_bytes_per_frame;
 }