OSDN Git Service

Added new PCM functions (pcm_misc.c).
[android-x86/external-alsa-lib.git] / src / pcm / pcm_misc.c
1 /*
2  *  PCM Interface - misc routines
3  *  Copyright (c) 1998 by Jaroslav Kysela <perex@suse.cz>
4  *
5  *
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.
10  *
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.
15  *
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.
19  *
20  */
21   
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <unistd.h>
25 #include <string.h>
26 #include <errno.h>
27 #include <fcntl.h>
28 #include <sys/ioctl.h>
29 #include <sys/mman.h>
30 #include "pcm_local.h"
31
32 int snd_pcm_format_signed(int format)
33 {
34         switch (format) {
35         case SND_PCM_SFMT_S8:
36         case SND_PCM_SFMT_S16_LE:
37         case SND_PCM_SFMT_S16_BE:
38         case SND_PCM_SFMT_S24_LE:
39         case SND_PCM_SFMT_S24_BE:
40         case SND_PCM_SFMT_S32_LE:
41         case SND_PCM_SFMT_S32_BE:
42                 return 1;
43         case SND_PCM_SFMT_U8:
44         case SND_PCM_SFMT_U16_LE:
45         case SND_PCM_SFMT_U16_BE:
46         case SND_PCM_SFMT_U24_LE:
47         case SND_PCM_SFMT_U24_BE:
48         case SND_PCM_SFMT_U32_LE:
49         case SND_PCM_SFMT_U32_BE:
50                 return 0;
51         default:
52                 return -EINVAL;
53         }
54 }
55
56 int snd_pcm_format_unsigned(int format)
57 {
58         int val;
59
60         val = snd_pcm_format_signed(format);
61         if (val >= 0)
62                 val ^= 1;
63         return val;
64 }
65
66 int snd_pcm_format_little_endian(int format)
67 {
68         switch (format) {
69         case SND_PCM_SFMT_S16_LE:
70         case SND_PCM_SFMT_U16_LE:
71         case SND_PCM_SFMT_S24_LE:
72         case SND_PCM_SFMT_U24_LE:
73         case SND_PCM_SFMT_S32_LE:
74         case SND_PCM_SFMT_U32_LE:
75         case SND_PCM_SFMT_FLOAT_LE:
76         case SND_PCM_SFMT_FLOAT64_LE:
77         case SND_PCM_SFMT_IEC958_SUBFRAME_LE:
78                 return 1;
79         case SND_PCM_SFMT_S16_BE:
80         case SND_PCM_SFMT_U16_BE:
81         case SND_PCM_SFMT_S24_BE:
82         case SND_PCM_SFMT_U24_BE:
83         case SND_PCM_SFMT_S32_BE:
84         case SND_PCM_SFMT_U32_BE:
85         case SND_PCM_SFMT_FLOAT_BE:
86         case SND_PCM_SFMT_FLOAT64_BE:
87         case SND_PCM_SFMT_IEC958_SUBFRAME_BE:
88                 return 0;
89         default:
90                 return -EINVAL;
91         }
92 }
93
94 int snd_pcm_format_big_endian(int format)
95 {
96         int val;
97
98         val = snd_pcm_format_little_endian(format);
99         if (val >= 0)
100                 val ^= 1;
101         return val;
102 }
103
104 int snd_pcm_format_width(int format)
105 {
106         switch (format) {
107         case SND_PCM_SFMT_S8:
108         case SND_PCM_SFMT_U8:
109                 return 8;
110         case SND_PCM_SFMT_S16_LE:
111         case SND_PCM_SFMT_S16_BE:
112         case SND_PCM_SFMT_U16_LE:
113         case SND_PCM_SFMT_U16_BE:
114                 return 16;
115         case SND_PCM_SFMT_S24_LE:
116         case SND_PCM_SFMT_S24_BE:
117         case SND_PCM_SFMT_U24_LE:
118         case SND_PCM_SFMT_U24_BE:
119                 return 24;
120         case SND_PCM_SFMT_S32_LE:
121         case SND_PCM_SFMT_S32_BE:
122         case SND_PCM_SFMT_U32_LE:
123         case SND_PCM_SFMT_U32_BE:
124         case SND_PCM_SFMT_FLOAT_LE:
125         case SND_PCM_SFMT_FLOAT_BE:
126                 return 32;
127         case SND_PCM_SFMT_FLOAT64_LE:
128         case SND_PCM_SFMT_FLOAT64_BE:
129                 return 64;
130         case SND_PCM_SFMT_IEC958_SUBFRAME_LE:
131         case SND_PCM_SFMT_IEC958_SUBFRAME_BE:
132                 return 24;
133         case SND_PCM_SFMT_MU_LAW:
134         case SND_PCM_SFMT_A_LAW:
135                 return 8;
136         case SND_PCM_SFMT_IMA_ADPCM:
137                 return 4;
138         default:
139                 return -EINVAL;
140         }
141 }
142
143 ssize_t snd_pcm_format_size(int format, size_t samples)
144 {
145         if (samples < 0)
146                 return -EINVAL;
147         if (samples == 0)
148                 return samples;
149         switch (format) {
150         case SND_PCM_SFMT_S8:
151         case SND_PCM_SFMT_U8:
152                 return samples;
153         case SND_PCM_SFMT_S16_LE:
154         case SND_PCM_SFMT_S16_BE:
155         case SND_PCM_SFMT_U16_LE:
156         case SND_PCM_SFMT_U16_BE:
157                 return samples * 2;
158         case SND_PCM_SFMT_S24_LE:
159         case SND_PCM_SFMT_S24_BE:
160         case SND_PCM_SFMT_U24_LE:
161         case SND_PCM_SFMT_U24_BE:
162         case SND_PCM_SFMT_S32_LE:
163         case SND_PCM_SFMT_S32_BE:
164         case SND_PCM_SFMT_U32_LE:
165         case SND_PCM_SFMT_U32_BE:
166         case SND_PCM_SFMT_FLOAT_LE:
167         case SND_PCM_SFMT_FLOAT_BE:
168                 return samples * 4;
169         case SND_PCM_SFMT_FLOAT64_LE:
170         case SND_PCM_SFMT_FLOAT64_BE:
171                 return samples * 8;
172         case SND_PCM_SFMT_IEC958_SUBFRAME_LE:
173         case SND_PCM_SFMT_IEC958_SUBFRAME_BE:
174                 return samples * 4;
175         case SND_PCM_SFMT_MU_LAW:
176         case SND_PCM_SFMT_A_LAW:
177                 return samples;
178         case SND_PCM_SFMT_IMA_ADPCM:
179                 if (samples & 1)
180                         return -EINVAL;
181                 return samples / 2;
182         default:
183                 return -EINVAL;
184         }
185 }