00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef MPD_AUDIO_FORMAT_H
00021 #define MPD_AUDIO_FORMAT_H
00022
00023 #include <glib.h>
00024 #include <stdint.h>
00025 #include <stdbool.h>
00026 #include <assert.h>
00027
00028 enum sample_format {
00029 SAMPLE_FORMAT_UNDEFINED = 0,
00030
00031 SAMPLE_FORMAT_S8,
00032 SAMPLE_FORMAT_S16,
00033
00038 SAMPLE_FORMAT_S24_P32,
00039
00040 SAMPLE_FORMAT_S32,
00041
00046 SAMPLE_FORMAT_FLOAT,
00047
00052 SAMPLE_FORMAT_DSD,
00053 };
00054
00055 static const unsigned MAX_CHANNELS = 8;
00056
00060 struct audio_format {
00066 uint32_t sample_rate;
00067
00072 uint8_t format;
00073
00078 uint8_t channels;
00079 };
00080
00084 struct audio_format_string {
00085 char buffer[24];
00086 };
00087
00092 static inline void audio_format_clear(struct audio_format *af)
00093 {
00094 af->sample_rate = 0;
00095 af->format = SAMPLE_FORMAT_UNDEFINED;
00096 af->channels = 0;
00097 }
00098
00103 static inline void audio_format_init(struct audio_format *af,
00104 uint32_t sample_rate,
00105 enum sample_format format, uint8_t channels)
00106 {
00107 af->sample_rate = sample_rate;
00108 af->format = (uint8_t)format;
00109 af->channels = channels;
00110 }
00111
00116 static inline bool audio_format_defined(const struct audio_format *af)
00117 {
00118 return af->sample_rate != 0;
00119 }
00120
00126 static inline bool
00127 audio_format_fully_defined(const struct audio_format *af)
00128 {
00129 return af->sample_rate != 0 && af->format != SAMPLE_FORMAT_UNDEFINED &&
00130 af->channels != 0;
00131 }
00132
00137 static inline bool
00138 audio_format_mask_defined(const struct audio_format *af)
00139 {
00140 return af->sample_rate != 0 || af->format != SAMPLE_FORMAT_UNDEFINED ||
00141 af->channels != 0;
00142 }
00143
00149 static inline bool
00150 audio_valid_sample_rate(unsigned sample_rate)
00151 {
00152 return sample_rate > 0 && sample_rate < (1 << 30);
00153 }
00154
00160 static inline bool
00161 audio_valid_sample_format(enum sample_format format)
00162 {
00163 switch (format) {
00164 case SAMPLE_FORMAT_S8:
00165 case SAMPLE_FORMAT_S16:
00166 case SAMPLE_FORMAT_S24_P32:
00167 case SAMPLE_FORMAT_S32:
00168 case SAMPLE_FORMAT_FLOAT:
00169 case SAMPLE_FORMAT_DSD:
00170 return true;
00171
00172 case SAMPLE_FORMAT_UNDEFINED:
00173 break;
00174 }
00175
00176 return false;
00177 }
00178
00182 static inline bool
00183 audio_valid_channel_count(unsigned channels)
00184 {
00185 return channels >= 1 && channels <= MAX_CHANNELS;
00186 }
00187
00192 G_GNUC_PURE
00193 static inline bool audio_format_valid(const struct audio_format *af)
00194 {
00195 return audio_valid_sample_rate(af->sample_rate) &&
00196 audio_valid_sample_format((enum sample_format)af->format) &&
00197 audio_valid_channel_count(af->channels);
00198 }
00199
00204 G_GNUC_PURE
00205 static inline bool audio_format_mask_valid(const struct audio_format *af)
00206 {
00207 return (af->sample_rate == 0 ||
00208 audio_valid_sample_rate(af->sample_rate)) &&
00209 (af->format == SAMPLE_FORMAT_UNDEFINED ||
00210 audio_valid_sample_format((enum sample_format)af->format)) &&
00211 (af->channels == 0 || audio_valid_channel_count(af->channels));
00212 }
00213
00214 static inline bool audio_format_equals(const struct audio_format *a,
00215 const struct audio_format *b)
00216 {
00217 return a->sample_rate == b->sample_rate &&
00218 a->format == b->format &&
00219 a->channels == b->channels;
00220 }
00221
00222 void
00223 audio_format_mask_apply(struct audio_format *af,
00224 const struct audio_format *mask);
00225
00226 G_GNUC_CONST
00227 static inline unsigned
00228 sample_format_size(enum sample_format format)
00229 {
00230 switch (format) {
00231 case SAMPLE_FORMAT_S8:
00232 return 1;
00233
00234 case SAMPLE_FORMAT_S16:
00235 return 2;
00236
00237 case SAMPLE_FORMAT_S24_P32:
00238 case SAMPLE_FORMAT_S32:
00239 case SAMPLE_FORMAT_FLOAT:
00240 return 4;
00241
00242 case SAMPLE_FORMAT_DSD:
00243
00244 return 1;
00245
00246 case SAMPLE_FORMAT_UNDEFINED:
00247 return 0;
00248 }
00249
00250 assert(false);
00251 return 0;
00252 }
00253
00257 G_GNUC_PURE
00258 static inline unsigned audio_format_sample_size(const struct audio_format *af)
00259 {
00260 return sample_format_size((enum sample_format)af->format);
00261 }
00262
00266 G_GNUC_PURE
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 G_GNUC_PURE
00278 static inline double audio_format_time_to_size(const struct audio_format *af)
00279 {
00280 return af->sample_rate * audio_format_frame_size(af);
00281 }
00282
00290 G_GNUC_PURE G_GNUC_MALLOC
00291 const char *
00292 sample_format_to_string(enum sample_format format);
00293
00302 G_GNUC_PURE G_GNUC_MALLOC
00303 const char *
00304 audio_format_to_string(const struct audio_format *af,
00305 struct audio_format_string *s);
00306
00307 #endif