+ struct check_wav_task *cwt = container_of(t, struct check_wav_task, task);
+ struct btr_node *btrn = cwt->btrn;
+ unsigned char *a;
+ size_t sz;
+ int ret;
+ uint16_t bps; /* bits per sample */
+ const char *sample_formats[] = {SAMPLE_FORMATS};
+
+ t->error = 0;
+ ret = btr_node_status(btrn, cwt->min_iqs, BTR_NT_INTERNAL);
+ if (ret <= 0)
+ goto out;
+ if (cwt->state != CWS_NEED_HEADER)
+ goto pushdown;
+ btr_merge(btrn, cwt->min_iqs);
+ sz = btr_next_buffer(btrn, (char **)&a);
+ if (sz < cwt->min_iqs) /* file size less than WAV_HEADER_SIZE */
+ goto pushdown;
+ cwt->min_iqs = 0;
+ /*
+ * The default byte ordering assumed for WAVE data files is
+ * little-endian. Files written using the big-endian byte ordering
+ * scheme have the identifier RIFX instead of RIFF.
+ */
+ if (a[0] != 'R' || a[1] != 'I' || a[2] != 'F' ||
+ (a[3] != 'F' && a[3] != 'X')) {
+ PARA_NOTICE_LOG("wav header not found\n");
+ cwt->state = CWS_NO_HEADER;
+ sprintf(t->status, "check wav: no header");
+ goto out;
+ }
+ PARA_INFO_LOG("found wav header\n");
+ cwt->state = CWS_HAVE_HEADER;
+ sprintf(t->status, "check wav: have header");
+ cwt->channels = (unsigned) a[22];
+ cwt->sample_rate = a[24] + (a[25] << 8) + (a[26] << 16) + (a[27] << 24);
+ bps = a[34] + ((unsigned)a[35] << 8);
+ if (bps != 8 && bps != 16) {
+ PARA_WARNING_LOG("%u bps not supported, assuming 16\n", bps);
+ bps = 16;
+ }
+ /*
+ * 8-bit samples are stored as unsigned bytes, ranging from 0 to 255.
+ * 16-bit samples are stored as 2's-complement signed integers, ranging
+ * from -32768 to 32767.
+ */
+ if (bps == 8)
+ cwt->sample_format = SF_U8;
+ else
+ cwt->sample_format = (a[3] == 'F')? SF_S16_LE : SF_S16_BE;
+ PARA_NOTICE_LOG("%dHz, %s, %s\n", cwt->sample_rate,
+ cwt->channels == 1? "mono" : "stereo",
+ sample_formats[cwt->sample_format]);
+ btr_consume(btrn, WAV_HEADER_LEN);
+pushdown:
+ btr_pushdown(btrn);
+out:
+ t->error = ret;
+ if (ret < 0)
+ btr_remove_node(btrn);