CheckAudioFormat: migrate from class Error to C++ exceptions

This commit is contained in:
Max Kellermann 2016-11-10 11:45:17 +01:00
parent 12f11c97ae
commit cfd51db229
22 changed files with 98 additions and 212 deletions

View File

@ -20,61 +20,43 @@
#include "config.h"
#include "CheckAudioFormat.hxx"
#include "AudioFormat.hxx"
#include "util/Error.hxx"
#include "util/Domain.hxx"
#include "util/RuntimeError.hxx"
#include <stdexcept>
#include <assert.h>
const Domain audio_format_domain("audio_format");
bool
audio_check_sample_rate(unsigned long sample_rate, Error &error)
void
CheckSampleRate(unsigned long sample_rate)
{
if (!audio_valid_sample_rate(sample_rate)) {
error.Format(audio_format_domain,
"Invalid sample rate: %lu", sample_rate);
return false;
}
return true;
if (!audio_valid_sample_rate(sample_rate))
throw FormatRuntimeError("Invalid sample rate: %lu",
sample_rate);
}
bool
audio_check_sample_format(SampleFormat sample_format, Error &error)
void
CheckSampleFormat(SampleFormat sample_format)
{
if (!audio_valid_sample_format(sample_format)) {
error.Format(audio_format_domain,
"Invalid sample format: %u",
unsigned(sample_format));
return false;
}
return true;
if (!audio_valid_sample_format(sample_format))
throw FormatRuntimeError("Invalid sample format: %u",
unsigned(sample_format));
}
bool
audio_check_channel_count(unsigned channels, Error &error)
void
CheckChannelCount(unsigned channels)
{
if (!audio_valid_channel_count(channels)) {
error.Format(audio_format_domain,
"Invalid channel count: %u", channels);
return false;
}
return true;
if (!audio_valid_channel_count(channels))
throw FormatRuntimeError("Invalid channel count: %u",
channels);
}
bool
audio_format_init_checked(AudioFormat &af, unsigned long sample_rate,
SampleFormat sample_format, unsigned channels,
Error &error)
AudioFormat
CheckAudioFormat(unsigned long sample_rate,
SampleFormat sample_format, unsigned channels)
{
if (audio_check_sample_rate(sample_rate, error) &&
audio_check_sample_format(sample_format, error) &&
audio_check_channel_count(channels, error)) {
af = AudioFormat(sample_rate, sample_format, channels);
assert(af.IsValid());
return true;
} else
return false;
CheckSampleRate(sample_rate);
CheckSampleFormat(sample_format);
CheckChannelCount(channels);
return AudioFormat(sample_rate, sample_format, channels);
}

View File

@ -22,25 +22,23 @@
#include "AudioFormat.hxx"
class Error;
void
CheckSampleRate(unsigned long sample_rate);
extern const class Domain audio_format_domain;
void
CheckSampleFormat(SampleFormat sample_format);
bool
audio_check_sample_rate(unsigned long sample_rate, Error &error);
bool
audio_check_sample_format(SampleFormat sample_format, Error &error);
bool
audio_check_channel_count(unsigned sample_format, Error &error);
void
CheckChannelCount(unsigned sample_format);
/**
* Wrapper for audio_format_init(), which checks all attributes.
* Check #AudioFormat attributes and construct an #AudioFormat
* instance.
*
* Throws #std::runtime_error on error.
*/
bool
audio_format_init_checked(AudioFormat &af, unsigned long sample_rate,
SampleFormat sample_format, unsigned channels,
Error &error);
AudioFormat
CheckAudioFormat(unsigned long sample_rate,
SampleFormat sample_format, unsigned channels);
#endif

View File

@ -23,7 +23,6 @@
#include "../DecoderAPI.hxx"
#include "CheckAudioFormat.hxx"
#include "fs/Path.hxx"
#include "util/Error.hxx"
#include "util/Domain.hxx"
#include "util/Macros.hxx"
#include "Log.hxx"
@ -43,13 +42,8 @@ adplug_init(const ConfigBlock &block)
FormatDebug(adplug_domain, "adplug %s",
CAdPlug::get_version().c_str());
Error error;
sample_rate = block.GetBlockValue("sample_rate", 48000u);
if (!audio_check_sample_rate(sample_rate, error)) {
LogError(error);
return false;
}
CheckSampleRate(sample_rate);
return true;
}

