Skip to contentSkip to conversation

alsabat: add round trip audio latency test

PATCH In review [email protected] 2 participants 2 messages Jun 3, 2016 – Jun 8, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 alsabat: add round trip audio latency test

From: "vivian,zhang" <[email protected]>

Audio latency is the time delay as an audio signal passes through a system. There are many kinds of audio latency metrics. One useful metric is the round trip latency, which is the sum of output latency and input latency.

The measurement step works like below:
1. Listen and measure the average loudness of the environment for one second;
2. Create a threshold value 16 decibels higher than the average loudness;
3. Begin playing a ~1000 Hz sine wave and start counting the samples elapsed;
4. Stop counting and playing if the input's loudness is higher than the threshold, as the output wave is probably coming back;
5. Calculate the audio latency value in milliseconds.

---
 bat/Makefile.am     |    2 +
 bat/alsa.c          |  144 +++++++++++++++++++++++++++++-
 bat/alsabat-test.sh |    2 +
 bat/alsabat.1       |    6 ++
 bat/bat.c           |   36 ++++++++
 bat/common.h        |   29 ++++++
 bat/latencytest.c   |  242 +++++++++++++++++++++++++++++++++++++++++++++++++++
 bat/latencytest.h   |   17 ++++
 bat/tinyalsa.c      |   95 +++++++++++++++++++-
 9 files changed, 568 insertions(+), 5 deletions(-)
 create mode 100644 bat/latencytest.c
 create mode 100644 bat/latencytest.h
bat/Makefile.am +2 -0
index 24e3eb8..6883826 100644
@@ -7,11 +7,13 @@ alsabat_SOURCES = \
 	bat.c \
 	common.c \
 	signal.c \
+	latencytest.c \
 	convert.c
 
 noinst_HEADERS = \
 	common.h \
 	bat-signal.h \
+	latencytest.h \
 	convert.h
 
 if HAVE_LIBFFTW3
bat/alsa.c +141 -3

192 lines not on this page · raw message

bat/alsabat-test.sh +2 -0
index 2d31a77..24caa0b 100755
@@ -81,6 +81,8 @@ feature_list_test () {
 	latestfile=`ls -t1 /tmp/bat.wav.* | head -n 1`
 	feature_test "--local -F $maxfreq --file $latestfile" \
 			"local mode: analyze local file"
+	feature_test "--roundtriplatency" \
+			"round trip latency test"
 
 	print_result
 }
bat/alsabat.1 +6 -0
index 3f9b767..231b154 100644
@@ -132,6 +132,12 @@ just like in normal mode, but will not be analyzed.
 The ALSABAT being built without libfftw3 support is always in standalone mode.
 The ALSABAT in normal mode can also bypass data analysis using option
 "--standalone".
+.TP
+\fI\-\-roundtriplatency\fP
+Round trip latency test.
+Audio latency is the time delay as an audio signal passes through a system.
+There are many kinds of audio latency metrics. One useful metric is the
+round trip latency, which is the sum of output latency and input latency.
 
 .SH EXAMPLES
 
bat/bat.c +36 -0
index cd4ea2d..9f4cc59 100644
@@ -40,6 +40,7 @@
 #ifdef HAVE_LIBFFTW3
 #include "analyze.h"
 #endif
+#include "latencytest.h"
 
 static int get_duration(struct bat *bat)
 {
@@ -299,6 +300,7 @@ _("Usage: alsabat [-options]...\n"
 "      --saveplay=#       file that storing playback content, for debug\n"
 "      --local            internal loop, set to bypass pcm hardware devices\n"
 "      --standalone       standalone mode, to bypass analysis\n"
+"      --roundtriplatency round trip latency mode\n"
 ));
 	fprintf(bat->log, _("Recognized sample formats are: "));
 	fprintf(bat->log, _("U8 S16_LE S24_3LE S32_LE\n"));
@@ -328,6 +330,7 @@ static void set_defaults(struct bat *bat)
 	bat->local = false;
 	bat->buffer_size = 0;
 	bat->period_size = 0;
+	bat->roundtriplatency = false;
 #ifdef HAVE_LIBTINYALSA
 	bat->channels = 2;
 	bat->playback.fct = &playback_tinyalsa;
@@ -355,6 +358,7 @@ static void parse_arguments(struct bat *bat, int argc, char *argv[])
 		{"saveplay", 1, 0, OPT_SAVEPLAY},
 		{"local",    0, 0, OPT_LOCAL},
 		{"standalone", 0, 0, OPT_STANDALONE},
+		{"roundtriplatency", 0, 0, OPT_ROUNDTRIPLATENCY},
 		{0, 0, 0, 0}
 	};
 
@@ -376,6 +380,9 @@ static void parse_arguments(struct bat *bat, int argc, char *argv[])
 		case OPT_STANDALONE:
 			bat->standalone = true;
 			break;
+		case OPT_ROUNDTRIPLATENCY:
+			bat->roundtriplatency = true;
+			break;
 		case 'D':
 			if (bat->playback.device == NULL)
 				bat->playback.device = optarg;
