| 
									
										
										
										
											2001-10-31 16:03:33 +00:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  *  Latency test program | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2007-10-15 10:24:55 +02:00
										 |  |  |  *     Author: Jaroslav Kysela <perex@perex.cz> | 
					
						
							| 
									
										
										
										
											2001-10-31 16:03:33 +00:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2002-03-12 20:14:33 +00:00
										 |  |  |  *     Author of bandpass filter sweep effect: | 
					
						
							| 
									
										
										
										
											2001-10-31 16:03:33 +00:00
										 |  |  |  *	       Maarten de Boer <mdeboer@iua.upf.es> | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  *  This small demo program can be used for measuring latency between | 
					
						
							|  |  |  |  *  capture and playback. This latency is measured from driver (diff when | 
					
						
							|  |  |  |  *  playback and capture was started). Scheduler is set to SCHED_RR. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2001-12-30 09:22:54 +00:00
										 |  |  |  *   This program is free software; you can redistribute it and/or modify | 
					
						
							|  |  |  |  *   it under the terms of the GNU General Public License as published by | 
					
						
							|  |  |  |  *   the Free Software Foundation; either version 2 of the License, or | 
					
						
							|  |  |  |  *   (at your option) any later version. | 
					
						
							| 
									
										
										
										
											2001-10-31 16:03:33 +00:00
										 |  |  |  * | 
					
						
							|  |  |  |  *   This program is distributed in the hope that it will be useful, | 
					
						
							|  |  |  |  *   but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  |  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							| 
									
										
										
										
											2001-12-30 09:22:54 +00:00
										 |  |  |  *   GNU General Public License for more details. | 
					
						
							| 
									
										
										
										
											2001-10-31 16:03:33 +00:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2001-12-30 09:22:54 +00:00
										 |  |  |  *   You should have received a copy of the GNU General Public License | 
					
						
							|  |  |  |  *   along with this program; if not, write to the Free Software | 
					
						
							| 
									
										
										
										
											2017-11-14 14:29:26 +01:00
										 |  |  |  *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA | 
					
						
							| 
									
										
										
										
											2001-10-31 16:03:33 +00:00
										 |  |  |  * | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-30 18:22:59 +02:00
										 |  |  | #include "config.h"
 | 
					
						
							| 
									
										
										
										
											1998-11-27 00:13:09 +00:00
										 |  |  | #include <stdio.h>
 | 
					
						
							|  |  |  | #include <stdlib.h>
 | 
					
						
							|  |  |  | #include <string.h>
 | 
					
						
							|  |  |  | #include <sched.h>
 | 
					
						
							| 
									
										
										
										
											1999-11-19 19:05:22 +00:00
										 |  |  | #include <errno.h>
 | 
					
						
							| 
									
										
										
										
											2001-08-10 16:44:08 +00:00
										 |  |  | #include <getopt.h>
 | 
					
						
							| 
									
										
										
										
											2022-11-14 20:42:10 +01:00
										 |  |  | #include <time.h>
 | 
					
						
							| 
									
										
										
										
											1998-11-27 00:13:09 +00:00
										 |  |  | #include "../include/asoundlib.h"
 | 
					
						
							| 
									
										
										
										
											2000-07-18 13:38:58 +00:00
										 |  |  | #include <sys/time.h>
 | 
					
						
							| 
									
										
										
										
											2001-10-31 16:03:33 +00:00
										 |  |  | #include <math.h>
 | 
					
						
							| 
									
										
										
										
											1998-11-27 00:13:09 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-02 16:00:45 +09:00
										 |  |  | #ifndef CLOCK_MONOTONIC_RAW
 | 
					
						
							|  |  |  | #define CLOCK_MONOTONIC_RAW CLOCK_MONOTONIC
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #if defined(__OpenBSD__)
 | 
					
						
							|  |  |  | #define sched_getparam(pid, parm) (-1)
 | 
					
						
							|  |  |  | #define sched_setscheduler(pid, policy, parm) (-1)
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-14 20:42:10 +01:00
										 |  |  | typedef struct timespec timestamp_t; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-24 08:33:47 +01:00
										 |  |  | char *sched_policy = "rr"; | 
					
						
							| 
									
										
										
										
											2001-08-10 08:56:25 +00:00
										 |  |  | char *pdevice = "hw:0,0"; | 
					
						
							|  |  |  | char *cdevice = "hw:0,0"; | 
					
						
							|  |  |  | snd_pcm_format_t format = SND_PCM_FORMAT_S16_LE; | 
					
						
							|  |  |  | int rate = 22050; | 
					
						
							|  |  |  | int channels = 2; | 
					
						
							| 
									
										
										
										
											2005-03-25 08:53:48 +00:00
										 |  |  | int buffer_size = 0;		/* auto */ | 
					
						
							|  |  |  | int period_size = 0;		/* auto */ | 
					
						
							| 
									
										
										
										
											2001-10-31 16:03:33 +00:00
										 |  |  | int latency_min = 32;		/* in frames / 2 */ | 
					
						
							|  |  |  | int latency_max = 2048;		/* in frames / 2 */ | 
					
						
							| 
									
										
										
										
											2001-08-10 08:56:25 +00:00
										 |  |  | int loop_sec = 30;		/* seconds */ | 
					
						
							| 
									
										
										
										
											2001-10-26 16:12:01 +00:00
										 |  |  | int block = 0;			/* block mode */ | 
					
						
							| 
									
										
										
										
											2001-10-31 16:03:33 +00:00
										 |  |  | int use_poll = 0; | 
					
						
							| 
									
										
										
										
											2022-11-23 17:56:20 +01:00
										 |  |  | int usleep_val = 0; | 
					
						
							| 
									
										
										
										
											2005-04-12 12:09:55 +00:00
										 |  |  | int resample = 1; | 
					
						
							| 
									
										
										
										
											2022-11-14 20:42:10 +01:00
										 |  |  | int sys_latency = 0;		/* data I/O: use system timings instead driver wakeups */ | 
					
						
							|  |  |  | int pos_dump = 0;		/* dump positions */ | 
					
						
							|  |  |  | int realtime_check = 0; | 
					
						
							| 
									
										
										
										
											2001-08-10 08:56:25 +00:00
										 |  |  | unsigned long loop_limit; | 
					
						
							| 
									
										
										
										
											2022-11-14 20:42:10 +01:00
										 |  |  | snd_pcm_uframes_t playback_buffer_size; | 
					
						
							| 
									
										
										
										
											2000-02-22 17:36:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-08-10 08:56:25 +00:00
										 |  |  | snd_output_t *output = NULL; | 
					
						
							| 
									
										
										
										
											1999-11-06 23:47:07 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-14 20:42:10 +01:00
										 |  |  | static inline long long frames_to_micro(size_t frames) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	return (long long)((frames * 1000000LL) + (rate / 2)) / rate; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void timestamp_now(timestamp_t *tstamp) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	if (clock_gettime(CLOCK_MONOTONIC_RAW, tstamp)) | 
					
						
							|  |  |  | 		printf("clock_gettime() failed\n"); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | long long timestamp_diff_micro(timestamp_t *tstamp) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	timestamp_t now, diff; | 
					
						
							|  |  |  | 	timestamp_now(&now); | 
					
						
							|  |  |  | 	if (tstamp->tv_nsec > now.tv_nsec) { | 
					
						
							|  |  |  | 		diff.tv_sec = now.tv_sec - tstamp->tv_sec - 1; | 
					
						
							|  |  |  | 		diff.tv_nsec = (now.tv_nsec + 1000000000L) - tstamp->tv_nsec; | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		diff.tv_sec = now.tv_sec - tstamp->tv_sec; | 
					
						
							|  |  |  | 		diff.tv_nsec = now.tv_nsec - tstamp->tv_nsec; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	/* microseconds */ | 
					
						
							|  |  |  | 	return (diff.tv_sec * 1000000) + ((diff.tv_nsec + 500L) / 1000L); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-08-10 08:56:25 +00:00
										 |  |  | int setparams_stream(snd_pcm_t *handle, | 
					
						
							|  |  |  | 		     snd_pcm_hw_params_t *params, | 
					
						
							|  |  |  | 		     const char *id) | 
					
						
							| 
									
										
										
										
											1998-11-27 00:13:09 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2000-07-18 13:38:58 +00:00
										 |  |  | 	int err; | 
					
						
							| 
									
										
										
										
											2002-09-17 12:33:05 +00:00
										 |  |  | 	unsigned int rrate; | 
					
						
							| 
									
										
										
										
											2000-07-18 13:38:58 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-08-10 08:56:25 +00:00
										 |  |  | 	err = snd_pcm_hw_params_any(handle, params); | 
					
						
							|  |  |  | 	if (err < 0) { | 
					
						
							|  |  |  | 		printf("Broken configuration for %s PCM: no configurations available: %s\n", snd_strerror(err), id); | 
					
						
							|  |  |  | 		return err; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2005-04-12 12:09:55 +00:00
										 |  |  | 	err = snd_pcm_hw_params_set_rate_resample(handle, params, resample); | 
					
						
							|  |  |  | 	if (err < 0) { | 
					
						
							|  |  |  | 		printf("Resample setup failed for %s (val %i): %s\n", id, resample, snd_strerror(err)); | 
					
						
							|  |  |  | 		return err; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2001-08-10 08:56:25 +00:00
										 |  |  | 	err = snd_pcm_hw_params_set_access(handle, params, SND_PCM_ACCESS_RW_INTERLEAVED); | 
					
						
							|  |  |  | 	if (err < 0) { | 
					
						
							|  |  |  | 		printf("Access type not available for %s: %s\n", id, snd_strerror(err)); | 
					
						
							|  |  |  | 		return err; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	err = snd_pcm_hw_params_set_format(handle, params, format); | 
					
						
							|  |  |  | 	if (err < 0) { | 
					
						
							|  |  |  | 		printf("Sample format not available for %s: %s\n", id, snd_strerror(err)); | 
					
						
							|  |  |  | 		return err; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	err = snd_pcm_hw_params_set_channels(handle, params, channels); | 
					
						
							|  |  |  | 	if (err < 0) { | 
					
						
							|  |  |  | 		printf("Channels count (%i) not available for %s: %s\n", channels, id, snd_strerror(err)); | 
					
						
							|  |  |  | 		return err; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2002-09-17 12:33:05 +00:00
										 |  |  | 	rrate = rate; | 
					
						
							|  |  |  | 	err = snd_pcm_hw_params_set_rate_near(handle, params, &rrate, 0); | 
					
						
							| 
									
										
										
										
											2001-08-10 08:56:25 +00:00
										 |  |  | 	if (err < 0) { | 
					
						
							|  |  |  | 		printf("Rate %iHz not available for %s: %s\n", rate, id, snd_strerror(err)); | 
					
						
							|  |  |  | 		return err; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2005-03-25 08:53:48 +00:00
										 |  |  | 	if ((int)rrate != rate) { | 
					
						
							| 
									
										
										
										
											2001-08-10 08:56:25 +00:00
										 |  |  | 		printf("Rate doesn't match (requested %iHz, get %iHz)\n", rate, err); | 
					
						
							|  |  |  | 		return -EINVAL; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2022-11-14 20:42:10 +01:00
										 |  |  | 	/* we do not want driver wakeups */ | 
					
						
							|  |  |  | 	if (sys_latency > 0 && snd_pcm_hw_params_can_disable_period_wakeup(params)) { | 
					
						
							|  |  |  | 		err = snd_pcm_hw_params_set_period_wakeup(handle, params, 0); | 
					
						
							|  |  |  | 		if (err < 0) { | 
					
						
							|  |  |  | 			printf("Cannot disable period wakeups for %s\n", id); | 
					
						
							|  |  |  | 			return err; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2001-08-10 08:56:25 +00:00
										 |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int setparams_bufsize(snd_pcm_t *handle, | 
					
						
							|  |  |  | 		      snd_pcm_hw_params_t *params, | 
					
						
							|  |  |  | 		      snd_pcm_hw_params_t *tparams, | 
					
						
							| 
									
										
										
										
											2001-10-26 16:12:01 +00:00
										 |  |  | 		      snd_pcm_uframes_t bufsize, | 
					
						
							| 
									
										
										
										
											2001-08-10 08:56:25 +00:00
										 |  |  | 		      const char *id) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2001-10-26 16:12:01 +00:00
										 |  |  | 	int err; | 
					
						
							| 
									
										
										
										
											2002-09-19 16:12:13 +00:00
										 |  |  | 	snd_pcm_uframes_t periodsize; | 
					
						
							| 
									
										
										
										
											2001-08-10 08:56:25 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	snd_pcm_hw_params_copy(params, tparams); | 
					
						
							| 
									
										
										
										
											2002-09-19 16:12:13 +00:00
										 |  |  | 	periodsize = bufsize * 2; | 
					
						
							|  |  |  | 	err = snd_pcm_hw_params_set_buffer_size_near(handle, params, &periodsize); | 
					
						
							| 
									
										
										
										
											2001-08-10 08:56:25 +00:00
										 |  |  | 	if (err < 0) { | 
					
						
							| 
									
										
										
										
											2001-10-26 16:12:01 +00:00
										 |  |  | 		printf("Unable to set buffer size %li for %s: %s\n", bufsize * 2, id, snd_strerror(err)); | 
					
						
							| 
									
										
										
										
											2001-08-10 08:56:25 +00:00
										 |  |  | 		return err; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2005-03-25 08:53:48 +00:00
										 |  |  | 	if (period_size > 0) | 
					
						
							|  |  |  | 		periodsize = period_size; | 
					
						
							|  |  |  | 	else | 
					
						
							|  |  |  | 		periodsize /= 2; | 
					
						
							| 
									
										
										
										
											2002-09-17 12:33:05 +00:00
										 |  |  | 	err = snd_pcm_hw_params_set_period_size_near(handle, params, &periodsize, 0); | 
					
						
							| 
									
										
										
										
											2001-08-10 08:56:25 +00:00
										 |  |  | 	if (err < 0) { | 
					
						
							| 
									
										
										
										
											2001-10-26 16:12:01 +00:00
										 |  |  | 		printf("Unable to set period size %li for %s: %s\n", periodsize, id, snd_strerror(err)); | 
					
						
							| 
									
										
										
										
											2001-08-10 08:56:25 +00:00
										 |  |  | 		return err; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int setparams_set(snd_pcm_t *handle, | 
					
						
							|  |  |  | 		  snd_pcm_hw_params_t *params, | 
					
						
							|  |  |  | 		  snd_pcm_sw_params_t *swparams, | 
					
						
							|  |  |  | 		  const char *id) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2002-09-19 16:12:13 +00:00
										 |  |  | 	int err; | 
					
						
							|  |  |  | 	snd_pcm_uframes_t val; | 
					
						
							| 
									
										
										
										
											2001-08-10 08:56:25 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	err = snd_pcm_hw_params(handle, params); | 
					
						
							|  |  |  | 	if (err < 0) { | 
					
						
							|  |  |  | 		printf("Unable to set hw params for %s: %s\n", id, snd_strerror(err)); | 
					
						
							|  |  |  | 		return err; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	err = snd_pcm_sw_params_current(handle, swparams); | 
					
						
							|  |  |  | 	if (err < 0) { | 
					
						
							|  |  |  | 		printf("Unable to determine current swparams for %s: %s\n", id, snd_strerror(err)); | 
					
						
							|  |  |  | 		return err; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	err = snd_pcm_sw_params_set_start_threshold(handle, swparams, 0x7fffffff); | 
					
						
							|  |  |  | 	if (err < 0) { | 
					
						
							|  |  |  | 		printf("Unable to set start threshold mode for %s: %s\n", id, snd_strerror(err)); | 
					
						
							|  |  |  | 		return err; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2002-09-19 16:12:13 +00:00
										 |  |  | 	if (!block) | 
					
						
							|  |  |  | 		val = 4; | 
					
						
							|  |  |  | 	else | 
					
						
							|  |  |  | 		snd_pcm_hw_params_get_period_size(params, &val, NULL); | 
					
						
							| 
									
										
										
										
											2001-10-31 16:03:33 +00:00
										 |  |  | 	err = snd_pcm_sw_params_set_avail_min(handle, swparams, val); | 
					
						
							| 
									
										
										
										
											2001-08-10 08:56:25 +00:00
										 |  |  | 	if (err < 0) { | 
					
						
							|  |  |  | 		printf("Unable to set avail min for %s: %s\n", id, snd_strerror(err)); | 
					
						
							|  |  |  | 		return err; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	err = snd_pcm_sw_params(handle, swparams); | 
					
						
							|  |  |  | 	if (err < 0) { | 
					
						
							|  |  |  | 		printf("Unable to set sw params for %s: %s\n", id, snd_strerror(err)); | 
					
						
							|  |  |  | 		return err; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2000-07-18 13:38:58 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-08-10 08:56:25 +00:00
										 |  |  | int setparams(snd_pcm_t *phandle, snd_pcm_t *chandle, int *bufsize) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2001-10-26 16:12:01 +00:00
										 |  |  | 	int err, last_bufsize = *bufsize; | 
					
						
							| 
									
										
										
										
											2001-08-10 08:56:25 +00:00
										 |  |  | 	snd_pcm_hw_params_t *pt_params, *ct_params;	/* templates with rate, format and channels */ | 
					
						
							|  |  |  | 	snd_pcm_hw_params_t *p_params, *c_params; | 
					
						
							|  |  |  | 	snd_pcm_sw_params_t *p_swparams, *c_swparams; | 
					
						
							| 
									
										
										
										
											2005-03-25 08:53:48 +00:00
										 |  |  | 	snd_pcm_uframes_t p_size, c_size, p_psize, c_psize; | 
					
						
							| 
									
										
										
										
											2002-09-19 16:12:13 +00:00
										 |  |  | 	unsigned int p_time, c_time; | 
					
						
							| 
									
										
										
										
											2011-04-20 16:18:50 +08:00
										 |  |  | 	unsigned int val; | 
					
						
							| 
									
										
										
										
											2001-08-10 08:56:25 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	snd_pcm_hw_params_alloca(&p_params); | 
					
						
							|  |  |  | 	snd_pcm_hw_params_alloca(&c_params); | 
					
						
							|  |  |  | 	snd_pcm_hw_params_alloca(&pt_params); | 
					
						
							|  |  |  | 	snd_pcm_hw_params_alloca(&ct_params); | 
					
						
							|  |  |  | 	snd_pcm_sw_params_alloca(&p_swparams); | 
					
						
							|  |  |  | 	snd_pcm_sw_params_alloca(&c_swparams); | 
					
						
							|  |  |  | 	if ((err = setparams_stream(phandle, pt_params, "playback")) < 0) { | 
					
						
							|  |  |  | 		printf("Unable to set parameters for playback stream: %s\n", snd_strerror(err)); | 
					
						
							| 
									
										
										
										
											1998-11-27 00:13:09 +00:00
										 |  |  | 		exit(0); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2001-08-10 08:56:25 +00:00
										 |  |  | 	if ((err = setparams_stream(chandle, ct_params, "capture")) < 0) { | 
					
						
							|  |  |  | 		printf("Unable to set parameters for playback stream: %s\n", snd_strerror(err)); | 
					
						
							| 
									
										
										
										
											1998-11-27 00:13:09 +00:00
										 |  |  | 		exit(0); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2001-08-10 08:56:25 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-03-25 08:53:48 +00:00
										 |  |  | 	if (buffer_size > 0) { | 
					
						
							|  |  |  | 		*bufsize = buffer_size; | 
					
						
							|  |  |  | 		goto __set_it; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-08-10 08:56:25 +00:00
										 |  |  |       __again: | 
					
						
							| 
									
										
										
										
											2005-03-25 08:53:48 +00:00
										 |  |  |       	if (buffer_size > 0) | 
					
						
							|  |  |  |       		return -1; | 
					
						
							| 
									
										
										
										
											2001-10-26 16:12:01 +00:00
										 |  |  |       	if (last_bufsize == *bufsize) | 
					
						
							| 
									
										
										
										
											2001-08-10 08:56:25 +00:00
										 |  |  | 		*bufsize += 4; | 
					
						
							| 
									
										
										
										
											2001-10-26 16:12:01 +00:00
										 |  |  | 	last_bufsize = *bufsize; | 
					
						
							| 
									
										
										
										
											2001-08-10 08:56:25 +00:00
										 |  |  | 	if (*bufsize > latency_max) | 
					
						
							|  |  |  | 		return -1; | 
					
						
							| 
									
										
										
										
											2005-03-25 08:53:48 +00:00
										 |  |  |       __set_it: | 
					
						
							| 
									
										
										
										
											2001-08-10 08:56:25 +00:00
										 |  |  | 	if ((err = setparams_bufsize(phandle, p_params, pt_params, *bufsize, "playback")) < 0) { | 
					
						
							| 
									
										
										
										
											2024-01-31 14:54:30 -05:00
										 |  |  | 		printf("Unable to set hw parameters for playback stream: %s\n", snd_strerror(err)); | 
					
						
							| 
									
										
										
										
											1998-11-27 00:13:09 +00:00
										 |  |  | 		exit(0); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2001-08-10 08:56:25 +00:00
										 |  |  | 	if ((err = setparams_bufsize(chandle, c_params, ct_params, *bufsize, "capture")) < 0) { | 
					
						
							| 
									
										
										
										
											2024-01-31 14:54:30 -05:00
										 |  |  | 		printf("Unable to set hw parameters for capture stream: %s\n", snd_strerror(err)); | 
					
						
							| 
									
										
										
										
											1998-11-27 00:13:09 +00:00
										 |  |  | 		exit(0); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2001-10-26 16:12:01 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-03-25 08:53:48 +00:00
										 |  |  | 	snd_pcm_hw_params_get_period_size(p_params, &p_psize, NULL); | 
					
						
							|  |  |  | 	if (p_psize > (unsigned int)*bufsize) | 
					
						
							|  |  |  | 		*bufsize = p_psize; | 
					
						
							|  |  |  | 	snd_pcm_hw_params_get_period_size(c_params, &c_psize, NULL); | 
					
						
							|  |  |  | 	if (c_psize > (unsigned int)*bufsize) | 
					
						
							|  |  |  | 		*bufsize = c_psize; | 
					
						
							| 
									
										
										
										
											2002-09-19 16:12:13 +00:00
										 |  |  | 	snd_pcm_hw_params_get_period_time(p_params, &p_time, NULL); | 
					
						
							|  |  |  | 	snd_pcm_hw_params_get_period_time(c_params, &c_time, NULL); | 
					
						
							|  |  |  | 	if (p_time != c_time) | 
					
						
							| 
									
										
										
										
											2001-11-16 10:59:29 +00:00
										 |  |  | 		goto __again; | 
					
						
							| 
									
										
										
										
											2002-09-19 16:12:13 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	snd_pcm_hw_params_get_buffer_size(p_params, &p_size); | 
					
						
							| 
									
										
										
										
											2022-11-14 20:42:10 +01:00
										 |  |  | 	playback_buffer_size = p_size; | 
					
						
							| 
									
										
										
										
											2011-04-20 16:18:50 +08:00
										 |  |  | 	if (p_psize * 2 < p_size) { | 
					
						
							|  |  |  |                 snd_pcm_hw_params_get_periods_min(p_params, &val, NULL); | 
					
						
							|  |  |  |                 if (val > 2) { | 
					
						
							|  |  |  | 			printf("playback device does not support 2 periods per buffer\n"); | 
					
						
							|  |  |  | 			exit(0); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2001-11-16 10:59:29 +00:00
										 |  |  | 		goto __again; | 
					
						
							| 
									
										
										
										
											2011-04-20 16:18:50 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2002-09-19 16:12:13 +00:00
										 |  |  | 	snd_pcm_hw_params_get_buffer_size(c_params, &c_size); | 
					
						
							| 
									
										
										
										
											2011-04-20 16:18:50 +08:00
										 |  |  | 	if (c_psize * 2 < c_size) { | 
					
						
							|  |  |  |                 snd_pcm_hw_params_get_periods_min(c_params, &val, NULL); | 
					
						
							|  |  |  | 		if (val > 2 ) { | 
					
						
							|  |  |  | 			printf("capture device does not support 2 periods per buffer\n"); | 
					
						
							|  |  |  | 			exit(0); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											1999-11-06 23:47:07 +00:00
										 |  |  | 		goto __again; | 
					
						
							| 
									
										
										
										
											2011-04-20 16:18:50 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2001-08-10 08:56:25 +00:00
										 |  |  | 	if ((err = setparams_set(phandle, p_params, p_swparams, "playback")) < 0) { | 
					
						
							|  |  |  | 		printf("Unable to set sw parameters for playback stream: %s\n", snd_strerror(err)); | 
					
						
							|  |  |  | 		exit(0); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if ((err = setparams_set(chandle, c_params, c_swparams, "capture")) < 0) { | 
					
						
							|  |  |  | 		printf("Unable to set sw parameters for playback stream: %s\n", snd_strerror(err)); | 
					
						
							|  |  |  | 		exit(0); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-07-18 13:38:58 +00:00
										 |  |  | 	if ((err = snd_pcm_prepare(phandle)) < 0) { | 
					
						
							| 
									
										
										
										
											2001-08-10 08:56:25 +00:00
										 |  |  | 		printf("Prepare error: %s\n", snd_strerror(err)); | 
					
						
							| 
									
										
										
										
											1998-11-27 00:13:09 +00:00
										 |  |  | 		exit(0); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2000-07-24 09:17:14 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-08-10 08:56:25 +00:00
										 |  |  | 	snd_pcm_dump(phandle, output); | 
					
						
							|  |  |  | 	snd_pcm_dump(chandle, output); | 
					
						
							| 
									
										
										
										
											1999-11-06 23:47:07 +00:00
										 |  |  | 	fflush(stdout); | 
					
						
							|  |  |  | 	return 0; | 
					
						
							| 
									
										
										
										
											1998-11-27 00:13:09 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-08-10 08:56:25 +00:00
										 |  |  | void showstat(snd_pcm_t *handle, size_t frames) | 
					
						
							| 
									
										
										
										
											1998-11-27 00:13:09 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	int err; | 
					
						
							| 
									
										
										
										
											2001-08-10 08:56:25 +00:00
										 |  |  | 	snd_pcm_status_t *status; | 
					
						
							| 
									
										
										
										
											1998-11-27 00:13:09 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-08-10 08:56:25 +00:00
										 |  |  | 	snd_pcm_status_alloca(&status); | 
					
						
							|  |  |  | 	if ((err = snd_pcm_status(handle, status)) < 0) { | 
					
						
							| 
									
										
										
										
											2000-07-18 13:38:58 +00:00
										 |  |  | 		printf("Stream status error: %s\n", snd_strerror(err)); | 
					
						
							| 
									
										
										
										
											1998-11-27 00:13:09 +00:00
										 |  |  | 		exit(0); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2001-08-10 08:56:25 +00:00
										 |  |  | 	printf("*** frames = %li ***\n", (long)frames); | 
					
						
							|  |  |  | 	snd_pcm_status_dump(status, output); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-10-31 16:03:33 +00:00
										 |  |  | void showlatency(size_t latency) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	double d; | 
					
						
							|  |  |  | 	latency *= 2; | 
					
						
							|  |  |  | 	d = (double)latency / (double)rate; | 
					
						
							|  |  |  | 	printf("Trying latency %li frames, %.3fus, %.6fms (%.4fHz)\n", (long)latency, d * 1000000, d * 1000, (double)1 / d); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void showinmax(size_t in_max) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	double d; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	printf("Maximum read: %li frames\n", (long)in_max); | 
					
						
							|  |  |  | 	d = (double)in_max / (double)rate; | 
					
						
							|  |  |  | 	printf("Maximum read latency: %.3fus, %.6fms (%.4fHz)\n", d * 1000000, d * 1000, (double)1 / d); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-08-10 08:56:25 +00:00
										 |  |  | void gettimestamp(snd_pcm_t *handle, snd_timestamp_t *timestamp) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	int err; | 
					
						
							|  |  |  | 	snd_pcm_status_t *status; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	snd_pcm_status_alloca(&status); | 
					
						
							|  |  |  | 	if ((err = snd_pcm_status(handle, status)) < 0) { | 
					
						
							|  |  |  | 		printf("Stream status error: %s\n", snd_strerror(err)); | 
					
						
							|  |  |  | 		exit(0); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	snd_pcm_status_get_trigger_tstamp(status, timestamp); | 
					
						
							| 
									
										
										
										
											1998-11-27 00:13:09 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void setscheduler(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	struct sched_param sched_param; | 
					
						
							| 
									
										
										
										
											2022-11-24 08:33:47 +01:00
										 |  |  | 	int policy = SCHED_RR; | 
					
						
							|  |  |  | 	const char *spolicy = "Round Robin"; | 
					
						
							| 
									
										
										
										
											1998-11-27 00:13:09 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-24 08:33:47 +01:00
										 |  |  | 	if (strcasecmp(sched_policy, "fifo") == 0) { | 
					
						
							|  |  |  | 		policy = SCHED_FIFO; | 
					
						
							|  |  |  | 		spolicy = "FIFO"; | 
					
						
							| 
									
										
										
										
											2022-11-24 10:55:32 +01:00
										 |  |  | 	} else if (strcasecmp(sched_policy, "other") == 0) { | 
					
						
							|  |  |  | 		policy = SCHED_OTHER; | 
					
						
							|  |  |  | 		spolicy = "OTHER"; | 
					
						
							| 
									
										
										
										
											2022-11-24 08:33:47 +01:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											1998-11-27 15:07:56 +00:00
										 |  |  | 	if (sched_getparam(0, &sched_param) < 0) { | 
					
						
							| 
									
										
										
										
											1998-11-27 00:13:09 +00:00
										 |  |  | 		printf("Scheduler getparam failed...\n"); | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2022-11-24 08:33:47 +01:00
										 |  |  | 	sched_param.sched_priority = sched_get_priority_max(policy); | 
					
						
							|  |  |  | 	if (!sched_setscheduler(0, policy, &sched_param)) { | 
					
						
							|  |  |  | 		printf("Scheduler set to %s with priority %i...\n", spolicy, sched_param.sched_priority); | 
					
						
							| 
									
										
										
										
											1998-11-27 00:13:09 +00:00
										 |  |  | 		fflush(stdout); | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2022-11-24 08:33:47 +01:00
										 |  |  | 	printf("!!!Scheduler set to %s with priority %i FAILED!!!\n", spolicy, sched_param.sched_priority); | 
					
						
							| 
									
										
										
										
											1998-11-27 00:13:09 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-08-10 08:56:25 +00:00
										 |  |  | long timediff(snd_timestamp_t t1, snd_timestamp_t t2) | 
					
						
							| 
									
										
										
										
											1998-11-27 00:13:09 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	signed long l; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	t1.tv_sec -= t2.tv_sec; | 
					
						
							| 
									
										
										
										
											1998-11-27 15:07:56 +00:00
										 |  |  | 	l = (signed long) t1.tv_usec - (signed long) t2.tv_usec; | 
					
						
							| 
									
										
										
										
											1998-11-27 00:13:09 +00:00
										 |  |  | 	if (l < 0) { | 
					
						
							|  |  |  | 		t1.tv_sec--; | 
					
						
							| 
									
										
										
										
											2010-08-04 19:47:01 +02:00
										 |  |  | 		l = 1000000 + l; | 
					
						
							| 
									
										
										
										
											1998-11-27 00:13:09 +00:00
										 |  |  | 		l %= 1000000; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return (t1.tv_sec * 1000000) + l; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-10-31 16:03:33 +00:00
										 |  |  | long readbuf(snd_pcm_t *handle, char *buf, long len, size_t *frames, size_t *max) | 
					
						
							| 
									
										
										
										
											1999-11-06 23:47:07 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	long r; | 
					
						
							| 
									
										
										
										
											2001-10-26 16:12:01 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (!block) { | 
					
						
							|  |  |  | 		do { | 
					
						
							|  |  |  | 			r = snd_pcm_readi(handle, buf, len); | 
					
						
							|  |  |  | 		} while (r == -EAGAIN); | 
					
						
							| 
									
										
										
										
											2001-10-31 16:03:33 +00:00
										 |  |  | 		if (r > 0) { | 
					
						
							| 
									
										
										
										
											2001-10-26 16:12:01 +00:00
										 |  |  | 			*frames += r; | 
					
						
							| 
									
										
										
										
											2005-03-25 08:53:48 +00:00
										 |  |  | 			if ((long)*max < r) | 
					
						
							| 
									
										
										
										
											2001-10-31 16:03:33 +00:00
										 |  |  | 				*max = r; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2001-10-26 16:12:01 +00:00
										 |  |  | 		// printf("read = %li\n", r);
 | 
					
						
							|  |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2022-11-14 12:36:04 +01:00
										 |  |  | 		int frame_bytes = (snd_pcm_format_physical_width(format) / 8) * channels; | 
					
						
							| 
									
										
										
										
											2001-10-26 16:12:01 +00:00
										 |  |  | 		do { | 
					
						
							|  |  |  | 			r = snd_pcm_readi(handle, buf, len); | 
					
						
							|  |  |  | 			if (r > 0) { | 
					
						
							|  |  |  | 				buf += r * frame_bytes; | 
					
						
							|  |  |  | 				len -= r; | 
					
						
							|  |  |  | 				*frames += r; | 
					
						
							| 
									
										
										
										
											2005-03-25 08:53:48 +00:00
										 |  |  | 				if ((long)*max < r) | 
					
						
							| 
									
										
										
										
											2001-10-31 16:03:33 +00:00
										 |  |  | 					*max = r; | 
					
						
							| 
									
										
										
										
											2001-10-26 16:12:01 +00:00
										 |  |  | 			} | 
					
						
							|  |  |  | 			// printf("r = %li, len = %li\n", r, len);
 | 
					
						
							|  |  |  | 		} while (r >= 1 && len > 0); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2001-08-10 08:56:25 +00:00
										 |  |  | 	// showstat(handle, 0);
 | 
					
						
							| 
									
										
										
										
											1999-11-06 23:47:07 +00:00
										 |  |  | 	return r; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-07-18 13:38:58 +00:00
										 |  |  | long writebuf(snd_pcm_t *handle, char *buf, long len, size_t *frames) | 
					
						
							| 
									
										
										
										
											1999-11-06 23:47:07 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	long r; | 
					
						
							| 
									
										
										
										
											2022-11-14 12:36:04 +01:00
										 |  |  | 	int frame_bytes = (snd_pcm_format_physical_width(format) / 8) * channels; | 
					
						
							| 
									
										
										
										
											1999-11-06 23:47:07 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	while (len > 0) { | 
					
						
							| 
									
										
										
										
											2000-09-24 09:57:26 +00:00
										 |  |  | 		r = snd_pcm_writei(handle, buf, len); | 
					
						
							| 
									
										
										
										
											1999-11-19 19:05:22 +00:00
										 |  |  | 		if (r == -EAGAIN) | 
					
						
							|  |  |  | 			continue; | 
					
						
							|  |  |  | 		// printf("write = %li\n", r);
 | 
					
						
							| 
									
										
										
										
											1999-11-06 23:47:07 +00:00
										 |  |  | 		if (r < 0) | 
					
						
							|  |  |  | 			return r; | 
					
						
							| 
									
										
										
										
											2001-08-10 08:56:25 +00:00
										 |  |  | 		// showstat(handle, 0);
 | 
					
						
							| 
									
										
										
										
											2019-03-01 12:43:19 +01:00
										 |  |  | 		buf += r * frame_bytes; | 
					
						
							| 
									
										
										
										
											1999-11-06 23:47:07 +00:00
										 |  |  | 		len -= r; | 
					
						
							| 
									
										
										
										
											2000-07-18 13:38:58 +00:00
										 |  |  | 		*frames += r; | 
					
						
							| 
									
										
										
										
											1999-11-06 23:47:07 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2022-11-14 20:42:10 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-10-31 16:03:33 +00:00
										 |  |  | #define FILTERSWEEP_LFO_CENTER 2000.
 | 
					
						
							|  |  |  | #define FILTERSWEEP_LFO_DEPTH 1800.
 | 
					
						
							|  |  |  | #define FILTERSWEEP_LFO_FREQ 0.2
 | 
					
						
							|  |  |  | #define FILTER_BANDWIDTH 50
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-03-12 20:14:33 +00:00
										 |  |  | /* filter the sweep variables */ | 
					
						
							| 
									
										
										
										
											2001-10-31 16:03:33 +00:00
										 |  |  | float lfo,dlfo,fs,fc,BW,C,D,a0,a1,a2,b1,b2,*x[3],*y[3]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void applyeffect(char* buffer,int r) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	short* samples = (short*) buffer; | 
					
						
							|  |  |  | 	int i; | 
					
						
							|  |  |  | 	for (i=0;i<r;i++) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		int chn; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		fc = sin(lfo)*FILTERSWEEP_LFO_DEPTH+FILTERSWEEP_LFO_CENTER; | 
					
						
							|  |  |  | 		lfo += dlfo; | 
					
						
							|  |  |  | 		if (lfo>2.*M_PI) lfo -= 2.*M_PI; | 
					
						
							|  |  |  | 		C = 1./tan(M_PI*BW/fs); | 
					
						
							|  |  |  | 		D = 2.*cos(2*M_PI*fc/fs); | 
					
						
							|  |  |  | 		a0 = 1./(1.+C); | 
					
						
							|  |  |  | 		a1 = 0; | 
					
						
							|  |  |  | 		a2 = -a0; | 
					
						
							|  |  |  | 		b1 = -C*D*a0; | 
					
						
							|  |  |  | 		b2 = (C-1)*a0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		for (chn=0;chn<channels;chn++) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			x[chn][2] = x[chn][1]; | 
					
						
							|  |  |  | 			x[chn][1] = x[chn][0]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			y[chn][2] = y[chn][1]; | 
					
						
							|  |  |  | 			y[chn][1] = y[chn][0]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			x[chn][0] = samples[i*channels+chn]; | 
					
						
							|  |  |  | 			y[chn][0] = a0*x[chn][0] + a1*x[chn][1] + a2*x[chn][2]  | 
					
						
							|  |  |  | 				- b1*y[chn][1] - b2*y[chn][2]; | 
					
						
							|  |  |  | 			samples[i*channels+chn] = y[chn][0]; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											1999-11-06 23:47:07 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-14 20:42:10 +01:00
										 |  |  | static ssize_t get_avail(snd_pcm_t *pcm) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	ssize_t avail; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	while (1) { | 
					
						
							|  |  |  | 		avail = snd_pcm_avail(pcm); | 
					
						
							|  |  |  | 		if (avail == -EAGAIN) | 
					
						
							|  |  |  | 			continue; | 
					
						
							|  |  |  | 		break; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return avail; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-08-10 16:44:08 +00:00
										 |  |  | void help(void) | 
					
						
							| 
									
										
										
										
											1998-11-27 00:13:09 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2001-08-10 16:44:08 +00:00
										 |  |  | 	int k; | 
					
						
							| 
									
										
										
										
											2003-07-14 10:22:39 +00:00
										 |  |  | 	printf( | 
					
						
							|  |  |  | "Usage: latency [OPTION]... [FILE]...\n" | 
					
						
							|  |  |  | "-h,--help      help\n" | 
					
						
							|  |  |  | "-P,--pdevice   playback device\n" | 
					
						
							|  |  |  | "-C,--cdevice   capture device\n" | 
					
						
							|  |  |  | "-m,--min       minimum latency in frames\n" | 
					
						
							|  |  |  | "-M,--max       maximum latency in frames\n" | 
					
						
							| 
									
										
										
										
											2022-11-14 20:42:10 +01:00
										 |  |  | "-U,--updates   I/O updates in milliseconds (0 = off)\n" | 
					
						
							| 
									
										
										
										
											2003-07-14 10:22:39 +00:00
										 |  |  | "-F,--frames    frames to transfer\n" | 
					
						
							|  |  |  | "-f,--format    sample format\n" | 
					
						
							|  |  |  | "-c,--channels  channels\n" | 
					
						
							|  |  |  | "-r,--rate      rate\n" | 
					
						
							| 
									
										
										
										
											2005-03-25 08:53:48 +00:00
										 |  |  | "-B,--buffer    buffer size in frames\n" | 
					
						
							|  |  |  | "-E,--period    period size in frames\n" | 
					
						
							| 
									
										
										
										
											2003-07-14 10:22:39 +00:00
										 |  |  | "-s,--seconds   duration of test in seconds\n" | 
					
						
							|  |  |  | "-b,--block     block mode\n" | 
					
						
							|  |  |  | "-p,--poll      use poll (wait for event - reduces CPU usage)\n" | 
					
						
							| 
									
										
										
										
											2022-11-23 17:56:20 +01:00
										 |  |  | "-y,--usleep    sleep for the specified amount of microseconds between\n" | 
					
						
							|  |  |  | "               stream updates (default 0 - off)\n" | 
					
						
							| 
									
										
										
										
											2003-07-14 10:22:39 +00:00
										 |  |  | "-e,--effect    apply an effect (bandpass filter sweep)\n" | 
					
						
							| 
									
										
										
										
											2022-11-14 20:42:10 +01:00
										 |  |  | "-x,--posdump   dump buffer positions\n" | 
					
						
							|  |  |  | "-X,--realtime  do a realtime check (buffering)\n" | 
					
						
							| 
									
										
										
										
											2022-11-24 10:55:32 +01:00
										 |  |  | "-O,--policy    set scheduler policy (RR, FIFO or OTHER)\n" | 
					
						
							| 
									
										
										
										
											2003-07-14 10:22:39 +00:00
										 |  |  | ); | 
					
						
							| 
									
										
										
										
											2001-08-10 16:44:08 +00:00
										 |  |  |         printf("Recognized sample formats are:"); | 
					
						
							| 
									
										
										
										
											2005-11-21 10:48:52 +00:00
										 |  |  |         for (k = 0; k < SND_PCM_FORMAT_LAST; ++k) { | 
					
						
							| 
									
										
										
										
											2001-08-10 16:44:08 +00:00
										 |  |  |                 const char *s = snd_pcm_format_name(k); | 
					
						
							|  |  |  |                 if (s) | 
					
						
							|  |  |  |                         printf(" %s", s); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2001-10-31 16:03:33 +00:00
										 |  |  |         printf("\n\n"); | 
					
						
							| 
									
										
										
										
											2003-07-14 10:22:39 +00:00
										 |  |  |         printf( | 
					
						
							|  |  |  | "Tip #1 (usable latency with large periods, non-blocking mode, good CPU usage,\n" | 
					
						
							|  |  |  | "        superb xrun prevention):\n" | 
					
						
							|  |  |  | "  latency -m 8192 -M 8192 -t 1 -p\n" | 
					
						
							|  |  |  | "Tip #2 (superb latency, non-blocking mode, but heavy CPU usage):\n" | 
					
						
							|  |  |  | "  latency -m 128 -M 128\n" | 
					
						
							|  |  |  | ); | 
					
						
							| 
									
										
										
										
											2001-08-10 16:44:08 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int main(int argc, char *argv[]) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	struct option long_option[] = | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		{"help", 0, NULL, 'h'}, | 
					
						
							|  |  |  | 		{"pdevice", 1, NULL, 'P'}, | 
					
						
							|  |  |  | 		{"cdevice", 1, NULL, 'C'}, | 
					
						
							|  |  |  | 		{"min", 1, NULL, 'm'}, | 
					
						
							|  |  |  | 		{"max", 1, NULL, 'M'}, | 
					
						
							| 
									
										
										
										
											2022-11-14 20:42:10 +01:00
										 |  |  | 		{"updates", 1, NULL, 'U'}, | 
					
						
							| 
									
										
										
										
											2001-08-10 16:44:08 +00:00
										 |  |  | 		{"frames", 1, NULL, 'F'}, | 
					
						
							|  |  |  | 		{"format", 1, NULL, 'f'}, | 
					
						
							|  |  |  | 		{"channels", 1, NULL, 'c'}, | 
					
						
							|  |  |  | 		{"rate", 1, NULL, 'r'}, | 
					
						
							| 
									
										
										
										
											2005-03-25 08:53:48 +00:00
										 |  |  | 		{"buffer", 1, NULL, 'B'}, | 
					
						
							|  |  |  | 		{"period", 1, NULL, 'E'}, | 
					
						
							| 
									
										
										
										
											2001-10-15 12:36:55 +00:00
										 |  |  | 		{"seconds", 1, NULL, 's'}, | 
					
						
							| 
									
										
										
										
											2001-10-26 16:12:01 +00:00
										 |  |  | 		{"block", 0, NULL, 'b'}, | 
					
						
							| 
									
										
										
										
											2001-10-31 16:03:33 +00:00
										 |  |  | 		{"poll", 0, NULL, 'p'}, | 
					
						
							| 
									
										
										
										
											2022-11-23 17:56:20 +01:00
										 |  |  | 		{"usleep", 1, NULL, 'y'}, | 
					
						
							| 
									
										
										
										
											2001-10-31 16:03:33 +00:00
										 |  |  | 		{"effect", 0, NULL, 'e'}, | 
					
						
							| 
									
										
										
										
											2022-11-14 20:42:10 +01:00
										 |  |  | 		{"posdump", 0, NULL, 'x'}, | 
					
						
							|  |  |  | 		{"realtime", 0, NULL, 'X'}, | 
					
						
							| 
									
										
										
										
											2022-11-24 08:33:47 +01:00
										 |  |  | 		{"policy", 1, NULL, 'O'}, | 
					
						
							| 
									
										
										
										
											2001-08-10 16:44:08 +00:00
										 |  |  | 		{NULL, 0, NULL, 0}, | 
					
						
							|  |  |  | 	}; | 
					
						
							| 
									
										
										
										
											1999-11-06 23:47:07 +00:00
										 |  |  | 	snd_pcm_t *phandle, *chandle; | 
					
						
							| 
									
										
										
										
											2001-08-10 08:56:25 +00:00
										 |  |  | 	char *buffer; | 
					
						
							| 
									
										
										
										
											2001-08-10 16:44:08 +00:00
										 |  |  | 	int err, latency, morehelp; | 
					
						
							| 
									
										
										
										
											2022-11-14 21:26:39 +01:00
										 |  |  | 	int ok; | 
					
						
							| 
									
										
										
										
											2001-08-10 08:56:25 +00:00
										 |  |  | 	snd_timestamp_t p_tstamp, c_tstamp; | 
					
						
							| 
									
										
										
										
											2022-11-14 20:42:10 +01:00
										 |  |  | 	ssize_t r, cap_avail, cap_avail_max, pbk_fill, pbk_fill_min; | 
					
						
							| 
									
										
										
										
											2001-10-31 16:03:33 +00:00
										 |  |  | 	size_t frames_in, frames_out, in_max; | 
					
						
							| 
									
										
										
										
											2022-11-14 20:42:10 +01:00
										 |  |  | 	timestamp_t tstamp_start; | 
					
						
							| 
									
										
										
										
											2001-10-31 16:03:33 +00:00
										 |  |  | 	int effect = 0; | 
					
						
							| 
									
										
										
										
											2001-08-10 16:44:08 +00:00
										 |  |  | 	morehelp = 0; | 
					
						
							|  |  |  | 	while (1) { | 
					
						
							|  |  |  | 		int c; | 
					
						
							| 
									
										
										
										
											2022-11-24 08:33:47 +01:00
										 |  |  | 		if ((c = getopt_long(argc, argv, "hP:C:m:M:U:F:f:c:r:B:E:s:y:O:bpenxX", long_option, NULL)) < 0) | 
					
						
							| 
									
										
										
										
											2001-08-10 16:44:08 +00:00
										 |  |  | 			break; | 
					
						
							|  |  |  | 		switch (c) { | 
					
						
							|  |  |  | 		case 'h': | 
					
						
							|  |  |  | 			morehelp++; | 
					
						
							|  |  |  | 			break; | 
					
						
							|  |  |  | 		case 'P': | 
					
						
							|  |  |  | 			pdevice = strdup(optarg); | 
					
						
							|  |  |  | 			break; | 
					
						
							|  |  |  | 		case 'C': | 
					
						
							|  |  |  | 			cdevice = strdup(optarg); | 
					
						
							|  |  |  | 			break; | 
					
						
							|  |  |  | 		case 'm': | 
					
						
							| 
									
										
										
										
											2001-10-31 16:03:33 +00:00
										 |  |  | 			err = atoi(optarg) / 2; | 
					
						
							| 
									
										
										
										
											2001-08-10 16:44:08 +00:00
										 |  |  | 			latency_min = err >= 4 ? err : 4; | 
					
						
							|  |  |  | 			if (latency_max < latency_min) | 
					
						
							|  |  |  | 				latency_max = latency_min; | 
					
						
							|  |  |  | 			break; | 
					
						
							|  |  |  | 		case 'M': | 
					
						
							| 
									
										
										
										
											2001-10-31 16:03:33 +00:00
										 |  |  | 			err = atoi(optarg) / 2; | 
					
						
							| 
									
										
										
										
											2001-08-10 16:44:08 +00:00
										 |  |  | 			latency_max = latency_min > err ? latency_min : err; | 
					
						
							|  |  |  | 			break; | 
					
						
							| 
									
										
										
										
											2022-11-14 20:42:10 +01:00
										 |  |  | 		case 'U': | 
					
						
							|  |  |  | 			err = atoi(optarg); | 
					
						
							|  |  |  | 			sys_latency = err <= 0 ? 0 : err; | 
					
						
							|  |  |  | 			break; | 
					
						
							| 
									
										
										
										
											2003-02-17 18:01:16 +00:00
										 |  |  | 		case 'f': | 
					
						
							| 
									
										
										
										
											2001-08-10 16:44:08 +00:00
										 |  |  | 			format = snd_pcm_format_value(optarg); | 
					
						
							|  |  |  | 			if (format == SND_PCM_FORMAT_UNKNOWN) { | 
					
						
							|  |  |  | 				printf("Unknown format, setting to default S16_LE\n"); | 
					
						
							|  |  |  | 				format = SND_PCM_FORMAT_S16_LE; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			break; | 
					
						
							|  |  |  | 		case 'c': | 
					
						
							|  |  |  | 			err = atoi(optarg); | 
					
						
							|  |  |  | 			channels = err >= 1 && err < 1024 ? err : 1; | 
					
						
							|  |  |  | 			break; | 
					
						
							|  |  |  | 		case 'r': | 
					
						
							|  |  |  | 			err = atoi(optarg); | 
					
						
							|  |  |  | 			rate = err >= 4000 && err < 200000 ? err : 44100; | 
					
						
							|  |  |  | 			break; | 
					
						
							| 
									
										
										
										
											2005-03-25 08:53:48 +00:00
										 |  |  | 		case 'B': | 
					
						
							|  |  |  | 			err = atoi(optarg); | 
					
						
							|  |  |  | 			buffer_size = err >= 32 && err < 200000 ? err : 0; | 
					
						
							|  |  |  | 			break; | 
					
						
							|  |  |  | 		case 'E': | 
					
						
							|  |  |  | 			err = atoi(optarg); | 
					
						
							|  |  |  | 			period_size = err >= 32 && err < 200000 ? err : 0; | 
					
						
							|  |  |  | 			break; | 
					
						
							| 
									
										
										
										
											2001-10-15 12:36:55 +00:00
										 |  |  | 		case 's': | 
					
						
							|  |  |  | 			err = atoi(optarg); | 
					
						
							|  |  |  | 			loop_sec = err >= 1 && err <= 100000 ? err : 30; | 
					
						
							|  |  |  | 			break; | 
					
						
							| 
									
										
										
										
											2001-10-26 16:12:01 +00:00
										 |  |  | 		case 'b': | 
					
						
							|  |  |  | 			block = 1; | 
					
						
							|  |  |  | 			break; | 
					
						
							| 
									
										
										
										
											2001-10-31 16:03:33 +00:00
										 |  |  | 		case 'p': | 
					
						
							|  |  |  | 			use_poll = 1; | 
					
						
							|  |  |  | 			break; | 
					
						
							| 
									
										
										
										
											2022-11-23 17:56:20 +01:00
										 |  |  | 		case 'y': | 
					
						
							|  |  |  | 			usleep_val = atoi(optarg); | 
					
						
							|  |  |  | 			break; | 
					
						
							| 
									
										
										
										
											2001-10-31 16:03:33 +00:00
										 |  |  | 		case 'e': | 
					
						
							|  |  |  | 			effect = 1; | 
					
						
							|  |  |  | 			break; | 
					
						
							| 
									
										
										
										
											2005-04-12 12:09:55 +00:00
										 |  |  | 		case 'n': | 
					
						
							|  |  |  | 			resample = 0; | 
					
						
							|  |  |  | 			break; | 
					
						
							| 
									
										
										
										
											2022-11-14 20:42:10 +01:00
										 |  |  | 		case 'x': | 
					
						
							|  |  |  | 			pos_dump = 1; | 
					
						
							|  |  |  | 			break; | 
					
						
							|  |  |  | 		case 'X': | 
					
						
							|  |  |  | 			realtime_check = 1; | 
					
						
							|  |  |  | 			break; | 
					
						
							| 
									
										
										
										
											2022-11-24 08:33:47 +01:00
										 |  |  | 		case 'O': | 
					
						
							|  |  |  | 			sched_policy = optarg; | 
					
						
							|  |  |  | 			break; | 
					
						
							| 
									
										
										
										
											2001-08-10 16:44:08 +00:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (morehelp) { | 
					
						
							|  |  |  | 		help(); | 
					
						
							|  |  |  | 		return 0; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2001-08-10 08:56:25 +00:00
										 |  |  | 	err = snd_output_stdio_attach(&output, stdout, 0); | 
					
						
							|  |  |  | 	if (err < 0) { | 
					
						
							|  |  |  | 		printf("Output failed: %s\n", snd_strerror(err)); | 
					
						
							|  |  |  | 		return 0; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	loop_limit = loop_sec * rate; | 
					
						
							|  |  |  | 	latency = latency_min - 4; | 
					
						
							| 
									
										
										
										
											2022-11-14 20:42:10 +01:00
										 |  |  | 	buffer = malloc((latency_max * 2 * snd_pcm_format_physical_width(format) / 8) * channels); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* I/O updates based on a system timer */ | 
					
						
							|  |  |  | 	if (sys_latency > 0) { | 
					
						
							|  |  |  | 		block = 0; | 
					
						
							|  |  |  | 		use_poll = 0; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2001-08-10 08:56:25 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-11-27 00:13:09 +00:00
										 |  |  | 	setscheduler(); | 
					
						
							| 
									
										
										
										
											2001-08-10 16:44:08 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	printf("Playback device is %s\n", pdevice); | 
					
						
							|  |  |  | 	printf("Capture device is %s\n", cdevice); | 
					
						
							| 
									
										
										
										
											2022-11-14 20:42:10 +01:00
										 |  |  | 	printf("Parameters are %iHz, %s, %i channels, %s mode, use poll %s\n", | 
					
						
							|  |  |  | 			rate, snd_pcm_format_name(format), | 
					
						
							|  |  |  | 			channels, block ? "blocking" : "non-blocking", | 
					
						
							|  |  |  | 			use_poll ? "yes" : "no"); | 
					
						
							|  |  |  | 	printf("Loop limit is %lu frames, minimum latency = %i, maximum latency = %i", | 
					
						
							|  |  |  | 			loop_limit, latency_min * 2, latency_max * 2); | 
					
						
							|  |  |  | 	if (sys_latency > 0) | 
					
						
							|  |  |  | 		printf(", I/O updates %ims", sys_latency); | 
					
						
							| 
									
										
										
										
											2022-11-23 19:45:15 +01:00
										 |  |  | 	else if (!block && !use_poll) | 
					
						
							| 
									
										
										
										
											2022-11-23 17:56:20 +01:00
										 |  |  | 		printf(", I/O usleep %ius", usleep_val); | 
					
						
							| 
									
										
										
										
											2022-11-14 20:42:10 +01:00
										 |  |  | 	printf("\n"); | 
					
						
							| 
									
										
										
										
											2001-08-10 16:44:08 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-10-26 16:12:01 +00:00
										 |  |  | 	if ((err = snd_pcm_open(&phandle, pdevice, SND_PCM_STREAM_PLAYBACK, block ? 0 : SND_PCM_NONBLOCK)) < 0) { | 
					
						
							| 
									
										
										
										
											1998-11-27 00:13:09 +00:00
										 |  |  | 		printf("Playback open error: %s\n", snd_strerror(err)); | 
					
						
							| 
									
										
										
										
											1999-06-03 21:41:29 +00:00
										 |  |  | 		return 0; | 
					
						
							| 
									
										
										
										
											1998-11-27 00:13:09 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2001-10-26 16:12:01 +00:00
										 |  |  | 	if ((err = snd_pcm_open(&chandle, cdevice, SND_PCM_STREAM_CAPTURE, block ? 0 : SND_PCM_NONBLOCK)) < 0) { | 
					
						
							| 
									
										
										
										
											1998-11-27 15:07:56 +00:00
										 |  |  | 		printf("Record open error: %s\n", snd_strerror(err)); | 
					
						
							| 
									
										
										
										
											1999-06-03 21:41:29 +00:00
										 |  |  | 		return 0; | 
					
						
							| 
									
										
										
										
											1998-11-27 00:13:09 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2001-10-31 16:03:33 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-03-12 20:14:33 +00:00
										 |  |  | 	/* initialize the filter sweep variables */ | 
					
						
							| 
									
										
										
										
											2001-10-31 16:03:33 +00:00
										 |  |  | 	if (effect) { | 
					
						
							|  |  |  | 		fs = (float) rate; | 
					
						
							|  |  |  | 		BW = FILTER_BANDWIDTH; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		lfo = 0; | 
					
						
							|  |  |  | 		dlfo = 2.*M_PI*FILTERSWEEP_LFO_FREQ/fs; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		x[0] = (float*) malloc(channels*sizeof(float));		 | 
					
						
							|  |  |  | 		x[1] = (float*) malloc(channels*sizeof(float));		 | 
					
						
							|  |  |  | 		x[2] = (float*) malloc(channels*sizeof(float));		 | 
					
						
							|  |  |  | 		y[0] = (float*) malloc(channels*sizeof(float));		 | 
					
						
							|  |  |  | 		y[1] = (float*) malloc(channels*sizeof(float));		 | 
					
						
							|  |  |  | 		y[2] = (float*) malloc(channels*sizeof(float));		 | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2022-11-14 20:42:10 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	cap_avail_max = 0; | 
					
						
							|  |  |  | 	pbk_fill_min = latency * 2; | 
					
						
							| 
									
										
										
										
											2001-10-31 16:03:33 +00:00
										 |  |  | 			   | 
					
						
							| 
									
										
										
										
											1998-11-27 00:13:09 +00:00
										 |  |  | 	while (1) { | 
					
						
							| 
									
										
										
										
											2000-07-18 13:38:58 +00:00
										 |  |  | 		frames_in = frames_out = 0; | 
					
						
							| 
									
										
										
										
											2000-07-24 09:17:14 +00:00
										 |  |  | 		if (setparams(phandle, chandle, &latency) < 0) | 
					
						
							| 
									
										
										
										
											1998-11-27 00:13:09 +00:00
										 |  |  | 			break; | 
					
						
							| 
									
										
										
										
											2001-10-31 16:03:33 +00:00
										 |  |  | 		showlatency(latency); | 
					
						
							| 
									
										
										
										
											2001-08-10 08:56:25 +00:00
										 |  |  | 		if ((err = snd_pcm_link(chandle, phandle)) < 0) { | 
					
						
							|  |  |  | 			printf("Streams link error: %s\n", snd_strerror(err)); | 
					
						
							|  |  |  | 			exit(0); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2022-11-14 20:42:10 +01:00
										 |  |  | 		if (snd_pcm_format_set_silence(format, buffer, latency * channels) < 0) { | 
					
						
							| 
									
										
										
										
											2000-05-08 18:53:38 +00:00
										 |  |  | 			fprintf(stderr, "silence error\n"); | 
					
						
							| 
									
										
										
										
											1999-11-06 23:47:07 +00:00
										 |  |  | 			break; | 
					
						
							| 
									
										
										
										
											2000-05-08 18:53:38 +00:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2001-10-15 11:47:53 +00:00
										 |  |  | 		if (writebuf(phandle, buffer, latency, &frames_out) < 0) { | 
					
						
							| 
									
										
										
										
											2000-05-08 18:53:38 +00:00
										 |  |  | 			fprintf(stderr, "write error\n"); | 
					
						
							|  |  |  | 			break; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2001-10-15 11:49:36 +00:00
										 |  |  | 		if (writebuf(phandle, buffer, latency, &frames_out) < 0) { | 
					
						
							|  |  |  | 			fprintf(stderr, "write error\n"); | 
					
						
							|  |  |  | 			break; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2000-07-18 13:38:58 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-14 21:26:39 +01:00
										 |  |  | 		if (realtime_check) | 
					
						
							|  |  |  | 			timestamp_now(&tstamp_start); | 
					
						
							| 
									
										
										
										
											2001-08-10 08:56:25 +00:00
										 |  |  | 		if ((err = snd_pcm_start(chandle)) < 0) { | 
					
						
							| 
									
										
										
										
											2000-07-24 09:17:14 +00:00
										 |  |  | 			printf("Go error: %s\n", snd_strerror(err)); | 
					
						
							| 
									
										
										
										
											2000-07-18 13:38:58 +00:00
										 |  |  | 			exit(0); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2022-11-14 20:42:10 +01:00
										 |  |  | 		if (realtime_check) | 
					
						
							| 
									
										
										
										
											2022-11-14 21:26:39 +01:00
										 |  |  | 			printf("[%lldus] Stream start\n", timestamp_diff_micro(&tstamp_start)); | 
					
						
							| 
									
										
										
										
											2001-08-10 08:56:25 +00:00
										 |  |  | 		gettimestamp(phandle, &p_tstamp); | 
					
						
							|  |  |  | 		gettimestamp(chandle, &c_tstamp); | 
					
						
							|  |  |  | #if 0
 | 
					
						
							|  |  |  | 		printf("Playback:\n"); | 
					
						
							|  |  |  | 		showstat(phandle, frames_out); | 
					
						
							|  |  |  | 		printf("Capture:\n"); | 
					
						
							|  |  |  | 		showstat(chandle, frames_in); | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2001-10-31 16:03:33 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1999-11-06 23:47:07 +00:00
										 |  |  | 		ok = 1; | 
					
						
							| 
									
										
										
										
											2001-10-31 16:03:33 +00:00
										 |  |  | 		in_max = 0; | 
					
						
							| 
									
										
										
										
											2001-08-10 08:56:25 +00:00
										 |  |  | 		while (ok && frames_in < loop_limit) { | 
					
						
							| 
									
										
										
										
											2022-11-14 20:42:10 +01:00
										 |  |  | 			cap_avail = latency; | 
					
						
							|  |  |  | 			if (sys_latency > 0) { | 
					
						
							|  |  |  | 				poll(NULL, 0, sys_latency); | 
					
						
							|  |  |  | 				cap_avail = get_avail(chandle); | 
					
						
							|  |  |  | 				if (cap_avail < 0) { | 
					
						
							|  |  |  | 					printf("Avail failed: %s\n", snd_strerror(cap_avail)); | 
					
						
							|  |  |  | 					ok = 0; | 
					
						
							|  |  |  | 					break; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} else if (use_poll) { | 
					
						
							| 
									
										
										
										
											2001-10-31 16:03:33 +00:00
										 |  |  | 				/* use poll to wait for next event */ | 
					
						
							|  |  |  | 				snd_pcm_wait(chandle, 1000); | 
					
						
							| 
									
										
										
										
											2022-11-23 19:45:15 +01:00
										 |  |  | 			} else if (!block && usleep_val > 0) { | 
					
						
							| 
									
										
										
										
											2022-11-23 17:56:20 +01:00
										 |  |  | 				usleep(usleep_val); | 
					
						
							| 
									
										
										
										
											2001-10-31 16:03:33 +00:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2022-11-14 20:42:10 +01:00
										 |  |  | 			if (pos_dump || realtime_check) { | 
					
						
							|  |  |  | 				if (sys_latency <= 0) | 
					
						
							|  |  |  | 					cap_avail = get_avail(chandle); | 
					
						
							|  |  |  | 				pbk_fill = get_avail(phandle); | 
					
						
							|  |  |  | 				if (pbk_fill >= 0) | 
					
						
							|  |  |  | 					pbk_fill = playback_buffer_size - pbk_fill; | 
					
						
							|  |  |  | 				if (cap_avail > cap_avail_max) | 
					
						
							|  |  |  | 					cap_avail_max = cap_avail; | 
					
						
							|  |  |  | 				if (pbk_fill >= 0 && pbk_fill < pbk_fill_min) | 
					
						
							|  |  |  | 					pbk_fill_min = pbk_fill; | 
					
						
							|  |  |  | 				if (realtime_check) { | 
					
						
							|  |  |  | 					long long diff = timestamp_diff_micro(&tstamp_start); | 
					
						
							|  |  |  | 					long long cap_pos = frames_to_micro(frames_in + cap_avail); | 
					
						
							|  |  |  | 					long long pbk_pos = frames_to_micro(frames_out - pbk_fill); | 
					
						
							| 
									
										
										
										
											2022-11-14 21:26:39 +01:00
										 |  |  | 					printf("[%lldus] POS: p=%zd (min=%zd, rt=%lldus) c=%zd (max=%zd, rt=%lldus)\n", | 
					
						
							|  |  |  | 							diff, pbk_fill, pbk_fill_min, pbk_pos - diff, | 
					
						
							| 
									
										
										
										
											2022-11-14 20:42:10 +01:00
										 |  |  | 							cap_avail, cap_avail_max, cap_pos - diff); | 
					
						
							|  |  |  | 				} else if (pos_dump) { | 
					
						
							|  |  |  | 					printf("POS: p=%zd (min=%zd), c=%zd (max=%zd)\n", | 
					
						
							|  |  |  | 							pbk_fill, pbk_fill_min, cap_avail, cap_avail_max); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			if ((r = readbuf(chandle, buffer, cap_avail, &frames_in, &in_max)) < 0) | 
					
						
							| 
									
										
										
										
											1999-11-06 23:47:07 +00:00
										 |  |  | 				ok = 0; | 
					
						
							| 
									
										
										
										
											2001-10-31 16:03:33 +00:00
										 |  |  | 			else { | 
					
						
							|  |  |  | 				if (effect) | 
					
						
							| 
									
										
										
										
											2022-11-14 20:42:10 +01:00
										 |  |  | 					applyeffect(buffer, r); | 
					
						
							| 
									
										
										
										
											2001-10-31 16:03:33 +00:00
										 |  |  | 			 	if (writebuf(phandle, buffer, r, &frames_out) < 0) | 
					
						
							|  |  |  | 					ok = 0; | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											1998-11-27 00:13:09 +00:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2001-10-31 16:03:33 +00:00
										 |  |  | 		if (ok) | 
					
						
							|  |  |  | 			printf("Success\n"); | 
					
						
							|  |  |  | 		else | 
					
						
							|  |  |  | 			printf("Failure\n"); | 
					
						
							| 
									
										
										
										
											2000-07-18 13:38:58 +00:00
										 |  |  | 		printf("Playback:\n"); | 
					
						
							| 
									
										
										
										
											2001-08-10 08:56:25 +00:00
										 |  |  | 		showstat(phandle, frames_out); | 
					
						
							| 
									
										
										
										
											2000-07-18 13:38:58 +00:00
										 |  |  | 		printf("Capture:\n"); | 
					
						
							| 
									
										
										
										
											2001-08-10 08:56:25 +00:00
										 |  |  | 		showstat(chandle, frames_in); | 
					
						
							| 
									
										
										
										
											2001-10-31 16:03:33 +00:00
										 |  |  | 		showinmax(in_max); | 
					
						
							| 
									
										
										
										
											2018-10-23 10:03:17 +02:00
										 |  |  | 		if (p_tstamp.tv_sec == c_tstamp.tv_sec && | 
					
						
							| 
									
										
										
										
											2001-08-10 08:56:25 +00:00
										 |  |  | 		    p_tstamp.tv_usec == c_tstamp.tv_usec) | 
					
						
							| 
									
										
										
										
											2000-07-24 09:17:14 +00:00
										 |  |  | 			printf("Hardware sync\n"); | 
					
						
							| 
									
										
										
										
											2022-11-14 20:42:10 +01:00
										 |  |  | 		if (realtime_check) { | 
					
						
							|  |  |  | 			long long diff = timestamp_diff_micro(&tstamp_start); | 
					
						
							|  |  |  | 			long long mtime = frames_to_micro(frames_in); | 
					
						
							|  |  |  | 			printf("Elapsed real time: %lldus\n", diff); | 
					
						
							|  |  |  | 			printf("Elapsed device time: %lldus\n", mtime); | 
					
						
							|  |  |  | 			printf("Test time diff (device - real): %lldus\n", mtime - diff); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2001-08-10 08:56:25 +00:00
										 |  |  | 		snd_pcm_drop(chandle); | 
					
						
							|  |  |  | 		snd_pcm_nonblock(phandle, 0); | 
					
						
							| 
									
										
										
										
											2000-09-29 20:49:18 +00:00
										 |  |  | 		snd_pcm_drain(phandle); | 
					
						
							| 
									
										
										
										
											2001-10-26 16:12:01 +00:00
										 |  |  | 		snd_pcm_nonblock(phandle, !block ? 1 : 0); | 
					
						
							| 
									
										
										
										
											1999-11-06 23:47:07 +00:00
										 |  |  | 		if (ok) { | 
					
						
							| 
									
										
										
										
											2001-08-10 08:56:25 +00:00
										 |  |  | #if 1
 | 
					
						
							| 
									
										
										
										
											1998-11-27 00:13:09 +00:00
										 |  |  | 			printf("Playback time = %li.%i, Record time = %li.%i, diff = %li\n", | 
					
						
							| 
									
										
										
										
											2023-02-02 16:00:45 +09:00
										 |  |  | 			       (long)p_tstamp.tv_sec, | 
					
						
							| 
									
										
										
										
											2001-08-10 08:56:25 +00:00
										 |  |  | 			       (int)p_tstamp.tv_usec, | 
					
						
							| 
									
										
										
										
											2023-02-02 16:00:45 +09:00
										 |  |  | 			       (long)c_tstamp.tv_sec, | 
					
						
							| 
									
										
										
										
											2001-08-10 08:56:25 +00:00
										 |  |  | 			       (int)c_tstamp.tv_usec, | 
					
						
							|  |  |  | 			       timediff(p_tstamp, c_tstamp)); | 
					
						
							| 
									
										
										
										
											2000-07-24 09:17:14 +00:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											1998-11-27 00:13:09 +00:00
										 |  |  | 			break; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2001-08-10 08:56:25 +00:00
										 |  |  | 		snd_pcm_unlink(chandle); | 
					
						
							|  |  |  | 		snd_pcm_hw_free(phandle); | 
					
						
							|  |  |  | 		snd_pcm_hw_free(chandle); | 
					
						
							| 
									
										
										
										
											1998-11-27 00:13:09 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	snd_pcm_close(phandle); | 
					
						
							| 
									
										
										
										
											1999-11-06 23:47:07 +00:00
										 |  |  | 	snd_pcm_close(chandle); | 
					
						
							| 
									
										
										
										
											2022-11-14 20:42:10 +01:00
										 |  |  | 	snd_output_close(output); | 
					
						
							|  |  |  | 	snd_config_update_free_global(); | 
					
						
							|  |  |  | 	free(buffer); | 
					
						
							|  |  |  | 	free(pdevice); | 
					
						
							|  |  |  | 	free(cdevice); | 
					
						
							| 
									
										
										
										
											1999-06-03 21:41:29 +00:00
										 |  |  | 	return 0; | 
					
						
							| 
									
										
										
										
											1998-11-27 00:13:09 +00:00
										 |  |  | } |