MPD
audio_format.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2003-2010 The Music Player Daemon Project
3  * http://www.musicpd.org
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 #ifndef MPD_AUDIO_FORMAT_H
21 #define MPD_AUDIO_FORMAT_H
22 
23 #include <stdint.h>
24 #include <stdbool.h>
25 #include <assert.h>
26 
29 
32 
37 
43 
45 };
46 
50 struct audio_format {
56  uint32_t sample_rate;
57 
62  uint8_t format;
63 
68  uint8_t channels;
69 
75  uint8_t reverse_endian;
76 };
77 
82  char buffer[24];
83 };
84 
89 static inline void audio_format_clear(struct audio_format *af)
90 {
91  af->sample_rate = 0;
93  af->channels = 0;
94  af->reverse_endian = 0;
95 }
96 
101 static inline void audio_format_init(struct audio_format *af,
102  uint32_t sample_rate,
103  enum sample_format format, uint8_t channels)
104 {
105  af->sample_rate = sample_rate;
106  af->format = (uint8_t)format;
107  af->channels = channels;
108  af->reverse_endian = 0;
109 }
110 
115 static inline bool audio_format_defined(const struct audio_format *af)
116 {
117  return af->sample_rate != 0;
118 }
119 
125 static inline bool
127 {
128  return af->sample_rate != 0 && af->format != SAMPLE_FORMAT_UNDEFINED &&
129  af->channels != 0;
130 }
131 
136 static inline bool
138 {
139  return af->sample_rate != 0 || af->format != SAMPLE_FORMAT_UNDEFINED ||
140  af->channels != 0;
141 }
142 
148 static inline bool
149 audio_valid_sample_rate(unsigned sample_rate)
150 {
151  return sample_rate > 0 && sample_rate < (1 << 30);
152 }
153 
159 static inline bool
161 {
162  switch (format) {
163  case SAMPLE_FORMAT_S8:
164  case SAMPLE_FORMAT_S16:
165  case SAMPLE_FORMAT_S24:
167  case SAMPLE_FORMAT_S32:
168  return true;
169 
171  break;
172  }
173 
174  return false;
175 }
176 
180 static inline bool
181 audio_valid_channel_count(unsigned channels)
182 {
183  return channels >= 1 && channels <= 8;
184 }
185 
190 static inline bool audio_format_valid(const struct audio_format *af)
191 {
192  return audio_valid_sample_rate(af->sample_rate) &&
195 }
196 
201 static inline bool audio_format_mask_valid(const struct audio_format *af)
202 {
203  return (af->sample_rate == 0 ||
207  (af->channels == 0 || audio_valid_channel_count(af->channels));
208 }
209 
210 static inline bool audio_format_equals(const struct audio_format *a,
211  const struct audio_format *b)
212 {
213  return a->sample_rate == b->sample_rate &&
214  a->format == b->format &&
215  a->channels == b->channels &&
217 }
218 
219 static inline void
221  const struct audio_format *mask)
222 {
223  assert(audio_format_valid(af));
224  assert(audio_format_mask_valid(mask));
225 
226  if (mask->sample_rate != 0)
227  af->sample_rate = mask->sample_rate;
228 
229  if (mask->format != SAMPLE_FORMAT_UNDEFINED)
230  af->format = mask->format;
231 
232  if (mask->channels != 0)
233  af->channels = mask->channels;
234 
235  assert(audio_format_valid(af));
236 }
237 
241 static inline unsigned audio_format_sample_size(const struct audio_format *af)
242 {
243  switch (af->format) {
244  case SAMPLE_FORMAT_S8:
245  return 1;
246 
247  case SAMPLE_FORMAT_S16:
248  return 2;
249 
250  case SAMPLE_FORMAT_S24:
251  return 3;
252 
254  case SAMPLE_FORMAT_S32:
255  return 4;
256 
258  break;
259  }
260 
261  return 0;
262 }
263 
267 static inline unsigned
269 {
270  return audio_format_sample_size(af) * af->channels;
271 }
272 
277 static inline double audio_format_time_to_size(const struct audio_format *af)
278 {
279  return af->sample_rate * audio_format_frame_size(af);
280 }
281 
289 const char *
291 
300 const char *
301 audio_format_to_string(const struct audio_format *af,
302  struct audio_format_string *s);
303 
304 #endif