@@ -616,6 +623,35 @@ int main(int argc, char *argv[])
 	if (err < 0)
 		goto out;
 
+	/* round trip latency test thread */
+	if (bat.roundtriplatency) {
+		while (1) {
+			fprintf(bat.log,
+				_("\nStart round trip latency\n"));
+			roundtrip_latency_init(&bat);
+			test_loopback(&bat);
+
+			if (bat.latency.xrun_error == false)
+				break;
+			else {
+				/* Xrun error in playback or capture,
+				increase period size and try again */
+				bat.period_size += bat.rate / 1000;
+				bat.buffer_size =
+					bat.period_size * DIV_BUFFERSIZE;
+
+				/* terminate the test if period_size is
+				large enough */
+				if (bat.period_size > bat.rate * 0.2)
+					break;
+			}
+
+			/* Waiting 500ms and start the next round */
+			usleep(CAPTURE_DELAY * 1000);
+		}
+		goto out;
+	}
+
 	/* single line playback thread: playback only, no capture */
 	if (bat.playback.mode == MODE_SINGLE) {
 		test_playback(&bat);
bat/common.h +29 -0
index ad02a5a..0e8f042 100644
@@ -22,6 +22,7 @@
 #define OPT_SAVEPLAY			(OPT_BASE + 3)
 #define OPT_LOCAL			(OPT_BASE + 4)
 #define OPT_STANDALONE			(OPT_BASE + 5)
+#define OPT_ROUNDTRIPLATENCY		(OPT_BASE + 6)
 
 #define COMPOSE(a, b, c, d)		((a) | ((b)<<8) | ((c)<<16) | ((d)<<24))
 #define WAV_RIFF			COMPOSE('R', 'I', 'F', 'F')
@@ -53,6 +54,10 @@
 /* default period size for tinyalsa */
 #define TINYALSA_PERIODSIZE			1024
 
+#define LATENCY_TEST_NUMBER			5
+#define LATENCY_TEST_TIME_LIMIT			25
+#define DIV_BUFFERSIZE			2
+
 #define EBATBASE			1000
 #define ENOPEAK				(EBATBASE + 1)
 #define EONLYDC				(EBATBASE + 2)
@@ -130,6 +135,14 @@ enum _bat_op_mode {
 	MODE_LAST
 };
 
+enum latency_state {
+	LATENCY_STATE_COMPLETE_FAILURE = -1,
+	LATENCY_STATE_COMPLETE_SUCCESS = 0,
+	LATENCY_STATE_MEASURE_FOR_1_SECOND,
+	LATENCY_STATE_PLAY_AND_LISTEN,
+	LATENCY_STATE_WAITING,
+};
+
 struct pcm {
 	unsigned int card_tiny;
 	unsigned int device_tiny;
@@ -151,6 +164,20 @@ struct sin_generator {
 	float magnitude;
 };
 
+struct roundtrip_latency {
+	int number;
+	enum latency_state state;
+	float result[LATENCY_TEST_NUMBER];
+	int final_result;
+	int samples;
+	float sum;
+	int threshold;
+	int error;
+	bool is_capturing;
+	bool is_playing;
+	bool xrun_error;
+};
+
 struct bat {
 	unsigned int rate;		/* sampling rate */
 	int channels;			/* nb of channels */
@@ -169,9 +196,11 @@ struct bat {
 	char *logarg;			/* path name of log file */
 	char *debugplay;		/* path name to store playback signal */
 	bool standalone;		/* enable to bypass analysis */
+	bool roundtriplatency;		/* enable round trip latency */
 
 	struct pcm playback;
 	struct pcm capture;
+	struct roundtrip_latency latency;
 
 	unsigned int periods_played;
 	unsigned int periods_total;
bat/latencytest.c new file +242 -0

242 lines not on this page · raw message

bat/latencytest.h new file +17 -0
new file mode 100644
index 0000000..be5e661
@@ -0,0 +1,17 @@
+/*
+ * Copyright (C) 2013-2015 Intel Corporation
+ *
+ * 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.
+ *
+ * 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
+ * GNU General Public License for more details.
+ *
+ */
+void roundtrip_latency_init(struct bat *);
+int handleinput(struct bat *, void *, int);
+int handleoutput(struct bat *, void *, int, int);
bat/tinyalsa.c +93 -2

125 lines not on this page · raw message

Signature · 1 line
1.7.9.5
#1 lore raw
Quoted 19 lines · On Fri, 03 Jun 2016 04:05:08 +0200, [email protected] wrote:

From: "vivian,zhang" <[email protected]>

Audio latency is the time delay as an audio signal passes through a system. There are many kinds of audio latency metrics. One useful metric is the round trip latency, which is the sum of output latency and input latency.

The measurement step works like below:
1. Listen and measure the average loudness of the environment for one second;
2. Create a threshold value 16 decibels higher than the average loudness;
3. Begin playing a ~1000 Hz sine wave and start counting the samples elapsed;
4. Stop counting and playing if the input's loudness is higher than the threshold, as the output wave is probably coming back;
5. Calculate the audio latency value in milliseconds.

Quoted 1 line

Applied now. Thanks.

Takashi