CheckAudioFormat.cxx 2.14 KB
Newer Older
1
/*
2
 * Copyright (C) 2003-2013 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 * 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.
 */

20
#include "config.h"
21
#include "CheckAudioFormat.hxx"
22
#include "AudioFormat.hxx"
23 24
#include "util/Error.hxx"
#include "util/Domain.hxx"
25 26 27

#include <assert.h>

28 29
const Domain audio_format_domain("audio_format");

30
bool
31
audio_check_sample_rate(unsigned long sample_rate, Error &error)
32 33
{
	if (!audio_valid_sample_rate(sample_rate)) {
34 35
		error.Format(audio_format_domain,
			     "Invalid sample rate: %lu", sample_rate);
36 37 38 39 40 41 42
		return false;
	}

	return true;
}

bool
43
audio_check_sample_format(SampleFormat sample_format, Error &error)
44 45
{
	if (!audio_valid_sample_format(sample_format)) {
46 47 48
		error.Format(audio_format_domain,
			     "Invalid sample format: %u",
			     unsigned(sample_format));
49 50 51 52 53 54 55
		return false;
	}

	return true;
}

bool
56
audio_check_channel_count(unsigned channels, Error &error)
57 58
{
	if (!audio_valid_channel_count(channels)) {
59 60
		error.Format(audio_format_domain,
			     "Invalid channel count: %u", channels);
61 62 63 64 65 66 67
		return false;
	}

	return true;
}

bool
68 69
audio_format_init_checked(AudioFormat &af, unsigned long sample_rate,
			  SampleFormat sample_format, unsigned channels,
70
			  Error &error)
71
{
72 73 74
	if (audio_check_sample_rate(sample_rate, error) &&
	    audio_check_sample_format(sample_format, error) &&
	    audio_check_channel_count(channels, error)) {
75 76
		af = AudioFormat(sample_rate, sample_format, channels);
		assert(af.IsValid());
77 78 79 80
		return true;
	} else
		return false;
}