2 * PCM Interface - misc routines
3 * Copyright (c) 1998 by Jaroslav Kysela <perex@suse.cz>
6 * This library is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Library General Public License as
8 * published by the Free Software Foundation; either version 2 of
9 * the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include "../include/driver.h"
24 #include "../include/pcm.h"
25 #include "../include/pcm_plugin.h"
33 #include "pcm_local.h"
36 int snd_pcm_format_signed(int format)
40 case SND_PCM_SFMT_S16_LE:
41 case SND_PCM_SFMT_S16_BE:
42 case SND_PCM_SFMT_S24_LE:
43 case SND_PCM_SFMT_S24_BE:
44 case SND_PCM_SFMT_S32_LE:
45 case SND_PCM_SFMT_S32_BE:
48 case SND_PCM_SFMT_U16_LE:
49 case SND_PCM_SFMT_U16_BE:
50 case SND_PCM_SFMT_U24_LE:
51 case SND_PCM_SFMT_U24_BE:
52 case SND_PCM_SFMT_U32_LE:
53 case SND_PCM_SFMT_U32_BE:
60 int snd_pcm_format_unsigned(int format)
64 val = snd_pcm_format_signed(format);
70 int snd_pcm_format_linear(int format)
72 return snd_pcm_format_signed(format) >= 0;
75 int snd_pcm_format_little_endian(int format)
78 case SND_PCM_SFMT_S16_LE:
79 case SND_PCM_SFMT_U16_LE:
80 case SND_PCM_SFMT_S24_LE:
81 case SND_PCM_SFMT_U24_LE:
82 case SND_PCM_SFMT_S32_LE:
83 case SND_PCM_SFMT_U32_LE:
84 case SND_PCM_SFMT_FLOAT_LE:
85 case SND_PCM_SFMT_FLOAT64_LE:
86 case SND_PCM_SFMT_IEC958_SUBFRAME_LE:
88 case SND_PCM_SFMT_S16_BE:
89 case SND_PCM_SFMT_U16_BE:
90 case SND_PCM_SFMT_S24_BE:
91 case SND_PCM_SFMT_U24_BE:
92 case SND_PCM_SFMT_S32_BE:
93 case SND_PCM_SFMT_U32_BE:
94 case SND_PCM_SFMT_FLOAT_BE:
95 case SND_PCM_SFMT_FLOAT64_BE:
96 case SND_PCM_SFMT_IEC958_SUBFRAME_BE:
103 int snd_pcm_format_big_endian(int format)
107 val = snd_pcm_format_little_endian(format);
113 int snd_pcm_format_width(int format)
116 case SND_PCM_SFMT_S8:
117 case SND_PCM_SFMT_U8:
119 case SND_PCM_SFMT_S16_LE:
120 case SND_PCM_SFMT_S16_BE:
121 case SND_PCM_SFMT_U16_LE:
122 case SND_PCM_SFMT_U16_BE:
124 case SND_PCM_SFMT_S24_LE:
125 case SND_PCM_SFMT_S24_BE:
126 case SND_PCM_SFMT_U24_LE:
127 case SND_PCM_SFMT_U24_BE:
129 case SND_PCM_SFMT_S32_LE:
130 case SND_PCM_SFMT_S32_BE:
131 case SND_PCM_SFMT_U32_LE:
132 case SND_PCM_SFMT_U32_BE:
133 case SND_PCM_SFMT_FLOAT_LE:
134 case SND_PCM_SFMT_FLOAT_BE:
136 case SND_PCM_SFMT_FLOAT64_LE:
137 case SND_PCM_SFMT_FLOAT64_BE:
139 case SND_PCM_SFMT_IEC958_SUBFRAME_LE:
140 case SND_PCM_SFMT_IEC958_SUBFRAME_BE:
142 case SND_PCM_SFMT_MU_LAW:
143 case SND_PCM_SFMT_A_LAW:
145 case SND_PCM_SFMT_IMA_ADPCM:
152 int snd_pcm_format_physical_width(int format)
155 case SND_PCM_SFMT_S8:
156 case SND_PCM_SFMT_U8:
158 case SND_PCM_SFMT_S16_LE:
159 case SND_PCM_SFMT_S16_BE:
160 case SND_PCM_SFMT_U16_LE:
161 case SND_PCM_SFMT_U16_BE:
163 case SND_PCM_SFMT_S24_LE:
164 case SND_PCM_SFMT_S24_BE:
165 case SND_PCM_SFMT_U24_LE:
166 case SND_PCM_SFMT_U24_BE:
167 case SND_PCM_SFMT_S32_LE:
168 case SND_PCM_SFMT_S32_BE:
169 case SND_PCM_SFMT_U32_LE:
170 case SND_PCM_SFMT_U32_BE:
171 case SND_PCM_SFMT_FLOAT_LE:
172 case SND_PCM_SFMT_FLOAT_BE:
173 case SND_PCM_SFMT_IEC958_SUBFRAME_LE:
174 case SND_PCM_SFMT_IEC958_SUBFRAME_BE:
176 case SND_PCM_SFMT_FLOAT64_LE:
177 case SND_PCM_SFMT_FLOAT64_BE:
179 case SND_PCM_SFMT_MU_LAW:
180 case SND_PCM_SFMT_A_LAW:
182 case SND_PCM_SFMT_IMA_ADPCM:
189 ssize_t snd_pcm_format_size(int format, size_t samples)
192 case SND_PCM_SFMT_S8:
193 case SND_PCM_SFMT_U8:
195 case SND_PCM_SFMT_S16_LE:
196 case SND_PCM_SFMT_S16_BE:
197 case SND_PCM_SFMT_U16_LE:
198 case SND_PCM_SFMT_U16_BE:
200 case SND_PCM_SFMT_S24_LE:
201 case SND_PCM_SFMT_S24_BE:
202 case SND_PCM_SFMT_U24_LE:
203 case SND_PCM_SFMT_U24_BE:
204 case SND_PCM_SFMT_S32_LE:
205 case SND_PCM_SFMT_S32_BE:
206 case SND_PCM_SFMT_U32_LE:
207 case SND_PCM_SFMT_U32_BE:
208 case SND_PCM_SFMT_FLOAT_LE:
209 case SND_PCM_SFMT_FLOAT_BE:
211 case SND_PCM_SFMT_FLOAT64_LE:
212 case SND_PCM_SFMT_FLOAT64_BE:
214 case SND_PCM_SFMT_IEC958_SUBFRAME_LE:
215 case SND_PCM_SFMT_IEC958_SUBFRAME_BE:
217 case SND_PCM_SFMT_MU_LAW:
218 case SND_PCM_SFMT_A_LAW:
220 case SND_PCM_SFMT_IMA_ADPCM:
229 ssize_t snd_pcm_format_bytes_per_second(snd_pcm_format_t *format)
231 return snd_pcm_format_size(format->format, format->channels * format->rate);
234 u_int64_t snd_pcm_format_silence_64(int format)
237 case SND_PCM_SFMT_S8:
238 case SND_PCM_SFMT_S16_LE:
239 case SND_PCM_SFMT_S16_BE:
240 case SND_PCM_SFMT_S24_LE:
241 case SND_PCM_SFMT_S24_BE:
242 case SND_PCM_SFMT_S32_LE:
243 case SND_PCM_SFMT_S32_BE:
245 case SND_PCM_SFMT_U8:
246 return 0x8080808080808080UL;
247 case SND_PCM_SFMT_U16_LE:
248 case SND_PCM_SFMT_U24_LE:
249 case SND_PCM_SFMT_U32_LE:
250 #ifdef SND_LITTLE_ENDIAN
251 return 0x8000800080008000UL;
253 return 0x0080008000800080UL;
255 case SND_PCM_SFMT_U16_BE:
256 case SND_PCM_SFMT_U24_BE:
257 case SND_PCM_SFMT_U32_BE:
258 #ifdef SND_LITTLE_ENDIAN
259 return 0x0000008000000080UL;
261 return 0x8000000080000000UL;
263 case SND_PCM_SFMT_FLOAT_LE:
264 #ifdef SND_LITTLE_ENDIAN
267 return bswap_32((u_int32_t)((float)0.0));
269 case SND_PCM_SFMT_FLOAT64_LE:
270 #ifdef SND_LITTLE_ENDIAN
273 return bswap_64((u_int64_t)((double)0.0));
275 case SND_PCM_SFMT_FLOAT_BE:
276 #ifdef SND_LITTLE_ENDIAN
277 return bswap_32((u_int32_t)((float)0.0));
281 case SND_PCM_SFMT_FLOAT64_BE:
282 #ifdef SND_LITTLE_ENDIAN
283 return bswap_64((u_int64_t)((double)0.0));
287 case SND_PCM_SFMT_IEC958_SUBFRAME_LE:
288 case SND_PCM_SFMT_IEC958_SUBFRAME_BE:
290 case SND_PCM_SFMT_MU_LAW:
291 return 0x7f7f7f7f7f7f7f7fUL;
292 case SND_PCM_SFMT_A_LAW:
293 return 0x5555555555555555UL;
294 case SND_PCM_SFMT_IMA_ADPCM: /* special case */
295 case SND_PCM_SFMT_MPEG:
296 case SND_PCM_SFMT_GSM:
302 u_int32_t snd_pcm_format_silence_32(int format)
304 return (u_int32_t)snd_pcm_format_silence_64(format);
307 u_int16_t snd_pcm_format_silence_16(int format)
309 return (u_int16_t)snd_pcm_format_silence_64(format);
312 u_int8_t snd_pcm_format_silence(int format)
314 return (u_int8_t)snd_pcm_format_silence_64(format);
317 ssize_t snd_pcm_format_set_silence(int format, void *data, size_t count)
323 switch (snd_pcm_format_width(format)) {
326 u_int8_t silence = snd_pcm_format_silence_64(format);
327 memset(data, silence, count);
331 u_int16_t silence = snd_pcm_format_silence_64(format);
336 *((u_int16_t *)data)++ = silence;
340 u_int32_t silence = snd_pcm_format_silence_64(format);
345 *((u_int32_t *)data)++ = silence;
349 u_int64_t silence = snd_pcm_format_silence_64(format);
354 *((u_int64_t *)data)++ = silence;
362 static int linear_formats[4*2*2] = {
381 int snd_pcm_build_linear_format(int width, int unsignd, int big_endian)
399 return ((int(*)[2][2])linear_formats)[width][!!unsignd][!!big_endian];