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.
This commit is contained in:
Max Kellermann
2009-11-10 17:57:14 +01:00
parent e5b119a324
commit cef5dcc0a1
9 changed files with 95 additions and 29 deletions

44
src/audio_format.c Normal file
View File

@@ -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;
}

View File

@@ -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

View File

@@ -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())) {

View File

@@ -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)

View File

@@ -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