|
MPD
|
00001 /* 00002 * Copyright (C) 2003-2010 The Music Player Daemon Project 00003 * http://www.musicpd.org 00004 * 00005 * This program is free software; you can redistribute it and/or modify 00006 * it under the terms of the GNU General Public License as published by 00007 * the Free Software Foundation; either version 2 of the License, or 00008 * (at your option) any later version. 00009 * 00010 * This program is distributed in the hope that it will be useful, 00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 * GNU General Public License for more details. 00014 * 00015 * You should have received a copy of the GNU General Public License along 00016 * with this program; if not, write to the Free Software Foundation, Inc., 00017 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 00018 */ 00019 00020 #ifndef MPD_AUDIO_FORMAT_H 00021 #define MPD_AUDIO_FORMAT_H 00022 00023 #include <stdint.h> 00024 #include <stdbool.h> 00025 #include <assert.h> 00026 00027 enum sample_format { 00028 SAMPLE_FORMAT_UNDEFINED = 0, 00029 00030 SAMPLE_FORMAT_S8, 00031 SAMPLE_FORMAT_S16, 00032 00036 SAMPLE_FORMAT_S24, 00037 00042 SAMPLE_FORMAT_S24_P32, 00043 00044 SAMPLE_FORMAT_S32, 00045 }; 00046 00050 struct audio_format { 00056 uint32_t sample_rate; 00057 00062 uint8_t format; 00063 00068 uint8_t channels; 00069 00075 uint8_t reverse_endian; 00076 }; 00077 00081 struct audio_format_string { 00082 char buffer[24]; 00083 }; 00084 00089 static inline void audio_format_clear(struct audio_format *af) 00090 { 00091 af->sample_rate = 0; 00092 af->format = SAMPLE_FORMAT_UNDEFINED; 00093 af->channels = 0; 00094 af->reverse_endian = 0; 00095 } 00096 00101 static inline void audio_format_init(struct audio_format *af, 00102 uint32_t sample_rate, 00103 enum sample_format format, uint8_t channels) 00104 { 00105 af->sample_rate = sample_rate; 00106 af->format = (uint8_t)format; 00107 af->channels = channels; 00108 af->reverse_endian = 0; 00109 } 00110 00115 static inline bool audio_format_defined(const struct audio_format *af) 00116 { 00117 return af->sample_rate != 0; 00118 } 00119 00125 static inline bool 00126 audio_format_fully_defined(const struct audio_format *af) 00127 { 00128 return af->sample_rate != 0 && af->format != SAMPLE_FORMAT_UNDEFINED && 00129 af->channels != 0; 00130 } 00131 00136 static inline bool 00137 audio_format_mask_defined(const struct audio_format *af) 00138 { 00139 return af->sample_rate != 0 || af->format != SAMPLE_FORMAT_UNDEFINED || 00140 af->channels != 0; 00141 } 00142 00148 static inline bool 00149 audio_valid_sample_rate(unsigned sample_rate) 00150 { 00151 return sample_rate > 0 && sample_rate < (1 << 30); 00152 } 00153 00159 static inline bool 00160 audio_valid_sample_format(enum sample_format format) 00161 { 00162 switch (format) { 00163 case SAMPLE_FORMAT_S8: 00164 case SAMPLE_FORMAT_S16: 00165 case SAMPLE_FORMAT_S24: 00166 case SAMPLE_FORMAT_S24_P32: 00167 case SAMPLE_FORMAT_S32: 00168 return true; 00169 00170 case SAMPLE_FORMAT_UNDEFINED: 00171 break; 00172 } 00173 00174 return false; 00175 } 00176 00180 static inline bool 00181 audio_valid_channel_count(unsigned channels) 00182 { 00183 return channels >= 1 && channels <= 8; 00184 } 00185 00190 static inline bool audio_format_valid(const struct audio_format *af) 00191 { 00192 return audio_valid_sample_rate(af->sample_rate) && 00193 audio_valid_sample_format((enum sample_format)af->format) && 00194 audio_valid_channel_count(af->channels); 00195 } 00196 00201 static inline bool audio_format_mask_valid(const struct audio_format *af) 00202 { 00203 return (af->sample_rate == 0 || 00204 audio_valid_sample_rate(af->sample_rate)) && 00205 (af->format == SAMPLE_FORMAT_UNDEFINED || 00206 audio_valid_sample_format((enum sample_format)af->format)) && 00207 (af->channels == 0 || audio_valid_channel_count(af->channels)); 00208 } 00209 00210 static inline bool audio_format_equals(const struct audio_format *a, 00211 const struct audio_format *b) 00212 { 00213 return a->sample_rate == b->sample_rate && 00214 a->format == b->format && 00215 a->channels == b->channels && 00216 a->reverse_endian == b->reverse_endian; 00217 } 00218 00219 static inline void 00220 audio_format_mask_apply(struct audio_format *af, 00221 const struct audio_format *mask) 00222 { 00223 assert(audio_format_valid(af)); 00224 assert(audio_format_mask_valid(mask)); 00225 00226 if (mask->sample_rate != 0) 00227 af->sample_rate = mask->sample_rate; 00228 00229 if (mask->format != SAMPLE_FORMAT_UNDEFINED) 00230 af->format = mask->format; 00231 00232 if (mask->channels != 0) 00233 af->channels = mask->channels; 00234 00235 assert(audio_format_valid(af)); 00236 } 00237 00241 static inline unsigned audio_format_sample_size(const struct audio_format *af) 00242 { 00243 switch (af->format) { 00244 case SAMPLE_FORMAT_S8: 00245 return 1; 00246 00247 case SAMPLE_FORMAT_S16: 00248 return 2; 00249 00250 case SAMPLE_FORMAT_S24: 00251 return 3; 00252 00253 case SAMPLE_FORMAT_S24_P32: 00254 case SAMPLE_FORMAT_S32: 00255 return 4; 00256 00257 case SAMPLE_FORMAT_UNDEFINED: 00258 break; 00259 } 00260 00261 return 0; 00262 } 00263 00267 static inline unsigned 00268 audio_format_frame_size(const struct audio_format *af) 00269 { 00270 return audio_format_sample_size(af) * af->channels; 00271 } 00272 00277 static inline double audio_format_time_to_size(const struct audio_format *af) 00278 { 00279 return af->sample_rate * audio_format_frame_size(af); 00280 } 00281 00289 const char * 00290 sample_format_to_string(enum sample_format format); 00291 00300 const char * 00301 audio_format_to_string(const struct audio_format *af, 00302 struct audio_format_string *s); 00303 00304 #endif
1.7.3