1 /*
2 ** Copyright 2010, The Android Open-Source Project
3 ** Copyright (c) 2011-2012, Code Aurora Forum. All rights reserved.
4 **
5 ** Licensed under the Apache License, Version 2.0 (the "License");
6 ** you may not use this file except in compliance with the License.
7 ** You may obtain a copy of the License at
8 **
9 **     http://www.apache.org/licenses/LICENSE-2.0
10 **
11 ** Unless required by applicable law or agreed to in writing, software
12 ** distributed under the License is distributed on an "AS IS" BASIS,
13 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 ** See the License for the specific language governing permissions and
15 ** limitations under the License.
16 */
17 
18 #ifndef _AUDIO_H_
19 #define _AUDIO_H_
20 
21 #include <sound/asound.h>
22 #define PCM_ERROR_MAX 128
23 
24 struct pcm {
25     int fd;
26     int timer_fd;
27     unsigned rate;
28     unsigned channels;
29     unsigned flags;
30     unsigned format;
31     unsigned running:1;
32     int underruns;
33     unsigned buffer_size;
34     unsigned period_size;
35     unsigned period_cnt;
36     char error[PCM_ERROR_MAX];
37     struct snd_pcm_hw_params *hw_p;
38     struct snd_pcm_sw_params *sw_p;
39     struct snd_pcm_sync_ptr *sync_ptr;
40     struct snd_pcm_channel_info ch[2];
41     void *addr;
42     int card_no;
43     int device_no;
44     int start;
45 };
46 
47 enum decoder_alias {
48     FORMAT_MP3,
49     FORMAT_AC3_PASS_THROUGH = 2,
50 };
51 
52 #define FORMAT(v) SNDRV_PCM_FORMAT_##v
53 
54 #define PCM_OUT        0x00000000
55 #define PCM_IN         0x10000000
56 
57 #define PCM_STEREO     0x00000000
58 #define PCM_MONO       0x01000000
59 #define PCM_QUAD       0x02000000
60 #define PCM_5POINT1    0x04000000
61 
62 #define PCM_44100HZ    0x00000000
63 #define PCM_48000HZ    0x00100000
64 #define PCM_8000HZ     0x00200000
65 #define PCM_RATE_MASK  0x00F00000
66 
67 #define PCM_MMAP       0x00010000
68 #define PCM_NMMAP      0x00000000
69 
70 #define DEBUG_ON       0x00000001
71 #define DEBUG_OFF      0x00000000
72 
73 #define PCM_PERIOD_CNT_MIN 2
74 #define PCM_PERIOD_CNT_SHIFT 16
75 #define PCM_PERIOD_CNT_MASK (0xF << PCM_PERIOD_CNT_SHIFT)
76 #define PCM_PERIOD_SZ_MIN 128
77 #define PCM_PERIOD_SZ_SHIFT 12
78 #define PCM_PERIOD_SZ_MASK (0xF << PCM_PERIOD_SZ_SHIFT)
79 
80 #define TIMEOUT_INFINITE  -1
81 
82 /* Acquire/release a pcm channel.
83  * Returns non-zero on error
84  */
85 
86 struct mixer_ctl {
87     struct mixer *mixer;
88     struct snd_ctl_elem_info *info;
89     char **ename;
90 };
91 
92 #define __snd_alloca(ptr,type) do { *ptr = (type *) alloca(sizeof(type)); memset(*ptr, 0, sizeof(type)); } while (0)
93 #define snd_ctl_elem_id_alloca(ptr) __snd_alloca(ptr, snd_ctl_elem_id)
94 #define snd_ctl_card_info_alloca(ptr) __snd_alloca(ptr, snd_ctl_card_info)
95 #define snd_ctl_event_alloca(ptr) __snd_alloca(ptr, snd_ctl_event)
96 #define snd_ctl_elem_list_alloca(ptr) __snd_alloca(ptr, snd_ctl_elem_list)
97 #define snd_ctl_elem_info_alloca(ptr) __snd_alloca(ptr, snd_ctl_elem_info)
98 #define snd_ctl_elem_value_alloca(ptr) __snd_alloca(ptr, snd_ctl_elem_value)
99 
100 
101 enum snd_pcm_stream_t {
102         /** Playback stream */
103         SND_PCM_STREAM_PLAYBACK = 0,
104         /** Capture stream */
105         SND_PCM_STREAM_CAPTURE,
106         SND_PCM_STREAM_LAST = SND_PCM_STREAM_CAPTURE
107 };
108 
109 enum _snd_ctl_elem_iface {
110         /** Card level */
111         SND_CTL_ELEM_IFACE_CARD = 0,
112         /** Hardware dependent device */
113         SND_CTL_ELEM_IFACE_HWDEP,
114         /** Mixer */
115         SND_CTL_ELEM_IFACE_MIXER,
116         /** PCM */
117         SND_CTL_ELEM_IFACE_PCM,
118         /** RawMidi */
119         SND_CTL_ELEM_IFACE_RAWMIDI,
120         /** Timer */
121         SND_CTL_ELEM_IFACE_TIMER,
122         /** Sequencer */
123         SND_CTL_ELEM_IFACE_SEQUENCER,
124         SND_CTL_ELEM_IFACE_LAST = SND_CTL_ELEM_IFACE_SEQUENCER
125 };
126 
127 struct mixer {
128     int fd;
129     struct snd_ctl_elem_info *info;
130     struct mixer_ctl *ctl;
131     unsigned count;
132 };
133 
134 int get_format(const char* name);
135 const char *get_format_name(int format);
136 const char *get_format_desc(int format);
137 struct pcm *pcm_open(unsigned flags, char *device);
138 int pcm_close(struct pcm *pcm);
139 int pcm_ready(struct pcm *pcm);
140 int mmap_buffer(struct pcm *pcm);
141 u_int8_t *dst_address(struct pcm *pcm);
142 int sync_ptr(struct pcm *pcm);
143 
144 void param_init(struct snd_pcm_hw_params *p);
145 void param_set_mask(struct snd_pcm_hw_params *p, int n, unsigned bit);
146 void param_set_min(struct snd_pcm_hw_params *p, int n, unsigned val);
147 void param_set_int(struct snd_pcm_hw_params *p, int n, unsigned val);
148 void param_set_max(struct snd_pcm_hw_params *p, int n, unsigned val);
149 int param_set_hw_refine(struct pcm *pcm, struct snd_pcm_hw_params *params);
150 int param_set_hw_params(struct pcm *pcm, struct snd_pcm_hw_params *params);
151 int param_set_sw_params(struct pcm *pcm, struct snd_pcm_sw_params *sparams);
152 void param_dump(struct snd_pcm_hw_params *p);
153 int pcm_prepare(struct pcm *pcm);
154 long pcm_avail(struct pcm *pcm);
155 
156 /* Returns a human readable reason for the last error. */
157 const char *pcm_error(struct pcm *pcm);
158 
159 /* Returns the buffer size (int bytes) that should be used for pcm_write.
160  * This will be 1/2 of the actual fifo size.
161  */
162 int pcm_buffer_size(struct snd_pcm_hw_params *params);
163 int pcm_period_size(struct snd_pcm_hw_params *params);
164 
165 /* Write data to the fifo.
166  * Will start playback on the first write or on a write that
167  * occurs after a fifo underrun.
168  */
169 int pcm_write(struct pcm *pcm, void *data, unsigned count);
170 int pcm_read(struct pcm *pcm, void *data, unsigned count);
171 
172 struct mixer;
173 struct mixer_ctl;
174 
175 struct mixer *mixer_open(const char *device);
176 void mixer_close(struct mixer *mixer);
177 void mixer_dump(struct mixer *mixer);
178 
179 struct mixer_ctl *mixer_get_control(struct mixer *mixer,
180                                     const char *name, unsigned index);
181 struct mixer_ctl *mixer_get_nth_control(struct mixer *mixer, unsigned n);
182 
183 int mixer_ctl_set(struct mixer_ctl *ctl, unsigned percent);
184 int mixer_ctl_select(struct mixer_ctl *ctl, const char *value);
185 void mixer_ctl_get(struct mixer_ctl *ctl, unsigned *value);
186 int mixer_ctl_set_value(struct mixer_ctl *ctl, int count, char ** argv);
187 
188 
189 #define MAX_NUM_CODECS 32
190 
191 /* compressed audio support */
192 #ifdef QCOM_COMPRESSED_AUDIO_ENABLED
193 struct snd_compr_caps {
194         __u32 num_codecs;
195         __u32 min_fragment_size;
196         __u32 max_fragment_size;
197         __u32 min_fragments;
198         __u32 max_fragments;
199         __u32 codecs[MAX_NUM_CODECS];
200         __u32 reserved[11];
201 };
202 
203 struct snd_enc_wma {
204         __u32 super_block_align; /* WMA Type-specific data */
205 	__u32 bits_per_sample;
206 	__u32 channelmask;
207 	__u32 encodeopt;
208 };
209 
210 struct snd_enc_vorbis {
211         int quality;
212         __u32 managed;
213         __u32 max_bit_rate;
214         __u32 min_bit_rate;
215         __u32 downmix;
216 };
217 
218 struct snd_enc_real {
219         __u32 quant_bits;
220         __u32 start_region;
221         __u32 num_regions;
222 };
223 
224 struct snd_enc_flac {
225         __u32 num;
226         __u32 gain;
227 };
228 
229 struct snd_enc_generic {
230         __u32 bw;       /* encoder bandwidth */
231         int reserved[15];
232 };
233 
234 union snd_codec_options {
235         struct snd_enc_wma wma;
236         struct snd_enc_vorbis vorbis;
237         struct snd_enc_real real;
238         struct snd_enc_flac flac;
239         struct snd_enc_generic generic;
240 };
241 
242 struct snd_codec {
243         __u32 id;
244         __u32 ch_in;
245         __u32 ch_out;
246         __u32 sample_rate;
247         __u32 bit_rate;
248         __u32 rate_control;
249         __u32 profile;
250         __u32 level;
251         __u32 ch_mode;
252         __u32 format;
253         __u32 align;
254         union snd_codec_options options;
255         __u32 reserved[3];
256 };
257 
258 struct snd_compressed_buffer {
259         size_t fragment_size;
260         int fragments;
261 };
262 
263 /* */
264 struct snd_compr_params {
265         struct snd_compressed_buffer buffer;
266         struct snd_codec codec;
267 };
268 
269 struct snd_compr_tstamp {
270         size_t copied_bytes;
271         size_t copied_total;
272         size_t decoded;
273         size_t rendered;
274         __u32 sampling_rate;
275         uint64_t timestamp;
276 };
277 
278 #define SNDRV_COMPRESS_GET_CAPS         _IOWR('C', 0x00, struct snd_compr_caps *)
279 #define SNDRV_COMPRESS_GET_CODEC_CAPS   _IOWR('C', 0x01, struct snd_compr_codec_caps *)
280 #define SNDRV_COMPRESS_SET_PARAMS       _IOW('C', 0x02, struct snd_compr_params *)
281 #define SNDRV_COMPRESS_GET_PARAMS       _IOR('C', 0x03, struct snd_compr_params *)
282 #define SNDRV_COMPRESS_TSTAMP           _IOR('C', 0x10, struct snd_compr_tstamp *)
283 #define SNDRV_COMPRESS_AVAIL            _IOR('C', 0x11, struct snd_compr_avail *)
284 #define SNDRV_COMPRESS_PAUSE            _IO('C', 0x20)
285 #define SNDRV_COMPRESS_RESUME           _IO('C', 0x21)
286 #define SNDRV_COMPRESS_START            _IO('C', 0x22)
287 #define SNDRV_COMPRESS_STOP             _IO('C', 0x23)
288 #define SNDRV_COMPRESS_DRAIN            _IO('C', 0x24)
289 #endif
290 
291 #endif
292