コード例 #1
0
ファイル: main.c プロジェクト: Jackygcc/spectrem
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;
}
コード例 #2
0
ファイル: play.c プロジェクト: mralexgray/baresip
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;
}
コード例 #3
0
ファイル: aufile.c プロジェクト: AmesianX/baresip
static int alloc_handler(struct ausrc_st **stp, const struct ausrc *as,
			 struct media_ctx **ctx,
			 struct ausrc_prm *prm, const char *dev,
			 ausrc_read_h *rh, ausrc_error_h *errh, void *arg)
{
	struct ausrc_st *st;
	struct aufile_prm fprm;
	int err;
	(void)ctx;

	if (!stp || !as || !prm || !rh)
		return EINVAL;

	info("aufile: loading input file '%s'\n", dev);

	st = mem_zalloc(sizeof(*st), destructor);
	if (!st)
		return ENOMEM;

	st->as   = as;
	st->rh   = rh;
	st->errh = errh;
	st->arg  = arg;

	err = aufile_open(&st->aufile, &fprm, dev, AUFILE_READ);
	if (err) {
		warning("aufile: failed to open file '%s' (%m)\n", dev, err);
		goto out;
	}

	info("aufile: %s: %u Hz, %d channels\n",
	     dev, fprm.srate, fprm.channels);

	if (fprm.srate != prm->srate) {
		warning("aufile: input file (%s) must have sample-rate"
			" %u Hz\n", dev, prm->srate);
		err = ENODEV;
		goto out;
	}
	if (fprm.channels != prm->ch) {
		warning("aufile: input file (%s) must have channels = %d\n",
			dev, prm->ch);
		err = ENODEV;
		goto out;
	}
	if (fprm.fmt != AUFMT_S16LE) {
		warning("aufile: input file must have format S16LE\n");
		err = ENODEV;
		goto out;
	}

	st->sampc = prm->srate * prm->ch * prm->ptime / 1000;

	st->ptime = prm->ptime;

	info("aufile: audio ptime=%u sampc=%zu aubuf=[%u:%u]\n",
	     st->ptime, st->sampc,
	     prm->srate * prm->ch * 2,
	     prm->srate * prm->ch * 40);

	/* 1 - inf seconds of audio */
	err = aubuf_alloc(&st->aubuf,
			  prm->srate * prm->ch * 2,
			  0);
	if (err)
		goto out;

	err = read_file(st);
	if (err)
		goto out;

	tmr_start(&st->tmr, 1000, timeout, st);

	st->run = true;
	err = pthread_create(&st->thread, NULL, play_thread, st);
	if (err) {
		st->run = false;
		goto out;
	}

 out:
	if (err)
		mem_deref(st);
	else
		*stp = st;

	return err;
}