1 /*
2  * Copyright (C) 2017-2019 Alibaba Group Holding Limited
3  */
4 
5 /******************************************************************************
6  * @file     drv_codec.h
7  * @brief    head file for codec
8  * @version  V1.0
9  * @date     12. April 2019
10  * @model    codec
11  ******************************************************************************/
12 #ifndef _DRV_CODEC_H_
13 #define _DRV_CODEC_H_
14 
15 #include <drv/common.h>
16 #include "stdint.h"
17 
18 #ifdef __cplusplus
19 extern "C" {
20 #endif
21 
22 typedef enum {
23     CODEC_SAMPLE_RATE_8000              = 8000,
24     CODEC_SAMPLE_RATE_11025             = 11025,
25     CODEC_SAMPLE_RATE_12000             = 12000,
26     CODEC_SAMPLE_RATE_16000             = 16000,
27     CODEC_SAMPLE_RATE_22050             = 22050,
28     CODEC_SAMPLE_RATE_24000             = 24000,
29     CODEC_SAMPLE_RATE_32000             = 32000,
30     CODEC_SAMPLE_RATE_44100             = 44100,
31     CODEC_SAMPLE_RATE_48000             = 48000,
32     CODEC_SAMPLE_RATE_96000             = 96000,
33 } codec_sample_rate_t;
34 
35 /****** CODEC Event *****/
36 typedef enum {
37     CODEC_EVENT_PERIOD_READ_COMPLETE        = 0,  ///< a peroid data read complete
38     CODEC_EVENT_PERIOD_WRITE_COMPLETE       = 1,  ///< a peroid data write complete
39     CODEC_EVENT_WRITE_BUFFER_EMPTY          = 2,  ///< fifo is empty
40     CODEC_EVENT_READ_BUFFER_FULL            = 3,  ///< fifo is full
41     CODEC_EVENT_VAD_TRIGGER                 = 4,  ///< vad is trigger
42     CODEC_EVENT_TRANSFER_ERROR              = 5,  ///< transfer error
43 } codec_event_t;
44 
45 typedef enum {
46     CODEC_MODE_RUN                  = 0,   ///< Running mode
47     CODEC_MODE_SLEEP,                      ///< Sleep mode
48     CODEC_MODE_SHUTDOWN                    ///< Shutdown mode
49 } codec_lpm_state_t;
50 
51 typedef void (*codec_event_cb_t)(int idx, codec_event_t event, void *arg);
52 
53 typedef void *codec_handle_t;
54 
55 typedef struct {
56     codec_sample_rate_t sample_rate;
57     uint32_t channel_num;       ///< record channel num
58     uint32_t bit_width;
59 } codec_input_config_t;
60 
61 typedef struct {
62     codec_sample_rate_t sample_rate;
63     uint32_t bit_width;
64     uint32_t mono_mode_en;
65 } codec_output_config_t;
66 
67 typedef struct {
68     int32_t codec_idx;
69     int32_t ch_idx;        ///< input channle number
70     codec_event_cb_t cb;
71     void *cb_arg;
72     uint8_t *buf;
73     uint32_t buf_size;     ///< Used to cache incoming data
74     uint32_t period;       ///< when a peroid data is reached,the callback function is called
75     uint32_t vad_threshold;
76     void *priv;
77 } codec_input_t;
78 
79 typedef struct {
80     int32_t codec_idx;
81     int32_t ch_idx;
82     codec_event_cb_t cb;
83     void *cb_arg;
84     uint8_t *buf;           ///< Used to cache incoming data
85     uint32_t buf_size;
86     uint32_t period;        ///< when a peroid data is reached,the callback function is called
87     void *priv;
88 } codec_output_t;
89 
90 /**
91   \brief  Init the CODEC according to the specified
92   \param[in]   idx codec index
93   \return 0 for success, negative for error code
94 */
95 int32_t csi_codec_init(uint32_t idx);
96 
97 /**
98   \brief  Uninit the CODEC
99   \param[in]   idx codec index
100   \return none
101 */
102 void csi_codec_uninit(uint32_t idx);
103 void csi_codec_lpm(uint32_t idx, codec_lpm_state_t state);
104 
105 /**
106   \brief  Control codec power
107   \param[in]   idx codec index
108   \param[in]   state power ctrl status
109   \return 0 for success, negative for error code
110 */
111 int32_t csi_codec_power_control(int32_t idx, csi_power_stat_e state);
112 
113 /**
114   \brief  Open the codec input channel
115   \param[in]   handle: codec input handle
116   \return 0 for success, negative for error code
117 */
118 int32_t csi_codec_input_open(codec_input_t *handle);
119 
120 /**
121   \brief  Config the codec input channel
122   \param[in]   handle: codec input handle
123   \param[in]   config: codec input config param
124   \return 0 for success, negative for error code
125 */
126 int32_t csi_codec_input_config(codec_input_t *handle, codec_input_config_t *config);
127 
128 /**
129   \brief  Close codec input channel
130   \param[in]   handle: codec input handle
131   \return 0 for success, negative for error code
132 */
133 int32_t csi_codec_input_close(codec_input_t *handle);
134 
135 /**
136   \brief  Read the record data
137   \param[in]   handle: codec input handle
138   \param[in]   buf: used to store read data
139   \param[in]   length: read data length
140   \return read data len, if the data is empty return 0;
141 */
142 uint32_t csi_codec_input_read(codec_input_t *handle, uint8_t *buf, uint32_t length);
143 
144 /**
145   \brief  Get buffer free space
146   \param[in]   handle: codec input handle
147   \return buffer free space (bytes)
148 */
149 uint32_t csi_codec_input_buf_avail(codec_input_t *handle);
150 
151 /**
152   \brief  Reset the buf, discard all data in the cache
153   \param[in]   handle: codec input handle
154   \return 0 for success, negative for error code
155 */
156 int32_t csi_codec_input_buf_reset(codec_input_t *handle);
157 
158 /**
159   \brief  Start the input channel.
160   \param[in]   handle: codec input handle
161   \return 0 for success, negative for error code
162 */
163 int32_t csi_codec_input_start(codec_input_t *handle);
164 
165 /**
166   \brief  Stop the input channel and reset the input cache.
167   \param[in]   handle: codec input handle
168   \return 0 for success, negative for error code
169 */
170 int32_t csi_codec_input_stop(codec_input_t *handle);
171 
172 /**
173   \brief  Pause the input channel, channel will be pause receive data.
174   \param[in]   handle: codec input handle
175   \return 0 for success, negative for error code
176 */
177 int32_t csi_codec_input_pause(codec_input_t *handle);
178 
179 /**
180   \brief  Resume the input channel
181   \param[in]   handle: codec input handle
182   \return 0 for success, negative for error code
183 */
184 int32_t csi_codec_input_resume(codec_input_t *handle);
185 
186 /**
187   \brief  Set input digital gain
188   \param[in]   handle: codec input handle
189   \param[in]   val: gain value
190   \return 0 for success, negative for error code
191 */
192 int32_t csi_codec_input_set_digital_gain(codec_input_t *handle, int32_t val);
193 
194 /**
195   \brief  Set input analog gain
196   \param[in]   handle: codec input handle
197   \param[in]   val: gain value
198   \return 0 for success, negative for error code
199 */
200 int32_t csi_codec_input_set_analog_gain(codec_input_t *handle, int32_t val);
201 
202 /**
203   \brief  Get input digital gain
204   \param[in]   handle: codec input handle
205   \param[out]  val: pointer, that used save gain val
206   \return 0 for success, negative for error code
207 */
208 int32_t csi_codec_input_get_digital_gain(codec_input_t *handle, int32_t *val);
209 
210 /**
211   \brief  Get input analog gain
212   \param[in]   handle: codec input handle
213   \param[out]  val: pointer, that used save gain val
214   \return 0 for success, negative for error code
215 */
216 int32_t csi_codec_input_get_analog_gain(codec_input_t *handle, int32_t *val);
217 
218 /**
219   \brief  Set input mixer gain
220   \param[in]   handle: codec input handle
221   \param[in]  val: gain val
222   \return 0 for success, negative for error code
223 */
224 int32_t csi_codec_input_set_mixer_gain(codec_input_t *handle, int32_t val);
225 
226 /**
227   \brief  Get input mixer gain
228   \param[in]   handle: codec input handle
229   \param[out]  val: pointer, that used save gain val
230   \return 0 for success, negative for error code
231 */
232 int32_t csi_codec_input_get_mixer_gain(codec_input_t *handle, int32_t *val);
233 
234 /**
235   \brief  Contol input mute
236   \param[in]  handle: codec input handle
237   \param[in]  en: 1 enable mute, 0 disable meute
238   \return 0 for success, negative for error code
239 */
240 int32_t csi_codec_input_mute(codec_input_t *handle, int en);
241 
242 /**
243   \brief  Open the codec output channel
244   \param[in]   handle: codec output handle
245   \return 0 for success, negative for error code
246 */
247 int32_t csi_codec_output_open(codec_output_t *handle);
248 
249 /**
250   \brief  Close the codec output channel
251   \param[in]   handle: codec output handle
252   \return 0 for success, negative for error code
253 */
254 int32_t csi_codec_output_close(codec_output_t *handle);
255 
256 /**
257   \brief  Close the codec output channel
258   \param[in]   handle: codec output handle
259   \param[in]   config: channel config param
260   \return 0 for success, negative for error code
261 */
262 int32_t csi_codec_output_config(codec_output_t *handle, codec_output_config_t *config);
263 
264 /**
265   \brief  Close the codec output channel
266   \param[in]   handle: codec output handle
267   \param[in]   buf: write data
268   \param[in]   lenght: write data length
269   \return The number of data written to the cache
270 */
271 uint32_t csi_codec_output_write(codec_output_t *handle, uint8_t *buf, uint32_t length);
272 
273 /**
274   \brief  Get buffer free space
275   \param[in]  handle: codec output handle
276   \return buffer free space (bytes)
277 */
278 uint32_t csi_codec_output_buf_avail(codec_output_t *handle);
279 
280 /**
281   \brief  Reset the buf, discard all data in the cache
282   \param[in]   handle: codec input handle
283   \return 0 for success, negative for error code
284 */
285 int32_t csi_codec_output_buf_reset(codec_output_t *handle);
286 
287 /**
288   \brief  Start the output channel.
289   \param[in]   handle: codec output handle
290   \return 0 for success, negative for error code
291 */
292 int32_t csi_codec_output_start(codec_output_t *handle);
293 
294 /**
295   \brief  Stop the output channel and reset the output cache.
296   \param[in]   handle: codec output handle
297   \return 0 for success, negative for error code
298 */
299 int32_t csi_codec_output_stop(codec_output_t *handle);
300 
301 /**
302   \brief  Pause the ouput channel, channel will be pause send data.
303   \param[in]   handle: codec input handle
304   \return 0 for success, negative for error code
305 */
306 int32_t csi_codec_output_pause(codec_output_t *handle);
307 
308 /**
309   \brief  Resume the ouput channel.
310   \param[in]   handle: codec input handle
311   \return 0 for success, negative for error code
312 */
313 int32_t csi_codec_output_resume(codec_output_t *handle);
314 
315 /**
316   \brief  Set output left channel digital gain
317   \param[in]   handle: codec output handle
318   \param[in]   val: gain value
319   \return 0 for success, negative for error code
320 */
321 int32_t csi_codec_output_set_digital_left_gain(codec_output_t *handle, int32_t val);
322 
323 /**
324   \brief  Set output right channel digital gain
325   \param[in]   handle: codec output handle
326   \param[in]   val: gain value
327   \return 0 for success, negative for error code
328 */
329 int32_t csi_codec_output_set_digital_right_gain(codec_output_t *handle, int32_t val);
330 
331 /**
332   \brief  Set output left channel analog gain
333   \param[in]   handle: codec output handle
334   \param[in]   val: gain value
335   \return 0 for success, negative for error code
336 */
337 int32_t csi_codec_output_set_analog_left_gain(codec_output_t *handle, int32_t val);
338 
339 /**
340   \brief  Set output right channel analog gain
341   \param[in]   handle: codec output handle
342   \param[in]   val: gain value
343   \return 0 for success, negative for error code
344 */
345 int32_t csi_codec_output_set_analog_right_gain(codec_output_t *handle, int32_t val);
346 
347 /**
348   \brief  Get output left channel digital gain
349   \param[in]   handle: codec output handle
350   \param[in]   val: pointer, that used save gain val
351   \return 0 for success, negative for error code
352 */
353 int32_t csi_codec_output_get_digital_left_gain(codec_output_t *handle, int32_t *val);
354 
355 /**
356   \brief  Get output right channel digital gain
357   \param[in]   handle: codec output handle
358   \param[in]   val: pointer, that used save gain val
359   \return 0 for success, negative for error code
360 */
361 int32_t csi_codec_output_get_digital_right_gain(codec_output_t *handle, int32_t *val);
362 
363 /**
364   \brief  Get output left channel analog gain
365   \param[in]   handle: codec output handle
366   \param[in]   val: pointer, that used save gain val
367   \return 0 for success, negative for error code
368 */
369 int32_t csi_codec_output_get_analog_left_gain(codec_output_t *handle, int32_t *val);
370 
371 /**
372   \brief  Get output right channel analog gain
373   \param[in]   handle: codec output handle
374   \param[in]   val: pointer, that used save gain val
375   \return 0 for success, negative for error code
376 */
377 int32_t csi_codec_output_get_analog_right_gain(codec_output_t *handle, int32_t *val);
378 
379 /**
380   \brief  Set output left channel mixer gain
381   \param[in]   handle: codec output handle
382   \param[in]   val: gain value
383   \return 0 for success, negative for error code
384 */
385 int32_t csi_codec_output_set_mixer_left_gain(codec_output_t *handle, int32_t val);
386 
387 /**
388   \brief  Set output right channel mixer gain
389   \param[in]   handle: codec output handle
390   \param[in]   val: gain value
391   \return 0 for success, negative for error code
392 */
393 int32_t csi_codec_output_set_mixer_right_gain(codec_output_t *handle, int32_t val);
394 
395 /**
396   \brief  Get output left channel mixer gain
397   \param[in]   handle: codec output handle
398   \param[in]   val: pointer, that used save gain val
399   \return 0 for success, negative for error code
400 */
401 int32_t csi_codec_output_get_mixer_left_gain(codec_output_t *handle, int32_t *val);
402 
403 /**
404   \brief  Get output right channel mixer gain
405   \param[in]   handle: codec output handle
406   \param[in]   val: pointer, that used save gain val
407   \return 0 for success, negative for error code
408 */
409 int32_t csi_codec_output_get_mixer_right_gain(codec_output_t *handle, int32_t *val);
410 
411 /**
412   \brief  Contol output channel mute
413   \param[in]  handle: codec output handle
414   \param[in]  en: 1 enable mute, 0 disable meute
415   \return 0 for success, negative for error code
416 */
417 int32_t csi_codec_output_mute(codec_output_t *handle, int en);
418 
419 /**
420   \brief  Contol codec wakeup
421   \param[in]  handle: codec input handle
422   \param[in]  en: 1 enable wakeup, 0 disable wakeup
423   \return 0 for success, negative for error code
424 */
425 int32_t csi_codec_vad_enable(codec_input_t *handle, int en);
426 
427 #ifdef __cplusplus
428 }
429 #endif
430 
431 #endif /* _DRV_CODEC_H_  */
432 
433