View File

@ -24,7 +24,6 @@
#include "CheckAudioFormat.hxx"
#include "tag/TagHandler.hxx"
#include "util/ScopeExit.hxx"
#include "util/Error.hxx"
#include "util/Domain.hxx"
#include "Log.hxx"
@ -198,16 +197,10 @@ audiofile_stream_decode(Decoder &decoder, InputStream &is)
AtScopeExit(fh) { afCloseFile(fh); };
Error error;
AudioFormat audio_format;
if (!audio_format_init_checked(audio_format,
afGetRate(fh, AF_DEFAULT_TRACK),
audiofile_setup_sample_format(fh),
afGetVirtualChannels(fh, AF_DEFAULT_TRACK),
error)) {
LogError(error);
return;
}
const auto audio_format =
CheckAudioFormat(afGetRate(fh, AF_DEFAULT_TRACK),
audiofile_setup_sample_format(fh),
afGetVirtualChannels(fh, AF_DEFAULT_TRACK));
const auto total_time = audiofile_get_duration(fh);

View File

@ -32,7 +32,6 @@
#include "input/InputStream.hxx"
#include "CheckAudioFormat.hxx"
#include "util/bit_reverse.h"
#include "util/Error.hxx"
#include "system/ByteOrder.hxx"
#include "tag/TagHandler.hxx"
#include "DsdLib.hxx"
@ -426,14 +425,9 @@ dsdiff_stream_decode(Decoder &decoder, InputStream &is)
if (!dsdiff_read_metadata(&decoder, is, &metadata, &chunk_header))
return;
Error error;
AudioFormat audio_format;
if (!audio_format_init_checked(audio_format, metadata.sample_rate / 8,
SampleFormat::DSD,
metadata.channels, error)) {
LogError(error);
return;
}
auto audio_format = CheckAudioFormat(metadata.sample_rate / 8,
SampleFormat::DSD,
metadata.channels);
/* calculate song time from DSD chunk size and sample frequency */
offset_type chunk_size = metadata.chunk_size;
@ -466,12 +460,9 @@ dsdiff_scan_stream(InputStream &is,
if (!dsdiff_read_metadata(nullptr, is, &metadata, &chunk_header))
return false;
AudioFormat audio_format;
if (!audio_format_init_checked(audio_format, metadata.sample_rate / 8,
SampleFormat::DSD,
metadata.channels, IgnoreError()))
/* refuse to parse files which we cannot play anyway */
return false;
auto audio_format = CheckAudioFormat(metadata.sample_rate / 8,
SampleFormat::DSD,
metadata.channels);
/* calculate song time and add as tag */
uint64_t n_frames = metadata.chunk_size / audio_format.channels;

View File

@ -33,7 +33,6 @@
#include "input/InputStream.hxx"
#include "CheckAudioFormat.hxx"
#include "util/bit_reverse.h"
#include "util/Error.hxx"
#include "system/ByteOrder.hxx"
#include "DsdLib.hxx"
#include "tag/TagHandler.hxx"
@ -307,14 +306,10 @@ dsf_stream_decode(Decoder &decoder, InputStream &is)
if (!dsf_read_metadata(&decoder, is, &metadata))
return;
Error error;
AudioFormat audio_format;
if (!audio_format_init_checked(audio_format, metadata.sample_rate / 8,
SampleFormat::DSD,
metadata.channels, error)) {
LogError(error);
return;
}
auto audio_format = CheckAudioFormat(metadata.sample_rate / 8,
SampleFormat::DSD,
metadata.channels);
/* Calculate song time from DSD chunk size and sample frequency */
const auto n_blocks = metadata.n_blocks;
auto songtime = SongTime::FromScale<uint64_t>(n_blocks * DSF_BLOCK_SIZE,
@ -339,12 +334,9 @@ dsf_scan_stream(InputStream &is,
if (!dsf_read_metadata(nullptr, is, &metadata))
return false;
AudioFormat audio_format;
if (!audio_format_init_checked(audio_format, metadata.sample_rate / 8,
SampleFormat::DSD,
metadata.channels, IgnoreError()))
/* refuse to parse files which we cannot play anyway */
return false;
auto audio_format = CheckAudioFormat(metadata.sample_rate / 8,
SampleFormat::DSD,
metadata.channels);
/* calculate song time and add as tag */
const auto n_blocks = metadata.n_blocks;

View File

@ -273,8 +273,9 @@ faad_decoder_init(NeAACDecHandle decoder, DecoderBuffer &buffer,
buffer.Consume(nbytes);
return audio_format_init_checked(audio_format, sample_rate,
SampleFormat::S16, channels, error);
audio_format = CheckAudioFormat(sample_rate, SampleFormat::S16,
channels);
return true;
}
/**

View File

@ -40,7 +40,6 @@
#include "CheckAudioFormat.hxx"
#include "util/ScopeExit.hxx"
#include "util/ConstBuffer.hxx"
#include "util/Error.hxx"
#include "LogV.hxx"
extern "C" {
@ -666,15 +665,9 @@ FfmpegDecode(Decoder &decoder, InputStream &input,
return;
}
Error error;
AudioFormat audio_format;
if (!audio_format_init_checked(audio_format,
codec_params.sample_rate,
sample_format,
codec_params.channels, error)) {
LogError(error);
return;
}
const auto audio_format = CheckAudioFormat(codec_params.sample_rate,
sample_format,
codec_params.channels);
/* the audio format must be read from AVCodecContext by now,
because avcodec_open() has been demonstrated to fill bogus

View File

@ -58,12 +58,7 @@ FlacPcmImport::Open(unsigned sample_rate, unsigned bits_per_sample,
return false;
}
if (!audio_format_init_checked(audio_format,
sample_rate,
sample_format,
channels, error))
return false;
audio_format = CheckAudioFormat(sample_rate, sample_format, channels);
return true;
}

View File

@ -39,9 +39,6 @@ class FlacPcmImport {
AudioFormat audio_format;
public:
/**
* @return false on error
*/
bool Open(unsigned sample_rate, unsigned bits_per_sample,
unsigned channels, Error &error);

View File

@ -22,7 +22,6 @@
#include "../DecoderAPI.hxx"
#include "CheckAudioFormat.hxx"
#include "fs/Path.hxx"
#include "util/Error.hxx"
#include "util/Domain.hxx"
#include "util/Macros.hxx"
#include "Log.hxx"
@ -75,13 +74,8 @@ fluidsynth_mpd_log_function(int level, char *message, gcc_unused void *data)
static bool
fluidsynth_init(const ConfigBlock &block)
{
Error error;
sample_rate = block.GetBlockValue("sample_rate", 48000u);
if (!audio_check_sample_rate(sample_rate, error)) {
LogError(error);
return false;
}
CheckSampleRate(sample_rate);
soundfont_path = block.GetBlockValue("soundfont",
"/usr/share/sounds/sf2/FluidR3_GM.sf2");

View File

@ -29,7 +29,6 @@
#include "util/FormatString.hxx"
#include "util/AllocatedString.hxx"
#include "util/UriUtil.hxx"
#include "util/Error.hxx"
#include "util/Domain.hxx"
#include "Log.hxx"
@ -168,14 +167,9 @@ gme_file_decode(Decoder &decoder, Path path_fs)
/* initialize the MPD decoder */
Error error;
AudioFormat audio_format;
if (!audio_format_init_checked(audio_format, GME_SAMPLE_RATE,
SampleFormat::S16, GME_CHANNELS,
error)) {
LogError(error);
return;
}
const auto audio_format = CheckAudioFormat(GME_SAMPLE_RATE,
SampleFormat::S16,
GME_CHANNELS);
decoder_initialized(decoder, audio_format, true, song_len);

View File

@ -29,7 +29,6 @@
#include "tag/MixRamp.hxx"
#include "CheckAudioFormat.hxx"
#include "util/StringCompare.hxx"
#include "util/Error.hxx"
#include "util/Domain.hxx"
#include "Log.hxx"
@ -1051,19 +1050,10 @@ mp3_decode(Decoder &decoder, InputStream &input_stream)
data.AllocateBuffers();
Error error;
AudioFormat audio_format;
if (!audio_format_init_checked(audio_format,
data.frame.header.samplerate,
SampleFormat::S24_P32,
MAD_NCHANNELS(&data.frame.header),
error)) {
LogError(error);
delete tag;
return;
}
decoder_initialized(decoder, audio_format,
decoder_initialized(decoder,
CheckAudioFormat(data.frame.header.samplerate,
SampleFormat::S24_P32,
MAD_NCHANNELS(&data.frame.header)),
input_stream.IsSeekable(),
data.total_time);

View File

@ -24,7 +24,6 @@
#include "CheckAudioFormat.hxx"
#include "pcm/Traits.hxx"
#include "tag/TagHandler.hxx"
#include "util/Error.hxx"
#include "util/Domain.hxx"
#include "util/Macros.hxx"
#include "util/Clamp.hxx"
@ -162,15 +161,9 @@ mpcdec_decode(Decoder &mpd_decoder, InputStream &is)
mpc_streaminfo info;
mpc_demux_get_info(demux, &info);
Error error;
AudioFormat audio_format;
if (!audio_format_init_checked(audio_format, info.sample_freq,
mpcdec_sample_format,
info.channels, error)) {
LogError(error);
mpc_demux_exit(demux);
return;
}
auto audio_format = CheckAudioFormat(info.sample_freq,
mpcdec_sample_format,
info.channels);
ReplayGainInfo rgi;
rgi.Clear();

View File

@ -26,7 +26,6 @@
#include "tag/ReplayGain.hxx"
#include "tag/MixRamp.hxx"
#include "fs/Path.hxx"
#include "util/Error.hxx"
#include "util/Domain.hxx"
#include "util/StringView.hxx"
#include "Log.hxx"
@ -95,13 +94,7 @@ mpd_mpg123_open(mpg123_handle *handle, const char *path_fs,
return false;
}
Error error2;
if (!audio_format_init_checked(audio_format, rate, SampleFormat::S16,
channels, error2)) {
LogError(error2);
return false;
}
audio_format = CheckAudioFormat(rate, SampleFormat::S16, channels);
return true;
}

View File

@ -32,7 +32,6 @@
#include "tag/TagHandler.hxx"
#include "tag/TagBuilder.hxx"
#include "input/InputStream.hxx"
#include "util/Error.hxx"
#include "util/RuntimeError.hxx"
#include "Log.hxx"

View File

@ -23,7 +23,7 @@
#include "CheckAudioFormat.hxx"
#include "input/InputStream.hxx"
#include "system/ByteOrder.hxx"
#include "util/Error.hxx"
#include "util/Domain.hxx"
#include "util/ByteReverse.hxx"
#include "util/NumberParser.hxx"
#include "util/MimeType.hxx"
@ -33,6 +33,8 @@
#include <string.h>
static constexpr Domain pcm_decoder_domain("pcm_decoder");
static void
pcm_stream_decode(Decoder &decoder, InputStream &is)
{
@ -62,7 +64,6 @@ pcm_stream_decode(Decoder &decoder, InputStream &is)
{
const auto mime_parameters = ParseMimeTypeParameters(mime);
Error error;
/* MIME type parameters according to RFC 2586 */
auto i = mime_parameters.find("rate");
@ -71,14 +72,16 @@ pcm_stream_decode(Decoder &decoder, InputStream &is)
char *endptr;
unsigned value = ParseUnsigned(s, &endptr);
if (endptr == s || *endptr != 0) {
FormatWarning(audio_format_domain,
FormatWarning(pcm_decoder_domain,
"Failed to parse sample rate: %s",
s);
return;
}
if (!audio_check_sample_rate(value, error)) {
LogError(error);
try {
CheckSampleRate(value);
} catch (const std::runtime_error &e) {
LogError(e);
return;
}
@ -91,14 +94,16 @@ pcm_stream_decode(Decoder &decoder, InputStream &is)
char *endptr;
unsigned value = ParseUnsigned(s, &endptr);
if (endptr == s || *endptr != 0) {
FormatWarning(audio_format_domain,
FormatWarning(pcm_decoder_domain,
"Failed to parse sample rate: %s",
s);
return;
}
if (!audio_check_channel_count(value, error)) {
LogError(error);
try {
CheckChannelCount(value);
} catch (const std::runtime_error &e) {
LogError(e);
return;
}
@ -107,7 +112,7 @@ pcm_stream_decode(Decoder &decoder, InputStream &is)
}
if (audio_format.sample_rate == 0) {
FormatWarning(audio_format_domain,
FormatWarning(pcm_decoder_domain,
"Missing 'rate' parameter: %s",
mime);
return;

View File

@ -27,7 +27,6 @@
#include "util/FormatString.hxx"
#include "util/AllocatedString.hxx"
#include "util/Domain.hxx"
#include "util/Error.hxx"
#include "system/ByteOrder.hxx"
#include "system/FatalError.hxx"
#include "Log.hxx"

View File

@ -23,7 +23,6 @@
#include "input/InputStream.hxx"
#include "CheckAudioFormat.hxx"
#include "tag/TagHandler.hxx"
#include "util/Error.hxx"
#include "util/Domain.hxx"
#include "Log.hxx"
@ -201,14 +200,10 @@ sndfile_stream_decode(Decoder &decoder, InputStream &is)
return;
}
Error error;
AudioFormat audio_format;
if (!audio_format_init_checked(audio_format, info.samplerate,
sndfile_sample_format(info),
info.channels, error)) {
LogError(error);
return;
}
const auto audio_format =
CheckAudioFormat(info.samplerate,
sndfile_sample_format(info),
info.channels);
decoder_initialized(decoder, audio_format, info.seekable,
sndfile_duration(info));

View File

@ -29,7 +29,6 @@
#include "input/Reader.hxx"
#include "OggCodec.hxx"
#include "pcm/Interleave.hxx"
#include "util/Error.hxx"
#include "util/Macros.hxx"
#include "util/ScopeExit.hxx"
#include "CheckAudioFormat.hxx"
@ -166,10 +165,7 @@ VorbisDecoder::SubmitInit()
{
assert(!dsp_initialized);
Error error;
if (!audio_format_init_checked(audio_format, vi.rate, sample_format,
vi.channels, error))
throw std::runtime_error(error.GetMessage());
audio_format = CheckAudioFormat(vi.rate, sample_format, vi.channels);
frame_size = audio_format.GetFrameSize();

View File

@ -25,7 +25,6 @@
#include "tag/TagHandler.hxx"
#include "tag/ApeTag.hxx"
#include "fs/Path.hxx"
#include "util/Error.hxx"
#include "util/Domain.hxx"
#include "util/Macros.hxx"
#include "util/Alloc.hxx"
@ -149,15 +148,9 @@ wavpack_decode(Decoder &decoder, WavpackContext *wpc, bool can_seek)
wavpack_bits_to_sample_format(is_float,
WavpackGetBytesPerSample(wpc));
Error error;
AudioFormat audio_format;
if (!audio_format_init_checked(audio_format,
WavpackGetSampleRate(wpc),
sample_format,
WavpackGetNumChannels(wpc), error)) {
LogError(error);
return;
}
auto audio_format = CheckAudioFormat(WavpackGetSampleRate(wpc),
sample_format,
WavpackGetNumChannels(wpc));
const format_samples_t format_samples = is_float
? format_samples_float

View File

@ -21,7 +21,6 @@
#include "WildmidiDecoderPlugin.hxx"
#include "../DecoderAPI.hxx"
#include "tag/TagHandler.hxx"
#include "util/Error.hxx"
#include "util/Domain.hxx"
#include "fs/AllocatedPath.hxx"
#include "fs/FileSystem.hxx"