From cef5dcc0a15759588fcfd079ec87592511e02df4 Mon Sep 17 00:00:00 2001
From: Max Kellermann <max@duempel.org>
Date: Tue, 10 Nov 2009 17:57:14 +0100
Subject: [PATCH] audio_format: added function audio_format_to_string()

Unified function for converting an audio_format object to a string,
for log messages and for the "status" command.
---
 Makefile.am         |  5 +++++
 src/audio_format.c  | 44 ++++++++++++++++++++++++++++++++++++++++++++
 src/audio_format.h  | 19 +++++++++++++++++++
 src/command.c       |  9 +++++----
 src/decoder_api.c   | 14 ++++++--------
 src/output_thread.c | 16 ++++++----------
 test/run_decoder.c  |  6 ++++--
 test/run_filter.c   |  6 +++---
 test/run_output.c   |  5 +++--
 9 files changed, 95 insertions(+), 29 deletions(-)
 create mode 100644 src/audio_format.c

diff --git a/Makefile.am b/Makefile.am
index dfe145635..13253d976 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -209,6 +209,7 @@ src_mpd_SOURCES = \
 	src/notify.c \
 	src/audio.c \
 	src/audio_check.c \
+	src/audio_format.c \
 	src/audio_parser.c \
 	src/command.c \
 	src/idle.c \
@@ -790,6 +791,7 @@ test_run_decoder_SOURCES = test/run_decoder.c \
 	src/uri.c \
 	src/fd_util.c \
 	src/audio_check.c \
+	src/audio_format.c \
 	$(ARCHIVE_SRC) \
 	$(INPUT_SRC) \
 	$(TAG_SRC) \
@@ -828,6 +830,7 @@ test_run_filter_SOURCES = test/run_filter.c \
 	src/pcm_format.c src/pcm_channels.c src/pcm_dither.c \
 	src/pcm_resample.c src/pcm_resample_fallback.c \
 	src/audio_check.c \
+	src/audio_format.c \
 	src/audio_parser.c \
 	$(FILTER_SRC)
 
@@ -842,6 +845,7 @@ test_run_encoder_SOURCES = test/run_encoder.c \
 	src/utils.c \
 	src/tag.c src/tag_pool.c \
 	src/audio_check.c \
+	src/audio_format.c \
 	src/audio_parser.c \
 	$(ENCODER_SRC)
 test_run_encoder_LDADD = $(MPD_LIBS) \
@@ -866,6 +870,7 @@ test_run_output_LDADD = $(MPD_LIBS) \
 test_run_output_SOURCES = test/run_output.c \
 	src/conf.c src/tokenizer.c src/utils.c src/log.c \
 	src/audio_check.c \
+	src/audio_format.c \
 	src/audio_parser.c \
 	src/timer.c \
 	src/tag.c src/tag_pool.c \
