static int read_file(struct ausrc_st *st) { struct mbuf *mb; int err; for (;;) { mb = mbuf_alloc(4096); if (!mb) return ENOMEM; mb->end = mb->size; err = aufile_read(st->aufile, mb->buf, &mb->end); if (err) break; if (mb->end == 0) { info("aufile: end of file\n"); break; } aubuf_append(st->aubuf, mb); mb = mem_deref(mb); } info("aufile: loaded %zu bytes\n", aubuf_cur_size(st->aubuf)); mem_deref(mb); return err; }
static int read_wav(kiss_fftr_cfg fft, const char *infile) { struct aufile *af_in = NULL; struct aufile_prm prm; size_t sampc_in_total = 0; size_t i; int err; err = aufile_open(&af_in, &prm, infile, AUFILE_READ); if (err) { re_fprintf(stderr, "%s: could not open input file (%m)\n", infile, err); goto out; } if (prm.fmt != AUFMT_S16LE) { err = EINVAL; goto out; } re_printf("%s: %u Hz, %d channels\n", infile, prm.srate, prm.channels); for (;;) { int16_t sampv[NUM_FFT]; size_t sz = sizeof(sampv); kiss_fft_cpx freqv[NUM_FREQ]; err = aufile_read(af_in, (void *)sampv, &sz); if (err || !sz) break; if (sz != sizeof(sampv)) { re_printf("skipping last %zu samples\n", sz); break; } sampc_in_total += (sz/2); /* do FFT transform */ kiss_fftr(fft, sampv, freqv); for (i=0; i<ARRAY_SIZE(freqv); i++) { kiss_fft_cpx cpx = freqv[i]; magv[i] += sqrt(cpx.r * cpx.r + cpx.i * cpx.i); } } re_printf("read %u samples\n", sampc_in_total); out: if (err) { re_fprintf(stderr, "file read error: %m\n", err); } mem_deref(af_in); return err; }
static int aufile_load(struct mbuf *mb, const char *filename, uint32_t *srate, uint8_t *channels) { struct aufile_prm prm; struct aufile *af; int err; err = aufile_open(&af, &prm, filename, AUFILE_READ); if (err) return err; while (!err) { uint8_t buf[4096]; size_t i, n; n = sizeof(buf); err = aufile_read(af, buf, &n); if (err || !n) break; switch (prm.fmt) { case AUFMT_S16LE: err = mbuf_write_mem(mb, buf, n); break; case AUFMT_PCMA: for (i=0; i<n; i++) { err |= mbuf_write_u16(mb, g711_alaw2pcm(buf[i])); } break; case AUFMT_PCMU: for (i=0; i<n; i++) { err |= mbuf_write_u16(mb, g711_ulaw2pcm(buf[i])); } break; default: err = ENOSYS; break; } } mem_deref(af); if (!err) { mb->pos = 0; *srate = prm.srate; *channels = prm.channels; } return err; }
static int read_file(struct ausrc_st *st) { struct mbuf *mb; int err; for (;;) { uint16_t *sampv; size_t i; mb = mbuf_alloc(4096); if (!mb) return ENOMEM; mb->end = mb->size; err = aufile_read(st->aufile, mb->buf, &mb->end); if (err) break; if (mb->end == 0) { info("aufile: end of file\n"); break; } /* convert from Little-Endian to Native-Endian */ sampv = (void *)mb->buf; for (i=0; i<mb->end/2; i++) { sampv[i] = sys_ltohs(sampv[i]); } aubuf_append(st->aubuf, mb); mb = mem_deref(mb); } info("aufile: loaded %zu bytes\n", aubuf_cur_size(st->aubuf)); mem_deref(mb); return err; }