00001 /* 00002 * Copyright (C) 2003-2009 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 00026 struct audio_format { 00027 uint32_t sample_rate; 00028 uint8_t bits; 00029 uint8_t channels; 00030 }; 00031 00032 static inline void audio_format_clear(struct audio_format *af) 00033 { 00034 af->sample_rate = 0; 00035 af->bits = 0; 00036 af->channels = 0; 00037 } 00038 00039 static inline bool audio_format_defined(const struct audio_format *af) 00040 { 00041 return af->sample_rate != 0; 00042 } 00043 00049 static inline bool 00050 audio_valid_sample_rate(unsigned sample_rate) 00051 { 00052 return sample_rate > 0 && sample_rate < (1 << 30); 00053 } 00054 00060 static inline bool 00061 audio_valid_sample_format(unsigned bits) 00062 { 00063 return bits == 16 || bits == 24 || bits == 32 || bits == 8; 00064 } 00065 00069 static inline bool 00070 audio_valid_channel_count(unsigned channels) 00071 { 00072 return channels >= 1 && channels <= 8; 00073 } 00074 00079 static inline bool audio_format_valid(const struct audio_format *af) 00080 { 00081 return audio_valid_sample_rate(af->sample_rate) && 00082 audio_valid_sample_format(af->bits) && 00083 audio_valid_channel_count(af->channels); 00084 } 00085 00086 static inline bool audio_format_equals(const struct audio_format *a, 00087 const struct audio_format *b) 00088 { 00089 return a->sample_rate == b->sample_rate && 00090 a->bits == b->bits && 00091 a->channels == b->channels; 00092 } 00093 00097 static inline unsigned audio_format_sample_size(const struct audio_format *af) 00098 { 00099 if (af->bits <= 8) 00100 return 1; 00101 else if (af->bits <= 16) 00102 return 2; 00103 else 00104 return 4; 00105 } 00106 00107 static inline unsigned 00108 audio_format_frame_size(const struct audio_format *af) 00109 { 00110 return audio_format_sample_size(af) * af->channels; 00111 } 00112 00113 static inline double audio_format_time_to_size(const struct audio_format *af) 00114 { 00115 return af->sample_rate * audio_format_frame_size(af); 00116 } 00117 00118 static inline double audioFormatSizeToTime(const struct audio_format *af) 00119 { 00120 return 1.0 / audio_format_time_to_size(af); 00121 } 00122 00123 #endif