b.liu | 1c74d69 | 2024-08-14 17:43:59 +0800 | [diff] [blame^] | 1 | #include "lynq-qser-audio.h" |
| 2 | #include "mbtk_type.h" |
| 3 | #include "mbtk_log.h" |
| 4 | #include "mbtk_audio2.h" |
| 5 | |
| 6 | #define AUDIO_DEV_PLAY "device1" |
| 7 | #define AUDIO_DEV_RECORDER "device2" |
| 8 | #define AUDIO_HDL_DEFAULT 0 |
| 9 | |
| 10 | static _cb_onPlayer play_cb = NULL; |
| 11 | static int play_hdl = -1; |
| 12 | |
| 13 | static _cb_onPlayer recv_cb = NULL; |
| 14 | static int recv_hdl = -1; |
| 15 | |
| 16 | int qser_AudPlayer_Open(char* device, _cb_onPlayer cb_fun) |
| 17 | { |
| 18 | UNUSED(device); |
| 19 | UNUSED(cb_fun); |
| 20 | if(device == NULL || strcmp(device, AUDIO_DEV_PLAY)) { |
| 21 | LOGE("device must be %s for play.", AUDIO_DEV_PLAY); |
| 22 | return -1; |
| 23 | } |
| 24 | |
| 25 | if(mbtk_audio_wav_init()) { |
| 26 | LOGE("mbtk_audio_wav_init() fail."); |
| 27 | return -1; |
| 28 | } |
| 29 | |
| 30 | play_cb = cb_fun; |
| 31 | |
| 32 | return 0; |
| 33 | } |
| 34 | |
| 35 | int qser_AudPlayer_PlayFrmFile(int hdl, const char *fd, int offset) |
| 36 | { |
| 37 | UNUSED(hdl); |
| 38 | UNUSED(fd); |
| 39 | UNUSED(offset); |
| 40 | if(play_hdl >= 0) { |
| 41 | LOGE("Play busy."); |
| 42 | if(play_cb) { |
| 43 | play_cb(-1); |
| 44 | } |
| 45 | return -1; |
| 46 | } |
| 47 | |
| 48 | if(mbtk_audio_wav_play_start(fd)) { |
| 49 | LOGE("mbtk_audio_wav_play_start() fail."); |
| 50 | if(play_cb) { |
| 51 | play_cb(-2); |
| 52 | } |
| 53 | return -1; |
| 54 | } |
| 55 | |
| 56 | play_hdl = hdl; |
| 57 | |
| 58 | if(play_cb) { |
| 59 | play_cb(0); |
| 60 | } |
| 61 | |
| 62 | return 0; |
| 63 | } |
| 64 | |
| 65 | int qser_AudPlayer_PlayPcmBuf(const unsigned char *pcm_data, int data_size, int period_size, |
| 66 | int period_count, int num_channels, int sample_rate, int ownerid) |
| 67 | { |
| 68 | |
| 69 | if(play_hdl >= 0) { |
| 70 | LOGE("Play busy."); |
| 71 | if(play_cb) { |
| 72 | play_cb(-1); |
| 73 | } |
| 74 | return -1; |
| 75 | } |
| 76 | |
| 77 | if(mbtk_audio_wav_stream_play_start(pcm_data, data_size, sample_rate, num_channels)) { |
| 78 | LOGE("mbtk_audio_wav_stream_play_start() fail."); |
| 79 | if(play_cb) { |
| 80 | play_cb(-2); |
| 81 | } |
| 82 | return -1; |
| 83 | } |
| 84 | |
| 85 | play_hdl = AUDIO_HDL_DEFAULT; |
| 86 | if(play_cb) { |
| 87 | play_cb(0); |
| 88 | } |
| 89 | return 0; |
| 90 | } |
| 91 | |
| 92 | int qser_AudPlayer_Pause(int hdl) |
| 93 | { |
| 94 | UNUSED(hdl); |
| 95 | if((play_hdl != AUDIO_HDL_DEFAULT && hdl != play_hdl) || play_hdl < 0) { |
| 96 | LOGE("Play busy or hdl error."); |
| 97 | return -1; |
| 98 | } |
| 99 | |
| 100 | if(mbtk_audio_wav_play_pause()) { |
| 101 | LOGE("mbtk_audio_wav_play_pause() fail."); |
| 102 | return -1; |
| 103 | } |
| 104 | |
| 105 | return 0; |
| 106 | } |
| 107 | |
| 108 | int qser_AudPlayer_Resume(int hdl) |
| 109 | { |
| 110 | UNUSED(hdl); |
| 111 | if((play_hdl != AUDIO_HDL_DEFAULT && hdl != play_hdl) || play_hdl < 0) { |
| 112 | LOGE("Play busy or hdl error."); |
| 113 | return -1; |
| 114 | } |
| 115 | |
| 116 | if(mbtk_audio_wav_play_resume()) { |
| 117 | LOGE("mbtk_audio_wav_play_resume() fail."); |
| 118 | return -1; |
| 119 | } |
| 120 | |
| 121 | return 0; |
| 122 | } |
| 123 | |
| 124 | void qser_AudPlayer_Stop(int hdl) |
| 125 | { |
| 126 | UNUSED(hdl); |
| 127 | if((play_hdl != AUDIO_HDL_DEFAULT && hdl != play_hdl) || play_hdl < 0) { |
| 128 | LOGE("Play busy or hdl error."); |
| 129 | return; |
| 130 | } |
| 131 | |
| 132 | if(mbtk_audio_wav_play_stop()) { |
| 133 | LOGE("mbtk_audio_wav_play_stop() fail."); |
| 134 | return; |
| 135 | } |
| 136 | |
| 137 | play_hdl = -1; |
| 138 | } |
| 139 | |
| 140 | |
| 141 | void qser_AudPlayer_Close(int hdl) |
| 142 | { |
| 143 | UNUSED(hdl); |
| 144 | |
| 145 | if(play_hdl >= 0) { |
| 146 | qser_AudPlayer_Stop(hdl); |
| 147 | } |
| 148 | |
| 149 | if(mbtk_audio_wav_deinit()) { |
| 150 | LOGE("mbtk_audio_wav_deinit() fail."); |
| 151 | return; |
| 152 | } |
| 153 | |
| 154 | play_cb = NULL; |
| 155 | } |
| 156 | |
| 157 | int qser_AudRecorder_Open(char* device, _cb_onPlayer cb_fun) |
| 158 | { |
| 159 | UNUSED(device); |
| 160 | UNUSED(cb_fun); |
| 161 | if(device == NULL || strcmp(device, AUDIO_DEV_RECORDER)) { |
| 162 | LOGE("device must be %s for recv.", AUDIO_DEV_RECORDER); |
| 163 | return -1; |
| 164 | } |
| 165 | |
| 166 | if(mbtk_audio_wav_init()) { |
| 167 | LOGE("mbtk_audio_wav_init() fail."); |
| 168 | return -1; |
| 169 | } |
| 170 | |
| 171 | recv_cb = cb_fun; |
| 172 | |
| 173 | return 0; |
| 174 | } |
| 175 | |
| 176 | int qser_AudRecorder_StartRecord(int hdl, const char *fd, int offset) |
| 177 | { |
| 178 | UNUSED(hdl); |
| 179 | UNUSED(fd); |
| 180 | UNUSED(offset); |
| 181 | if(recv_hdl >= 0) { |
| 182 | LOGE("Recv busy."); |
| 183 | if(recv_cb) { |
| 184 | recv_cb(-1); |
| 185 | } |
| 186 | return -1; |
| 187 | } |
| 188 | |
| 189 | if(mbtk_audio_wav_recorder_start(fd, MBTK_AUDIO_SAMPLE_RATE_8000)) { |
| 190 | LOGE("mbtk_audio_wav_recorder_start() fail."); |
| 191 | if(recv_cb) { |
| 192 | recv_cb(-2); |
| 193 | } |
| 194 | return -1; |
| 195 | } |
| 196 | |
| 197 | recv_hdl = hdl; |
| 198 | if(recv_cb) { |
| 199 | recv_cb(0); |
| 200 | } |
| 201 | return 0; |
| 202 | } |
| 203 | |
| 204 | int qser_AudRecorder_StartRecord_Custom(char *file, int period_size, int period_count, |
| 205 | int num_channels, int sample_rate) |
| 206 | { |
| 207 | return qser_AudRecorder_StartRecord(0, file, 0); |
| 208 | } |
| 209 | |
| 210 | int qser_AudRecorder_Pause(void) |
| 211 | { |
| 212 | if(recv_hdl < 0) { |
| 213 | LOGE("Recv busy or hdl error."); |
| 214 | return -1; |
| 215 | } |
| 216 | |
| 217 | if(mbtk_audio_wav_recorder_pause()) { |
| 218 | LOGE("mbtk_audio_wav_recorder_pause() fail."); |
| 219 | return -1; |
| 220 | } |
| 221 | return 0; |
| 222 | } |
| 223 | |
| 224 | int qser_AudRecorder_Resume(void) |
| 225 | { |
| 226 | if(recv_hdl < 0) { |
| 227 | LOGE("Recv busy or hdl error."); |
| 228 | return -1; |
| 229 | } |
| 230 | |
| 231 | if(mbtk_audio_wav_recorder_resume()) { |
| 232 | LOGE("mbtk_audio_wav_recorder_resume() fail."); |
| 233 | return -1; |
| 234 | } |
| 235 | return 0; |
| 236 | } |
| 237 | |
| 238 | void qser_AudRecorder_Stop(void) |
| 239 | { |
| 240 | if(recv_hdl < 0) { |
| 241 | LOGE("Recv busy or hdl error."); |
| 242 | return; |
| 243 | } |
| 244 | |
| 245 | if(mbtk_audio_wav_recorder_stop()) { |
| 246 | LOGE("mbtk_audio_wav_recorder_stop() fail."); |
| 247 | return; |
| 248 | } |
| 249 | |
| 250 | recv_hdl = -1; |
| 251 | } |
| 252 | |
| 253 | void qser_AudRecorder_Close(void) |
| 254 | { |
| 255 | if(recv_hdl >= 0) { |
| 256 | qser_AudRecorder_Stop(); |
| 257 | } |
| 258 | |
| 259 | if(mbtk_audio_wav_deinit()) { |
| 260 | LOGE("mbtk_audio_wav_deinit() fail."); |
| 261 | return; |
| 262 | } |
| 263 | |
| 264 | recv_cb = NULL; |
| 265 | } |
| 266 | |
| 267 | void qser_Audio_Deinit(void) |
| 268 | { |
| 269 | // Do nothing... |
| 270 | } |
| 271 | |