Blame test/latency.c

Packit 4a16fb
/*
Packit 4a16fb
 *  Latency test program
Packit 4a16fb
 *
Packit 4a16fb
 *     Author: Jaroslav Kysela <perex@perex.cz>
Packit 4a16fb
 *
Packit 4a16fb
 *     Author of bandpass filter sweep effect:
Packit 4a16fb
 *	       Maarten de Boer <mdeboer@iua.upf.es>
Packit 4a16fb
 *
Packit 4a16fb
 *  This small demo program can be used for measuring latency between
Packit 4a16fb
 *  capture and playback. This latency is measured from driver (diff when
Packit 4a16fb
 *  playback and capture was started). Scheduler is set to SCHED_RR.
Packit 4a16fb
 *
Packit 4a16fb
 *
Packit 4a16fb
 *   This program is free software; you can redistribute it and/or modify
Packit 4a16fb
 *   it under the terms of the GNU General Public License as published by
Packit 4a16fb
 *   the Free Software Foundation; either version 2 of the License, or
Packit 4a16fb
 *   (at your option) any later version.
Packit 4a16fb
 *
Packit 4a16fb
 *   This program is distributed in the hope that it will be useful,
Packit 4a16fb
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 4a16fb
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 4a16fb
 *   GNU General Public License for more details.
Packit 4a16fb
 *
Packit 4a16fb
 *   You should have received a copy of the GNU General Public License
Packit 4a16fb
 *   along with this program; if not, write to the Free Software
Packit 4a16fb
 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
Packit 4a16fb
 *
Packit 4a16fb
 */
Packit 4a16fb
Packit 4a16fb
#include <stdio.h>
Packit 4a16fb
#include <stdlib.h>
Packit 4a16fb
#include <string.h>
Packit 4a16fb
#include <sched.h>
Packit 4a16fb
#include <errno.h>
Packit 4a16fb
#include <getopt.h>
Packit 4a16fb
#include "../include/asoundlib.h"
Packit 4a16fb
#include <sys/time.h>
Packit 4a16fb
#include <math.h>
Packit 4a16fb
Packit 4a16fb
char *pdevice = "hw:0,0";
Packit 4a16fb
char *cdevice = "hw:0,0";
Packit 4a16fb
snd_pcm_format_t format = SND_PCM_FORMAT_S16_LE;
Packit 4a16fb
int rate = 22050;
Packit 4a16fb
int channels = 2;
Packit 4a16fb
int buffer_size = 0;		/* auto */
Packit 4a16fb
int period_size = 0;		/* auto */
Packit 4a16fb
int latency_min = 32;		/* in frames / 2 */
Packit 4a16fb
int latency_max = 2048;		/* in frames / 2 */
Packit 4a16fb
int loop_sec = 30;		/* seconds */
Packit 4a16fb
int block = 0;			/* block mode */
Packit 4a16fb
int use_poll = 0;
Packit 4a16fb
int resample = 1;
Packit 4a16fb
unsigned long loop_limit;
Packit 4a16fb
Packit 4a16fb
snd_output_t *output = NULL;
Packit 4a16fb
Packit 4a16fb
int setparams_stream(snd_pcm_t *handle,
Packit 4a16fb
		     snd_pcm_hw_params_t *params,
Packit 4a16fb
		     const char *id)
