Commit 361404fd authored by Max Kellermann's avatar Max Kellermann

pcm_convert: convert to C++

parent 762c91b7
......@@ -331,7 +331,7 @@ libevent_a_SOURCES = \
libpcm_a_SOURCES = \
src/pcm_buffer.c src/pcm_buffer.h \
src/pcm_export.c src/pcm_export.h \
src/pcm_convert.c src/pcm_convert.h \
src/PcmConvert.cxx src/PcmConvert.hxx \
src/dsd2pcm/dsd2pcm.c src/dsd2pcm/dsd2pcm.h \
src/pcm_dsd.c src/pcm_dsd.h \
src/pcm_dsd_usb.c src/pcm_dsd_usb.h \
......
......@@ -402,10 +402,11 @@ decoder_data(struct decoder *decoder,
}
if (!audio_format_equals(&dc->in_audio_format, &dc->out_audio_format)) {
data = pcm_convert(&decoder->conv_state,
&dc->in_audio_format, data, length,
&dc->out_audio_format, &length,
&error);
data = decoder->conv_state.Convert(&dc->in_audio_format,
data, length,
&dc->out_audio_format,
&length,
&error);
if (data == NULL) {
/* the PCM conversion has failed - stop
playback, since we have no better way to
......
......@@ -40,8 +40,6 @@ decoder::~decoder()
if (decoder_tag != nullptr)
tag_free(decoder_tag);
pcm_convert_deinit(&conv_state);
}
/**
......
......@@ -21,7 +21,7 @@
#define MPD_DECODER_INTERNAL_HXX
#include "decoder_command.h"
#include "pcm_convert.h"
#include "PcmConvert.hxx"
#include "replay_gain_info.h"
struct input_stream;
......@@ -29,7 +29,7 @@ struct input_stream;
struct decoder {
struct decoder_control *dc;
struct pcm_convert_state conv_state;
PcmConvert conv_state;
/**
* The time stamp of the next data chunk, in seconds.
......@@ -91,7 +91,6 @@ struct decoder {
song_tag(_tag), stream_tag(nullptr), decoder_tag(nullptr),
chunk(nullptr),
replay_gain_serial(0) {
pcm_convert_init(&conv_state);
}
~decoder();
......
......@@ -17,13 +17,15 @@
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#ifndef PCM_CONVERT_H
#define PCM_CONVERT_H
#ifndef PCM_CONVERT_HXX
#define PCM_CONVERT_HXX
extern "C" {
#include "pcm_dsd.h"
#include "pcm_resample.h"
#include "pcm_dither.h"
#include "pcm_buffer.h"
}
#include <glib.h>
......@@ -34,7 +36,7 @@ struct audio_format;
* holds buffer allocations and the state for all kinds of PCM
* conversions.
*/
struct pcm_convert_state {
class PcmConvert {
struct pcm_dsd dsd;
struct pcm_resample_state resample;
......@@ -46,6 +48,61 @@ struct pcm_convert_state {
/** the buffer for converting the channel count */
struct pcm_buffer channels_buffer;
public:
PcmConvert();
~PcmConvert();
/**
* Reset the pcm_convert_state object. Use this at the
* boundary between two distinct songs and each time the
* format changes.
*/
void Reset();
/**
* Converts PCM data between two audio formats.
*
* @param src_format the source audio format
* @param src the source PCM buffer
* @param src_size the size of #src in bytes
* @param dest_format the requested destination audio format
* @param dest_size_r returns the number of bytes of the destination buffer
* @param error_r location to store the error occurring, or NULL to
* ignore errors
* @return the destination buffer, or NULL on error
*/
const void *Convert(const audio_format *src_format,
const void *src, size_t src_size,
const audio_format *dest_format,
size_t *dest_size_r,
GError **error_r);
private:
const int16_t *Convert16(const audio_format *src_format,
const void *src_buffer, size_t src_size,
const audio_format *dest_format,
size_t *dest_size_r,
GError **error_r);
const int32_t *Convert24(const audio_format *src_format,
const void *src_buffer, size_t src_size,
const audio_format *dest_format,
size_t *dest_size_r,
GError **error_r);
const int32_t *Convert32(const audio_format *src_format,
const void *src_buffer, size_t src_size,
const audio_format *dest_format,
size_t *dest_size_r,
GError **error_r);
const float *ConvertFloat(const audio_format *src_format,
const void *src_buffer, size_t src_size,
const audio_format *dest_format,
size_t *dest_size_r,
GError **error_r);
};
static inline GQuark
......@@ -54,47 +111,4 @@ pcm_convert_quark(void)
return g_quark_from_static_string("pcm_convert");
}
G_BEGIN_DECLS
/**
* Initializes a pcm_convert_state object.
*/
void pcm_convert_init(struct pcm_convert_state *state);
/**
* Deinitializes a pcm_convert_state object and frees allocated
* memory.
*/
void pcm_convert_deinit(struct pcm_convert_state *state);
/**
* Reset the pcm_convert_state object. Use this at the boundary
* between two distinct songs and each time the format changes.
*/
void
pcm_convert_reset(struct pcm_convert_state *state);
/**
* Converts PCM data between two audio formats.
*
* @param state an initialized pcm_convert_state object
* @param src_format the source audio format
* @param src the source PCM buffer
* @param src_size the size of #src in bytes
* @param dest_format the requested destination audio format
* @param dest_size_r returns the number of bytes of the destination buffer
* @param error_r location to store the error occurring, or NULL to
* ignore errors
* @return the destination buffer, or NULL on error
*/
const void *
pcm_convert(struct pcm_convert_state *state,
const struct audio_format *src_format,
const void *src, size_t src_size,
const struct audio_format *dest_format,
size_t *dest_size_r,
GError **error_r);
G_END_DECLS
#endif
......@@ -23,7 +23,8 @@
#include "filter_internal.h"
#include "filter_registry.h"
#include "conf.h"
#include "pcm_convert.h"
#include "PcmConvert.hxx"
#include "util/Manual.hxx"
#include "audio_format.h"
#include "poison.h"
......@@ -51,7 +52,7 @@ struct ConvertFilter {
*/
struct audio_format out_audio_format;
struct pcm_convert_state state;
Manual<PcmConvert> state;
ConvertFilter() {
filter_init(&base, &convert_filter_plugin);
......@@ -81,7 +82,7 @@ convert_filter_open(struct filter *_filter, struct audio_format *audio_format,
assert(audio_format_valid(audio_format));
filter->in_audio_format = filter->out_audio_format = *audio_format;
pcm_convert_init(&filter->state);
filter->state.Construct();
return &filter->in_audio_format;
}
......@@ -91,7 +92,7 @@ convert_filter_close(struct filter *_filter)
{
ConvertFilter *filter = (ConvertFilter *)_filter;
pcm_convert_deinit(&filter->state);
filter->state.Destruct();
poison_undefined(&filter->in_audio_format,
sizeof(filter->in_audio_format));
......@@ -113,10 +114,10 @@ convert_filter_filter(struct filter *_filter, const void *src, size_t src_size,
return src;
}
dest = pcm_convert(&filter->state, &filter->in_audio_format,
src, src_size,
&filter->out_audio_format, dest_size_r,
error_r);
dest = filter->state->Convert(&filter->in_audio_format,
src, src_size,
&filter->out_audio_format, dest_size_r,
error_r);
if (dest == NULL)
return NULL;
......
......@@ -26,7 +26,7 @@
#include "config.h"
#include "AudioParser.hxx"
#include "audio_format.h"
#include "pcm_convert.h"
#include "PcmConvert.hxx"
#include "conf.h"
#include "util/fifo_buffer.h"
#include "stdbin.h"
......@@ -58,7 +58,6 @@ int main(int argc, char **argv)
{
GError *error = NULL;
struct audio_format in_audio_format, out_audio_format;
struct pcm_convert_state state;
const void *output;
ssize_t nbytes;
size_t length;
......@@ -90,7 +89,7 @@ int main(int argc, char **argv)
const size_t in_frame_size = audio_format_frame_size(&in_audio_format);
pcm_convert_init(&state);
PcmConvert state;
struct fifo_buffer *buffer = fifo_buffer_new(4096);
......@@ -113,8 +112,8 @@ int main(int argc, char **argv)
fifo_buffer_consume(buffer, length);
output = pcm_convert(&state, &in_audio_format, src, length,
&out_audio_format, &length, &error);
output = state.Convert(&in_audio_format, src, length,
&out_audio_format, &length, &error);
if (output == NULL) {
g_printerr("Failed to convert: %s\n", error->message);
return 2;
......@@ -123,5 +122,5 @@ int main(int argc, char **argv)
G_GNUC_UNUSED ssize_t ignored = write(1, output, length);
}
pcm_convert_deinit(&state);
return EXIT_SUCCESS;
}
......@@ -27,12 +27,12 @@
#include "IOThread.hxx"
#include "fs/Path.hxx"
#include "AudioParser.hxx"
#include "PcmConvert.hxx"
extern "C" {
#include "output_plugin.h"
#include "output_internal.h"
#include "filter_registry.h"
#include "pcm_convert.h"
}
#include "PlayerControl.hxx"
......@@ -52,21 +52,15 @@ GlobalEvents::Emit(gcc_unused Event event)
{
}
void pcm_convert_init(G_GNUC_UNUSED struct pcm_convert_state *state)
{
}
void pcm_convert_deinit(G_GNUC_UNUSED struct pcm_convert_state *state)
{
}
PcmConvert::PcmConvert() {}
PcmConvert::~PcmConvert() {}
const void *
pcm_convert(G_GNUC_UNUSED struct pcm_convert_state *state,
G_GNUC_UNUSED const struct audio_format *src_format,
G_GNUC_UNUSED const void *src, G_GNUC_UNUSED size_t src_size,
G_GNUC_UNUSED const struct audio_format *dest_format,
G_GNUC_UNUSED size_t *dest_size_r,
GError **error_r)
PcmConvert::Convert(gcc_unused const audio_format *src_format,
gcc_unused const void *src, gcc_unused size_t src_size,
gcc_unused const audio_format *dest_format,
gcc_unused size_t *dest_size_r,
gcc_unused GError **error_r)
{
g_set_error(error_r, pcm_convert_quark(), 0,
"Not implemented");
......
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