Commit cef5dcc0 authored by Max Kellermann's avatar Max Kellermann

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.
parent e5b119a3
...@@ -209,6 +209,7 @@ src_mpd_SOURCES = \ ...@@ -209,6 +209,7 @@ src_mpd_SOURCES = \
src/notify.c \ src/notify.c \
src/audio.c \ src/audio.c \
src/audio_check.c \ src/audio_check.c \
src/audio_format.c \
src/audio_parser.c \ src/audio_parser.c \
src/command.c \ src/command.c \
src/idle.c \ src/idle.c \
...@@ -790,6 +791,7 @@ test_run_decoder_SOURCES = test/run_decoder.c \ ...@@ -790,6 +791,7 @@ test_run_decoder_SOURCES = test/run_decoder.c \
src/uri.c \ src/uri.c \
src/fd_util.c \ src/fd_util.c \
src/audio_check.c \ src/audio_check.c \
src/audio_format.c \
$(ARCHIVE_SRC) \ $(ARCHIVE_SRC) \
$(INPUT_SRC) \ $(INPUT_SRC) \
$(TAG_SRC) \ $(TAG_SRC) \
...@@ -828,6 +830,7 @@ test_run_filter_SOURCES = test/run_filter.c \ ...@@ -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_format.c src/pcm_channels.c src/pcm_dither.c \
src/pcm_resample.c src/pcm_resample_fallback.c \ src/pcm_resample.c src/pcm_resample_fallback.c \
src/audio_check.c \ src/audio_check.c \
src/audio_format.c \
src/audio_parser.c \ src/audio_parser.c \
$(FILTER_SRC) $(FILTER_SRC)
...@@ -842,6 +845,7 @@ test_run_encoder_SOURCES = test/run_encoder.c \ ...@@ -842,6 +845,7 @@ test_run_encoder_SOURCES = test/run_encoder.c \
src/utils.c \ src/utils.c \
src/tag.c src/tag_pool.c \ src/tag.c src/tag_pool.c \
src/audio_check.c \ src/audio_check.c \
src/audio_format.c \
src/audio_parser.c \ src/audio_parser.c \
$(ENCODER_SRC) $(ENCODER_SRC)
test_run_encoder_LDADD = $(MPD_LIBS) \ test_run_encoder_LDADD = $(MPD_LIBS) \
...@@ -866,6 +870,7 @@ test_run_output_LDADD = $(MPD_LIBS) \ ...@@ -866,6 +870,7 @@ test_run_output_LDADD = $(MPD_LIBS) \
test_run_output_SOURCES = test/run_output.c \ test_run_output_SOURCES = test/run_output.c \
src/conf.c src/tokenizer.c src/utils.c src/log.c \ src/conf.c src/tokenizer.c src/utils.c src/log.c \
src/audio_check.c \ src/audio_check.c \
src/audio_format.c \
src/audio_parser.c \ src/audio_parser.c \
src/timer.c \ src/timer.c \
src/tag.c src/tag_pool.c \ src/tag.c src/tag_pool.c \
......
/*
* 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;
}
...@@ -56,6 +56,13 @@ struct audio_format { ...@@ -56,6 +56,13 @@ struct audio_format {
}; };
/** /**
* Buffer for audio_format_string().
*/
struct audio_format_string {
char buffer[24];
};
/**
* Clears the #audio_format object, i.e. sets all attributes to an * Clears the #audio_format object, i.e. sets all attributes to an
* undefined (invalid) value. * undefined (invalid) value.
*/ */
...@@ -219,4 +226,16 @@ static inline double audio_format_time_to_size(const struct audio_format *af) ...@@ -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); 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 #endif
...@@ -515,18 +515,19 @@ handle_status(struct client *client, ...@@ -515,18 +515,19 @@ handle_status(struct client *client,
} }
if (player_status.state != PLAYER_STATE_STOP) { if (player_status.state != PLAYER_STATE_STOP) {
struct audio_format_string af_string;
client_printf(client, client_printf(client,
COMMAND_STATUS_TIME ": %i:%i\n" COMMAND_STATUS_TIME ": %i:%i\n"
"elapsed: %1.3f\n" "elapsed: %1.3f\n"
COMMAND_STATUS_BITRATE ": %u\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.elapsed_time + 0.5),
(int)(player_status.total_time + 0.5), (int)(player_status.total_time + 0.5),
player_status.elapsed_time, player_status.elapsed_time,
player_status.bit_rate, player_status.bit_rate,
player_status.audio_format.sample_rate, audio_format_to_string(&player_status.audio_format,
player_status.audio_format.bits, &af_string));
player_status.audio_format.channels);
} }
if ((updateJobId = isUpdatingDB())) { if ((updateJobId = isUpdatingDB())) {
......
...@@ -44,6 +44,7 @@ decoder_initialized(struct decoder *decoder, ...@@ -44,6 +44,7 @@ decoder_initialized(struct decoder *decoder,
bool seekable, float total_time) bool seekable, float total_time)
{ {
struct decoder_control *dc = decoder->dc; struct decoder_control *dc = decoder->dc;
struct audio_format_string af_string;
assert(dc->state == DECODE_STATE_START); assert(dc->state == DECODE_STATE_START);
assert(dc->pipe != NULL); assert(dc->pipe != NULL);
...@@ -67,18 +68,15 @@ decoder_initialized(struct decoder *decoder, ...@@ -67,18 +68,15 @@ decoder_initialized(struct decoder *decoder,
player_lock_signal(); player_lock_signal();
g_debug("audio_format=%u:%u:%u, seekable=%s", g_debug("audio_format=%s, seekable=%s",
dc->in_audio_format.sample_rate, audio_format_to_string(&dc->in_audio_format, &af_string),
dc->in_audio_format.bits,
dc->in_audio_format.channels,
seekable ? "true" : "false"); seekable ? "true" : "false");
if (!audio_format_equals(&dc->in_audio_format, if (!audio_format_equals(&dc->in_audio_format,
&dc->out_audio_format)) &dc->out_audio_format))
g_debug("converting to %u:%u:%u", g_debug("converting to %s",
dc->out_audio_format.sample_rate, audio_format_to_string(&dc->out_audio_format,
dc->out_audio_format.bits, &af_string));
dc->out_audio_format.channels);
} }
char *decoder_get_uri(G_GNUC_UNUSED struct decoder *decoder) char *decoder_get_uri(G_GNUC_UNUSED struct decoder *decoder)
......
...@@ -93,6 +93,7 @@ ao_open(struct audio_output *ao) ...@@ -93,6 +93,7 @@ ao_open(struct audio_output *ao)
bool success; bool success;
GError *error = NULL; GError *error = NULL;
const struct audio_format *filter_audio_format; const struct audio_format *filter_audio_format;
struct audio_format_string af_string;
assert(!ao->open); assert(!ao->open);
assert(ao->fail_timer == NULL); assert(ao->fail_timer == NULL);
...@@ -145,20 +146,15 @@ ao_open(struct audio_output *ao) ...@@ -145,20 +146,15 @@ ao_open(struct audio_output *ao)
ao->open = true; ao->open = true;
g_debug("opened plugin=%s name=\"%s\" " g_debug("opened plugin=%s name=\"%s\" "
"audio_format=%u:%u:%u:%u", "audio_format=%s",
ao->plugin->name, ao->name, ao->plugin->name, ao->name,
ao->out_audio_format.sample_rate, audio_format_to_string(&ao->out_audio_format, &af_string));
ao->out_audio_format.bits,
ao->out_audio_format.channels,
ao->out_audio_format.reverse_endian);
if (!audio_format_equals(&ao->in_audio_format, if (!audio_format_equals(&ao->in_audio_format,
&ao->out_audio_format)) &ao->out_audio_format))
g_debug("converting from %u:%u:%u:%u", g_debug("converting from %s",
ao->in_audio_format.sample_rate, audio_format_to_string(&ao->in_audio_format,
ao->in_audio_format.bits, &af_string));
ao->in_audio_format.channels,
ao->in_audio_format.reverse_endian);
} }
static void static void
......
...@@ -63,11 +63,13 @@ decoder_initialized(struct decoder *decoder, ...@@ -63,11 +63,13 @@ decoder_initialized(struct decoder *decoder,
G_GNUC_UNUSED bool seekable, G_GNUC_UNUSED bool seekable,
G_GNUC_UNUSED float total_time) G_GNUC_UNUSED float total_time)
{ {
struct audio_format_string af_string;
assert(!decoder->initialized); assert(!decoder->initialized);
assert(audio_format_valid(audio_format)); assert(audio_format_valid(audio_format));
g_printerr("audio_format=%u:%u:%u\n", audio_format->sample_rate, g_printerr("audio_format=%s\n",
audio_format->bits, audio_format->channels); audio_format_to_string(audio_format, &af_string));
decoder->initialized = true; decoder->initialized = true;
} }
......
...@@ -72,6 +72,7 @@ load_filter(const char *name) ...@@ -72,6 +72,7 @@ load_filter(const char *name)
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
struct audio_format audio_format; struct audio_format audio_format;
struct audio_format_string af_string;
bool success; bool success;
GError *error = NULL; GError *error = NULL;
struct filter *filter; struct filter *filter;
...@@ -127,9 +128,8 @@ int main(int argc, char **argv) ...@@ -127,9 +128,8 @@ int main(int argc, char **argv)
return 1; return 1;
} }
g_printerr("audio_format=%s\n",
g_printerr("audio_format=%u:%u:%u\n", out_audio_format->sample_rate, audio_format_to_string(out_audio_format, &af_string));
out_audio_format->bits, out_audio_format->channels);
frame_size = audio_format_frame_size(&audio_format); frame_size = audio_format_frame_size(&audio_format);
......
...@@ -107,6 +107,7 @@ int main(int argc, char **argv) ...@@ -107,6 +107,7 @@ int main(int argc, char **argv)
{ {
struct audio_output ao; struct audio_output ao;
struct audio_format audio_format; struct audio_format audio_format;
struct audio_format_string af_string;
bool success; bool success;
GError *error = NULL; GError *error = NULL;
char buffer[4096]; char buffer[4096];
...@@ -160,8 +161,8 @@ int main(int argc, char **argv) ...@@ -160,8 +161,8 @@ int main(int argc, char **argv)
return 1; return 1;
} }
g_printerr("audio_format=%u:%u:%u\n", audio_format.sample_rate, g_printerr("audio_format=%s\n",
audio_format.bits, audio_format.channels); audio_format_to_string(&audio_format, &af_string));
frame_size = audio_format_frame_size(&audio_format); frame_size = audio_format_frame_size(&audio_format);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment