From d664baff2699842f2182968f5c26dbf63babdeff Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Wed, 30 Jan 2013 21:47:12 +0100 Subject: audio_{parser,config}: convert to C++ --- src/AudioCompress/compress.h | 9 ++ src/AudioConfig.cxx | 50 ++++++++++ src/AudioConfig.hxx | 31 ++++++ src/AudioParser.cxx | 222 +++++++++++++++++++++++++++++++++++++++++++ src/AudioParser.hxx | 47 +++++++++ src/DecoderAPI.cxx | 6 +- src/Main.cxx | 2 +- src/OutputInit.cxx | 2 +- src/audio_check.h | 8 ++ src/audio_config.c | 52 ---------- src/audio_config.h | 31 ------ src/audio_parser.c | 222 ------------------------------------------- src/audio_parser.h | 57 ----------- 13 files changed, 370 insertions(+), 369 deletions(-) create mode 100644 src/AudioConfig.cxx create mode 100644 src/AudioConfig.hxx create mode 100644 src/AudioParser.cxx create mode 100644 src/AudioParser.hxx delete mode 100644 src/audio_config.c delete mode 100644 src/audio_config.h delete mode 100644 src/audio_parser.c delete mode 100644 src/audio_parser.h (limited to 'src') diff --git a/src/AudioCompress/compress.h b/src/AudioCompress/compress.h index 073d4af9..8556d16b 100644 --- a/src/AudioCompress/compress.h +++ b/src/AudioCompress/compress.h @@ -19,6 +19,10 @@ struct CompressorConfig { struct Compressor; +#ifdef __cplusplus +extern "C" { +#endif + //! Create a new compressor (use history value of 0 for default) struct Compressor *Compressor_new(unsigned int history); @@ -34,7 +38,12 @@ struct CompressorConfig *Compressor_getConfig(struct Compressor *); //! Process 16-bit signed data void Compressor_Process_int16(struct Compressor *, int16_t *data, unsigned int count); +#ifdef __cplusplus +} +#endif + //! TODO: Compressor_Process_int32, Compressor_Process_float, others as needed //! TODO: functions for getting at the peak/gain/clip history buffers (for monitoring) + #endif diff --git a/src/AudioConfig.cxx b/src/AudioConfig.cxx new file mode 100644 index 00000000..e546aed2 --- /dev/null +++ b/src/AudioConfig.cxx @@ -0,0 +1,50 @@ +/* + * Copyright (C) 2003-2013 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 "config.h" +#include "AudioConfig.hxx" +#include "audio_format.h" +#include "AudioParser.hxx" +#include "conf.h" +#include "mpd_error.h" + +static struct audio_format configured_audio_format; + +void getOutputAudioFormat(const struct audio_format *inAudioFormat, + struct audio_format *outAudioFormat) +{ + *outAudioFormat = *inAudioFormat; + audio_format_mask_apply(outAudioFormat, &configured_audio_format); +} + +void initAudioConfig(void) +{ + const struct config_param *param = config_get_param(CONF_AUDIO_OUTPUT_FORMAT); + GError *error = NULL; + bool ret; + + if (param == NULL) + return; + + ret = audio_format_parse(&configured_audio_format, param->value, + true, &error); + if (!ret) + MPD_ERROR("error parsing line %i: %s", + param->line, error->message); +} diff --git a/src/AudioConfig.hxx b/src/AudioConfig.hxx new file mode 100644 index 00000000..717a8e2e --- /dev/null +++ b/src/AudioConfig.hxx @@ -0,0 +1,31 @@ +/* + * Copyright (C) 2003-2013 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. + */ + +#ifndef MPD_AUDIO_CONFIG_HXX +#define MPD_AUDIO_CONFIG_HXX + +struct audio_format; + +void getOutputAudioFormat(const struct audio_format *inFormat, + struct audio_format *outFormat); + +/* make sure initPlayerData is called before this function!! */ +void initAudioConfig(void); + +#endif diff --git a/src/AudioParser.cxx b/src/AudioParser.cxx new file mode 100644 index 00000000..9178c3e1 --- /dev/null +++ b/src/AudioParser.cxx @@ -0,0 +1,222 @@ +/* + * Copyright (C) 2003-2011 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. + */ + +/* + * Parser functions for audio related objects. + * + */ + +#include "config.h" +#include "AudioParser.hxx" +#include "audio_format.h" +#include "audio_check.h" +#include "gcc.h" + +#include +#include +#include + +/** + * The GLib quark used for errors reported by this library. + */ +static inline GQuark +audio_parser_quark(void) +{ + return g_quark_from_static_string("audio_parser"); +} + +static bool +parse_sample_rate(const char *src, bool mask, uint32_t *sample_rate_r, + const char **endptr_r, GError **error_r) +{ + unsigned long value; + char *endptr; + + if (mask && *src == '*') { + *sample_rate_r = 0; + *endptr_r = src + 1; + return true; + } + + value = strtoul(src, &endptr, 10); + if (endptr == src) { + g_set_error(error_r, audio_parser_quark(), 0, + "Failed to parse the sample rate"); + return false; + } else if (!audio_check_sample_rate(value, error_r)) + return false; + + *sample_rate_r = value; + *endptr_r = endptr; + return true; +} + +static bool +parse_sample_format(const char *src, bool mask, + enum sample_format *sample_format_r, + const char **endptr_r, GError **error_r) +{ + unsigned long value; + char *endptr; + enum sample_format sample_format; + + if (mask && *src == '*') { + *sample_format_r = SAMPLE_FORMAT_UNDEFINED; + *endptr_r = src + 1; + return true; + } + + if (*src == 'f') { + *sample_format_r = SAMPLE_FORMAT_FLOAT; + *endptr_r = src + 1; + return true; + } + + if (memcmp(src, "dsd", 3) == 0) { + *sample_format_r = SAMPLE_FORMAT_DSD; + *endptr_r = src + 3; + return true; + } + + value = strtoul(src, &endptr, 10); + if (endptr == src) { + g_set_error(error_r, audio_parser_quark(), 0, + "Failed to parse the sample format"); + return false; + } + + switch (value) { + case 8: + sample_format = SAMPLE_FORMAT_S8; + break; + + case 16: + sample_format = SAMPLE_FORMAT_S16; + break; + + case 24: + if (memcmp(endptr, "_3", 2) == 0) + /* for backwards compatibility */ + endptr += 2; + + sample_format = SAMPLE_FORMAT_S24_P32; + break; + + case 32: + sample_format = SAMPLE_FORMAT_S32; + break; + + default: + g_set_error(error_r, audio_parser_quark(), 0, + "Invalid sample format: %lu", value); + return false; + } + + assert(audio_valid_sample_format(sample_format)); + + *sample_format_r = sample_format; + *endptr_r = endptr; + return true; +} + +static bool +parse_channel_count(const char *src, bool mask, uint8_t *channels_r, + const char **endptr_r, GError **error_r) +{ + unsigned long value; + char *endptr; + + if (mask && *src == '*') { + *channels_r = 0; + *endptr_r = src + 1; + return true; + } + + value = strtoul(src, &endptr, 10); + if (endptr == src) { + g_set_error(error_r, audio_parser_quark(), 0, + "Failed to parse the channel count"); + return false; + } else if (!audio_check_channel_count(value, error_r)) + return false; + + *channels_r = value; + *endptr_r = endptr; + return true; +} + +bool +audio_format_parse(struct audio_format *dest, const char *src, + bool mask, GError **error_r) +{ + uint32_t rate; + enum sample_format sample_format; + uint8_t channels; + + audio_format_clear(dest); + + /* parse sample rate */ + +#if GCC_CHECK_VERSION(4,7) + /* workaround -Wmaybe-uninitialized false positive */ + rate = 0; +#endif + + if (!parse_sample_rate(src, mask, &rate, &src, error_r)) + return false; + + if (*src++ != ':') { + g_set_error(error_r, audio_parser_quark(), 0, + "Sample format missing"); + return false; + } + + /* parse sample format */ + +#if GCC_CHECK_VERSION(4,7) + /* workaround -Wmaybe-uninitialized false positive */ + sample_format = SAMPLE_FORMAT_UNDEFINED; +#endif + + if (!parse_sample_format(src, mask, &sample_format, &src, error_r)) + return false; + + if (*src++ != ':') { + g_set_error(error_r, audio_parser_quark(), 0, + "Channel count missing"); + return false; + } + + /* parse channel count */ + + if (!parse_channel_count(src, mask, &channels, &src, error_r)) + return false; + + if (*src != 0) { + g_set_error(error_r, audio_parser_quark(), 0, + "Extra data after channel count: %s", src); + return false; + } + + audio_format_init(dest, rate, sample_format, channels); + assert(mask ? audio_format_mask_valid(dest) + : audio_format_valid(dest)); + + return true; +} diff --git a/src/AudioParser.hxx b/src/AudioParser.hxx new file mode 100644 index 00000000..f7855e8e --- /dev/null +++ b/src/AudioParser.hxx @@ -0,0 +1,47 @@ +/* + * Copyright (C) 2003-2013 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. + */ + +/** \file + * + * Parser functions for audio related objects. + */ + +#ifndef MPD_AUDIO_PARSER_HXX +#define MPD_AUDIO_PARSER_HXX + +#include "gerror.h" + +struct audio_format; + +/** + * Parses a string in the form "SAMPLE_RATE:BITS:CHANNELS" into an + * #audio_format. + * + * @param dest the destination #audio_format struct + * @param src the input string + * @param mask if true, then "*" is allowed for any number of items + * @param error_r location to store the error occurring, or NULL to + * ignore errors + * @return true on success + */ +bool +audio_format_parse(struct audio_format *dest, const char *src, + bool mask, GError **error_r); + +#endif diff --git a/src/DecoderAPI.cxx b/src/DecoderAPI.cxx index 29b39688..b68af19b 100644 --- a/src/DecoderAPI.cxx +++ b/src/DecoderAPI.cxx @@ -19,11 +19,7 @@ #include "config.h" #include "decoder_api.h" - -extern "C" { -#include "audio_config.h" -} - +#include "AudioConfig.hxx" #include "replay_gain_config.h" #include "MusicChunk.hxx" #include "MusicBuffer.hxx" diff --git a/src/Main.cxx b/src/Main.cxx index a22ee631..63a78d0e 100644 --- a/src/Main.cxx +++ b/src/Main.cxx @@ -51,11 +51,11 @@ #include "PlaylistRegistry.hxx" #include "ZeroconfGlue.hxx" #include "DecoderList.hxx" +#include "AudioConfig.hxx" extern "C" { #include "daemon.h" #include "stats.h" -#include "audio_config.h" #include "pcm_resample.h" } diff --git a/src/OutputInit.cxx b/src/OutputInit.cxx index dfb246a0..0d3dd05a 100644 --- a/src/OutputInit.cxx +++ b/src/OutputInit.cxx @@ -22,10 +22,10 @@ #include "OutputList.hxx" #include "FilterConfig.hxx" #include "output_api.h" +#include "AudioParser.hxx" extern "C" { #include "output_internal.h" -#include "audio_parser.h" #include "mixer_control.h" #include "mixer_type.h" #include "mixer_list.h" diff --git a/src/audio_check.h b/src/audio_check.h index e2302126..d4d3f13f 100644 --- a/src/audio_check.h +++ b/src/audio_check.h @@ -35,6 +35,10 @@ audio_format_quark(void) return g_quark_from_static_string("audio_format"); } +#ifdef __cplusplus +extern "C" { +#endif + bool audio_check_sample_rate(unsigned long sample_rate, GError **error_r); @@ -52,4 +56,8 @@ audio_format_init_checked(struct audio_format *af, unsigned long sample_rate, enum sample_format sample_format, unsigned channels, GError **error_r); +#ifdef __cplusplus +} +#endif + #endif diff --git a/src/audio_config.c b/src/audio_config.c deleted file mode 100644 index bae3c184..00000000 --- a/src/audio_config.c +++ /dev/null @@ -1,52 +0,0 @@ -/* - * Copyright (C) 2003-2013 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 "config.h" -#include "audio_config.h" -#include "audio_format.h" -#include "audio_parser.h" -#include "conf.h" -#include "mpd_error.h" - -#include - -static struct audio_format configured_audio_format; - -void getOutputAudioFormat(const struct audio_format *inAudioFormat, - struct audio_format *outAudioFormat) -{ - *outAudioFormat = *inAudioFormat; - audio_format_mask_apply(outAudioFormat, &configured_audio_format); -} - -void initAudioConfig(void) -{ - const struct config_param *param = config_get_param(CONF_AUDIO_OUTPUT_FORMAT); - GError *error = NULL; - bool ret; - - if (param == NULL) - return; - - ret = audio_format_parse(&configured_audio_format, param->value, - true, &error); - if (!ret) - MPD_ERROR("error parsing line %i: %s", - param->line, error->message); -} diff --git a/src/audio_config.h b/src/audio_config.h deleted file mode 100644 index fe43dbbc..00000000 --- a/src/audio_config.h +++ /dev/null @@ -1,31 +0,0 @@ -/* - * Copyright (C) 2003-2013 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. - */ - -#ifndef MPD_AUDIO_CONFIG_H -#define MPD_AUDIO_CONFIG_H - -struct audio_format; - -void getOutputAudioFormat(const struct audio_format *inFormat, - struct audio_format *outFormat); - -/* make sure initPlayerData is called before this function!! */ -void initAudioConfig(void); - -#endif diff --git a/src/audio_parser.c b/src/audio_parser.c deleted file mode 100644 index 152eab5d..00000000 --- a/src/audio_parser.c +++ /dev/null @@ -1,222 +0,0 @@ -/* - * Copyright (C) 2003-2011 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. - */ - -/* - * Parser functions for audio related objects. - * - */ - -#include "config.h" -#include "audio_parser.h" -#include "audio_format.h" -#include "audio_check.h" -#include "gcc.h" - -#include -#include -#include - -/** - * The GLib quark used for errors reported by this library. - */ -static inline GQuark -audio_parser_quark(void) -{ - return g_quark_from_static_string("audio_parser"); -} - -static bool -parse_sample_rate(const char *src, bool mask, uint32_t *sample_rate_r, - const char **endptr_r, GError **error_r) -{ - unsigned long value; - char *endptr; - - if (mask && *src == '*') { - *sample_rate_r = 0; - *endptr_r = src + 1; - return true; - } - - value = strtoul(src, &endptr, 10); - if (endptr == src) { - g_set_error(error_r, audio_parser_quark(), 0, - "Failed to parse the sample rate"); - return false; - } else if (!audio_check_sample_rate(value, error_r)) - return false; - - *sample_rate_r = value; - *endptr_r = endptr; - return true; -} - -static bool -parse_sample_format(const char *src, bool mask, - enum sample_format *sample_format_r, - const char **endptr_r, GError **error_r) -{ - unsigned long value; - char *endptr; - enum sample_format sample_format; - - if (mask && *src == '*') { - *sample_format_r = SAMPLE_FORMAT_UNDEFINED; - *endptr_r = src + 1; - return true; - } - - if (*src == 'f') { - *sample_format_r = SAMPLE_FORMAT_FLOAT; - *endptr_r = src + 1; - return true; - } - - if (memcmp(src, "dsd", 3) == 0) { - *sample_format_r = SAMPLE_FORMAT_DSD; - *endptr_r = src + 3; - return true; - } - - value = strtoul(src, &endptr, 10); - if (endptr == src) { - g_set_error(error_r, audio_parser_quark(), 0, - "Failed to parse the sample format"); - return false; - } - - switch (value) { - case 8: - sample_format = SAMPLE_FORMAT_S8; - break; - - case 16: - sample_format = SAMPLE_FORMAT_S16; - break; - - case 24: - if (memcmp(endptr, "_3", 2) == 0) - /* for backwards compatibility */ - endptr += 2; - - sample_format = SAMPLE_FORMAT_S24_P32; - break; - - case 32: - sample_format = SAMPLE_FORMAT_S32; - break; - - default: - g_set_error(error_r, audio_parser_quark(), 0, - "Invalid sample format: %lu", value); - return false; - } - - assert(audio_valid_sample_format(sample_format)); - - *sample_format_r = sample_format; - *endptr_r = endptr; - return true; -} - -static bool -parse_channel_count(const char *src, bool mask, uint8_t *channels_r, - const char **endptr_r, GError **error_r) -{ - unsigned long value; - char *endptr; - - if (mask && *src == '*') { - *channels_r = 0; - *endptr_r = src + 1; - return true; - } - - value = strtoul(src, &endptr, 10); - if (endptr == src) { - g_set_error(error_r, audio_parser_quark(), 0, - "Failed to parse the channel count"); - return false; - } else if (!audio_check_channel_count(value, error_r)) - return false; - - *channels_r = value; - *endptr_r = endptr; - return true; -} - -bool -audio_format_parse(struct audio_format *dest, const char *src, - bool mask, GError **error_r) -{ - uint32_t rate; - enum sample_format sample_format; - uint8_t channels; - - audio_format_clear(dest); - - /* parse sample rate */ - -#if GCC_CHECK_VERSION(4,7) - /* workaround -Wmaybe-uninitialized false positive */ - rate = 0; -#endif - - if (!parse_sample_rate(src, mask, &rate, &src, error_r)) - return false; - - if (*src++ != ':') { - g_set_error(error_r, audio_parser_quark(), 0, - "Sample format missing"); - return false; - } - - /* parse sample format */ - -#if GCC_CHECK_VERSION(4,7) - /* workaround -Wmaybe-uninitialized false positive */ - sample_format = SAMPLE_FORMAT_UNDEFINED; -#endif - - if (!parse_sample_format(src, mask, &sample_format, &src, error_r)) - return false; - - if (*src++ != ':') { - g_set_error(error_r, audio_parser_quark(), 0, - "Channel count missing"); - return false; - } - - /* parse channel count */ - - if (!parse_channel_count(src, mask, &channels, &src, error_r)) - return false; - - if (*src != 0) { - g_set_error(error_r, audio_parser_quark(), 0, - "Extra data after channel count: %s", src); - return false; - } - - audio_format_init(dest, rate, sample_format, channels); - assert(mask ? audio_format_mask_valid(dest) - : audio_format_valid(dest)); - - return true; -} diff --git a/src/audio_parser.h b/src/audio_parser.h deleted file mode 100644 index bbe868f6..00000000 --- a/src/audio_parser.h +++ /dev/null @@ -1,57 +0,0 @@ -/* - * Copyright (C) 2003-2011 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. - */ - -/** \file - * - * Parser functions for audio related objects. - */ - -#ifndef AUDIO_PARSER_H -#define AUDIO_PARSER_H - -#include "gerror.h" - -#include - -struct audio_format; - -#ifdef __cplusplus -extern "C" { -#endif - -/** - * Parses a string in the form "SAMPLE_RATE:BITS:CHANNELS" into an - * #audio_format. - * - * @param dest the destination #audio_format struct - * @param src the input string - * @param mask if true, then "*" is allowed for any number of items - * @param error_r location to store the error occurring, or NULL to - * ignore errors - * @return true on success - */ -bool -audio_format_parse(struct audio_format *dest, const char *src, - bool mask, GError **error_r); - -#ifdef __cplusplus -} -#endif - -#endif -- cgit v1.2.3