+#if HAVE_SAMPLERATE_H
+ /* We have libsamplerate */
+ if(formats_equal(&header, &config->sample_format))
+ /* If the format is already correct then we just write out the data */
+ copy(0, 1, header.nbytes);
+ else {
+ /* If we have a resampler active already check it is suitable and destroy
+ * it if not */
+ if(rs_in_use) {
+ D(("call resample_close"));
+ resample_close(rs);
+ rs_in_use = 0;
+ }
+ /*syslog(LOG_INFO, "%d/%d/%d/%d/%d -> %d/%d/%d/%d/%d",
+ header.bits,
+ header.channels,
+ header.rate,
+ 1,
+ header.endian,
+ config->sample_format.bits,
+ config->sample_format.channels,
+ config->sample_format.rate,
+ 1,
+ config->sample_format.endian);*/
+ if(!rs_in_use) {
+ /* Create a suitable resampler. */
+ D(("call resample_init"));
+ resample_init(rs,
+ header.bits,
+ header.channels,
+ header.rate,
+ 1, /* signed */
+ header.endian,
+ config->sample_format.bits,
+ config->sample_format.channels,
+ config->sample_format.rate,
+ 1, /* signed */
+ config->sample_format.endian);
+ latest_format = header;
+ rs_in_use = 1;
+ /* TODO speaker protocol does not record signedness of samples. It's
+ * assumed that they are always signed. This should be fixed in the
+ * future (and the sample format syntax extended in a compatible
+ * way). */
+ }
+ /* Feed data through the resampler */
+ size_t used = 0, left = header.nbytes;
+ while(used || left) {
+ if(left) {
+ size_t limit = (sizeof buffer) - used;
+ if(limit > left)
+ limit = left;
+ ssize_t r = read(0, buffer + used, limit);
+ if(r < 0)
+ disorder_fatal(errno, "reading from stdin");
+ if(r == 0)
+ disorder_fatal(0, "unexpected EOF");
+ left -= r;
+ used += r;
+ //syslog(LOG_INFO, "read %zd bytes", r);
+ D(("read %zd bytes", r));
+ }
+ /*syslog(LOG_INFO, " in: %02x %02x %02x %02x",
+ (uint8_t)buffer[0],
+ (uint8_t)buffer[1],
+ (uint8_t)buffer[2],
+ (uint8_t)buffer[3]);*/
+ D(("calling resample_convert used=%zu !left=%d", used, !left));
+ const size_t consumed = resample_convert(rs,
+ (uint8_t *)buffer, used,
+ !left,
+ converted, 0);
+ //syslog(LOG_INFO, "used=%zu consumed=%zu", used, consumed);
+ D(("consumed=%zu", consumed));
+ memmove(buffer, buffer + consumed, used - consumed);
+ used -= consumed;
+ }
+ }
+#else
+ /* We do not have libsamplerate. We will use sox instead. */