1 #ifndef __ALSA_H__
2 #define __ALSA_H__
3 
4 #include <stdint.h>
5 
6 #ifdef __cplusplus
7 extern "C" {
8 #endif
9 
10 /**
11  * for 48K sample rate, 1 channels, 16bit
12  * 96Bytes means 1ms data size.
13  * bitrate 48000*16*1/1000 = 768Kbps
14  * in 1ms data size is 768*1000/1000/8 = 96 Bytes
15  * dma buffer size == trigger size , start delay all dma buffer size.
16  * for 48K sample rate, 1 channels, 24bit
17  * 192Bytes means 1ms data size.
18 */
19 #define ALSA_SLOT_MS                  (128)
20 #define ALSA_ONE_MS_PCM_SIZE          (192)
21 #define ALSA_PCM_DMA_BUFFER_SIZE      (ALSA_ONE_MS_PCM_SIZE*ALSA_SLOT_MS*2)
22 
23 #ifdef ALSA_FILL_DATA_ON_START
24 #define ALSA_PCM_THR_TRIGGER_PLAY     (ALSA_PCM_DMA_BUFFER_SIZE)
25 #else
26 #define ALSA_PCM_THR_TRIGGER_PLAY     (ALSA_PCM_DMA_BUFFER_SIZE/2)
27 #endif
28 
29 #if (ALSA_SLOT_MS < 64)
30 #define ALSA_PCM_QUEUE_BUFFER_SIZE    (ALSA_PCM_DMA_BUFFER_SIZE*4/ALSA_SLOT_MS*64) /* for sw gain fade out */
31 #else
32 #define ALSA_PCM_QUEUE_BUFFER_SIZE    (ALSA_PCM_DMA_BUFFER_SIZE*4)
33 #endif
34 
35 #define ALSA_INSTANCE_COUNT           (3)
36 
37 #ifdef ALIOS_THINGS_RELEASE
38 #define ALSA_DEFAULT_VOL_DAC          (80)
39 #define ALSA_DEFAULT_VOL_PERCENT      (90)
40 #define ALSA_MUTE_VOL                 (2)
41 #else
42 #define ALSA_DEFAULT_VOL_DAC          (10)
43 #define ALSA_DEFAULT_VOL_PERCENT      (50)
44 #define ALSA_MUTE_VOL                 (0)
45 #endif
46 
47 typedef enum {
48     ALSA_MODE_UNKNOWN = -1,
49     ALSA_MODE_OUT = 0,
50     ALSA_MODE_IN = 1,
51     ALSA_MODE_ULTRASONIC = (1 << 1),
52     ALSA_MODE_MAX,
53 } alsa_mode_t;
54 
55 typedef enum {
56     /** Signed, 8-bit */
57     ALSA_PCM_FORMAT_S8 = 1,
58     /** Signed 16-bit, little endian */
59     ALSA_PCM_FORMAT_S16_LE = 0,
60     /** Signed, 16-bit, big endian */
61     ALSA_PCM_FORMAT_S16_BE = 2,
62     /** Signed, 24-bit (32-bit in memory), little endian */
63     ALSA_PCM_FORMAT_S24_LE,
64     /** Signed, 24-bit (32-bit in memory), big endian */
65     ALSA_PCM_FORMAT_S24_BE,
66     /** Signed, 24-bit, little endian */
67     ALSA_PCM_FORMAT_S24_3LE,
68     /** Signed, 24-bit, big endian */
69     ALSA_PCM_FORMAT_S24_3BE,
70     /** Signed, 32-bit, little endian */
71     ALSA_PCM_FORMAT_S32_LE,
72     /** Signed, 32-bit, big endian */
73     ALSA_PCM_FORMAT_S32_BE,
74     /** Max of the enumeration list, not an actual format. */
75     ALSA_PCM_FORMAT_MAX
76 } alsa_pcm_format_t;
77 
78 typedef enum {
79     ALSA_PCM_STATE_UNDERRUN,
80     ALSA_PCM_STATE_UNDERRUN_EXIT,
81     ALSA_PCM_STATE_OVERRUN,
82     ALSA_PCM_STATE_OVERRUN_EXIT,
83 } alsa_pcm_state_t;
84 
85 typedef void (*alsa_pcm_state_callback_t)(alsa_pcm_state_t state, void *user, void *info);
86 
87 typedef struct alsa_handle alsa_handle_t;
88 
89 void alsa_init(void);
90 alsa_handle_t * alsa_open(alsa_mode_t mode, int sample_rate, int channels, alsa_pcm_format_t format);
91 int alsa_write(alsa_handle_t * h, uint8_t *buf, uint32_t size);
92 int alsa_start(alsa_handle_t * h);
93 int alsa_stop(alsa_handle_t * h);
94 int alsa_close(alsa_handle_t * h);
95 void alsa_register_pcm_state_callback(alsa_handle_t * h, alsa_pcm_state_callback_t cb, void * arg);
96 
97 void alsa_mute_set(void);
98 void alsa_mute_cancel(void);
99 void alsa_volume_set(uint8_t vol_percent, uint8_t vol_dac);
100 uint8_t alsa_volume_get(void);
101 uint8_t alsa_volume_percent_get(void);
102 uint8_t alsa_volume_adc_get(uint8_t mic_id);
103 void alsa_volume_adc_set(uint8_t mic_id, uint8_t vol);
104 uint8_t alsa_is_playing_state(void);
105 uint8_t alsa_is_close_state(void);
106 
107 #ifdef __cplusplus
108 }
109 #endif
110 
111 #endif