Permalink
Browse files

status: added mpd_status_get_audio_format()

Replaced mpd_status_get_sample_rate(), mpd_status_get_bits(),
mpd_status_get_channels() by returning an audio_format struct, similar
to MPD's audio_format struct.
  • Loading branch information...
1 parent 69cf897 commit b028fc0d58b2592c506000d39431d04d3b61a6dc @MaxKellermann MaxKellermann committed Sep 12, 2009
Showing with 119 additions and 52 deletions.
  1. +1 −0 Makefile.am
  2. +65 −0 include/mpd/audio_format.h
  3. +1 −0 include/mpd/client.h
  4. +5 −12 include/mpd/status.h
  5. +8 −3 src/example.c
  6. +30 −34 src/status.c
  7. +9 −3 test/main.c
View
@@ -4,6 +4,7 @@ AUTOMAKE_OPTIONS = foreign 1.9 dist-bzip2
mpdincludedir = $(includedir)/mpd
mpdinclude_HEADERS = \
include/mpd/async.h \
+ include/mpd/audio_format.h \
include/mpd/client.h \
include/mpd/command.h \
include/mpd/connection.h \
@@ -0,0 +1,65 @@
+/* libmpdclient
+ (c) 2003-2009 The Music Player Daemon Project
+ This project's homepage is: http://www.musicpd.org
+
+ Redistribution and use in source and binary forms, with or without
+ modification, are permitted provided that the following conditions
+ are met:
+
+ - Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+
+ - Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
+ CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+*/
+
+#ifndef MPD_AUDIO_FORMAT_H
+#define MPD_AUDIO_FORMAT_H
+
+#include <stdint.h>
+
+/**
+ * This structure describes the format of a raw PCM stream.
+ */
+struct mpd_audio_format {
+ /**
+ * The sample rate in Hz. A better name for this attribute is
+ * "frame rate", because technically, you have two samples per
+ * frame in stereo sound.
+ */
+ uint32_t sample_rate;
+
+ /**
+ * The number of significant bits per sample. Samples are
+ * currently always signed. Supported values are 8, 16, 24,
+ * 32. 24 bit samples are packed in 32 bit integers.
+ */
+ uint8_t bits;
+
+ /**
+ * The number of channels. Only mono (1) and stereo (2) are
+ * fully supported currently.
+ */
+ uint8_t channels;
+
+ /** reserved for future use */
+ uint16_t reserved0;
+
+ /** reserved for future use */
+ uint32_t reserved1;
+};
+
+#endif
@@ -45,6 +45,7 @@
#ifndef MPD_CLIENT_H
#define MPD_CLIENT_H
+#include <mpd/audio_format.h>
#include <mpd/connection.h>
#include <mpd/command.h>
#include <mpd/database.h>
View
@@ -57,6 +57,7 @@ enum mpd_state {
struct mpd_connection;
struct mpd_pair;
+struct mpd_audio_format;
/**
* \struct mpd_status
@@ -209,19 +210,11 @@ unsigned
mpd_status_get_kbit_rate(const struct mpd_status *status);
/**
- * Returns audio sample rate
+ * Returns audio format which MPD is currently playing. May return
+ * NULL if MPD is not playing or if the audio format is unknown.
*/
-unsigned int mpd_status_get_sample_rate(const struct mpd_status *status);
-
-/**
- * Returns audio bits
- */
-int mpd_status_get_bits(const struct mpd_status *status);
-
-/**
- * Returns audio channels
- */
-int mpd_status_get_channels(const struct mpd_status *status);
+const struct mpd_audio_format *
+mpd_status_get_audio_format(const struct mpd_status *status);
/**
* Returns 1 if mpd is updating, 0 otherwise
View
@@ -81,6 +81,7 @@ int main(int argc, char ** argv) {
if(argc==1) {
struct mpd_status * status;
struct mpd_song *song;
+ const struct mpd_audio_format *audio_format;
mpd_command_list_begin(conn, true);
mpd_send_status(conn);
@@ -107,9 +108,13 @@ int main(int argc, char ** argv) {
printf("elaspedTime: %i\n",mpd_status_get_elapsed_time(status));
printf("totalTime: %i\n", mpd_status_get_total_time(status));
printf("bitRate: %i\n", mpd_status_get_kbit_rate(status));
- printf("sampleRate: %i\n", mpd_status_get_sample_rate(status));
- printf("bits: %i\n", mpd_status_get_bits(status));
- printf("channels: %i\n", mpd_status_get_channels(status));
+ }
+
+ audio_format = mpd_status_get_audio_format(status);
+ if (audio_format != NULL) {
+ printf("sampleRate: %i\n", audio_format->sample_rate);
+ printf("bits: %i\n", audio_format->bits);
+ printf("channels: %i\n", audio_format->channels);
}
if (mpd_get_error(conn) != MPD_ERROR_SUCCESS) {
View
@@ -32,6 +32,7 @@
#include <mpd/status.h>
#include <mpd/pair.h>
+#include <mpd/audio_format.h>
#include <stdlib.h>
#include <string.h>
@@ -89,14 +90,8 @@ struct mpd_status {
/** current bit rate in kbps */
unsigned kbit_rate;
- /** audio sample rate */
- unsigned int sample_rate;
-
- /** audio bits */
- int bits;
-
- /** audio channels */
- int channels;
+ /** the current audio format */
+ struct mpd_audio_format audio_format;
/** non-zero if MPD is updating, 0 otherwise */
unsigned update_id;
@@ -125,9 +120,7 @@ mpd_status_new(void)
status->elapsed_time = 0;
status->total_time = 0;
status->kbit_rate = 0;
- status->sample_rate = 0;
- status->bits = 0;
- status->channels = 0;
+ memset(&status->audio_format, 0, sizeof(status->audio_format));
status->crossfade = 0;
status->error = NULL;
status->update_id = 0;
@@ -148,6 +141,23 @@ parse_mpd_state(const char *p)
return MPD_STATE_UNKNOWN;
}
+static void
+parse_audio_format(struct mpd_audio_format *audio_format, const char *p)
+{
+ char *endptr;
+
+ audio_format->sample_rate = strtol(p, &endptr, 10);
+ if (*endptr == ':') {
+ audio_format->bits = strtol(endptr + 1, &endptr, 10);
+ audio_format->channels = *endptr == ':'
+ ? strtol(endptr + 1, NULL, 10)
+ : 0;
+ } else {
+ audio_format->bits = 0;
+ audio_format->channels = 0;
+ }
+}
+
void
mpd_status_feed(struct mpd_status *status, const struct mpd_pair *pair)
{
@@ -188,17 +198,8 @@ mpd_status_feed(struct mpd_status *status, const struct mpd_pair *pair)
status->crossfade = atoi(pair->value);
else if (strcmp(pair->name, "updating_db") == 0)
status->update_id = atoi(pair->value);
- else if (strcmp(pair->name, "audio") == 0) {
- char * tok = strchr(pair->value,':');
- if (tok && (strchr(tok,0) > (tok+1))) {
- status->sample_rate = atoi(pair->value);
- status->bits = atoi(++tok);
- tok = strchr(tok,':');
- if (tok && (strchr(tok,0) > (tok+1)))
- status->channels = atoi(tok+1);
- }
- }
-
+ else if (strcmp(pair->name, "audio") == 0)
+ parse_audio_format(&status->audio_format, pair->value);
}
void mpd_status_free(struct mpd_status * status) {
@@ -289,19 +290,14 @@ mpd_status_get_kbit_rate(const struct mpd_status *status)
return status->kbit_rate;
}
-unsigned int mpd_status_get_sample_rate(const struct mpd_status *status)
-{
- return status->sample_rate;
-}
-
-int mpd_status_get_bits(const struct mpd_status *status)
-{
- return status->bits;
-}
-
-int mpd_status_get_channels(const struct mpd_status *status)
+const struct mpd_audio_format *
+mpd_status_get_audio_format(const struct mpd_status *status)
{
- return status->channels;
+ return status->audio_format.sample_rate > 0 ||
+ status->audio_format.bits > 0 ||
+ status->audio_format.channels > 0
+ ? &status->audio_format
+ : NULL;
}
unsigned
View
@@ -115,6 +115,8 @@ test_version(struct mpd_connection *conn)
static void
print_status(struct mpd_status *status)
{
+ const struct mpd_audio_format *audio_format;
+
LOG_INFO("volume: %i", mpd_status_get_volume(status));
LOG_INFO("repeat: %i", mpd_status_get_repeat(status));
LOG_INFO("single: %i", mpd_status_get_single(status));
@@ -129,9 +131,13 @@ print_status(struct mpd_status *status)
LOG_INFO("elaspedTime: %i", mpd_status_get_elapsed_time(status));
LOG_INFO("totalTime: %i", mpd_status_get_total_time(status));
LOG_INFO("bitRate: %i", mpd_status_get_kbit_rate(status));
- LOG_INFO("sampleRate: %i", mpd_status_get_sample_rate(status));
- LOG_INFO("bits: %i", mpd_status_get_bits(status));
- LOG_INFO("channels: %i", mpd_status_get_channels(status));
+ }
+
+ audio_format = mpd_status_get_audio_format(status);
+ if (audio_format != NULL) {
+ printf("sampleRate: %i\n", audio_format->sample_rate);
+ printf("bits: %i\n", audio_format->bits);
+ printf("channels: %i\n", audio_format->channels);
}
}

0 comments on commit b028fc0

Please sign in to comment.