diff --git a/src/audio_format.c b/src/audio_format.c
new file mode 100644
index 000000000..f88735c73
--- /dev/null
+++ b/src/audio_format.c
@@ -0,0 +1,44 @@
+/*
+ * Copyright (C) 2003-2009 The Music Player Daemon Project
+ * http://www.musicpd.org
+ *
+ * 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.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#include "audio_format.h"
+
+#include <assert.h>
+#include <stdio.h>
+
+#if G_BYTE_ORDER == G_BIG_ENDIAN
+#define REVERSE_ENDIAN_SUFFIX "_le"
+#else
+#define REVERSE_ENDIAN_SUFFIX "_be"
+#endif
+
+const char *
+audio_format_to_string(const struct audio_format *af,
+		       struct audio_format_string *s)
+{
+	assert(af != NULL);
+	assert(s != NULL);
+
+	snprintf(s->buffer, sizeof(s->buffer), "%u:%u%s:%u",
+		 af->sample_rate, af->bits,
+		 af->reverse_endian ? REVERSE_ENDIAN_SUFFIX : "",
+		 af->channels);
+
+	return s->buffer;
+}
diff --git a/src/audio_format.h b/src/audio_format.h
index a4f5ba2e0..0c1e425a9 100644
--- a/src/audio_format.h
+++ b/src/audio_format.h
@@ -55,6 +55,13 @@ struct audio_format {
 	uint8_t reverse_endian;
 };
 
+/**
+ * Buffer for audio_format_string().
+ */
+struct audio_format_string {
+	char buffer[24];
+};
+
 /**
  * Clears the #audio_format object, i.e. sets all attributes to an
  * undefined (invalid) value.
@@ -219,4 +226,16 @@ static inline double audio_format_time_to_size(const struct audio_format *af)
 	return af->sample_rate * audio_format_frame_size(af);
 }
 
+/**
+ * Renders the #audio_format object into a string, e.g. for printing
+ * it in a log file.
+ *
+ * @param af the #audio_format object
+ * @param s a buffer to print into
+ * @return the string, or NULL if the #audio_format object is invalid
+ */
+const char *
+audio_format_to_string(const struct audio_format *af,
+		       struct audio_format_string *s);
+
 #endif
