Add asr1806 Audio API.
Change-Id: I0cefc67d0825e2bf4476fd0a07cb0d1fc5971457
diff --git a/mbtk/mbtk_audio_lib/src/mbtk_mp3.c b/mbtk/mbtk_audio_lib/src/mbtk_mp3.c
new file mode 100755
index 0000000..8b13789
--- /dev/null
+++ b/mbtk/mbtk_audio_lib/src/mbtk_mp3.c
@@ -0,0 +1 @@
+
diff --git a/mbtk/mbtk_audio_lib/src/mbtk_pcm_stream.c b/mbtk/mbtk_audio_lib/src/mbtk_pcm_stream.c
new file mode 100755
index 0000000..54ae1be
--- /dev/null
+++ b/mbtk/mbtk_audio_lib/src/mbtk_pcm_stream.c
@@ -0,0 +1,526 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <errno.h>
+
+#include "mbtk_log.h"
+#include "mbtk_audio_internal.h"
+
+#define LOCK_FILE "/var/run/mbtk_audio.lock"
+#define AUDIO_DEBUG 0
+
+#define AUDIO_LOG(fmt, args ...) \
+ do{ \
+ if(AUDIO_DEBUG) {LOGD(fmt, ##args);} \
+ } while(0)
+
+extern void* audio_hal_install(void);
+extern void audio_hal_uninstall(void);
+extern void configure_vcm(unsigned int data[]);
+
+static audio_inter_info_t *audio_info = NULL;
+static pthread_t recorder_thread_play;
+
+static int lock_get() {
+ int fd = open(LOCK_FILE, O_WRONLY | O_CREAT | O_TRUNC);
+ if (fd == -1) {
+ LOGE("Open(%s) fail:%d", LOCK_FILE, errno);
+ return -1;
+ }
+ // 尝试对文件进行加锁
+ struct flock fl;
+ fl.l_type = F_WRLCK;
+ fl.l_whence = SEEK_SET;
+ fl.l_start = 0;
+ fl.l_len = 1;
+ int ret = fcntl(fd, F_SETLK, &fl);
+ if (ret == -1) {
+ LOGE("Another instance is running, exiting...");
+ close(fd);
+ return -1;
+ }
+ close(fd);
+ LOGD("Get file lock.");
+ return 0;
+}
+
+static int lock_free() {
+ int fd = open(LOCK_FILE, O_WRONLY | O_CREAT | O_TRUNC);
+ if (fd == -1) {
+ LOGE("Open(%s) fail:%d", LOCK_FILE, errno);
+ return -1;
+ }
+ // 释放文件锁
+ struct flock fl;
+ fl.l_type = F_UNLCK;
+ fl.l_whence = SEEK_SET;
+ fl.l_start = 0;
+ fl.l_len = 1;
+ int ret = fcntl(fd, F_SETLK, &fl);
+ if (ret == -1) {
+ LOGE("Another instance is running, exiting...");
+ close(fd);
+ return -1;
+ }
+
+ LOGD("Free file lock.");
+ return 0;
+}
+
+static void audio_recorder_thread(void *arg)
+{
+ int rc, len, frames = 0;
+ char buff[MBTK_PCM_WB_BUF_SIZE];
+
+ audio_info->info.recorder.state = AUDIO_RECORDER_STATE_RUNNING;
+ pthread_mutex_init(&audio_info->info.recorder.mutex, NULL);
+ pthread_cond_init(&audio_info->info.recorder.cond, NULL);
+
+ while (TRUE) {
+ /* Playback loop */
+ pthread_mutex_lock(&audio_info->info.recorder.mutex);
+ if(audio_info->info.recorder.state == AUDIO_RECORDER_STATE_STOP) {
+ LOGD("Stop recorder...");
+ pthread_mutex_unlock(&audio_info->info.recorder.mutex);
+ break;
+ } else if(audio_info->info.recorder.state == AUDIO_RECORDER_STATE_PAUSE) {
+ pthread_cond_wait(&audio_info->info.recorder.cond, &audio_info->info.recorder.mutex);
+ pthread_mutex_unlock(&audio_info->info.recorder.mutex);
+ continue;
+ } else {
+ pthread_mutex_unlock(&audio_info->info.recorder.mutex);
+ }
+
+ //record the needed format stream from the device.
+ //only read pcm stream, no send command.
+ len = audio_info->info.recorder.stream_in->read(audio_info->info.recorder.stream_in, buff,
+ audio_info->playback_size);
+ if (len <= 0) {
+ LOGE("%s: error reading!", __FUNCTION__);
+ goto thread_end;
+ }
+
+ AUDIO_LOG("Recorder data : len - %d", len);
+
+ if(audio_info->info.recorder.recorder_cb) {
+ audio_info->info.recorder.recorder_cb(buff, len);
+ }
+
+ LOGD("%s: No.%d frame playback.", __FUNCTION__, ++frames);
+ }
+
+
+thread_end:
+ pthread_mutex_destroy(&audio_info->info.recorder.mutex);
+ pthread_cond_destroy(&audio_info->info.recorder.cond);
+
+ audio_info->info.recorder.stream_in->common.standby(&audio_info->info.recorder.stream_in->common);
+ audio_info->audio_ahw_dev_ubus->close_input_stream(audio_info->audio_ahw_dev_ubus, audio_info->info.recorder.stream_in);
+ VCMDeinit();//close the fd of audiostub_ctl when exit the thread.
+ audio_info->info.recorder.stream_in = NULL;
+ LOGD("%s: finished pcm playback.", __FUNCTION__);
+
+ // Notify audio recorder data end.
+ if(audio_info->info.recorder.recorder_cb) {
+ audio_info->info.recorder.recorder_cb(NULL, 0);
+ }
+ return;
+}
+
+
+static int config_parameters(mbtk_audio_direction_enum direction, mbtk_audio_sample_rate_enum NBWB)
+{
+ unsigned int srcdst, priority, dest;
+ char kvpair[128];
+ struct str_parms *param = NULL;
+ int data[5];
+ const char *key = NULL;
+ bool update_vcm = false;
+
+ srcdst = 1;/* 0-None, 1-Near end, 2-Far end, 3-Both ends */
+ priority = 1;/* 0-Do not combine(override), 1-Combine */
+ dest = 1;/* 0-Near codec, 1-Near Vocoder */
+
+ if(direction == MBTK_AUDIO_DIRECTION_OUTPUT){//output
+ if(NBWB == MBTK_AUDIO_SAMPLE_RATE_8000)
+ audio_info->playback_size = MBTK_PCM_NB_BUF_SIZE;
+ else
+ audio_info->playback_size = MBTK_PCM_WB_BUF_SIZE;
+
+ LOGD("config playback parameters.");
+ }
+ else if(direction == MBTK_AUDIO_DIRECTION_INPUT){//input
+ if(NBWB == MBTK_AUDIO_SAMPLE_RATE_8000)
+ audio_info->playback_size = MBTK_PCM_NB_BUF_SIZE;
+ else
+ audio_info->playback_size = MBTK_PCM_WB_BUF_SIZE;
+
+ LOGD("config record parameters.");
+ }
+
+ memset(kvpair, 0x00, sizeof(kvpair));
+ sprintf(kvpair, "%s=%d;%s=%d;%s=%d;%s=%d;%s=%d", VCM_CONFIG_DIRECTION, direction,
+ VCM_CONFIG_TYPE, NBWB, VCM_CONFIG_SRC_DST, srcdst,
+ VCM_CONFIG_PRIORITY, priority, VCM_CONFIG_DEST, dest);
+
+ LOGD("%s: config information kvpair is %s.\n", __FUNCTION__, kvpair);
+
+ //extract the parameter and config from string
+ param = str_parms_create_str(kvpair);
+ if (!param) {
+ LOGE("%s: param create str is null!", __FUNCTION__);
+ return -1;
+ }
+
+ //set vcm configurations
+ key = VCM_CONFIG_DIRECTION;
+ if (str_parms_get_int(param, key, &data[0]) == 0) {
+ update_vcm = true;
+ str_parms_del(param, key);
+ }
+ key = VCM_CONFIG_TYPE;
+ if (str_parms_get_int(param, key, &data[1]) == 0) {
+ update_vcm = true;
+ str_parms_del(param, key);
+ }
+ key = VCM_CONFIG_SRC_DST;
+ if (str_parms_get_int(param, key, &data[2]) == 0) {
+ update_vcm = true;
+ str_parms_del(param, key);
+ }
+ key = VCM_CONFIG_PRIORITY;
+ if (str_parms_get_int(param, key, &data[3]) == 0) {
+ update_vcm = true;
+ str_parms_del(param, key);
+ }
+ key = VCM_CONFIG_DEST;
+ if (str_parms_get_int(param, key, &data[4]) == 0) {
+ update_vcm = true;
+ str_parms_del(param, key);
+ }
+
+ //printf("Direction is %d, Type is %d, Src_Dst is %d, Priority is %d, Dest is %d. \n",data[0], data[1], data[2], data[3], data[4]);
+
+ if (update_vcm) {
+ configure_vcm(data); /*TODO check if all inputs got all values successfully*/
+ }
+
+ return 0;
+}
+
+int mbtk_audio_pcm_init()
+{
+ //mbtk_log_init("radio", "MBTK_AUDIO");
+ // Audio is running...
+ if(lock_get()) {
+ return -1;
+ }
+
+ if(audio_info) {
+ return 0;
+ }
+
+ audio_info = (audio_inter_info_t*)malloc(sizeof(audio_inter_info_t));
+ if(audio_info == NULL) {
+ LOGE("malloc() fail:%d", errno);
+ return -1;
+ }
+ memset(audio_info, 0x00, sizeof(audio_inter_info_t));
+
+ // Set default audio parameter.
+ audio_info->channel = 1;
+ audio_info->sample_rate = MBTK_AUDIO_SAMPLE_RATE_8000;
+ audio_info->playback_size = MBTK_PCM_NB_BUF_SIZE;
+
+ audio_info->audio_ahw_dev_ubus = audio_hal_install();
+ if (audio_info->audio_ahw_dev_ubus == NULL) {
+ LOGE("audio_hal_install() failed!");
+ goto init_fail;
+ }
+ return 0;
+init_fail:
+ free(audio_info);
+ audio_info = NULL;
+ return -1;
+}
+
+int mbtk_audio_pcm_sample_rate_set(mbtk_audio_sample_rate_enum sample_rate)
+{
+ if(!audio_info) {
+ LOGE("Not inited.");
+ return -1;
+ }
+
+ audio_info->sample_rate = sample_rate;
+
+ return 0;
+}
+
+int mbtk_audio_pcm_play_start()
+{
+ if(!audio_info) {
+ LOGE("Not inited.");
+ return -1;
+ }
+
+ if(!audio_info->audio_ahw_dev_ubus) {
+ LOGE("audio_info->audio_ahw_dev_ubus is NULL.");
+ return -1;
+ }
+
+ config_parameters(MBTK_AUDIO_DIRECTION_OUTPUT, audio_info->sample_rate);
+
+ int rc = audio_info->audio_ahw_dev_ubus->open_output_stream(audio_info->audio_ahw_dev_ubus, 0,
+ audio_info->audio_ahw_dev_ubus->get_supported_devices(audio_info->audio_ahw_dev_ubus),
+ AUDIO_OUTPUT_FLAG_DIRECT, NULL, &(audio_info->info.play.stream_out), 0);
+ if (rc < 0) {
+ LOGE("Open output device fail:%d", rc);
+ goto play_start_fail;
+ }
+
+ audio_info->direction = MBTK_AUDIO_DIRECTION_OUTPUT;
+ audio_info->info.play.buff_remain_len = 0;
+
+ return 0;
+play_start_fail:
+
+ return -1;
+}
+
+int mbtk_audio_pcm_play_data_send(const void* data,uint32 data_len)
+{
+ UNUSED(data);
+ UNUSED(data_len);
+
+ if(!audio_info) {
+ LOGE("Not inited.");
+ return -1;
+ }
+
+ if(!audio_info->info.play.stream_out) {
+ LOGE("Output device not open.");
+ return -1;
+ }
+
+ uint32 index = 0;
+ // There are remaining data from the previous package。
+ if(audio_info->info.play.buff_remain_len > 0) {
+ // Too less for one package.
+ if(data_len + audio_info->info.play.buff_remain_len < audio_info->playback_size) {
+ AUDIO_LOG("Save remain data : len - %d", data_len);
+ memcpy(audio_info->info.play.buff_remain + audio_info->info.play.buff_remain_len, data, data_len);
+ audio_info->info.play.buff_remain_len += data_len;
+ return data_len;
+ } else {
+ AUDIO_LOG("Write remain data : %d + %d", audio_info->info.play.buff_remain_len, audio_info->playback_size - audio_info->info.play.buff_remain_len);
+ memcpy(audio_info->info.play.buff_remain + audio_info->info.play.buff_remain_len, data, audio_info->playback_size - audio_info->info.play.buff_remain_len);
+
+ int rc = audio_info->info.play.stream_out->write(audio_info->info.play.stream_out, audio_info->info.play.buff_remain, audio_info->playback_size);
+ if (rc < 0) {
+ LOGE("%s: error writing (child).", __FUNCTION__);
+ goto send_fail;
+ } else if (rc < (signed int)audio_info->playback_size) {
+ LOGW("%s: wrote less than buffer size, rc=%d.", __FUNCTION__, rc);
+ index += rc;
+ goto send_fail;
+ }
+
+ index += (audio_info->playback_size - audio_info->info.play.buff_remain_len);
+ audio_info->info.play.buff_remain_len = 0;
+ }
+ }
+
+ while(data_len - index >= audio_info->playback_size) {
+ AUDIO_LOG("Package : %d -> %d", index, index + audio_info->playback_size - 1);
+ int rc = audio_info->info.play.stream_out->write(audio_info->info.play.stream_out, (const char*)data + index, audio_info->playback_size);
+ if (rc < 0) {
+ LOGE("%s: error writing (child).", __FUNCTION__);
+ goto send_fail;
+ } else if (rc < (signed int)audio_info->playback_size) {
+ LOGW("%s: wrote less than buffer size, rc=%d.", __FUNCTION__, rc);
+ goto send_fail;
+ }
+
+ index += rc;
+ }
+
+ // Last package.( less then audio_info->playback_size)
+ // Save to buffer audio_info->play_buff_remain
+ if(data_len - index > 0) {
+ AUDIO_LOG("Save remain data : len - %d", data_len - index);
+ memcpy(audio_info->info.play.buff_remain, data + index, data_len - index);
+ audio_info->info.play.buff_remain_len = data_len - index;
+ }
+
+ return data_len;
+send_fail:
+ return -1;
+}
+
+int mbtk_audio_pcm_play_stop()
+{
+ if(!audio_info) {
+ LOGE("Not inited.");
+ return -1;
+ }
+
+ if(!audio_info->info.play.stream_out) {
+ LOGE("Output device not open.");
+ return -1;
+ }
+
+ // Write last package.
+ if(audio_info->info.play.buff_remain_len > 0) {
+ char buf[MBTK_PCM_WB_BUF_SIZE];
+ memset(buf, 0x00, sizeof(buf));
+ memcpy(buf, audio_info->info.play.buff_remain, audio_info->info.play.buff_remain_len);
+
+ LOGD("len %d is smaller than needed %d, so fill the buffer with 0.", audio_info->info.play.buff_remain_len, audio_info->playback_size);
+
+ int rc = audio_info->info.play.stream_out->write(audio_info->info.play.stream_out, buf, audio_info->playback_size);
+ if (rc < 0) {
+ LOGE("%s: error writing (child).", __FUNCTION__);
+ //goto send_fail;
+ } else if (rc < (signed int)audio_info->playback_size) {
+ LOGW("%s: wrote less than buffer size, rc=%d.", __FUNCTION__, rc);
+ //goto send_fail;
+ }
+ audio_info->info.play.buff_remain_len = 0;
+ }
+
+ vcm_playback_drain(0);//wait for drain the AP audiostub queue.
+ usleep(80000);//delay 80ms until DSP play out its buffered data.
+ audio_info->info.play.stream_out->common.standby(&(audio_info->info.play.stream_out->common));
+ audio_info->audio_ahw_dev_ubus->close_output_stream(audio_info->audio_ahw_dev_ubus, audio_info->info.play.stream_out);
+
+ audio_info->info.play.stream_out = NULL;
+ return 0;
+}
+
+
+int mbtk_audio_pcm_recorder_start(mbtk_recorder_callback_func recorder_cb)
+{
+ if(!audio_info) {
+ LOGE("Not inited.");
+ return -1;
+ }
+
+ if(!audio_info->audio_ahw_dev_ubus) {
+ LOGE("audio_info->audio_ahw_dev_ubus is NULL.");
+ return -1;
+ }
+
+ if(audio_info->info.recorder.state != AUDIO_RECORDER_STATE_STOP) {
+ LOGW("Audio is recorder...");
+ return -1;
+ }
+
+ config_parameters(MBTK_AUDIO_DIRECTION_INPUT, audio_info->sample_rate);
+
+ VCMInit();
+
+ int rc = audio_info->audio_ahw_dev_ubus->open_input_stream(audio_info->audio_ahw_dev_ubus, 0,
+ audio_info->audio_ahw_dev_ubus->get_supported_devices(audio_info->audio_ahw_dev_ubus),
+ NULL, &(audio_info->info.recorder.stream_in), 0, 0, AUDIO_SOURCE_VOICE_CALL);
+ if (rc < 0) {
+ LOGE("Open input device fail:%d", rc);
+ goto recorder_start_fail;
+ }
+
+ audio_info->direction = MBTK_AUDIO_DIRECTION_INPUT;
+ audio_info->info.recorder.recorder_cb = recorder_cb;
+
+ pthread_attr_t thread_attr;
+ pthread_attr_init(&thread_attr);
+ if(pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED))
+ {
+ LOGE("pthread_attr_setdetachstate() fail.");
+ return -1;
+ }
+
+ if (pthread_create(&recorder_thread_play, NULL, (void *)&audio_recorder_thread, NULL) < 0) {
+ LOGE("%s: error creating thread_recorder!", __FUNCTION__);
+ return -1;
+ }
+
+ return 0;
+recorder_start_fail:
+
+ return -1;
+}
+
+int mbtk_audio_pcm_recorder_pause()
+{
+ int result = 0;
+ pthread_mutex_lock(&audio_info->info.recorder.mutex);
+ if(audio_info->info.recorder.state == AUDIO_RECORDER_STATE_RUNNING) {
+ audio_info->info.recorder.state = AUDIO_RECORDER_STATE_PAUSE;
+ } else {
+ result = -1;
+ LOGW("Audio state : %d", audio_info->info.recorder.state);
+ }
+ pthread_mutex_unlock(&audio_info->info.recorder.mutex);
+ return result;
+}
+
+int mbtk_audio_pcm_recorder_resume()
+{
+ int result = 0;
+ pthread_mutex_lock(&audio_info->info.recorder.mutex);
+ if(audio_info->info.recorder.state == AUDIO_RECORDER_STATE_PAUSE) {
+ audio_info->info.recorder.state = AUDIO_RECORDER_STATE_RUNNING;
+ pthread_cond_signal(&audio_info->info.recorder.cond);
+ } else {
+ result = -1;
+ LOGW("Audio state : %d", audio_info->info.recorder.state);
+ }
+ pthread_mutex_unlock(&audio_info->info.recorder.mutex);
+ return result;
+}
+
+
+int mbtk_audio_pcm_recorder_stop()
+{
+ int result = 0;
+ pthread_mutex_lock(&audio_info->info.recorder.mutex);
+ if(audio_info->info.recorder.state == AUDIO_RECORDER_STATE_PAUSE || audio_info->info.recorder.state == AUDIO_RECORDER_STATE_RUNNING) {
+ if(audio_info->info.recorder.state == AUDIO_RECORDER_STATE_PAUSE) {
+ pthread_cond_signal(&audio_info->info.recorder.cond);
+ }
+ audio_info->info.recorder.state = AUDIO_RECORDER_STATE_STOP;
+ pthread_mutex_unlock(&audio_info->info.recorder.mutex);
+
+ LOGD("Waitting recorder thread exit...");
+ if (pthread_join(recorder_thread_play, NULL)) {
+ LOGE("error join thread_recorder!");
+ // abort();
+ }
+ LOGD("Recorder thread exit success.");
+ } else {
+ pthread_mutex_unlock(&audio_info->info.recorder.mutex);
+ result = -1;
+ LOGW("Audio state : %d", audio_info->info.recorder.state);
+ }
+
+ return result;
+}
+
+int mbtk_audio_pcm_deinit()
+{
+ if(!audio_info) {
+ LOGE("Not inited.");
+ return -1;
+ }
+
+ audio_hal_uninstall();
+
+ if(lock_free()) {
+ return -1;
+ }
+
+ free(audio_info);
+ audio_info = NULL;
+ return 0;
+}
+
diff --git a/mbtk/mbtk_audio_lib/src/mbtk_wav.c b/mbtk/mbtk_audio_lib/src/mbtk_wav.c
new file mode 100755
index 0000000..dca245c
--- /dev/null
+++ b/mbtk/mbtk_audio_lib/src/mbtk_wav.c
@@ -0,0 +1,333 @@
+#include <sys/stat.h>
+#include <unistd.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <errno.h>
+
+#include "mbtk_log.h"
+#include "mbtk_audio_internal.h"
+
+#define WAV_PLAY_BUFF 2048
+
+static int wav_play_fd = -1;
+static audio_play_state_enum play_state = AUDIO_PLAY_STATE_STOP;
+static pthread_cond_t play_cond;
+static pthread_mutex_t play_mutex;
+static pthread_t play_thread_play;
+
+static int wav_recorder_fd = -1;
+static struct wav_header recorder_header;
+static uint32 recorver_data_count = 0;
+
+static void audio_play_thread(void *arg)
+{
+ int rc, len, frames = 0;
+ char buf[WAV_PLAY_BUFF];
+
+ play_state = AUDIO_PLAY_STATE_RUNNING;
+ pthread_mutex_init(&play_mutex, NULL);
+ pthread_cond_init(&play_cond, NULL);
+
+ while (TRUE) {
+ /* Playback loop */
+ pthread_mutex_lock(&play_mutex);
+ if(play_state == AUDIO_PLAY_STATE_STOP) {
+ LOGD("Stop play...");
+ pthread_mutex_unlock(&play_mutex);
+ break;
+ } else if(play_state == AUDIO_PLAY_STATE_PAUSE) {
+ pthread_cond_wait(&play_cond, &play_mutex);
+ pthread_mutex_unlock(&play_mutex);
+ continue;
+ } else {
+ pthread_mutex_unlock(&play_mutex);
+ }
+
+ memset(buf, 0x00, sizeof(buf));
+ len = read(wav_play_fd, buf, WAV_PLAY_BUFF);
+ if (len == -1) {
+ LOGE("%s: error reading from file", __FUNCTION__);
+ goto thread_end;
+ }
+
+ if (len == 0) {
+ /* reached EOF */
+ LOGE("%s: Read wav file end.", __FUNCTION__);
+ break;
+ }
+
+ if((rc = mbtk_audio_pcm_play_data_send(buf, len)) < len) {
+ LOGE("Send data %d/%d", rc, len);
+ goto thread_end;
+ }
+
+ LOGD("%s: No.%d frame playback", __FUNCTION__, ++frames);
+ }
+
+ play_state = AUDIO_PLAY_STATE_STOP;
+
+thread_end:
+ pthread_mutex_destroy(&play_mutex);
+ pthread_cond_destroy(&play_cond);
+ mbtk_audio_pcm_play_stop();
+
+ if (close(wav_play_fd))
+ LOGE("%s: error closing file", __FUNCTION__);
+
+ wav_play_fd = -1;
+ LOGD("%s: finished pcm playback.", __FUNCTION__);
+ return;
+}
+
+static void audio_recorder_cb(void *data, uint32 data_len)
+{
+ if(data_len > 0) {
+ LOGD("Recorver data:%d, count:%d", data_len, recorver_data_count);
+ if (write(wav_recorder_fd, data, data_len) < data_len) {
+ LOGE("%s: error writing to file!", __FUNCTION__);
+ }
+ recorver_data_count += data_len;
+ } else {
+ LOGD("Recorver data end.");
+ recorder_header.data_sz = recorver_data_count;
+ recorder_header.riff_sz = recorder_header.data_sz + sizeof(recorder_header) - 8;
+ lseek(wav_recorder_fd, 0, SEEK_SET);
+ write(wav_recorder_fd, &recorder_header, sizeof(struct wav_header));
+
+ close(wav_recorder_fd);
+ wav_recorder_fd = -1;
+ }
+}
+
+int mbtk_audio_wav_init()
+{
+ //mbtk_log_init("radio", "MBTK_AUDIO");
+ return mbtk_audio_pcm_init();
+}
+
+int mbtk_audio_wav_play_start(const void *wav_file)
+{
+ struct stat st;
+ const char *path = (const char *)wav_file;
+ struct riff_wave_header riff_wave_header;
+ struct chunk_header chunk_header;
+ struct chunk_fmt chunk_fmt = {0};
+ unsigned int more_chunks = 1;
+
+ if(play_state != AUDIO_PLAY_STATE_STOP) {
+ LOGW("Audio is playing...");
+ return -1;
+ }
+
+ /* Check and open source file */
+ if (access(path, F_OK) || stat(path, &st)) {
+ LOGE("%s: error reading from file %s", __FUNCTION__, path);
+ return -1;
+ }
+
+ if (!st.st_size) {
+ LOGE("%s: empty file %s", __FUNCTION__, path);
+ return -1;
+ }
+
+ wav_play_fd = open(path, O_RDONLY);
+ if (wav_play_fd < 0) {
+ LOGE("%s: error opening file %s", __FUNCTION__, path);
+ return -1;
+ }
+
+ read(wav_play_fd, &riff_wave_header, sizeof(riff_wave_header));
+ if ((riff_wave_header.riff_id != ID_RIFF) || (riff_wave_header.wave_id != ID_WAVE)) {
+ LOGE("Error: '%s' is not a riff/wave file", path);
+ close(wav_play_fd);
+ return -1;
+ }
+
+ do {
+ read(wav_play_fd, &chunk_header, sizeof(chunk_header));
+
+ switch (chunk_header.id) {
+ case ID_FMT:
+ read(wav_play_fd, &chunk_fmt, sizeof(chunk_fmt));
+ /* If the format header is larger, skip the rest */
+ if (chunk_header.sz > sizeof(chunk_fmt))
+ lseek(wav_play_fd, chunk_header.sz - sizeof(chunk_fmt), SEEK_CUR);
+ break;
+ case ID_DATA:
+ /* Stop looking for chunks */
+ more_chunks = 0;
+ break;
+ default:
+ /* Unknown chunk, skip bytes */
+ lseek(wav_play_fd, chunk_header.sz, SEEK_CUR);
+ }
+ } while (more_chunks);
+
+ //Support 8k/16k & mono wave file
+ if (((chunk_fmt.sample_rate != 8000) && (chunk_fmt.sample_rate != 16000))
+ || (chunk_fmt.num_channels != 1) ) {
+ LOGD("%s: error wave file:sample_rate = %d, num_channels = %d!!",
+ __FUNCTION__,chunk_fmt.sample_rate, chunk_fmt.num_channels);
+ close(wav_play_fd);
+ return -1;
+ }
+
+ LOGE("%s: success open wave file:%s, sample_rate = %d, num_channels = %d.",
+ __FUNCTION__, path, chunk_fmt.sample_rate, chunk_fmt.num_channels);
+
+ if ((8000 == chunk_fmt.sample_rate) && (1 == chunk_fmt.num_channels)) {
+ mbtk_audio_pcm_sample_rate_set(MBTK_AUDIO_SAMPLE_RATE_8000);
+ } else if ((16000 == chunk_fmt.sample_rate) && (1 == chunk_fmt.num_channels)) {
+ mbtk_audio_pcm_sample_rate_set(MBTK_AUDIO_SAMPLE_RATE_16000);
+ }
+
+ if (mbtk_audio_pcm_play_start()) {
+ LOGE("%s: error opening output device.", __FUNCTION__);
+ return -1;
+ }
+
+ LOGD("Start play wav file...");
+
+ pthread_attr_t thread_attr;
+ pthread_attr_init(&thread_attr);
+ if(pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED))
+ {
+ LOGE("pthread_attr_setdetachstate() fail.");
+ return -1;
+ }
+
+ if (pthread_create(&play_thread_play, NULL, (void *)&audio_play_thread, NULL) < 0) {
+ LOGE("%s: error creating thread_play!", __FUNCTION__);
+ return -1;
+ }
+
+ return 0;
+}
+
+int mbtk_audio_wav_play_pause()
+{
+ int result = 0;
+ pthread_mutex_lock(&play_mutex);
+ if(play_state == AUDIO_PLAY_STATE_RUNNING) {
+ play_state = AUDIO_PLAY_STATE_PAUSE;
+ } else {
+ result = -1;
+ LOGW("Audio state : %d", play_state);
+ }
+ pthread_mutex_unlock(&play_mutex);
+ return result;
+}
+
+int mbtk_audio_wav_play_resume()
+{
+ int result = 0;
+ pthread_mutex_lock(&play_mutex);
+ if(play_state == AUDIO_PLAY_STATE_PAUSE) {
+ play_state = AUDIO_PLAY_STATE_RUNNING;
+ pthread_cond_signal(&play_cond);
+ } else {
+ result = -1;
+ LOGW("Audio state : %d", play_state);
+ }
+ pthread_mutex_unlock(&play_mutex);
+ return result;
+}
+
+
+int mbtk_audio_wav_play_stop()
+{
+ int result = 0;
+ pthread_mutex_lock(&play_mutex);
+ if(play_state == AUDIO_PLAY_STATE_PAUSE || play_state == AUDIO_PLAY_STATE_RUNNING) {
+ if(play_state == AUDIO_PLAY_STATE_PAUSE) {
+ pthread_cond_signal(&play_cond);
+ }
+ play_state = AUDIO_PLAY_STATE_STOP;
+ pthread_mutex_unlock(&play_mutex);
+
+ LOGD("Waitting play thread exit...");
+ if (pthread_join(play_thread_play, NULL)) {
+ LOGE("error join thread_play!");
+ // abort();
+ }
+ LOGD("Play thread exit success.");
+ } else {
+ pthread_mutex_unlock(&play_mutex);
+ result = -1;
+ LOGW("Audio state : %d", play_state);
+ }
+
+ return result;
+}
+
+int mbtk_audio_wav_recorder_start(const void *wav_file, mbtk_audio_sample_rate_enum sample_rate)
+{
+ int rc;
+ const char *path = (const char *)wav_file;
+
+ LOGD("wav_file is %s.", path);
+ if(wav_recorder_fd > 0) {
+ LOGW("Audio is recorder...");
+ }
+
+ memset(&recorder_header, 0x0, sizeof(struct wav_header));
+ recorder_header.riff_id = ID_RIFF;
+ recorder_header.riff_sz = 0;
+ recorder_header.riff_fmt = ID_WAVE;
+ recorder_header.fmt_id = ID_FMT;
+ recorder_header.fmt_sz = 16;
+ recorder_header.audio_format = 1; //FORMAT_PCM;
+ recorder_header.num_channels = 1; //Modem ONLY support mono recording
+ recorder_header.sample_rate = (sample_rate == MBTK_AUDIO_SAMPLE_RATE_8000 ? 8000 : 16000);
+ recorder_header.bits_per_sample = 16; //PCM_SAMPLEBITS_S16_LE;
+ recorder_header.byte_rate = (recorder_header.bits_per_sample / 8) * recorder_header.num_channels * recorder_header.sample_rate;
+ recorder_header.block_align = recorder_header.num_channels * (recorder_header.bits_per_sample / 8);
+ recorder_header.data_id = ID_DATA;
+
+ mbtk_audio_pcm_sample_rate_set(sample_rate);
+
+ wav_recorder_fd = open(path, O_WRONLY | O_CREAT | O_TRUNC);
+ if (wav_recorder_fd < 0) {
+ LOGE("%s: error opening file %s!", __FUNCTION__, path);
+ return -1;
+ }
+
+ //leave enough room for header
+ lseek(wav_recorder_fd, sizeof(struct wav_header), SEEK_SET);
+
+ recorver_data_count = 0;
+
+ return mbtk_audio_pcm_recorder_start(audio_recorder_cb);
+}
+
+int mbtk_audio_wav_recorder_pause()
+{
+ return mbtk_audio_pcm_recorder_pause();
+}
+
+int mbtk_audio_wav_recorder_resume()
+{
+ return mbtk_audio_pcm_recorder_resume();
+}
+
+
+int mbtk_audio_wav_recorder_stop()
+{
+ return mbtk_audio_pcm_recorder_stop();
+}
+
+int mbtk_audio_wav_deinit()
+{
+ if(play_state != AUDIO_PLAY_STATE_STOP) {
+ if(mbtk_audio_wav_play_stop()) {
+ LOGE("mbtk_audio_wav_play_stop() fail.");
+ }
+ }
+
+ if(wav_recorder_fd > 0) {
+ mbtk_audio_wav_recorder_stop();
+ }
+
+ return mbtk_audio_pcm_deinit();
+}
+