Packit 4a16fb
{
Packit 4a16fb
	int err;
Packit 4a16fb
	unsigned int rrate;
Packit 4a16fb
Packit 4a16fb
	err = snd_pcm_hw_params_any(handle, params);
Packit 4a16fb
	if (err < 0) {
Packit 4a16fb
		printf("Broken configuration for %s PCM: no configurations available: %s\n", snd_strerror(err), id);
Packit 4a16fb
		return err;
Packit 4a16fb
	}
Packit 4a16fb
	err = snd_pcm_hw_params_set_rate_resample(handle, params, resample);
Packit 4a16fb
	if (err < 0) {
Packit 4a16fb
		printf("Resample setup failed for %s (val %i): %s\n", id, resample, snd_strerror(err));
Packit 4a16fb
		return err;
Packit 4a16fb
	}
Packit 4a16fb
	err = snd_pcm_hw_params_set_access(handle, params, SND_PCM_ACCESS_RW_INTERLEAVED);
Packit 4a16fb
	if (err < 0) {
Packit 4a16fb
		printf("Access type not available for %s: %s\n", id, snd_strerror(err));
Packit 4a16fb
		return err;
Packit 4a16fb
	}
Packit 4a16fb
	err = snd_pcm_hw_params_set_format(handle, params, format);
Packit 4a16fb
	if (err < 0) {
Packit 4a16fb
		printf("Sample format not available for %s: %s\n", id, snd_strerror(err));
Packit 4a16fb
		return err;
Packit 4a16fb
	}
Packit 4a16fb
	err = snd_pcm_hw_params_set_channels(handle, params, channels);
Packit 4a16fb
	if (err < 0) {
Packit 4a16fb
		printf("Channels count (%i) not available for %s: %s\n", channels, id, snd_strerror(err));
Packit 4a16fb
		return err;
Packit 4a16fb
	}
Packit 4a16fb
	rrate = rate;
Packit 4a16fb
	err = snd_pcm_hw_params_set_rate_near(handle, params, &rrate, 0);
Packit 4a16fb
	if (err < 0) {
Packit 4a16fb
		printf("Rate %iHz not available for %s: %s\n", rate, id, snd_strerror(err));
Packit 4a16fb
		return err;
Packit 4a16fb
	}
Packit 4a16fb
	if ((int)rrate != rate) {
Packit 4a16fb
		printf("Rate doesn't match (requested %iHz, get %iHz)\n", rate, err);
Packit 4a16fb
		return -EINVAL;
Packit 4a16fb
	}
Packit 4a16fb
	return 0;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
int setparams_bufsize(snd_pcm_t *handle,
Packit 4a16fb
		      snd_pcm_hw_params_t *params,
Packit 4a16fb
		      snd_pcm_hw_params_t *tparams,
Packit 4a16fb
		      snd_pcm_uframes_t bufsize,
Packit 4a16fb
		      const char *id)
Packit 4a16fb
{
Packit 4a16fb
	int err;
Packit 4a16fb
	snd_pcm_uframes_t periodsize;
Packit 4a16fb
Packit 4a16fb
	snd_pcm_hw_params_copy(params, tparams);
Packit 4a16fb
	periodsize = bufsize * 2;
Packit 4a16fb
	err = snd_pcm_hw_params_set_buffer_size_near(handle, params, &periodsize);
Packit 4a16fb
	if (err < 0) {
Packit 4a16fb
		printf("Unable to set buffer size %li for %s: %s\n", bufsize * 2, id, snd_strerror(err));
Packit 4a16fb
		return err;
Packit 4a16fb
	}
Packit 4a16fb
	if (period_size > 0)
Packit 4a16fb
		periodsize = period_size;
Packit 4a16fb
	else
Packit 4a16fb
		periodsize /= 2;
Packit 4a16fb
	err = snd_pcm_hw_params_set_period_size_near(handle, params, &periodsize, 0);
Packit 4a16fb
	if (err < 0) {
Packit 4a16fb
		printf("Unable to set period size %li for %s: %s\n", periodsize, id, snd_strerror(err));
Packit 4a16fb
		return err;
Packit 4a16fb
	}
Packit 4a16fb
	return 0;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
int setparams_set(snd_pcm_t *handle,
Packit 4a16fb
		  snd_pcm_hw_params_t *params,
Packit 4a16fb
		  snd_pcm_sw_params_t *swparams,
Packit 4a16fb
		  const char *id)
Packit 4a16fb
{
Packit 4a16fb
	int err;
Packit 4a16fb
	snd_pcm_uframes_t val;
Packit 4a16fb
Packit 4a16fb
	err = snd_pcm_hw_params(handle, params);
Packit 4a16fb
	if (err < 0) {
Packit 4a16fb
		printf("Unable to set hw params for %s: %s\n", id, snd_strerror(err));
Packit 4a16fb
		return err;
Packit 4a16fb
	}
Packit 4a16fb
	err = snd_pcm_sw_params_current(handle, swparams);
Packit 4a16fb
	if (err < 0) {
Packit 4a16fb
		printf("Unable to determine current swparams for %s: %s\n", id, snd_strerror(err));
Packit 4a16fb
		return err;
Packit 4a16fb
	}
Packit 4a16fb
	err = snd_pcm_sw_params_set_start_threshold(handle, swparams, 0x7fffffff);
Packit 4a16fb
	if (err < 0) {
Packit 4a16fb
		printf("Unable to set start threshold mode for %s: %s\n", id, snd_strerror(err));
Packit 4a16fb
		return err;
Packit 4a16fb
	}
Packit 4a16fb
	if (!block)
Packit 4a16fb
		val = 4;
Packit 4a16fb
	else
Packit 4a16fb
		snd_pcm_hw_params_get_period_size(params, &val, NULL);
Packit 4a16fb
	err = snd_pcm_sw_params_set_avail_min(handle, swparams, val);
Packit 4a16fb
	if (err < 0) {
Packit 4a16fb
		printf("Unable to set avail min for %s: %s\n", id, snd_strerror(err));
Packit 4a16fb
		return err;
Packit 4a16fb
	}
Packit 4a16fb
	err = snd_pcm_sw_params(handle, swparams);
Packit 4a16fb
	if (err < 0) {
Packit 4a16fb
		printf("Unable to set sw params for %s: %s\n", id, snd_strerror(err));
Packit 4a16fb
		return err;
Packit 4a16fb
	}
Packit 4a16fb
	return 0;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
int setparams(snd_pcm_t *phandle, snd_pcm_t *chandle, int *bufsize)
Packit 4a16fb
{
Packit 4a16fb
	int err, last_bufsize = *bufsize;
Packit 4a16fb
	snd_pcm_hw_params_t *pt_params, *ct_params;	/* templates with rate, format and channels */
Packit 4a16fb
	snd_pcm_hw_params_t *p_params, *c_params;
Packit 4a16fb
	snd_pcm_sw_params_t *p_swparams, *c_swparams;
Packit 4a16fb
	snd_pcm_uframes_t p_size, c_size, p_psize, c_psize;
Packit 4a16fb
	unsigned int p_time, c_time;
Packit 4a16fb
	unsigned int val;
Packit 4a16fb
Packit 4a16fb
	snd_pcm_hw_params_alloca(&p_params);
Packit 4a16fb
	snd_pcm_hw_params_alloca(&c_params);
Packit 4a16fb
	snd_pcm_hw_params_alloca(&pt_params);
Packit 4a16fb
	snd_pcm_hw_params_alloca(&ct_params);
Packit 4a16fb
	snd_pcm_sw_params_alloca(&p_swparams);
Packit 4a16fb
	snd_pcm_sw_params_alloca(&c_swparams);
Packit 4a16fb
	if ((err = setparams_stream(phandle, pt_params, "playback")) < 0) {
Packit 4a16fb
		printf("Unable to set parameters for playback stream: %s\n", snd_strerror(err));
Packit 4a16fb
		exit(0);
Packit 4a16fb
	}
Packit 4a16fb
	if ((err = setparams_stream(chandle, ct_params, "capture")) < 0) {
Packit 4a16fb
		printf("Unable to set parameters for playback stream: %s\n", snd_strerror(err));
Packit 4a16fb
		exit(0);
Packit 4a16fb
	}
Packit 4a16fb
Packit 4a16fb
	if (buffer_size > 0) {
Packit 4a16fb
		*bufsize = buffer_size;
Packit 4a16fb
		goto __set_it;
Packit 4a16fb
	}
Packit 4a16fb
Packit 4a16fb
      __again:
Packit 4a16fb
      	if (buffer_size > 0)
Packit 4a16fb
      		return -1;
Packit 4a16fb
      	if (last_bufsize == *bufsize)
Packit 4a16fb
		*bufsize += 4;
Packit 4a16fb
	last_bufsize = *bufsize;
Packit 4a16fb
	if (*bufsize > latency_max)
Packit 4a16fb
		return -1;
Packit 4a16fb
      __set_it:
Packit 4a16fb
	if ((err = setparams_bufsize(phandle, p_params, pt_params, *bufsize, "playback")) < 0) {
Packit 4a16fb
		printf("Unable to set sw parameters for playback stream: %s\n", snd_strerror(err));
Packit 4a16fb
		exit(0);
Packit 4a16fb
	}
Packit 4a16fb
	if ((err = setparams_bufsize(chandle, c_params, ct_params, *bufsize, "capture")) < 0) {
Packit 4a16fb
		printf("Unable to set sw parameters for playback stream: %s\n", snd_strerror(err));
Packit 4a16fb
		exit(0);
Packit 4a16fb
	}
Packit 4a16fb
Packit 4a16fb
	snd_pcm_hw_params_get_period_size(p_params, &p_psize, NULL);
Packit 4a16fb
	if (p_psize > (unsigned int)*bufsize)
Packit 4a16fb
		*bufsize = p_psize;
Packit 4a16fb
	snd_pcm_hw_params_get_period_size(c_params, &c_psize, NULL);
Packit 4a16fb
	if (c_psize > (unsigned int)*bufsize)
Packit 4a16fb
		*bufsize = c_psize;
Packit 4a16fb
	snd_pcm_hw_params_get_period_time(p_params, &p_time, NULL);
Packit 4a16fb
	snd_pcm_hw_params_get_period_time(c_params, &c_time, NULL);
Packit 4a16fb
	if (p_time != c_time)
Packit 4a16fb
		goto __again;
Packit 4a16fb
Packit 4a16fb
	snd_pcm_hw_params_get_buffer_size(p_params, &p_size);
Packit 4a16fb
	if (p_psize * 2 < p_size) {
Packit 4a16fb
                snd_pcm_hw_params_get_periods_min(p_params, &val, NULL);
Packit 4a16fb
                if (val > 2) {
Packit 4a16fb
			printf("playback device does not support 2 periods per buffer\n");
Packit 4a16fb
			exit(0);
Packit 4a16fb
		}
Packit 4a16fb
		goto __again;
Packit 4a16fb
	}
Packit 4a16fb
	snd_pcm_hw_params_get_buffer_size(c_params, &c_size);
Packit 4a16fb
	if (c_psize * 2 < c_size) {
Packit 4a16fb
                snd_pcm_hw_params_get_periods_min(c_params, &val, NULL);
Packit 4a16fb
		if (val > 2 ) {
Packit 4a16fb
			printf("capture device does not support 2 periods per buffer\n");
Packit 4a16fb
			exit(0);
Packit 4a16fb
		}
Packit 4a16fb
		goto __again;
Packit 4a16fb
	}
Packit 4a16fb
	if ((err = setparams_set(phandle, p_params, p_swparams, "playback")) < 0) {
Packit 4a16fb
		printf("Unable to set sw parameters for playback stream: %s\n", snd_strerror(err));
Packit 4a16fb
		exit(0);
Packit 4a16fb
	}
Packit 4a16fb
	if ((err = setparams_set(chandle, c_params, c_swparams, "capture")) < 0) {
Packit 4a16fb
		printf("Unable to set sw parameters for playback stream: %s\n", snd_strerror(err));
Packit 4a16fb
		exit(0);
Packit 4a16fb
	}
Packit 4a16fb
Packit 4a16fb
	if ((err = snd_pcm_prepare(phandle)) < 0) {
Packit 4a16fb
		printf("Prepare error: %s\n", snd_strerror(err));
Packit 4a16fb
		exit(0);
Packit 4a16fb
	}
Packit 4a16fb
Packit 4a16fb
	snd_pcm_dump(phandle, output);
Packit 4a16fb
	snd_pcm_dump(chandle, output);
Packit 4a16fb
	fflush(stdout);
Packit 4a16fb
	return 0;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
void showstat(snd_pcm_t *handle, size_t frames)
Packit 4a16fb
{
Packit 4a16fb
	int err;
Packit 4a16fb
	snd_pcm_status_t *status;
Packit 4a16fb
Packit 4a16fb
	snd_pcm_status_alloca(&status);
Packit 4a16fb
	if ((err = snd_pcm_status(handle, status)) < 0) {
Packit 4a16fb
		printf("Stream status error: %s\n", snd_strerror(err));
Packit 4a16fb
		exit(0);
Packit 4a16fb
	}
Packit 4a16fb
	printf("*** frames = %li ***\n", (long)frames);
Packit 4a16fb
	snd_pcm_status_dump(status, output);
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
void showlatency(size_t latency)
Packit 4a16fb
{
Packit 4a16fb
	double d;
Packit 4a16fb
	latency *= 2;
Packit 4a16fb
	d = (double)latency / (double)rate;
Packit 4a16fb
	printf("Trying latency %li frames, %.3fus, %.6fms (%.4fHz)\n", (long)latency, d * 1000000, d * 1000, (double)1 / d);
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
void showinmax(size_t in_max)
Packit 4a16fb
{
Packit 4a16fb
	double d;
Packit 4a16fb
Packit 4a16fb
	printf("Maximum read: %li frames\n", (long)in_max);
Packit 4a16fb
	d = (double)in_max / (double)rate;
Packit 4a16fb
	printf("Maximum read latency: %.3fus, %.6fms (%.4fHz)\n", d * 1000000, d * 1000, (double)1 / d);
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
void gettimestamp(snd_pcm_t *handle, snd_timestamp_t *timestamp)
Packit 4a16fb
{
Packit 4a16fb
	int err;
Packit 4a16fb
	snd_pcm_status_t *status;
Packit 4a16fb
Packit 4a16fb
	snd_pcm_status_alloca(&status);
Packit 4a16fb
	if ((err = snd_pcm_status(handle, status)) < 0) {
Packit 4a16fb
		printf("Stream status error: %s\n", snd_strerror(err));
Packit 4a16fb
		exit(0);
Packit 4a16fb
	}
Packit 4a16fb
	snd_pcm_status_get_trigger_tstamp(status, timestamp);
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
void setscheduler(void)
Packit 4a16fb
{
Packit 4a16fb
	struct sched_param sched_param;
Packit 4a16fb
Packit 4a16fb
	if (sched_getparam(0, &sched_param) < 0) {
Packit 4a16fb
		printf("Scheduler getparam failed...\n");
Packit 4a16fb
		return;
Packit 4a16fb
	}
Packit 4a16fb
	sched_param.sched_priority = sched_get_priority_max(SCHED_RR);
Packit 4a16fb
	if (!sched_setscheduler(0, SCHED_RR, &sched_param)) {
Packit 4a16fb
		printf("Scheduler set to Round Robin with priority %i...\n", sched_param.sched_priority);
Packit 4a16fb
		fflush(stdout);
Packit 4a16fb
		return;
Packit 4a16fb
	}
Packit 4a16fb
	printf("!!!Scheduler set to Round Robin with priority %i FAILED!!!\n", sched_param.sched_priority);
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
long timediff(snd_timestamp_t t1, snd_timestamp_t t2)
Packit 4a16fb
{
Packit 4a16fb
	signed long l;
Packit 4a16fb
Packit 4a16fb
	t1.tv_sec -= t2.tv_sec;
Packit 4a16fb
	l = (signed long) t1.tv_usec - (signed long) t2.tv_usec;
Packit 4a16fb
	if (l < 0) {
Packit 4a16fb
		t1.tv_sec--;
Packit 4a16fb
		l = 1000000 + l;
Packit 4a16fb
		l %= 1000000;
Packit 4a16fb
	}
Packit 4a16fb
	return (t1.tv_sec * 1000000) + l;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
long readbuf(snd_pcm_t *handle, char *buf, long len, size_t *frames, size_t *max)
Packit 4a16fb
{
Packit 4a16fb
	long r;
Packit 4a16fb
Packit 4a16fb
	if (!block) {
Packit 4a16fb
		do {
Packit 4a16fb
			r = snd_pcm_readi(handle, buf, len);
Packit 4a16fb
		} while (r == -EAGAIN);
Packit 4a16fb
		if (r > 0) {
Packit 4a16fb
			*frames += r;
Packit 4a16fb
			if ((long)*max < r)
Packit 4a16fb
				*max = r;
Packit 4a16fb
		}
Packit 4a16fb
		// printf("read = %li\n", r);
Packit 4a16fb
	} else {
Packit 4a16fb
		int frame_bytes = (snd_pcm_format_width(format) / 8) * channels;
Packit 4a16fb
		do {
Packit 4a16fb
			r = snd_pcm_readi(handle, buf, len);
Packit 4a16fb
			if (r > 0) {
Packit 4a16fb
				buf += r * frame_bytes;
Packit 4a16fb
				len -= r;
Packit 4a16fb
				*frames += r;
Packit 4a16fb
				if ((long)*max < r)
Packit 4a16fb
					*max = r;
Packit 4a16fb
			}
Packit 4a16fb
			// printf("r = %li, len = %li\n", r, len);
Packit 4a16fb
		} while (r >= 1 && len > 0);
Packit 4a16fb
	}
Packit 4a16fb
	// showstat(handle, 0);
Packit 4a16fb
	return r;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
long writebuf(snd_pcm_t *handle, char *buf, long len, size_t *frames)
Packit 4a16fb
{
Packit 4a16fb
	long r;
Packit 4a16fb
	int frame_bytes = (snd_pcm_format_width(format) / 8) * channels;
Packit 4a16fb
Packit 4a16fb
	while (len > 0) {
Packit 4a16fb
		r = snd_pcm_writei(handle, buf, len);
Packit 4a16fb
		if (r == -EAGAIN)
Packit 4a16fb
			continue;
Packit 4a16fb
		// printf("write = %li\n", r);
Packit 4a16fb
		if (r < 0)
Packit 4a16fb
			return r;
Packit 4a16fb
		// showstat(handle, 0);
Packit 4a16fb
		buf += r * frame_bytes;
Packit 4a16fb
		len -= r;
Packit 4a16fb
		*frames += r;
Packit 4a16fb
	}
Packit 4a16fb
	return 0;
Packit 4a16fb
}
Packit 4a16fb
			
Packit 4a16fb
#define FILTERSWEEP_LFO_CENTER 2000.
Packit 4a16fb
#define FILTERSWEEP_LFO_DEPTH 1800.
Packit 4a16fb
#define FILTERSWEEP_LFO_FREQ 0.2
Packit 4a16fb
#define FILTER_BANDWIDTH 50
Packit 4a16fb
Packit 4a16fb
/* filter the sweep variables */
Packit 4a16fb
float lfo,dlfo,fs,fc,BW,C,D,a0,a1,a2,b1,b2,*x[3],*y[3];
Packit 4a16fb
Packit 4a16fb
void applyeffect(char* buffer,int r)
Packit 4a16fb
{
Packit 4a16fb
	short* samples = (short*) buffer;
Packit 4a16fb
	int i;
Packit 4a16fb
	for (i=0;i
Packit 4a16fb
	{
Packit 4a16fb
		int chn;
Packit 4a16fb
Packit 4a16fb
		fc = sin(lfo)*FILTERSWEEP_LFO_DEPTH+FILTERSWEEP_LFO_CENTER;
Packit 4a16fb
		lfo += dlfo;
Packit 4a16fb
		if (lfo>2.*M_PI) lfo -= 2.*M_PI;
Packit 4a16fb
		C = 1./tan(M_PI*BW/fs);
Packit 4a16fb
		D = 2.*cos(2*M_PI*fc/fs);
Packit 4a16fb
		a0 = 1./(1.+C);
Packit 4a16fb
		a1 = 0;
Packit 4a16fb
		a2 = -a0;
Packit 4a16fb
		b1 = -C*D*a0;
Packit 4a16fb
		b2 = (C-1)*a0;
Packit 4a16fb
Packit 4a16fb
		for (chn=0;chn
Packit 4a16fb
		{
Packit 4a16fb
			x[chn][2] = x[chn][1];
Packit 4a16fb
			x[chn][1] = x[chn][0];
Packit 4a16fb
Packit 4a16fb
			y[chn][2] = y[chn][1];
Packit 4a16fb
			y[chn][1] = y[chn][0];
Packit 4a16fb
Packit 4a16fb
			x[chn][0] = samples[i*channels+chn];
Packit 4a16fb
			y[chn][0] = a0*x[chn][0] + a1*x[chn][1] + a2*x[chn][2] 
Packit 4a16fb
				- b1*y[chn][1] - b2*y[chn][2];
Packit 4a16fb
			samples[i*channels+chn] = y[chn][0];
Packit 4a16fb
		}
Packit 4a16fb
	}
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
void help(void)
Packit 4a16fb
{
Packit 4a16fb
	int k;
Packit 4a16fb
	printf(
Packit 4a16fb
"Usage: latency [OPTION]... [FILE]...\n"
Packit 4a16fb
"-h,--help      help\n"
Packit 4a16fb
"-P,--pdevice   playback device\n"
Packit 4a16fb
"-C,--cdevice   capture device\n"
Packit 4a16fb
"-m,--min       minimum latency in frames\n"
Packit 4a16fb
"-M,--max       maximum latency in frames\n"
Packit 4a16fb
"-F,--frames    frames to transfer\n"
Packit 4a16fb
"-f,--format    sample format\n"
Packit 4a16fb
"-c,--channels  channels\n"
Packit 4a16fb
"-r,--rate      rate\n"
Packit 4a16fb
"-B,--buffer    buffer size in frames\n"
Packit 4a16fb
"-E,--period    period size in frames\n"
Packit 4a16fb
"-s,--seconds   duration of test in seconds\n"
Packit 4a16fb
"-b,--block     block mode\n"
Packit 4a16fb
"-p,--poll      use poll (wait for event - reduces CPU usage)\n"
Packit 4a16fb
"-e,--effect    apply an effect (bandpass filter sweep)\n"
Packit 4a16fb
);
Packit 4a16fb
        printf("Recognized sample formats are:");
Packit 4a16fb
        for (k = 0; k < SND_PCM_FORMAT_LAST; ++k) {
Packit 4a16fb
                const char *s = snd_pcm_format_name(k);
Packit 4a16fb
                if (s)
Packit 4a16fb
                        printf(" %s", s);
Packit 4a16fb
        }
Packit 4a16fb
        printf("\n\n");
Packit 4a16fb
        printf(
Packit 4a16fb
"Tip #1 (usable latency with large periods, non-blocking mode, good CPU usage,\n"
Packit 4a16fb
"        superb xrun prevention):\n"
Packit 4a16fb
"  latency -m 8192 -M 8192 -t 1 -p\n"
Packit 4a16fb
"Tip #2 (superb latency, non-blocking mode, but heavy CPU usage):\n"
Packit 4a16fb
"  latency -m 128 -M 128\n"
Packit 4a16fb
);
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
int main(int argc, char *argv[])
Packit 4a16fb
{
Packit 4a16fb
	struct option long_option[] =
Packit 4a16fb
	{
Packit 4a16fb
		{"help", 0, NULL, 'h'},
Packit 4a16fb
		{"pdevice", 1, NULL, 'P'},
Packit 4a16fb
		{"cdevice", 1, NULL, 'C'},
Packit 4a16fb
		{"min", 1, NULL, 'm'},
Packit 4a16fb
		{"max", 1, NULL, 'M'},
Packit 4a16fb
		{"frames", 1, NULL, 'F'},
Packit 4a16fb
		{"format", 1, NULL, 'f'},
Packit 4a16fb
		{"channels", 1, NULL, 'c'},
Packit 4a16fb
		{"rate", 1, NULL, 'r'},
Packit 4a16fb
		{"buffer", 1, NULL, 'B'},
Packit 4a16fb
		{"period", 1, NULL, 'E'},
Packit 4a16fb
		{"seconds", 1, NULL, 's'},
Packit 4a16fb
		{"block", 0, NULL, 'b'},
Packit 4a16fb
		{"poll", 0, NULL, 'p'},
Packit 4a16fb
		{"effect", 0, NULL, 'e'},
Packit 4a16fb
		{NULL, 0, NULL, 0},
Packit 4a16fb
	};
Packit 4a16fb
	snd_pcm_t *phandle, *chandle;
Packit 4a16fb
	char *buffer;
Packit 4a16fb
	int err, latency, morehelp;
Packit 4a16fb
	int ok;
Packit 4a16fb
	snd_timestamp_t p_tstamp, c_tstamp;
Packit 4a16fb
	ssize_t r;
Packit 4a16fb
	size_t frames_in, frames_out, in_max;
Packit 4a16fb
	int effect = 0;
Packit 4a16fb
	morehelp = 0;
Packit 4a16fb
	while (1) {
Packit 4a16fb
		int c;
Packit 4a16fb
		if ((c = getopt_long(argc, argv, "hP:C:m:M:F:f:c:r:B:E:s:bpen", long_option, NULL)) < 0)
Packit 4a16fb
			break;
Packit 4a16fb
		switch (c) {
Packit 4a16fb
		case 'h':
Packit 4a16fb
			morehelp++;
Packit 4a16fb
			break;
Packit 4a16fb
		case 'P':
Packit 4a16fb
			pdevice = strdup(optarg);
Packit 4a16fb
			break;
Packit 4a16fb
		case 'C':
Packit 4a16fb
			cdevice = strdup(optarg);
Packit 4a16fb
			break;
Packit 4a16fb
		case 'm':
Packit 4a16fb
			err = atoi(optarg) / 2;
Packit 4a16fb
			latency_min = err >= 4 ? err : 4;
Packit 4a16fb
			if (latency_max < latency_min)
Packit 4a16fb
				latency_max = latency_min;
Packit 4a16fb
			break;
Packit 4a16fb
		case 'M':
Packit 4a16fb
			err = atoi(optarg) / 2;
Packit 4a16fb
			latency_max = latency_min > err ? latency_min : err;
Packit 4a16fb
			break;
Packit 4a16fb
		case 'f':
Packit 4a16fb
			format = snd_pcm_format_value(optarg);
Packit 4a16fb
			if (format == SND_PCM_FORMAT_UNKNOWN) {
Packit 4a16fb
				printf("Unknown format, setting to default S16_LE\n");
Packit 4a16fb
				format = SND_PCM_FORMAT_S16_LE;
Packit 4a16fb
			}
Packit 4a16fb
			break;
Packit 4a16fb
		case 'c':
Packit 4a16fb
			err = atoi(optarg);
Packit 4a16fb
			channels = err >= 1 && err < 1024 ? err : 1;
Packit 4a16fb
			break;
Packit 4a16fb
		case 'r':
Packit 4a16fb
			err = atoi(optarg);
Packit 4a16fb
			rate = err >= 4000 && err < 200000 ? err : 44100;
Packit 4a16fb
			break;
Packit 4a16fb
		case 'B':
Packit 4a16fb
			err = atoi(optarg);
Packit 4a16fb
			buffer_size = err >= 32 && err < 200000 ? err : 0;
Packit 4a16fb
			break;
Packit 4a16fb
		case 'E':
Packit 4a16fb
			err = atoi(optarg);
Packit 4a16fb
			period_size = err >= 32 && err < 200000 ? err : 0;
Packit 4a16fb
			break;
Packit 4a16fb
		case 's':
Packit 4a16fb
			err = atoi(optarg);
Packit 4a16fb
			loop_sec = err >= 1 && err <= 100000 ? err : 30;
Packit 4a16fb
			break;
Packit 4a16fb
		case 'b':
Packit 4a16fb
			block = 1;
Packit 4a16fb
			break;
Packit 4a16fb
		case 'p':
Packit 4a16fb
			use_poll = 1;
Packit 4a16fb
			break;
Packit 4a16fb
		case 'e':
Packit 4a16fb
			effect = 1;
Packit 4a16fb
			break;
Packit 4a16fb
		case 'n':
Packit 4a16fb
			resample = 0;
Packit 4a16fb
			break;
Packit 4a16fb
		}
Packit 4a16fb
	}
Packit 4a16fb
Packit 4a16fb
	if (morehelp) {
Packit 4a16fb
		help();
Packit 4a16fb
		return 0;
Packit 4a16fb
	}
Packit 4a16fb
	err = snd_output_stdio_attach(&output, stdout, 0);
Packit 4a16fb
	if (err < 0) {
Packit 4a16fb
		printf("Output failed: %s\n", snd_strerror(err));
Packit 4a16fb
		return 0;
Packit 4a16fb
	}
Packit 4a16fb
Packit 4a16fb
	loop_limit = loop_sec * rate;
Packit 4a16fb
	latency = latency_min - 4;
Packit 4a16fb
	buffer = malloc((latency_max * snd_pcm_format_width(format) / 8) * 2);
Packit 4a16fb
Packit 4a16fb
	setscheduler();
Packit 4a16fb
Packit 4a16fb
	printf("Playback device is %s\n", pdevice);
Packit 4a16fb
	printf("Capture device is %s\n", cdevice);
Packit 4a16fb
	printf("Parameters are %iHz, %s, %i channels, %s mode\n", rate, snd_pcm_format_name(format), channels, block ? "blocking" : "non-blocking");
Packit 4a16fb
	printf("Poll mode: %s\n", use_poll ? "yes" : "no");
Packit 4a16fb
	printf("Loop limit is %lu frames, minimum latency = %i, maximum latency = %i\n", loop_limit, latency_min * 2, latency_max * 2);
Packit 4a16fb
Packit 4a16fb
	if ((err = snd_pcm_open(&phandle, pdevice, SND_PCM_STREAM_PLAYBACK, block ? 0 : SND_PCM_NONBLOCK)) < 0) {
Packit 4a16fb
		printf("Playback open error: %s\n", snd_strerror(err));
Packit 4a16fb
		return 0;
Packit 4a16fb
	}
Packit 4a16fb
	if ((err = snd_pcm_open(&chandle, cdevice, SND_PCM_STREAM_CAPTURE, block ? 0 : SND_PCM_NONBLOCK)) < 0) {
Packit 4a16fb
		printf("Record open error: %s\n", snd_strerror(err));
Packit 4a16fb
		return 0;
Packit 4a16fb
	}
Packit 4a16fb
Packit 4a16fb
	/* initialize the filter sweep variables */
Packit 4a16fb
	if (effect) {
Packit 4a16fb
		fs = (float) rate;
Packit 4a16fb
		BW = FILTER_BANDWIDTH;
Packit 4a16fb
Packit 4a16fb
		lfo = 0;
Packit 4a16fb
		dlfo = 2.*M_PI*FILTERSWEEP_LFO_FREQ/fs;
Packit 4a16fb
Packit 4a16fb
		x[0] = (float*) malloc(channels*sizeof(float));		
Packit 4a16fb
		x[1] = (float*) malloc(channels*sizeof(float));		
Packit 4a16fb
		x[2] = (float*) malloc(channels*sizeof(float));		
Packit 4a16fb
		y[0] = (float*) malloc(channels*sizeof(float));		
Packit 4a16fb
		y[1] = (float*) malloc(channels*sizeof(float));		
Packit 4a16fb
		y[2] = (float*) malloc(channels*sizeof(float));		
Packit 4a16fb
	}
Packit 4a16fb
			  
Packit 4a16fb
	while (1) {
Packit 4a16fb
		frames_in = frames_out = 0;
Packit 4a16fb
		if (setparams(phandle, chandle, &latency) < 0)
Packit 4a16fb
			break;
Packit 4a16fb
		showlatency(latency);
Packit 4a16fb
		if ((err = snd_pcm_link(chandle, phandle)) < 0) {
Packit 4a16fb
			printf("Streams link error: %s\n", snd_strerror(err));
Packit 4a16fb
			exit(0);
Packit 4a16fb
		}
Packit 4a16fb
		if (snd_pcm_format_set_silence(format, buffer, latency*channels) < 0) {
Packit 4a16fb
			fprintf(stderr, "silence error\n");
Packit 4a16fb
			break;
Packit 4a16fb
		}
Packit 4a16fb
		if (writebuf(phandle, buffer, latency, &frames_out) < 0) {
Packit 4a16fb
			fprintf(stderr, "write error\n");
Packit 4a16fb
			break;
Packit 4a16fb
		}
Packit 4a16fb
		if (writebuf(phandle, buffer, latency, &frames_out) < 0) {
Packit 4a16fb
			fprintf(stderr, "write error\n");
Packit 4a16fb
			break;
Packit 4a16fb
		}
Packit 4a16fb
Packit 4a16fb
		if ((err = snd_pcm_start(chandle)) < 0) {
Packit 4a16fb
			printf("Go error: %s\n", snd_strerror(err));
Packit 4a16fb
			exit(0);
Packit 4a16fb
		}
Packit 4a16fb
		gettimestamp(phandle, &p_tstamp);
Packit 4a16fb
		gettimestamp(chandle, &c_tstamp);
Packit 4a16fb
#if 0
Packit 4a16fb
		printf("Playback:\n");
Packit 4a16fb
		showstat(phandle, frames_out);
Packit 4a16fb
		printf("Capture:\n");
Packit 4a16fb
		showstat(chandle, frames_in);
Packit 4a16fb
#endif
Packit 4a16fb
Packit 4a16fb
		ok = 1;
Packit 4a16fb
		in_max = 0;
Packit 4a16fb
		while (ok && frames_in < loop_limit) {
Packit 4a16fb
			if (use_poll) {
Packit 4a16fb
				/* use poll to wait for next event */
Packit 4a16fb
				snd_pcm_wait(chandle, 1000);
Packit 4a16fb
			}
Packit 4a16fb
			if ((r = readbuf(chandle, buffer, latency, &frames_in, &in_max)) < 0)
Packit 4a16fb
				ok = 0;
Packit 4a16fb
			else {
Packit 4a16fb
				if (effect)
Packit 4a16fb
					applyeffect(buffer,r);
Packit 4a16fb
			 	if (writebuf(phandle, buffer, r, &frames_out) < 0)
Packit 4a16fb
					ok = 0;
Packit 4a16fb
			}
Packit 4a16fb
		}
Packit 4a16fb
		if (ok)
Packit 4a16fb
			printf("Success\n");
Packit 4a16fb
		else
Packit 4a16fb
			printf("Failure\n");
Packit 4a16fb
		printf("Playback:\n");
Packit 4a16fb
		showstat(phandle, frames_out);
Packit 4a16fb
		printf("Capture:\n");
Packit 4a16fb
		showstat(chandle, frames_in);
Packit 4a16fb
		showinmax(in_max);
Packit 4a16fb
		if (p_tstamp.tv_sec == c_tstamp.tv_sec &&
Packit 4a16fb
		    p_tstamp.tv_usec == c_tstamp.tv_usec)
Packit 4a16fb
			printf("Hardware sync\n");
Packit 4a16fb
		snd_pcm_drop(chandle);
Packit 4a16fb
		snd_pcm_nonblock(phandle, 0);
Packit 4a16fb
		snd_pcm_drain(phandle);
Packit 4a16fb
		snd_pcm_nonblock(phandle, !block ? 1 : 0);
Packit 4a16fb
		if (ok) {
Packit 4a16fb
#if 1
Packit 4a16fb
			printf("Playback time = %li.%i, Record time = %li.%i, diff = %li\n",
Packit 4a16fb
			       p_tstamp.tv_sec,
Packit 4a16fb
			       (int)p_tstamp.tv_usec,
Packit 4a16fb
			       c_tstamp.tv_sec,
Packit 4a16fb
			       (int)c_tstamp.tv_usec,
Packit 4a16fb
			       timediff(p_tstamp, c_tstamp));
Packit 4a16fb
#endif
Packit 4a16fb
			break;
Packit 4a16fb
		}
Packit 4a16fb
		snd_pcm_unlink(chandle);
Packit 4a16fb
		snd_pcm_hw_free(phandle);
Packit 4a16fb
		snd_pcm_hw_free(chandle);
Packit 4a16fb
	}
Packit 4a16fb
	snd_pcm_close(phandle);
Packit 4a16fb
	snd_pcm_close(chandle);
Packit 4a16fb
	return 0;
Packit 4a16fb
}