From d1e7b4e38136f9342aad76c685a13adf0e69f869 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Sat, 3 Aug 2013 21:00:50 +0200 Subject: audio_format: convert to C++ --- src/filter/AutoConvertFilterPlugin.cxx | 40 ++++++++++++++-------------------- src/filter/ChainFilterPlugin.cxx | 34 ++++++++++++++--------------- src/filter/ConvertFilterPlugin.cxx | 32 +++++++++++++-------------- src/filter/ConvertFilterPlugin.hxx | 4 ++-- src/filter/NormalizeFilterPlugin.cxx | 12 +++++----- src/filter/NullFilterPlugin.cxx | 7 +++--- src/filter/ReplayGainFilterPlugin.cxx | 14 ++++++------ src/filter/RouteFilterPlugin.cxx | 21 +++++++++--------- src/filter/VolumeFilterPlugin.cxx | 14 ++++++------ 9 files changed, 85 insertions(+), 93 deletions(-) (limited to 'src/filter') diff --git a/src/filter/AutoConvertFilterPlugin.cxx b/src/filter/AutoConvertFilterPlugin.cxx index 55ee46948..1ad42c2ab 100644 --- a/src/filter/AutoConvertFilterPlugin.cxx +++ b/src/filter/AutoConvertFilterPlugin.cxx @@ -23,18 +23,11 @@ #include "FilterPlugin.hxx" #include "FilterInternal.hxx" #include "FilterRegistry.hxx" -#include "audio_format.h" +#include "AudioFormat.hxx" #include class AutoConvertFilter final : public Filter { - /** - * The audio format being fed to the underlying filter. This - * plugin actually doesn't need this variable, we have it here - * just so our open() method doesn't return a stack pointer. - */ - audio_format child_audio_format; - /** * The underlying filter. */ @@ -52,46 +45,45 @@ public: delete filter; } - virtual const audio_format *Open(audio_format &af, GError **error_r); + virtual AudioFormat Open(AudioFormat &af, GError **error_r); virtual void Close(); virtual const void *FilterPCM(const void *src, size_t src_size, size_t *dest_size_r, GError **error_r); }; -const struct audio_format * -AutoConvertFilter::Open(audio_format &in_audio_format, GError **error_r) +AudioFormat +AutoConvertFilter::Open(AudioFormat &in_audio_format, GError **error_r) { - assert(audio_format_valid(&in_audio_format)); + assert(in_audio_format.IsValid()); /* open the "real" filter */ - child_audio_format = in_audio_format; - const audio_format *out_audio_format = - filter->Open(child_audio_format, error_r); - if (out_audio_format == nullptr) - return nullptr; + const AudioFormat child_audio_format = in_audio_format; + AudioFormat out_audio_format = filter->Open(in_audio_format, error_r); + if (!out_audio_format.IsDefined()) + return out_audio_format; /* need to convert? */ - if (!audio_format_equals(&child_audio_format, &in_audio_format)) { + if (in_audio_format != child_audio_format) { /* yes - create a convert_filter */ convert = filter_new(&convert_filter_plugin, nullptr, error_r); if (convert == nullptr) { filter->Close(); - return nullptr; + return AudioFormat::Undefined(); } - audio_format audio_format2 = in_audio_format; - const audio_format *audio_format3 = + AudioFormat audio_format2 = in_audio_format; + AudioFormat audio_format3 = convert->Open(audio_format2, error_r); - if (audio_format3 == nullptr) { + if (!audio_format3.IsDefined()) { delete convert; filter->Close(); - return nullptr; + return AudioFormat::Undefined(); } - assert(audio_format_equals(&audio_format2, &in_audio_format)); + assert(audio_format2 == in_audio_format); convert_filter_set(convert, child_audio_format); } else diff --git a/src/filter/ChainFilterPlugin.cxx b/src/filter/ChainFilterPlugin.cxx index ac0f10980..2d7fdf938 100644 --- a/src/filter/ChainFilterPlugin.cxx +++ b/src/filter/ChainFilterPlugin.cxx @@ -23,7 +23,7 @@ #include "FilterPlugin.hxx" #include "FilterInternal.hxx" #include "FilterRegistry.hxx" -#include "audio_format.h" +#include "AudioFormat.hxx" #include @@ -53,7 +53,7 @@ public: children.emplace_back(name, filter); } - virtual const audio_format *Open(audio_format &af, GError **error_r); + virtual AudioFormat Open(AudioFormat &af, GError **error_r) override; virtual void Close(); virtual const void *FilterPCM(const void *src, size_t src_size, size_t *dest_size_r, GError **error_r); @@ -96,43 +96,43 @@ ChainFilter::CloseUntil(const Filter *until) gcc_unreachable(); } -static const struct audio_format * +static AudioFormat chain_open_child(const char *name, Filter *filter, - const audio_format &prev_audio_format, + const AudioFormat &prev_audio_format, GError **error_r) { - audio_format conv_audio_format = prev_audio_format; - const audio_format *next_audio_format = + AudioFormat conv_audio_format = prev_audio_format; + const AudioFormat next_audio_format = filter->Open(conv_audio_format, error_r); - if (next_audio_format == NULL) - return NULL; + if (!next_audio_format.IsDefined()) + return next_audio_format; - if (!audio_format_equals(&conv_audio_format, &prev_audio_format)) { + if (conv_audio_format != prev_audio_format) { struct audio_format_string s; filter->Close(); g_set_error(error_r, filter_quark(), 0, "Audio format not supported by filter '%s': %s", name, - audio_format_to_string(&prev_audio_format, &s)); - return NULL; + audio_format_to_string(prev_audio_format, &s)); + return AudioFormat::Undefined(); } return next_audio_format; } -const audio_format * -ChainFilter::Open(audio_format &in_audio_format, GError **error_r) +AudioFormat +ChainFilter::Open(AudioFormat &in_audio_format, GError **error_r) { - const audio_format *audio_format = &in_audio_format; + AudioFormat audio_format = in_audio_format; for (auto &child : children) { audio_format = chain_open_child(child.name, child.filter, - *audio_format, error_r); - if (audio_format == NULL) { + audio_format, error_r); + if (!audio_format.IsDefined()) { /* rollback, close all children */ CloseUntil(child.filter); - return NULL; + break; } } diff --git a/src/filter/ConvertFilterPlugin.cxx b/src/filter/ConvertFilterPlugin.cxx index 09a2c9848..f98184489 100644 --- a/src/filter/ConvertFilterPlugin.cxx +++ b/src/filter/ConvertFilterPlugin.cxx @@ -25,7 +25,7 @@ #include "conf.h" #include "pcm/PcmConvert.hxx" #include "util/Manual.hxx" -#include "audio_format.h" +#include "AudioFormat.hxx" #include "poison.h" #include @@ -36,27 +36,27 @@ class ConvertFilter final : public Filter { * The input audio format; PCM data is passed to the filter() * method in this format. */ - audio_format in_audio_format; + AudioFormat in_audio_format; /** * The output audio format; the consumer of this plugin * expects PCM data in this format. This defaults to * #in_audio_format, and can be set with convert_filter_set(). */ - audio_format out_audio_format; + AudioFormat out_audio_format; Manual state; public: - void Set(const audio_format &_out_audio_format) { - assert(audio_format_valid(&in_audio_format)); - assert(audio_format_valid(&out_audio_format)); - assert(audio_format_valid(&_out_audio_format)); + void Set(const AudioFormat &_out_audio_format) { + assert(in_audio_format.IsValid()); + assert(out_audio_format.IsValid()); + assert(_out_audio_format.IsValid()); out_audio_format = _out_audio_format; } - virtual const audio_format *Open(audio_format &af, GError **error_r); + virtual AudioFormat Open(AudioFormat &af, GError **error_r) override; virtual void Close(); virtual const void *FilterPCM(const void *src, size_t src_size, size_t *dest_size_r, GError **error_r); @@ -69,15 +69,15 @@ convert_filter_init(gcc_unused const struct config_param *param, return new ConvertFilter(); } -const struct audio_format * -ConvertFilter::Open(audio_format &audio_format, gcc_unused GError **error_r) +AudioFormat +ConvertFilter::Open(AudioFormat &audio_format, gcc_unused GError **error_r) { - assert(audio_format_valid(&audio_format)); + assert(audio_format.IsValid()); in_audio_format = out_audio_format = audio_format; state.Construct(); - return &in_audio_format; + return in_audio_format; } void @@ -93,15 +93,15 @@ const void * ConvertFilter::FilterPCM(const void *src, size_t src_size, size_t *dest_size_r, GError **error_r) { - if (audio_format_equals(&in_audio_format, &out_audio_format)) { + if (in_audio_format == out_audio_format) { /* optimized special case: no-op */ *dest_size_r = src_size; return src; } - return state->Convert(&in_audio_format, + return state->Convert(in_audio_format, src, src_size, - &out_audio_format, dest_size_r, + out_audio_format, dest_size_r, error_r); } @@ -111,7 +111,7 @@ const struct filter_plugin convert_filter_plugin = { }; void -convert_filter_set(Filter *_filter, const audio_format &out_audio_format) +convert_filter_set(Filter *_filter, const AudioFormat out_audio_format) { ConvertFilter *filter = (ConvertFilter *)_filter; diff --git a/src/filter/ConvertFilterPlugin.hxx b/src/filter/ConvertFilterPlugin.hxx index 840bf496f..c814aaf49 100644 --- a/src/filter/ConvertFilterPlugin.hxx +++ b/src/filter/ConvertFilterPlugin.hxx @@ -21,7 +21,7 @@ #define MPD_CONVERT_FILTER_PLUGIN_HXX class Filter; -struct audio_format; +struct AudioFormat; /** * Sets the output audio format for the specified filter. You must @@ -30,6 +30,6 @@ struct audio_format; * the last in a chain. */ void -convert_filter_set(Filter *filter, const audio_format &out_audio_format); +convert_filter_set(Filter *filter, AudioFormat out_audio_format); #endif diff --git a/src/filter/NormalizeFilterPlugin.cxx b/src/filter/NormalizeFilterPlugin.cxx index f4e2963cc..31bcabd36 100644 --- a/src/filter/NormalizeFilterPlugin.cxx +++ b/src/filter/NormalizeFilterPlugin.cxx @@ -22,7 +22,7 @@ #include "FilterInternal.hxx" #include "FilterRegistry.hxx" #include "pcm/PcmBuffer.hxx" -#include "audio_format.h" +#include "AudioFormat.hxx" #include "AudioCompress/compress.h" #include @@ -34,7 +34,7 @@ class NormalizeFilter final : public Filter { PcmBuffer buffer; public: - virtual const audio_format *Open(audio_format &af, GError **error_r); + virtual AudioFormat Open(AudioFormat &af, GError **error_r) override; virtual void Close(); virtual const void *FilterPCM(const void *src, size_t src_size, size_t *dest_size_r, GError **error_r); @@ -47,14 +47,14 @@ normalize_filter_init(gcc_unused const struct config_param *param, return new NormalizeFilter(); } -const struct audio_format * -NormalizeFilter::Open(audio_format &audio_format, gcc_unused GError **error_r) +AudioFormat +NormalizeFilter::Open(AudioFormat &audio_format, gcc_unused GError **error_r) { - audio_format.format = SAMPLE_FORMAT_S16; + audio_format.format = SampleFormat::S16; compressor = Compressor_new(0); - return &audio_format; + return audio_format; } void diff --git a/src/filter/NullFilterPlugin.cxx b/src/filter/NullFilterPlugin.cxx index d68065a39..3ff78aa6f 100644 --- a/src/filter/NullFilterPlugin.cxx +++ b/src/filter/NullFilterPlugin.cxx @@ -28,13 +28,14 @@ #include "FilterPlugin.hxx" #include "FilterInternal.hxx" #include "FilterRegistry.hxx" +#include "AudioFormat.hxx" #include "gcc.h" class NullFilter final : public Filter { public: - virtual const audio_format *Open(audio_format &af, - gcc_unused GError **error_r) { - return ⁡ + virtual AudioFormat Open(AudioFormat &af, + gcc_unused GError **error_r) { + return af; } virtual void Close() {} diff --git a/src/filter/ReplayGainFilterPlugin.cxx b/src/filter/ReplayGainFilterPlugin.cxx index d736c910f..be6c7ad2b 100644 --- a/src/filter/ReplayGainFilterPlugin.cxx +++ b/src/filter/ReplayGainFilterPlugin.cxx @@ -22,7 +22,7 @@ #include "FilterPlugin.hxx" #include "FilterInternal.hxx" #include "FilterRegistry.hxx" -#include "audio_format.h" +#include "AudioFormat.hxx" #include "replay_gain_info.h" #include "replay_gain_config.h" #include "MixerControl.hxx" @@ -66,7 +66,7 @@ class ReplayGainFilter final : public Filter { */ unsigned volume; - struct audio_format format; + AudioFormat format; PcmBuffer buffer; @@ -112,7 +112,7 @@ public: */ void Update(); - virtual const audio_format *Open(audio_format &af, GError **error_r); + virtual AudioFormat Open(AudioFormat &af, GError **error_r) override; virtual void Close(); virtual const void *FilterPCM(const void *src, size_t src_size, size_t *dest_size_r, GError **error_r); @@ -159,12 +159,12 @@ replay_gain_filter_init(gcc_unused const struct config_param *param, return new ReplayGainFilter(); } -const audio_format * -ReplayGainFilter::Open(audio_format &af, gcc_unused GError **error_r) +AudioFormat +ReplayGainFilter::Open(AudioFormat &af, gcc_unused GError **error_r) { format = af; - return &format; + return format; } void @@ -196,7 +196,7 @@ ReplayGainFilter::FilterPCM(const void *src, size_t src_size, memcpy(dest, src, src_size); bool success = pcm_volume(dest, src_size, - sample_format(format.format), + format.format, volume); if (!success) { g_set_error(error_r, replay_gain_quark(), 0, diff --git a/src/filter/RouteFilterPlugin.cxx b/src/filter/RouteFilterPlugin.cxx index 3dc0991f9..4f4ceacde 100644 --- a/src/filter/RouteFilterPlugin.cxx +++ b/src/filter/RouteFilterPlugin.cxx @@ -42,7 +42,7 @@ #include "config.h" #include "conf.h" #include "ConfigQuark.hxx" -#include "audio_format.h" +#include "AudioFormat.hxx" #include "CheckAudioFormat.hxx" #include "FilterPlugin.hxx" #include "FilterInternal.hxx" @@ -79,12 +79,12 @@ class RouteFilter final : public Filter { /** * The actual input format of our signal, once opened */ - struct audio_format input_format; + AudioFormat input_format; /** * The decided upon output format, once opened */ - struct audio_format output_format; + AudioFormat output_format; /** * The size, in bytes, of each multichannel frame in the @@ -120,7 +120,7 @@ public: */ bool Configure(const config_param *param, GError **error_r); - virtual const audio_format *Open(audio_format &af, GError **error_r); + virtual AudioFormat Open(AudioFormat &af, GError **error_r) override; virtual void Close(); virtual const void *FilterPCM(const void *src, size_t src_size, size_t *dest_size_r, GError **error_r); @@ -241,12 +241,12 @@ route_filter_init(const config_param *param, GError **error_r) return filter; } -const struct audio_format * -RouteFilter::Open(audio_format &audio_format, gcc_unused GError **error_r) +AudioFormat +RouteFilter::Open(AudioFormat &audio_format, gcc_unused GError **error_r) { // Copy the input format for later reference input_format = audio_format; - input_frame_size = audio_format_frame_size(&input_format); + input_frame_size = input_format.GetFrameSize(); // Decide on an output format which has enough channels, // and is otherwise identical @@ -254,9 +254,9 @@ RouteFilter::Open(audio_format &audio_format, gcc_unused GError **error_r) output_format.channels = min_output_channels; // Precalculate this simple value, to speed up allocation later - output_frame_size = audio_format_frame_size(&output_format); + output_frame_size = output_format.GetFrameSize(); - return &output_format; + return output_format; } void @@ -271,8 +271,7 @@ RouteFilter::FilterPCM(const void *src, size_t src_size, { size_t number_of_frames = src_size / input_frame_size; - size_t bytes_per_frame_per_channel = - audio_format_sample_size(&input_format); + const size_t bytes_per_frame_per_channel = input_format.GetSampleSize(); // A moving pointer that always refers to channel 0 in the input, at the currently handled frame const uint8_t *base_source = (const uint8_t *)src; diff --git a/src/filter/VolumeFilterPlugin.cxx b/src/filter/VolumeFilterPlugin.cxx index 824ad0ab4..3888cefe4 100644 --- a/src/filter/VolumeFilterPlugin.cxx +++ b/src/filter/VolumeFilterPlugin.cxx @@ -25,7 +25,7 @@ #include "conf.h" #include "pcm/PcmVolume.hxx" #include "pcm/PcmBuffer.hxx" -#include "audio_format.h" +#include "AudioFormat.hxx" #include #include @@ -36,7 +36,7 @@ class VolumeFilter final : public Filter { */ unsigned volume; - struct audio_format format; + AudioFormat format; PcmBuffer buffer; @@ -56,7 +56,7 @@ public: volume = _volume; } - virtual const audio_format *Open(audio_format &af, GError **error_r); + virtual AudioFormat Open(AudioFormat &af, GError **error_r) override; virtual void Close(); virtual const void *FilterPCM(const void *src, size_t src_size, size_t *dest_size_r, GError **error_r); @@ -75,12 +75,12 @@ volume_filter_init(gcc_unused const struct config_param *param, return new VolumeFilter(); } -const struct audio_format * -VolumeFilter::Open(audio_format &audio_format, gcc_unused GError **error_r) +AudioFormat +VolumeFilter::Open(AudioFormat &audio_format, gcc_unused GError **error_r) { format = audio_format; - return &format; + return format; } void @@ -112,7 +112,7 @@ VolumeFilter::FilterPCM(const void *src, size_t src_size, memcpy(dest, src, src_size); bool success = pcm_volume(dest, src_size, - sample_format(format.format), + format.format, volume); if (!success) { g_set_error(error_r, volume_quark(), 0, -- cgit v1.2.3