AudioParser.cxx 4.16 KB
Newer Older
1
/*
2
 * Copyright 2003-2018 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13
 * 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.
14 15 16 17
 *
 * 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.
18 19 20 21 22 23 24
 */

/*
 * Parser functions for audio related objects.
 *
 */

25
#include "AudioParser.hxx"
26
#include "AudioFormat.hxx"
27
#include "util/RuntimeError.hxx"
28

29
#include <assert.h>
30
#include <string.h>
31 32
#include <stdlib.h>

33 34
static uint32_t
ParseSampleRate(const char *src, bool mask, const char **endptr_r)
35 36
{
	unsigned long value;
37
	char *endptr;
38

39 40
	if (mask && *src == '*') {
		*endptr_r = src + 1;
41
		return 0;
42 43
	}

44 45
	value = strtoul(src, &endptr, 10);
	if (endptr == src) {
46
		throw std::invalid_argument("Failed to parse the sample rate");
47
	} else if (!audio_valid_sample_rate(value))
48
		throw FormatInvalidArgument("Invalid sample rate: %lu", value);
49

50
	*endptr_r = endptr;
51
	return value;
52
}
53

54 55
static SampleFormat
ParseSampleFormat(const char *src, bool mask, const char **endptr_r)
56 57 58
{
	unsigned long value;
	char *endptr;
59
	SampleFormat sample_format;
60

61 62
	if (mask && *src == '*') {
		*endptr_r = src + 1;
63
		return SampleFormat::UNDEFINED;
64 65
	}

66 67
	if (*src == 'f') {
		*endptr_r = src + 1;
68
		return SampleFormat::FLOAT;
69 70
	}

71
	if (memcmp(src, "dsd", 3) == 0) {
72
		*endptr_r = src + 3;
73
		return SampleFormat::DSD;
74 75
	}

76
	value = strtoul(src, &endptr, 10);
77
	if (endptr == src)
78
		throw std::invalid_argument("Failed to parse the sample format");
79 80 81

	switch (value) {
	case 8:
82
		sample_format = SampleFormat::S8;
83 84 85
		break;

	case 16:
86
		sample_format = SampleFormat::S16;
87 88 89
		break;

	case 24:
90 91
		if (memcmp(endptr, "_3", 2) == 0)
			/* for backwards compatibility */
92
			endptr += 2;
93

94
		sample_format = SampleFormat::S24_P32;
95 96 97
		break;

	case 32:
98
		sample_format = SampleFormat::S32;
99 100 101
		break;

	default:
102 103
		throw FormatInvalidArgument("Invalid sample format: %lu",
					    value);
104 105 106
	}

	assert(audio_valid_sample_format(sample_format));
107

108
	*endptr_r = endptr;
109
	return sample_format;
110
}
111

112 113
static uint8_t
ParseChannelCount(const char *src, bool mask, const char **endptr_r)
114 115 116
{
	unsigned long value;
	char *endptr;
117

118 119
	if (mask && *src == '*') {
		*endptr_r = src + 1;
120
		return 0;
121 122
	}

123
	value = strtoul(src, &endptr, 10);
124
	if (endptr == src)
125
		throw std::invalid_argument("Failed to parse the channel count");
126
	else if (!audio_valid_channel_count(value))
127 128
		throw FormatInvalidArgument("Invalid channel count: %u",
					    value);
129 130

	*endptr_r = endptr;
131
	return value;
132 133
}

134 135
AudioFormat
ParseAudioFormat(const char *src, bool mask)
136
{
137
	AudioFormat dest;
138
	dest.Clear();
139

140 141 142 143 144 145 146 147 148 149 150 151 152 153
	if (strncmp(src, "dsd", 3) == 0) {
		/* allow format specifications such as "dsd64" which
		   implies the sample rate */

		char *endptr;
		auto dsd = strtoul(src + 3, &endptr, 10);
		if (endptr > src + 3 && *endptr == ':' &&
		    dsd >= 32 && dsd <= 4096 && dsd % 2 == 0) {
			dest.sample_rate = dsd * 44100 / 8;
			dest.format = SampleFormat::DSD;

			src = endptr + 1;
			dest.channels = ParseChannelCount(src, mask, &src);
			if (*src != 0)
154 155
				throw FormatInvalidArgument("Extra data after channel count: %s",
							    src);
156 157 158 159 160

			return dest;
		}
	}

161 162
	/* parse sample rate */

163
	dest.sample_rate = ParseSampleRate(src, mask, &src);
164

165
	if (*src++ != ':')
166
		throw std::invalid_argument("Sample format missing");
167

168 169
	/* parse sample format */

170
	dest.format = ParseSampleFormat(src, mask, &src);
171

172
	if (*src++ != ':')
173
		throw std::invalid_argument("Channel count missing");
174 175 176

	/* parse channel count */

177
	dest.channels = ParseChannelCount(src, mask, &src);
178

179
	if (*src != 0)
180 181
		throw FormatInvalidArgument("Extra data after channel count: %s",
					    src);
182

183 184 185
	assert(mask
	       ? dest.IsMaskValid()
	       : dest.IsValid());
186
	return dest;
187
}