|
1 |
| -/* |
2 |
| - * ESPRESSIF MIT License |
3 |
| - * |
4 |
| - * Copyright (c) 2018 <ESPRESSIF SYSTEMS (SHANGHAI) PTE LTD> |
5 |
| - * |
6 |
| - * Permission is hereby granted for use on all ESPRESSIF SYSTEMS products, in which case, |
7 |
| - * it is free of charge, to any person obtaining a copy of this software and associated |
8 |
| - * documentation files (the "Software"), to deal in the Software without restriction, including |
9 |
| - * without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, |
10 |
| - * and/or sell copies of the Software, and to permit persons to whom the Software is furnished |
11 |
| - * to do so, subject to the following conditions: |
12 |
| - * |
13 |
| - * The above copyright notice and this permission notice shall be included in all copies or |
14 |
| - * substantial portions of the Software. |
15 |
| - * |
16 |
| - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
17 |
| - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS |
18 |
| - * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
19 |
| - * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
20 |
| - * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
21 |
| - * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
22 |
| - * |
23 |
| - */ |
24 |
| - |
25 |
| -#include <string.h> |
26 |
| -#include "driver/gpio.h" |
27 |
| -#include "esp_log.h" |
28 |
| -#include "audio_hal.h" |
29 |
| - |
30 |
| -#include "audio_mem.h" |
31 |
| -#include "audio_mutex.h" |
32 |
| - |
33 |
| -static const char *TAG = "AUDIO_HAL"; |
34 |
| - |
35 |
| -#define AUDIO_HAL_CHECK_NULL(a, format, b, ...) \ |
36 |
| - if ((a) == 0) { \ |
37 |
| - ESP_LOGE(TAG, format, ##__VA_ARGS__); \ |
38 |
| - return b;\ |
39 |
| - } |
40 |
| - |
41 |
| -audio_hal_handle_t audio_hal_init(audio_hal_codec_config_t *audio_hal_conf, audio_hal_func_t *audio_hal_func) |
42 |
| -{ |
43 |
| - esp_err_t ret = 0; |
44 |
| - audio_hal_handle_t audio_hal = (audio_hal_handle_t) audio_calloc(1, sizeof(audio_hal_func_t)); |
45 |
| - AUDIO_MEM_CHECK(TAG, audio_hal, return NULL); |
46 |
| - memcpy(audio_hal, audio_hal_func, sizeof(audio_hal_func_t)); |
47 |
| - audio_hal->audio_hal_lock = mutex_create(); |
48 |
| - |
49 |
| - AUDIO_MEM_CHECK(TAG, audio_hal->audio_hal_lock, { |
50 |
| - audio_free(audio_hal); |
51 |
| - return NULL; |
52 |
| - }); |
53 |
| - mutex_lock(audio_hal->audio_hal_lock); |
54 |
| - ret = audio_hal->audio_codec_initialize(audio_hal_conf); |
55 |
| - if (ret == ESP_FAIL) { |
56 |
| - audio_free(audio_hal); |
57 |
| - if (audio_hal_func->handle) { |
58 |
| - return audio_hal_func->handle; |
59 |
| - } else { |
60 |
| - ESP_LOGE(TAG, "codec init failed!"); |
61 |
| - return NULL; |
62 |
| - } |
63 |
| - } |
64 |
| - ret |= audio_hal->audio_codec_config_iface(audio_hal_conf->codec_mode, &audio_hal_conf->i2s_iface); |
65 |
| - ret |= audio_hal->audio_codec_set_volume(AUDIO_HAL_VOL_DEFAULT); |
66 |
| - audio_hal->handle = audio_hal; |
67 |
| - audio_hal_func->handle = audio_hal; |
68 |
| - mutex_unlock(audio_hal->audio_hal_lock); |
69 |
| - return audio_hal; |
70 |
| -} |
71 |
| - |
72 |
| -esp_err_t audio_hal_deinit(audio_hal_handle_t audio_hal) |
73 |
| -{ |
74 |
| - esp_err_t ret; |
75 |
| - AUDIO_HAL_CHECK_NULL(audio_hal, "audio_hal handle is null", -1); |
76 |
| - mutex_destroy(audio_hal->audio_hal_lock); |
77 |
| - ret = audio_hal->audio_codec_deinitialize(); |
78 |
| - audio_hal->audio_hal_lock = NULL; |
79 |
| - audio_hal->handle = NULL; |
80 |
| - audio_free(audio_hal); |
81 |
| - audio_hal = NULL; |
82 |
| - return ret; |
83 |
| -} |
84 |
| - |
85 |
| -esp_err_t audio_hal_ctrl_codec(audio_hal_handle_t audio_hal, audio_hal_codec_mode_t mode, audio_hal_ctrl_t audio_hal_state) |
86 |
| -{ |
87 |
| - esp_err_t ret; |
88 |
| - AUDIO_HAL_CHECK_NULL(audio_hal, "audio_hal handle is null", -1); |
89 |
| - mutex_lock(audio_hal->audio_hal_lock); |
90 |
| - ESP_LOGI(TAG, "Codec mode is %d, Ctrl:%d", mode, audio_hal_state); |
91 |
| - ret = audio_hal->audio_codec_ctrl(mode, audio_hal_state); |
92 |
| - mutex_unlock(audio_hal->audio_hal_lock); |
93 |
| - return ret; |
94 |
| -} |
95 |
| - |
96 |
| -esp_err_t audio_hal_codec_iface_config(audio_hal_handle_t audio_hal, audio_hal_codec_mode_t mode, audio_hal_codec_i2s_iface_t *iface) |
97 |
| -{ |
98 |
| - esp_err_t ret = 0; |
99 |
| - AUDIO_HAL_CHECK_NULL(audio_hal, "audio_hal handle is null", -1); |
100 |
| - AUDIO_HAL_CHECK_NULL(iface, "Get volume para is null", -1); |
101 |
| - mutex_lock(audio_hal->audio_hal_lock); |
102 |
| - ret = audio_hal->audio_codec_config_iface(mode, iface); |
103 |
| - mutex_unlock(audio_hal->audio_hal_lock); |
104 |
| - return ret; |
105 |
| -} |
106 |
| - |
107 |
| -esp_err_t audio_hal_set_mute(audio_hal_handle_t audio_hal, bool mute) |
108 |
| -{ |
109 |
| - esp_err_t ret; |
110 |
| - AUDIO_HAL_CHECK_NULL(audio_hal, "audio_hal handle is null", -1); |
111 |
| - mutex_lock(audio_hal->audio_hal_lock); |
112 |
| - ret = audio_hal->audio_codec_set_mute(mute); |
113 |
| - mutex_unlock(audio_hal->audio_hal_lock); |
114 |
| - return ret; |
115 |
| -} |
116 |
| - |
117 |
| -esp_err_t audio_hal_set_volume(audio_hal_handle_t audio_hal, int volume) |
118 |
| -{ |
119 |
| - esp_err_t ret; |
120 |
| - AUDIO_HAL_CHECK_NULL(audio_hal, "audio_hal handle is null", -1); |
121 |
| - mutex_lock(audio_hal->audio_hal_lock); |
122 |
| - ret = audio_hal->audio_codec_set_volume(volume); |
123 |
| - mutex_unlock(audio_hal->audio_hal_lock); |
124 |
| - return ret; |
125 |
| -} |
126 |
| - |
127 |
| -esp_err_t audio_hal_get_volume(audio_hal_handle_t audio_hal, int *volume) |
128 |
| -{ |
129 |
| - esp_err_t ret; |
130 |
| - AUDIO_HAL_CHECK_NULL(audio_hal, "audio_hal handle is null", -1); |
131 |
| - AUDIO_HAL_CHECK_NULL(volume, "Get volume para is null", -1); |
132 |
| - mutex_lock(audio_hal->audio_hal_lock); |
133 |
| - ret = audio_hal->audio_codec_get_volume(volume); |
134 |
| - mutex_unlock(audio_hal->audio_hal_lock); |
135 |
| - return ret; |
136 |
| -} |
| 1 | +/* |
| 2 | + * ESPRESSIF MIT License |
| 3 | + * |
| 4 | + * Copyright (c) 2018 <ESPRESSIF SYSTEMS (SHANGHAI) PTE LTD> |
| 5 | + * |
| 6 | + * Permission is hereby granted for use on all ESPRESSIF SYSTEMS products, in which case, |
| 7 | + * it is free of charge, to any person obtaining a copy of this software and associated |
| 8 | + * documentation files (the "Software"), to deal in the Software without restriction, including |
| 9 | + * without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 10 | + * and/or sell copies of the Software, and to permit persons to whom the Software is furnished |
| 11 | + * to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in all copies or |
| 14 | + * substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS |
| 18 | + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
| 19 | + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
| 20 | + * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 21 | + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 22 | + * |
| 23 | + */ |
| 24 | + |
| 25 | +#include <string.h> |
| 26 | +#include "driver/gpio.h" |
| 27 | +#include "esp_log.h" |
| 28 | +#include "audio_hal.h" |
| 29 | + |
| 30 | +#include "audio_mem.h" |
| 31 | +#include "audio_mutex.h" |
| 32 | + |
| 33 | +static const char *TAG = "AUDIO_HAL"; |
| 34 | + |
| 35 | +#define AUDIO_HAL_CHECK_NULL(a, format, b, ...) \ |
| 36 | + if ((a) == 0) { \ |
| 37 | + ESP_LOGE(TAG, format, ##__VA_ARGS__); \ |
| 38 | + return b;\ |
| 39 | + } |
| 40 | + |
| 41 | +audio_hal_handle_t audio_hal_init(audio_hal_codec_config_t *audio_hal_conf, audio_hal_func_t *audio_hal_func) |
| 42 | +{ |
| 43 | + esp_err_t ret = 0; |
| 44 | + audio_hal_handle_t audio_hal = (audio_hal_handle_t) audio_calloc(1, sizeof(audio_hal_func_t)); |
| 45 | + AUDIO_MEM_CHECK(TAG, audio_hal, return NULL); |
| 46 | + memcpy(audio_hal, audio_hal_func, sizeof(audio_hal_func_t)); |
| 47 | + audio_hal->audio_hal_lock = mutex_create(); |
| 48 | + |
| 49 | + AUDIO_MEM_CHECK(TAG, audio_hal->audio_hal_lock, { |
| 50 | + audio_free(audio_hal); |
| 51 | + return NULL; |
| 52 | + }); |
| 53 | + mutex_lock(audio_hal->audio_hal_lock); |
| 54 | + ret = audio_hal->audio_codec_initialize(audio_hal_conf); |
| 55 | + if (ret == ESP_FAIL) { |
| 56 | + audio_free(audio_hal); |
| 57 | + if (audio_hal_func->handle) { |
| 58 | + return audio_hal_func->handle; |
| 59 | + } else { |
| 60 | + ESP_LOGE(TAG, "codec init failed!"); |
| 61 | + return NULL; |
| 62 | + } |
| 63 | + } |
| 64 | + ret |= audio_hal->audio_codec_config_iface(audio_hal_conf->codec_mode, &audio_hal_conf->i2s_iface); |
| 65 | + ret |= audio_hal->audio_codec_set_volume(AUDIO_HAL_VOL_DEFAULT); |
| 66 | + audio_hal->handle = audio_hal; |
| 67 | + audio_hal_func->handle = audio_hal; |
| 68 | + mutex_unlock(audio_hal->audio_hal_lock); |
| 69 | + return audio_hal; |
| 70 | +} |
| 71 | + |
| 72 | +esp_err_t audio_hal_deinit(audio_hal_handle_t audio_hal) |
| 73 | +{ |
| 74 | + esp_err_t ret; |
| 75 | + AUDIO_HAL_CHECK_NULL(audio_hal, "audio_hal handle is null", -1); |
| 76 | + mutex_destroy(audio_hal->audio_hal_lock); |
| 77 | + ret = audio_hal->audio_codec_deinitialize(); |
| 78 | + audio_hal->audio_hal_lock = NULL; |
| 79 | + audio_hal->handle = NULL; |
| 80 | + audio_free(audio_hal); |
| 81 | + audio_hal = NULL; |
| 82 | + return ret; |
| 83 | +} |
| 84 | + |
| 85 | +esp_err_t audio_hal_ctrl_codec(audio_hal_handle_t audio_hal, audio_hal_codec_mode_t mode, audio_hal_ctrl_t audio_hal_state) |
| 86 | +{ |
| 87 | + esp_err_t ret; |
| 88 | + AUDIO_HAL_CHECK_NULL(audio_hal, "audio_hal handle is null", -1); |
| 89 | + mutex_lock(audio_hal->audio_hal_lock); |
| 90 | + ESP_LOGI(TAG, "Codec mode is %d, Ctrl:%d", mode, audio_hal_state); |
| 91 | + ret = audio_hal->audio_codec_ctrl(mode, audio_hal_state); |
| 92 | + mutex_unlock(audio_hal->audio_hal_lock); |
| 93 | + return ret; |
| 94 | +} |
| 95 | + |
| 96 | +esp_err_t audio_hal_codec_iface_config(audio_hal_handle_t audio_hal, audio_hal_codec_mode_t mode, audio_hal_codec_i2s_iface_t *iface) |
| 97 | +{ |
| 98 | + esp_err_t ret = 0; |
| 99 | + AUDIO_HAL_CHECK_NULL(audio_hal, "audio_hal handle is null", -1); |
| 100 | + AUDIO_HAL_CHECK_NULL(iface, "Get volume para is null", -1); |
| 101 | + mutex_lock(audio_hal->audio_hal_lock); |
| 102 | + ret = audio_hal->audio_codec_config_iface(mode, iface); |
| 103 | + mutex_unlock(audio_hal->audio_hal_lock); |
| 104 | + return ret; |
| 105 | +} |
| 106 | + |
| 107 | +esp_err_t audio_hal_set_mute(audio_hal_handle_t audio_hal, bool mute) |
| 108 | +{ |
| 109 | + esp_err_t ret; |
| 110 | + AUDIO_HAL_CHECK_NULL(audio_hal, "audio_hal handle is null", -1); |
| 111 | + mutex_lock(audio_hal->audio_hal_lock); |
| 112 | + ret = audio_hal->audio_codec_set_mute(mute); |
| 113 | + mutex_unlock(audio_hal->audio_hal_lock); |
| 114 | + return ret; |
| 115 | +} |
| 116 | + |
| 117 | +esp_err_t audio_hal_set_volume(audio_hal_handle_t audio_hal, int volume) |
| 118 | +{ |
| 119 | + esp_err_t ret; |
| 120 | + AUDIO_HAL_CHECK_NULL(audio_hal, "audio_hal handle is null", -1); |
| 121 | + mutex_lock(audio_hal->audio_hal_lock); |
| 122 | + ret = audio_hal->audio_codec_set_volume(volume); |
| 123 | + mutex_unlock(audio_hal->audio_hal_lock); |
| 124 | + return ret; |
| 125 | +} |
| 126 | + |
| 127 | +esp_err_t audio_hal_get_volume(audio_hal_handle_t audio_hal, int *volume) |
| 128 | +{ |
| 129 | + esp_err_t ret; |
| 130 | + AUDIO_HAL_CHECK_NULL(audio_hal, "audio_hal handle is null", -1); |
| 131 | + AUDIO_HAL_CHECK_NULL(volume, "Get volume para is null", -1); |
| 132 | + mutex_lock(audio_hal->audio_hal_lock); |
| 133 | + ret = audio_hal->audio_codec_get_volume(volume); |
| 134 | + mutex_unlock(audio_hal->audio_hal_lock); |
| 135 | + return ret; |
| 136 | +} |
0 commit comments