blob: dca245c69731158f2203528e64bd760e3612dbc4 [file] [log] [blame]
b.liu1c1c7212023-12-22 16:35:27 +08001#include <sys/stat.h>
2#include <unistd.h>
3#include <stdio.h>
4#include <fcntl.h>
5#include <errno.h>
6
7#include "mbtk_log.h"
8#include "mbtk_audio_internal.h"
9
10#define WAV_PLAY_BUFF 2048
11
12static int wav_play_fd = -1;
13static audio_play_state_enum play_state = AUDIO_PLAY_STATE_STOP;
14static pthread_cond_t play_cond;
15static pthread_mutex_t play_mutex;
16static pthread_t play_thread_play;
17
18static int wav_recorder_fd = -1;
19static struct wav_header recorder_header;
20static uint32 recorver_data_count = 0;
21
22static void audio_play_thread(void *arg)
23{
24 int rc, len, frames = 0;
25 char buf[WAV_PLAY_BUFF];
26
27 play_state = AUDIO_PLAY_STATE_RUNNING;
28 pthread_mutex_init(&play_mutex, NULL);
29 pthread_cond_init(&play_cond, NULL);
30
31 while (TRUE) {
32 /* Playback loop */
33 pthread_mutex_lock(&play_mutex);
34 if(play_state == AUDIO_PLAY_STATE_STOP) {
35 LOGD("Stop play...");
36 pthread_mutex_unlock(&play_mutex);
37 break;
38 } else if(play_state == AUDIO_PLAY_STATE_PAUSE) {
39 pthread_cond_wait(&play_cond, &play_mutex);
40 pthread_mutex_unlock(&play_mutex);
41 continue;
42 } else {
43 pthread_mutex_unlock(&play_mutex);
44 }
45
46 memset(buf, 0x00, sizeof(buf));
47 len = read(wav_play_fd, buf, WAV_PLAY_BUFF);
48 if (len == -1) {
49 LOGE("%s: error reading from file", __FUNCTION__);
50 goto thread_end;
51 }
52
53 if (len == 0) {
54 /* reached EOF */
55 LOGE("%s: Read wav file end.", __FUNCTION__);
56 break;
57 }
58
59 if((rc = mbtk_audio_pcm_play_data_send(buf, len)) < len) {
60 LOGE("Send data %d/%d", rc, len);
61 goto thread_end;
62 }
63
64 LOGD("%s: No.%d frame playback", __FUNCTION__, ++frames);
65 }
66
67 play_state = AUDIO_PLAY_STATE_STOP;
68
69thread_end:
70 pthread_mutex_destroy(&play_mutex);
71 pthread_cond_destroy(&play_cond);
72 mbtk_audio_pcm_play_stop();
73
74 if (close(wav_play_fd))
75 LOGE("%s: error closing file", __FUNCTION__);
76
77 wav_play_fd = -1;
78 LOGD("%s: finished pcm playback.", __FUNCTION__);
79 return;
80}
81
82static void audio_recorder_cb(void *data, uint32 data_len)
83{
84 if(data_len > 0) {
85 LOGD("Recorver data:%d, count:%d", data_len, recorver_data_count);
86 if (write(wav_recorder_fd, data, data_len) < data_len) {
87 LOGE("%s: error writing to file!", __FUNCTION__);
88 }
89 recorver_data_count += data_len;
90 } else {
91 LOGD("Recorver data end.");
92 recorder_header.data_sz = recorver_data_count;
93 recorder_header.riff_sz = recorder_header.data_sz + sizeof(recorder_header) - 8;
94 lseek(wav_recorder_fd, 0, SEEK_SET);
95 write(wav_recorder_fd, &recorder_header, sizeof(struct wav_header));
96
97 close(wav_recorder_fd);
98 wav_recorder_fd = -1;
99 }
100}
101
102int mbtk_audio_wav_init()
103{
104 //mbtk_log_init("radio", "MBTK_AUDIO");
105 return mbtk_audio_pcm_init();
106}
107
108int mbtk_audio_wav_play_start(const void *wav_file)
109{
110 struct stat st;
111 const char *path = (const char *)wav_file;
112 struct riff_wave_header riff_wave_header;
113 struct chunk_header chunk_header;
114 struct chunk_fmt chunk_fmt = {0};
115 unsigned int more_chunks = 1;
116
117 if(play_state != AUDIO_PLAY_STATE_STOP) {
118 LOGW("Audio is playing...");
119 return -1;
120 }
121
122 /* Check and open source file */
123 if (access(path, F_OK) || stat(path, &st)) {
124 LOGE("%s: error reading from file %s", __FUNCTION__, path);
125 return -1;
126 }
127
128 if (!st.st_size) {
129 LOGE("%s: empty file %s", __FUNCTION__, path);
130 return -1;
131 }
132
133 wav_play_fd = open(path, O_RDONLY);
134 if (wav_play_fd < 0) {
135 LOGE("%s: error opening file %s", __FUNCTION__, path);
136 return -1;
137 }
138
139 read(wav_play_fd, &riff_wave_header, sizeof(riff_wave_header));
140 if ((riff_wave_header.riff_id != ID_RIFF) || (riff_wave_header.wave_id != ID_WAVE)) {
141 LOGE("Error: '%s' is not a riff/wave file", path);
142 close(wav_play_fd);
143 return -1;
144 }
145
146 do {
147 read(wav_play_fd, &chunk_header, sizeof(chunk_header));
148
149 switch (chunk_header.id) {
150 case ID_FMT:
151 read(wav_play_fd, &chunk_fmt, sizeof(chunk_fmt));
152 /* If the format header is larger, skip the rest */
153 if (chunk_header.sz > sizeof(chunk_fmt))
154 lseek(wav_play_fd, chunk_header.sz - sizeof(chunk_fmt), SEEK_CUR);
155 break;
156 case ID_DATA:
157 /* Stop looking for chunks */
158 more_chunks = 0;
159 break;
160 default:
161 /* Unknown chunk, skip bytes */
162 lseek(wav_play_fd, chunk_header.sz, SEEK_CUR);
163 }
164 } while (more_chunks);
165
166 //Support 8k/16k & mono wave file
167 if (((chunk_fmt.sample_rate != 8000) && (chunk_fmt.sample_rate != 16000))
168 || (chunk_fmt.num_channels != 1) ) {
169 LOGD("%s: error wave file:sample_rate = %d, num_channels = %d!!",
170 __FUNCTION__,chunk_fmt.sample_rate, chunk_fmt.num_channels);
171 close(wav_play_fd);
172 return -1;
173 }
174
175 LOGE("%s: success open wave file:%s, sample_rate = %d, num_channels = %d.",
176 __FUNCTION__, path, chunk_fmt.sample_rate, chunk_fmt.num_channels);
177
178 if ((8000 == chunk_fmt.sample_rate) && (1 == chunk_fmt.num_channels)) {
179 mbtk_audio_pcm_sample_rate_set(MBTK_AUDIO_SAMPLE_RATE_8000);
180 } else if ((16000 == chunk_fmt.sample_rate) && (1 == chunk_fmt.num_channels)) {
181 mbtk_audio_pcm_sample_rate_set(MBTK_AUDIO_SAMPLE_RATE_16000);
182 }
183
184 if (mbtk_audio_pcm_play_start()) {
185 LOGE("%s: error opening output device.", __FUNCTION__);
186 return -1;
187 }
188
189 LOGD("Start play wav file...");
190
191 pthread_attr_t thread_attr;
192 pthread_attr_init(&thread_attr);
193 if(pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED))
194 {
195 LOGE("pthread_attr_setdetachstate() fail.");
196 return -1;
197 }
198
199 if (pthread_create(&play_thread_play, NULL, (void *)&audio_play_thread, NULL) < 0) {
200 LOGE("%s: error creating thread_play!", __FUNCTION__);
201 return -1;
202 }
203
204 return 0;
205}
206
207int mbtk_audio_wav_play_pause()
208{
209 int result = 0;
210 pthread_mutex_lock(&play_mutex);
211 if(play_state == AUDIO_PLAY_STATE_RUNNING) {
212 play_state = AUDIO_PLAY_STATE_PAUSE;
213 } else {
214 result = -1;
215 LOGW("Audio state : %d", play_state);
216 }
217 pthread_mutex_unlock(&play_mutex);
218 return result;
219}
220
221int mbtk_audio_wav_play_resume()
222{
223 int result = 0;
224 pthread_mutex_lock(&play_mutex);
225 if(play_state == AUDIO_PLAY_STATE_PAUSE) {
226 play_state = AUDIO_PLAY_STATE_RUNNING;
227 pthread_cond_signal(&play_cond);
228 } else {
229 result = -1;
230 LOGW("Audio state : %d", play_state);
231 }
232 pthread_mutex_unlock(&play_mutex);
233 return result;
234}
235
236
237int mbtk_audio_wav_play_stop()
238{
239 int result = 0;
240 pthread_mutex_lock(&play_mutex);
241 if(play_state == AUDIO_PLAY_STATE_PAUSE || play_state == AUDIO_PLAY_STATE_RUNNING) {
242 if(play_state == AUDIO_PLAY_STATE_PAUSE) {
243 pthread_cond_signal(&play_cond);
244 }
245 play_state = AUDIO_PLAY_STATE_STOP;
246 pthread_mutex_unlock(&play_mutex);
247
248 LOGD("Waitting play thread exit...");
249 if (pthread_join(play_thread_play, NULL)) {
250 LOGE("error join thread_play!");
251 // abort();
252 }
253 LOGD("Play thread exit success.");
254 } else {
255 pthread_mutex_unlock(&play_mutex);
256 result = -1;
257 LOGW("Audio state : %d", play_state);
258 }
259
260 return result;
261}
262
263int mbtk_audio_wav_recorder_start(const void *wav_file, mbtk_audio_sample_rate_enum sample_rate)
264{
265 int rc;
266 const char *path = (const char *)wav_file;
267
268 LOGD("wav_file is %s.", path);
269 if(wav_recorder_fd > 0) {
270 LOGW("Audio is recorder...");
271 }
272
273 memset(&recorder_header, 0x0, sizeof(struct wav_header));
274 recorder_header.riff_id = ID_RIFF;
275 recorder_header.riff_sz = 0;
276 recorder_header.riff_fmt = ID_WAVE;
277 recorder_header.fmt_id = ID_FMT;
278 recorder_header.fmt_sz = 16;
279 recorder_header.audio_format = 1; //FORMAT_PCM;
280 recorder_header.num_channels = 1; //Modem ONLY support mono recording
281 recorder_header.sample_rate = (sample_rate == MBTK_AUDIO_SAMPLE_RATE_8000 ? 8000 : 16000);
282 recorder_header.bits_per_sample = 16; //PCM_SAMPLEBITS_S16_LE;
283 recorder_header.byte_rate = (recorder_header.bits_per_sample / 8) * recorder_header.num_channels * recorder_header.sample_rate;
284 recorder_header.block_align = recorder_header.num_channels * (recorder_header.bits_per_sample / 8);
285 recorder_header.data_id = ID_DATA;
286
287 mbtk_audio_pcm_sample_rate_set(sample_rate);
288
289 wav_recorder_fd = open(path, O_WRONLY | O_CREAT | O_TRUNC);
290 if (wav_recorder_fd < 0) {
291 LOGE("%s: error opening file %s!", __FUNCTION__, path);
292 return -1;
293 }
294
295 //leave enough room for header
296 lseek(wav_recorder_fd, sizeof(struct wav_header), SEEK_SET);
297
298 recorver_data_count = 0;
299
300 return mbtk_audio_pcm_recorder_start(audio_recorder_cb);
301}
302
303int mbtk_audio_wav_recorder_pause()
304{
305 return mbtk_audio_pcm_recorder_pause();
306}
307
308int mbtk_audio_wav_recorder_resume()
309{
310 return mbtk_audio_pcm_recorder_resume();
311}
312
313
314int mbtk_audio_wav_recorder_stop()
315{
316 return mbtk_audio_pcm_recorder_stop();
317}
318
319int mbtk_audio_wav_deinit()
320{
321 if(play_state != AUDIO_PLAY_STATE_STOP) {
322 if(mbtk_audio_wav_play_stop()) {
323 LOGE("mbtk_audio_wav_play_stop() fail.");
324 }
325 }
326
327 if(wav_recorder_fd > 0) {
328 mbtk_audio_wav_recorder_stop();
329 }
330
331 return mbtk_audio_pcm_deinit();
332}
333