MPD 0.17~git
src/audio_format.h
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2003-2011 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 <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 
00037         SAMPLE_FORMAT_S24,
00038 
00043         SAMPLE_FORMAT_S24_P32,
00044 
00045         SAMPLE_FORMAT_S32,
00046 
00051         SAMPLE_FORMAT_FLOAT,
00052 };
00053 
00054 static const unsigned MAX_CHANNELS = 8;
00055 
00059 struct audio_format {
00065         uint32_t sample_rate;
00066 
00071         uint8_t format;
00072 
00077         uint8_t channels;
00078 
00084         bool reverse_endian;
00085 };
00086 
00090 struct audio_format_string {
00091         char buffer[24];
00092 };
00093 
00098 static inline void audio_format_clear(struct audio_format *af)
00099 {
00100         af->sample_rate = 0;
00101         af->format = SAMPLE_FORMAT_UNDEFINED;
00102         af->channels = 0;
00103         af->reverse_endian = false;
00104 }
00105 
00110 static inline void audio_format_init(struct audio_format *af,
00111                                      uint32_t sample_rate,
00112                                      enum sample_format format, uint8_t channels)
00113 {
00114         af->sample_rate = sample_rate;
00115         af->format = (uint8_t)format;
00116         af->channels = channels;
00117         af->reverse_endian = false;
00118 }
00119 
00124 static inline bool audio_format_defined(const struct audio_format *af)
00125 {
00126         return af->sample_rate != 0;
00127 }
00128 
00134 static inline bool
00135 audio_format_fully_defined(const struct audio_format *af)
00136 {
00137         return af->sample_rate != 0 && af->format != SAMPLE_FORMAT_UNDEFINED &&
00138                 af->channels != 0;
00139 }
00140 
00145 static inline bool
00146 audio_format_mask_defined(const struct audio_format *af)
00147 {
00148         return af->sample_rate != 0 || af->format != SAMPLE_FORMAT_UNDEFINED ||
00149                 af->channels != 0;
00150 }
00151 
00157 static inline bool
00158 audio_valid_sample_rate(unsigned sample_rate)
00159 {
00160         return sample_rate > 0 && sample_rate < (1 << 30);
00161 }
00162 
00168 static inline bool
00169 audio_valid_sample_format(enum sample_format format)
00170 {
00171         switch (format) {
00172         case SAMPLE_FORMAT_S8:
00173         case SAMPLE_FORMAT_S16:
00174         case SAMPLE_FORMAT_S24:
00175         case SAMPLE_FORMAT_S24_P32:
00176         case SAMPLE_FORMAT_S32:
00177         case SAMPLE_FORMAT_FLOAT:
00178                 return true;
00179 
00180         case SAMPLE_FORMAT_UNDEFINED:
00181                 break;
00182         }
00183 
00184         return false;
00185 }
00186 
00190 static inline bool
00191 audio_valid_channel_count(unsigned channels)
00192 {
00193         return channels >= 1 && channels <= MAX_CHANNELS;
00194 }
00195 
00200 G_GNUC_PURE
00201 static inline bool audio_format_valid(const struct audio_format *af)
00202 {
00203         return audio_valid_sample_rate(af->sample_rate) &&
00204                 audio_valid_sample_format((enum sample_format)af->format) &&
00205                 audio_valid_channel_count(af->channels);
00206 }
00207 
00212 G_GNUC_PURE
00213 static inline bool audio_format_mask_valid(const struct audio_format *af)
00214 {
00215         return (af->sample_rate == 0 ||
00216                 audio_valid_sample_rate(af->sample_rate)) &&
00217                 (af->format == SAMPLE_FORMAT_UNDEFINED ||
00218                  audio_valid_sample_format((enum sample_format)af->format)) &&
00219                 (af->channels == 0 || audio_valid_channel_count(af->channels));
00220 }
00221 
00222 static inline bool audio_format_equals(const struct audio_format *a,
00223                                        const struct audio_format *b)
00224 {
00225         return a->sample_rate == b->sample_rate &&
00226                 a->format == b->format &&
00227                 a->channels == b->channels &&
00228                 a->reverse_endian == b->reverse_endian;
00229 }
00230 
00231 void
00232 audio_format_mask_apply(struct audio_format *af,
00233                         const struct audio_format *mask);
00234 
00235 G_GNUC_CONST
00236 static inline unsigned
00237 sample_format_size(enum sample_format format)
00238 {
00239         switch (format) {
00240         case SAMPLE_FORMAT_S8:
00241                 return 1;
00242 
00243         case SAMPLE_FORMAT_S16:
00244                 return 2;
00245 
00246         case SAMPLE_FORMAT_S24:
00247                 return 3;
00248 
00249         case SAMPLE_FORMAT_S24_P32:
00250         case SAMPLE_FORMAT_S32:
00251         case SAMPLE_FORMAT_FLOAT:
00252                 return 4;
00253 
00254         case SAMPLE_FORMAT_UNDEFINED:
00255                 return 0;
00256         }
00257 
00258         assert(false);
00259         return 0;
00260 }
00261 
00265 G_GNUC_PURE
00266 static inline unsigned audio_format_sample_size(const struct audio_format *af)
00267 {
00268         return sample_format_size((enum sample_format)af->format);
00269 }
00270 
00274 G_GNUC_PURE
00275 static inline unsigned
00276 audio_format_frame_size(const struct audio_format *af)
00277 {
00278         return audio_format_sample_size(af) * af->channels;
00279 }
00280 
00285 G_GNUC_PURE
00286 static inline double audio_format_time_to_size(const struct audio_format *af)
00287 {
00288         return af->sample_rate * audio_format_frame_size(af);
00289 }
00290 
00298 G_GNUC_PURE G_GNUC_MALLOC
00299 const char *
00300 sample_format_to_string(enum sample_format format);
00301 
00310 G_GNUC_PURE G_GNUC_MALLOC
00311 const char *
00312 audio_format_to_string(const struct audio_format *af,
00313                        struct audio_format_string *s);
00314 
00315 #endif