diff --git a/src/command.c b/src/command.c
index 3466da4d1..db0bafa31 100644
--- a/src/command.c
+++ b/src/command.c
@@ -515,18 +515,19 @@ handle_status(struct client *client,
 	}
 
 	if (player_status.state != PLAYER_STATE_STOP) {
+		struct audio_format_string af_string;
+
 		client_printf(client,
 			      COMMAND_STATUS_TIME ": %i:%i\n"
 			      "elapsed: %1.3f\n"
 			      COMMAND_STATUS_BITRATE ": %u\n"
-			      COMMAND_STATUS_AUDIO ": %u:%u:%u\n",
+			      COMMAND_STATUS_AUDIO ": %s\n",
 			      (int)(player_status.elapsed_time + 0.5),
 			      (int)(player_status.total_time + 0.5),
 			      player_status.elapsed_time,
 			      player_status.bit_rate,
-			      player_status.audio_format.sample_rate,
-			      player_status.audio_format.bits,
-			      player_status.audio_format.channels);
+			      audio_format_to_string(&player_status.audio_format,
+						     &af_string));
 	}
 
 	if ((updateJobId = isUpdatingDB())) {
diff --git a/src/decoder_api.c b/src/decoder_api.c
index c6c23182e..eb316bc49 100644
--- a/src/decoder_api.c
+++ b/src/decoder_api.c
@@ -44,6 +44,7 @@ decoder_initialized(struct decoder *decoder,
 		    bool seekable, float total_time)
 {
 	struct decoder_control *dc = decoder->dc;
+	struct audio_format_string af_string;
 
 	assert(dc->state == DECODE_STATE_START);
 	assert(dc->pipe != NULL);
@@ -67,18 +68,15 @@ decoder_initialized(struct decoder *decoder,
 
 	player_lock_signal();
 
-	g_debug("audio_format=%u:%u:%u, seekable=%s",
-		dc->in_audio_format.sample_rate,
-		dc->in_audio_format.bits,
-		dc->in_audio_format.channels,
+	g_debug("audio_format=%s, seekable=%s",
+		audio_format_to_string(&dc->in_audio_format, &af_string),
 		seekable ? "true" : "false");
 
 	if (!audio_format_equals(&dc->in_audio_format,
 				 &dc->out_audio_format))
-		g_debug("converting to %u:%u:%u",
-			dc->out_audio_format.sample_rate,
-			dc->out_audio_format.bits,
-			dc->out_audio_format.channels);
+		g_debug("converting to %s",
+			audio_format_to_string(&dc->out_audio_format,
+					       &af_string));
 }
 
 char *decoder_get_uri(G_GNUC_UNUSED struct decoder *decoder)
diff --git a/src/output_thread.c b/src/output_thread.c
index 0b61ab171..fccbad5eb 100644
--- a/src/output_thread.c
+++ b/src/output_thread.c
@@ -93,6 +93,7 @@ ao_open(struct audio_output *ao)
 	bool success;
 	GError *error = NULL;
 	const struct audio_format *filter_audio_format;
+	struct audio_format_string af_string;
 
 	assert(!ao->open);
 	assert(ao->fail_timer == NULL);
@@ -145,20 +146,15 @@ ao_open(struct audio_output *ao)
 	ao->open = true;
 
 	g_debug("opened plugin=%s name=\"%s\" "
-		"audio_format=%u:%u:%u:%u",
+		"audio_format=%s",
 		ao->plugin->name, ao->name,
-		ao->out_audio_format.sample_rate,
-		ao->out_audio_format.bits,
-		ao->out_audio_format.channels,
-		ao->out_audio_format.reverse_endian);
+		audio_format_to_string(&ao->out_audio_format, &af_string));
 
 	if (!audio_format_equals(&ao->in_audio_format,
 				 &ao->out_audio_format))
-		g_debug("converting from %u:%u:%u:%u",
-			ao->in_audio_format.sample_rate,
-			ao->in_audio_format.bits,
-			ao->in_audio_format.channels,
-			ao->in_audio_format.reverse_endian);
+		g_debug("converting from %s",
+			audio_format_to_string(&ao->in_audio_format,
+					       &af_string));
 }
 
 static void
diff --git a/test/run_decoder.c b/test/run_decoder.c
index 7e0c582a2..72e0783f0 100644
--- a/test/run_decoder.c
+++ b/test/run_decoder.c
@@ -63,11 +63,13 @@ decoder_initialized(struct decoder *decoder,
 		    G_GNUC_UNUSED bool seekable,
 		    G_GNUC_UNUSED float total_time)
 {
+	struct audio_format_string af_string;
+
 	assert(!decoder->initialized);
 	assert(audio_format_valid(audio_format));
 
-	g_printerr("audio_format=%u:%u:%u\n", audio_format->sample_rate,
-		   audio_format->bits, audio_format->channels);
+	g_printerr("audio_format=%s\n",
+		   audio_format_to_string(audio_format, &af_string));
 
 	decoder->initialized = true;
 }
diff --git a/test/run_filter.c b/test/run_filter.c
index de5f9a925..30495b1fd 100644
--- a/test/run_filter.c
+++ b/test/run_filter.c
@@ -72,6 +72,7 @@ load_filter(const char *name)
 int main(int argc, char **argv)
 {
 	struct audio_format audio_format;
+	struct audio_format_string af_string;
 	bool success;
 	GError *error = NULL;
 	struct filter *filter;
@@ -127,9 +128,8 @@ int main(int argc, char **argv)
 		return 1;
 	}
 
-
-	g_printerr("audio_format=%u:%u:%u\n", out_audio_format->sample_rate,
-		   out_audio_format->bits, out_audio_format->channels);
+	g_printerr("audio_format=%s\n",
+		   audio_format_to_string(out_audio_format, &af_string));
 
 	frame_size = audio_format_frame_size(&audio_format);
 
diff --git a/test/run_output.c b/test/run_output.c
index 36bff7721..1acc7b377 100644
--- a/test/run_output.c
+++ b/test/run_output.c
@@ -107,6 +107,7 @@ int main(int argc, char **argv)
 {
 	struct audio_output ao;
 	struct audio_format audio_format;
+	struct audio_format_string af_string;
 	bool success;
 	GError *error = NULL;
 	char buffer[4096];
@@ -160,8 +161,8 @@ int main(int argc, char **argv)
 		return 1;
 	}
 
-	g_printerr("audio_format=%u:%u:%u\n", audio_format.sample_rate,
-		   audio_format.bits, audio_format.channels);
+	g_printerr("audio_format=%s\n",
+		   audio_format_to_string(&audio_format, &af_string));
 
 	frame_size = audio_format_frame_size(&audio_format);