Add asr1806 Audio API.
Change-Id: I0cefc67d0825e2bf4476fd0a07cb0d1fc5971457
diff --git a/build.sh b/build.sh
index 4cd3a7b..2691cb2 100755
--- a/build.sh
+++ b/build.sh
@@ -17,6 +17,8 @@
export BUILD_PLATFORM=asr1803
fi
+export STAGING_DIR=
+
ROOT_DIR=`pwd`
#TOOLCHAIN_DIR=$ROOT_DIR/toolchain/$BUILD_PLATFORM
#echo "toolchain : $ROOT_DIR"
@@ -86,7 +88,8 @@
file_copy
else
if [ ! -d out/bin ];then
- mkdir -p out/bin
+ mkdir -p out/bin/asr1803
+ mkdir -p out/bin/asr1806
fi
if [ ! -d out/lib ];then
diff --git a/mbtk/Make.defines b/mbtk/Make.defines
index 9c17fde..ce5ab9f 100755
--- a/mbtk/Make.defines
+++ b/mbtk/Make.defines
@@ -29,7 +29,8 @@
$(info CC=$(CC))
$(info AR=$(AR))
-CFLAGS= --sysroot=$(BUILD_TOOLCHAIN_DIR) -Os -pipe -march=armv7-a -mtune=cortex-a7 -mfpu=neon-vfpv4 -fno-caller-saves -fhonour-copts -Wno-error=unused-but-set-variable -mfloat-abi=hard -fno-exceptions -Wno-multichar -ffunction-sections -fdata-sections -funwind-tables -Wa,--noexecstack -fno-short-enums -no-canonical-prefixes -fno-canonical-system-headers -mfpu=neon -fno-builtin-sin -fno-strict-volatile-bitfields -Wno-psabi -mthumb-interwork -fmessage-length=0 -W -Wall -Wno-unused -Winit-self -Wpointer-arith -g -Wstrict-aliasing=2 -fgcse-after-reload -frerun-cse-after-loop -frename-registers -mthumb -fomit-frame-pointer -fno-strict-aliasing -Werror=format-security -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -ffunction-sections -Wl,--gc-sections -Wl,--no-undefined
+# -Werror=non-virtual-dtor
+CFLAGS= --sysroot=$(BUILD_TOOLCHAIN_DIR) -Os -pipe -march=armv7-a -mtune=cortex-a7 -mfpu=neon-vfpv4 -fno-caller-saves -fhonour-copts -Wno-error=unused-but-set-variable -mfloat-abi=hard -fno-exceptions -Wno-multichar -ffunction-sections -fdata-sections -funwind-tables -Wa,--noexecstack -fno-short-enums -no-canonical-prefixes -fno-canonical-system-headers -mfpu=neon -fno-builtin-sin -fno-strict-volatile-bitfields -Wno-psabi -mthumb-interwork -fmessage-length=0 -W -Wall -Wno-unused -Winit-self -Wpointer-arith -g -Wstrict-aliasing=2 -fgcse-after-reload -frerun-cse-after-loop -frename-registers -mthumb -fomit-frame-pointer -fno-strict-aliasing -Werror=format-security -Werror=return-type -Werror=address -Werror=sequence-point -ffunction-sections -Wl,--gc-sections -Wl,--no-undefined
AWK=awk
diff --git a/mbtk/include/mbtk/mbtk_audio2.h b/mbtk/include/mbtk/mbtk_audio2.h
new file mode 100755
index 0000000..adba822
--- /dev/null
+++ b/mbtk/include/mbtk/mbtk_audio2.h
@@ -0,0 +1,62 @@
+/*
+* mbtk_audio2.h
+*
+* MBTK audio API for asr1806.
+*
+* Author : lb
+* Date : 2023/12/20 10:12:15
+*/
+#ifndef _MBTK_AUDIO2_H
+#define _MBTK_AUDIO2_H
+#include "mbtk_type.h"
+
+#define MBTK_PCM_NB_BUF_SIZE 320
+#define MBTK_PCM_WB_BUF_SIZE 640
+
+typedef void (*mbtk_recorder_callback_func)(void *data, uint32 data_len);
+
+typedef enum {
+ MBTK_AUDIO_DIRECTION_OUTPUT = 0, // Play
+ MBTK_AUDIO_DIRECTION_INPUT // Recorder
+} mbtk_audio_direction_enum;
+
+typedef enum {
+ MBTK_AUDIO_SAMPLE_RATE_8000 = 0,
+ MBTK_AUDIO_SAMPLE_RATE_16000
+} mbtk_audio_sample_rate_enum;
+
+int mbtk_audio_pcm_init();
+
+int mbtk_audio_pcm_sample_rate_set(mbtk_audio_sample_rate_enum sample_rate);
+
+int mbtk_audio_pcm_play_start();
+
+int mbtk_audio_pcm_play_data_send(const void* data,uint32 data_len);
+
+int mbtk_audio_pcm_play_stop();
+
+int mbtk_audio_pcm_recorder_start(mbtk_recorder_callback_func recorder_cb);
+
+int mbtk_audio_pcm_deinit();
+
+int mbtk_audio_wav_init();
+
+int mbtk_audio_wav_play_start(const void *wav_file);
+
+int mbtk_audio_wav_play_pause();
+
+int mbtk_audio_wav_play_resume();
+
+int mbtk_audio_wav_play_stop();
+
+int mbtk_audio_wav_recorder_start(const void *wav_file, mbtk_audio_sample_rate_enum sample_rate);
+
+int mbtk_audio_wav_recorder_pause();
+
+int mbtk_audio_wav_recorder_resume();
+
+int mbtk_audio_wav_recorder_stop();
+
+int mbtk_audio_wav_deinit();
+
+#endif /* _MBTK_AUDIO2_H */
diff --git a/mbtk/mbtk_audio_lib/Makefile b/mbtk/mbtk_audio_lib/Makefile
index 683f271..01d734d 100755
--- a/mbtk/mbtk_audio_lib/Makefile
+++ b/mbtk/mbtk_audio_lib/Makefile
@@ -4,10 +4,10 @@
LOCAL_PATH=$(BUILD_ROOT)/mbtk_audio_lib
INC_DIR += -I$(BUILD_ROOT)/mbtk_lib/inc
-
+
LIB_DIR +=
-LIBS += -llog -lubus -lubox -lblobmsg_json -lrilutil -lmbtk_lib
+LIBS += -llog -lubus -lubox -lblobmsg_json -lrilutil -lmbtk_lib -lcutils -laudio-apu
CFLAGS += -shared -Wl,-shared,-Bsymbolic
@@ -42,7 +42,7 @@
%.o:%.c
$(CC) $(CFLAGS) $(INC_DIR) $(DEFINE) -c $< -o $@
-
+
%.o:%.cpp
$(CC) $(CFLAGS) $(INC_DIR) $(DEFINE) -c $< -o $@
diff --git a/mbtk/mbtk_audio_lib/inc/mbtk_audio_internal.h b/mbtk/mbtk_audio_lib/inc/mbtk_audio_internal.h
new file mode 100755
index 0000000..b4b3dc2
--- /dev/null
+++ b/mbtk/mbtk_audio_lib/inc/mbtk_audio_internal.h
@@ -0,0 +1,55 @@
+/*
+* mbtk_audio_internal.h
+*
+* MBTK Audio internal header.
+*
+* Author : lb
+* Date : 2023/12/20 10:13:09
+*/
+#ifndef _MBTK_AUDIO_INTERNAL_H
+#define _MBTK_AUDIO_INTERNAL_H
+#include "mbtk_audio2.h"
+#include "audio_if_api.h"
+#include "audio_if_audio_hw_mrvl.h"
+
+typedef enum {
+ AUDIO_PLAY_STATE_STOP,
+ AUDIO_PLAY_STATE_RUNNING,
+ AUDIO_PLAY_STATE_PAUSE
+} audio_play_state_enum;
+
+typedef enum {
+ AUDIO_RECORDER_STATE_STOP,
+ AUDIO_RECORDER_STATE_RUNNING,
+ AUDIO_RECORDER_STATE_PAUSE
+} audio_recorder_state_enum;
+
+typedef struct {
+ struct audio_stream_out *stream_out;
+ char buff_remain[MBTK_PCM_WB_BUF_SIZE];
+ int buff_remain_len;
+} audio_play_info_t;
+
+typedef struct {
+ struct audio_stream_in *stream_in;
+ audio_recorder_state_enum state;
+ pthread_cond_t cond;
+ pthread_mutex_t mutex;
+
+ mbtk_recorder_callback_func recorder_cb;
+} audio_recorder_info_t;
+
+typedef struct {
+ mbtk_audio_sample_rate_enum sample_rate;
+ int channel;
+ unsigned int playback_size;
+ audio_hw_device_t *audio_ahw_dev_ubus;
+
+ mbtk_audio_direction_enum direction;
+ union {
+ audio_play_info_t play;
+ audio_recorder_info_t recorder;
+ }info;
+} audio_inter_info_t;
+
+#endif /* _MBTK_AUDIO_INTERNAL_H */
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();
+}
+
diff --git a/mbtk/mbtk_lib/src/mbtk_audio.c b/mbtk/mbtk_lib/src/mbtk_audio.c
index f026769..f22120b 100755
--- a/mbtk/mbtk_lib/src/mbtk_audio.c
+++ b/mbtk/mbtk_lib/src/mbtk_audio.c
@@ -322,36 +322,25 @@
void mbtk_audio_mode_set(int mode)
{
int rc = 0;
- struct ubus_request *req = NULL;
if(NULL == mbtk_audio_ubus_db)
{
printf("mbtk_dtmf_ubus not init!\n");
return;
}
- req = (struct ubus_request *)malloc(sizeof(struct ubus_request));
- if (req == NULL)
- {
- printf("leave %s: lack of memory\n", __FUNCTION__);
- return;
- }
- memset(req, 0, sizeof(struct ubus_request));
blob_buf_init(&audio_cm_b, 0);
blobmsg_add_u32(&audio_cm_b, "param0", mode);
- if ((rc = ubus_invoke_async(mbtk_audio_ubus_db->ctx,
+ if ((rc = ubus_invoke(mbtk_audio_ubus_db->ctx,
mbtk_audio_ubus_db->audioif_request_id,
AUDIO_UBUS_MODE_SET,
- audio_cm_b.head, req)) != UBUS_STATUS_OK)
+ audio_cm_b.head, NULL, NULL, 0)) != UBUS_STATUS_OK)
{
- free(req);
printf("%s, ubus_invoke_async %s failed %s\n", __FUNCTION__, AUDIO_UBUS_MODE_SET, ubus_strerror(rc));
}
else
{
printf("%s: ubus_invoke_async success\n", __FUNCTION__);
mbtk_audio_ubus_db->work_state++;
- req->complete_cb = mbtk_ubus_complete_cb;
- ubus_complete_request_async(mbtk_audio_ubus_db->ctx, req);
}
}
@@ -359,36 +348,26 @@
void mbtk_audio_path_enable(int mode)
{
int rc = 0;
- struct ubus_request *req = NULL;
if(NULL == mbtk_audio_ubus_db)
{
printf("mbtk_dtmf_ubus not init!\n");
return;
}
- req = (struct ubus_request *)malloc(sizeof(struct ubus_request));
- if (req == NULL)
- {
- printf("leave %s: lack of memory\n", __FUNCTION__);
- return;
- }
- memset(req, 0, sizeof(struct ubus_request));
+
blob_buf_init(&audio_cm_b, 0);
blobmsg_add_u32(&audio_cm_b, "param0", mode);
- if ((rc = ubus_invoke_async(mbtk_audio_ubus_db->ctx,
+ if ((rc = ubus_invoke(mbtk_audio_ubus_db->ctx,
mbtk_audio_ubus_db->audioif_request_id,
AUDIO_UBUS_PATH_ENABLE,
- audio_cm_b.head, req)) != UBUS_STATUS_OK)
+ audio_cm_b.head, NULL, NULL, 0)) != UBUS_STATUS_OK)
{
- free(req);
printf("%s, ubus_invoke_async %s failed %s\n", __FUNCTION__, AUDIO_UBUS_PATH_ENABLE, ubus_strerror(rc));
}
else
{
printf("%s: ubus_invoke_async success\n", __FUNCTION__);
mbtk_audio_ubus_db->work_state++;
- req->complete_cb = mbtk_ubus_complete_cb;
- ubus_complete_request_async(mbtk_audio_ubus_db->ctx, req);
}
}
@@ -408,36 +387,25 @@
void mbtk_audio_switch_pcm(int mode)
{
int rc = 0;
- struct ubus_request *req = NULL;
if(NULL == mbtk_audio_ubus_db)
{
printf("mbtk_dtmf_ubus not init!\n");
return;
}
- req = (struct ubus_request *)malloc(sizeof(struct ubus_request));
- if (req == NULL)
- {
- printf("leave %s: lack of memory\n", __FUNCTION__);
- return;
- }
- memset(req, 0, sizeof(struct ubus_request));
blob_buf_init(&audio_cm_b, 0);
blobmsg_add_u32(&audio_cm_b, "param0", mode);
- if ((rc = ubus_invoke_async(mbtk_audio_ubus_db->ctx,
+ if ((rc = ubus_invoke(mbtk_audio_ubus_db->ctx,
mbtk_audio_ubus_db->audioif_request_id,
AUDIO_UBUS_SWITCH_PCM,
- audio_cm_b.head, req)) != UBUS_STATUS_OK)
+ audio_cm_b.head, NULL, NULL, 0)) != UBUS_STATUS_OK)
{
- free(req);
printf("%s, ubus_invoke_async %s failed %s\n", __FUNCTION__, AUDIO_UBUS_MODE_SET, ubus_strerror(rc));
}
else
{
printf("%s: ubus_invoke_async success\n", __FUNCTION__);
mbtk_audio_ubus_db->work_state++;
- req->complete_cb = mbtk_ubus_complete_cb;
- ubus_complete_request_async(mbtk_audio_ubus_db->ctx, req);
}
}
diff --git a/mbtk/test/Makefile b/mbtk/test/Makefile
index 239c6a0..f51be47 100755
--- a/mbtk/test/Makefile
+++ b/mbtk/test/Makefile
@@ -11,6 +11,8 @@
ifeq ($(BUILD_PLATFORM), asr1803)
LIBS += -lpolarssl
+else
+LIBS += -lmbtk_audio_lib
endif
CFLAGS +=
@@ -27,6 +29,12 @@
#endif
LOCAL_SRC_FILES = $(wildcard *.c) $(wildcard *.cpp)
+ifeq ($(BUILD_PLATFORM), asr1803)
+LOCAL_SRC_FILES += $(wildcard asr1803/*.c) $(wildcard asr1803/*.cpp)
+else
+LOCAL_SRC_FILES += $(wildcard asr1806/*.c) $(wildcard asr1806/*.cpp)
+endif
+
$(info LOCAL_SRC_FILES = $(LOCAL_SRC_FILES))
OBJS = $(patsubst %.c,%.o,$(patsubst %.cpp,%.o,$(LOCAL_SRC_FILES)))
@@ -37,10 +45,10 @@
$(BINS):$(OBJS)
@echo " BIN $@"
$(CC) $(CFLAGS) $(LIB_DIR) $(LIBS) $@.o -o $(OUT_DIR)/bin/$@
-
+
%.o:%.c
$(CC) $(CFLAGS) $(INC_DIR) $(DEFINE) -c $< -o $@
-
+
%.o:%.cpp
$(CC) $(CFLAGS) $(INC_DIR) $(DEFINE) -c $< -o $@
diff --git a/mbtk/test/audio_gain_test.c b/mbtk/test/asr1803/audio_gain_test.c
similarity index 100%
rename from mbtk/test/audio_gain_test.c
rename to mbtk/test/asr1803/audio_gain_test.c
diff --git a/mbtk/test/gnss_basic_demo.c b/mbtk/test/asr1803/gnss_basic_demo.c
similarity index 100%
rename from mbtk/test/gnss_basic_demo.c
rename to mbtk/test/asr1803/gnss_basic_demo.c
diff --git a/mbtk/test/gnss_update_demo.c b/mbtk/test/asr1803/gnss_update_demo.c
similarity index 100%
rename from mbtk/test/gnss_update_demo.c
rename to mbtk/test/asr1803/gnss_update_demo.c
diff --git a/mbtk/test/asr1806/mbtk_pcm_play_test.c b/mbtk/test/asr1806/mbtk_pcm_play_test.c
new file mode 100755
index 0000000..c11e06a
--- /dev/null
+++ b/mbtk/test/asr1806/mbtk_pcm_play_test.c
@@ -0,0 +1,139 @@
+#include <sys/stat.h>
+#include <unistd.h>
+#include <stdio.h>
+#include <fcntl.h>
+
+#include "audio_if_api.h"
+#include "mbtk_audio2.h"
+
+#define BUFF_SIZE 4096
+
+int main(int argc, char *argv[])
+{
+ if(argc != 3) {
+ printf("mbtk_pcm_play_test <wav_file> <pack_size>\n");
+ return -1;
+ }
+
+ mbtk_log_init("radio", "MBTK_AUDIO");
+
+ if(mbtk_audio_pcm_init()) {
+ printf("mbtk_audio_pcm_init() fail.\n");
+ return -1;
+ }
+
+ int pack_size = atoi(argv[2]);
+ if(pack_size <= 0) {
+ pack_size = 1024;
+ }
+
+ int rc, len, fd, frames = 0;
+ //char buf[MBTK_PCM_WB_BUF_SIZE];
+ char buf[BUFF_SIZE];
+ char *path = (char*)argv[1];
+ struct stat st;
+ struct riff_wave_header riff_wave_header;
+ struct chunk_header chunk_header;
+ struct chunk_fmt chunk_fmt = {0};
+ unsigned int more_chunks = 1;
+
+ /* Check and open source file */
+ if (access(path, F_OK) || stat(path, &st)) {
+ printf("%s: error reading from file %s\n", __FUNCTION__, path);
+ return -1;
+ }
+
+ if (!st.st_size) {
+ printf("%s: empty file %s\n", __FUNCTION__, path);
+ return -1;
+ }
+
+ fd = open(path, O_RDONLY);
+ if (fd < 0) {
+ printf("%s: error opening file %s\n", __FUNCTION__, path);
+ return -1;
+ }
+
+ read(fd, &riff_wave_header, sizeof(riff_wave_header));
+ if ((riff_wave_header.riff_id != ID_RIFF) || (riff_wave_header.wave_id != ID_WAVE)) {
+ printf("Error: '%s' is not a riff/wave file\n", path);
+ close(fd);
+ return -1;
+ }
+
+ do {
+ read(fd, &chunk_header, sizeof(chunk_header));
+
+ switch (chunk_header.id) {
+ case ID_FMT:
+ read(fd, &chunk_fmt, sizeof(chunk_fmt));
+ /* If the format header is larger, skip the rest */
+ if (chunk_header.sz > sizeof(chunk_fmt))
+ lseek(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(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) ) {
+ printf("%s: error wave file:sample_rate = %d, num_channels = %d!! \n",
+ __FUNCTION__,chunk_fmt.sample_rate, chunk_fmt.num_channels);
+ close(fd);
+ return -1;
+ }
+
+ printf("%s: success open wave file:%s, sample_rate = %d, num_channels = %d.\n",
+ __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()) {
+ printf("mbtk_audio_pcm_play_start() fail.\n");
+ return -1;
+ }
+
+ printf("%s: starting playback %d bytes every 20ms.\n", __FUNCTION__, pack_size);
+
+ while (TRUE) {
+ /* Playback loop */
+ memset(buf, 0x00, sizeof(buf));
+ len = read(fd, buf, pack_size);
+ if (len == -1) {
+ printf("%s: error reading from file\n", __FUNCTION__);
+ break;
+ }
+
+ if (len == 0) {
+ /* reached EOF */
+ printf("%s: nothing to read\n", __FUNCTION__);
+ break;
+ }
+
+ if((rc = mbtk_audio_pcm_play_data_send(buf, len)) < len) {
+ printf("Send data %d/%d\n", rc, len);
+ break;
+ }
+
+ printf("%s: No.%d frame playback[len - %d]\n", __FUNCTION__, ++frames, len);
+ }
+
+ if(mbtk_audio_pcm_deinit()) {
+ printf("mbtk_audio_pcm_deinit() fail.\n");
+ return -1;
+ }
+
+ return 0;
+}
+
diff --git a/mbtk/test/asr1806/mbtk_pcm_recorder_test.c b/mbtk/test/asr1806/mbtk_pcm_recorder_test.c
new file mode 100755
index 0000000..748e599
--- /dev/null
+++ b/mbtk/test/asr1806/mbtk_pcm_recorder_test.c
@@ -0,0 +1,116 @@
+#include <sys/stat.h>
+#include <unistd.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <signal.h>
+
+#include "mbtk_audio2.h"
+#include "mbtk_log.h"
+
+static void sig_handler(int sig)
+{
+ if(mbtk_audio_pcm_recorder_stop()) {
+ printf("mbtk_audio_pcm_recorder_stop() fail.\n");
+ //goto exit;
+ }
+
+ if(mbtk_audio_pcm_deinit()) {
+ printf("mbtk_audio_pcm_deinit() fail.\n");
+ }
+
+ printf("Success exit by signal...\n");
+ exit(0);
+}
+
+static void recorder_cb(void *data, uint32 data_len)
+{
+ if(data_len > 0) {
+ LOGD("Recorver data:%d", data_len);
+ } else {
+ LOGD("Recorver data end.");
+ }
+}
+
+int main(int argc, char *argv[])
+{
+ if(argc != 1) {
+ printf("mbtk_pcm_recorder_test\n");
+ return -1;
+ }
+
+ mbtk_log_init("radio", "MBTK_AUDIO");
+
+ if(mbtk_audio_pcm_init()) {
+ printf("mbtk_audio_pcm_init() fail.\n");
+ return -1;
+ }
+
+ signal(SIGINT, sig_handler);
+ signal(SIGTERM, sig_handler);
+
+ char cmd[100];
+ bool running = TRUE;
+ while(running)
+ {
+ memset(cmd, 0, 100);
+ int err;
+ printf("1 : Recorder 2 : Pause 3 : Resume 4 : Stop Other : Exit\n");
+ if(fgets(cmd, 100, stdin))
+ {
+ if(cmd[0] == '\r' || cmd[0] == '\n')
+ continue;
+ int state = atoi(cmd);
+ switch(state){
+ case 1:
+ {
+ if(mbtk_audio_pcm_recorder_start(recorder_cb)) {
+ printf("mbtk_audio_pcm_recorder_start() fail.\n");
+ //goto exit;
+ }
+ break;
+ }
+ case 2:
+ {
+ if(mbtk_audio_pcm_recorder_pause()) {
+ printf("mbtk_audio_pcm_recorder_pause() fail.\n");
+ //goto exit;
+ }
+ break;
+ }
+ case 3:
+ {
+ if(mbtk_audio_pcm_recorder_resume()) {
+ printf("mbtk_audio_pcm_recorder_resume() fail.\n");
+ //goto exit;
+ }
+ break;
+ }
+ case 4:
+ {
+ if(mbtk_audio_pcm_recorder_stop()) {
+ printf("mbtk_audio_pcm_recorder_stop() fail.\n");
+ //goto exit;
+ }
+ break;
+ }
+ default:
+ {
+ running = FALSE;
+ break;
+ }
+ }
+ }
+ }
+
+exit:
+ if(mbtk_audio_pcm_deinit()) {
+ printf("mbtk_audio_pcm_deinit() fail.\n");
+ return -1;
+ }
+
+ printf("Success exit.\n");
+ return 0;
+}
+
+
+
diff --git a/mbtk/test/asr1806/mbtk_wav_play_test.c b/mbtk/test/asr1806/mbtk_wav_play_test.c
new file mode 100755
index 0000000..deaaa97
--- /dev/null
+++ b/mbtk/test/asr1806/mbtk_wav_play_test.c
@@ -0,0 +1,106 @@
+#include <sys/stat.h>
+#include <unistd.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <signal.h>
+
+#include "mbtk_audio2.h"
+
+static void sig_handler(int sig)
+{
+ if(mbtk_audio_wav_play_stop()) {
+ printf("mbtk_audio_wav_play_stop() fail.\n");
+ //goto exit;
+ }
+
+ if(mbtk_audio_wav_deinit()) {
+ printf("mbtk_audio_wav_deinit() fail.\n");
+ }
+
+ printf("Success exit by signal...\n");
+ exit(0);
+}
+
+
+int main(int argc, char *argv[])
+{
+ if(argc != 2) {
+ printf("mbtk_wav_play_test <wav_file>\n");
+ return -1;
+ }
+
+ mbtk_log_init("radio", "MBTK_AUDIO");
+
+ if(mbtk_audio_wav_init()) {
+ printf("mbtk_audio_wav_init() fail.\n");
+ return -1;
+ }
+
+ signal(SIGINT, sig_handler);
+ signal(SIGTERM, sig_handler);
+
+ char cmd[100];
+ bool running = TRUE;
+ while(running)
+ {
+ memset(cmd, 0, 100);
+ int err;
+ printf("1 : Play 2 : Pause 3 : Resume 4 : Stop Other : Exit\n");
+ if(fgets(cmd, 100, stdin))
+ {
+ if(cmd[0] == '\r' || cmd[0] == '\n')
+ continue;
+ int state = atoi(cmd);
+ switch(state){
+ case 1:
+ {
+ if(mbtk_audio_wav_play_start(argv[1])) {
+ printf("mbtk_audio_wav_play_start() fail.\n");
+ //goto exit;
+ }
+ break;
+ }
+ case 2:
+ {
+ if(mbtk_audio_wav_play_pause()) {
+ printf("mbtk_audio_wav_play_pause() fail.\n");
+ //goto exit;
+ }
+ break;
+ }
+ case 3:
+ {
+ if(mbtk_audio_wav_play_resume()) {
+ printf("mbtk_audio_wav_play_resume() fail.\n");
+ //goto exit;
+ }
+ break;
+ }
+ case 4:
+ {
+ if(mbtk_audio_wav_play_stop()) {
+ printf("mbtk_audio_wav_play_stop() fail.\n");
+ //goto exit;
+ }
+ break;
+ }
+ default:
+ {
+ running = FALSE;
+ break;
+ }
+ }
+ }
+ }
+
+exit:
+ if(mbtk_audio_wav_deinit()) {
+ printf("mbtk_audio_wav_deinit() fail.\n");
+ return -1;
+ }
+
+ printf("Success exit.\n");
+ return 0;
+}
+
+
diff --git a/mbtk/test/asr1806/mbtk_wav_recorder_test.c b/mbtk/test/asr1806/mbtk_wav_recorder_test.c
new file mode 100755
index 0000000..2f9a1a2
--- /dev/null
+++ b/mbtk/test/asr1806/mbtk_wav_recorder_test.c
@@ -0,0 +1,105 @@
+#include <sys/stat.h>
+#include <unistd.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <signal.h>
+
+#include "mbtk_audio2.h"
+
+static void sig_handler(int sig)
+{
+ if(mbtk_audio_wav_recorder_stop()) {
+ printf("mbtk_audio_wav_recorder_stop() fail.\n");
+ //goto exit;
+ }
+
+ if(mbtk_audio_wav_deinit()) {
+ printf("mbtk_audio_wav_deinit() fail.\n");
+ }
+
+ printf("Success exit by signal...\n");
+ exit(0);
+}
+
+
+int main(int argc, char *argv[])
+{
+ if(argc != 2) {
+ printf("mbtk_wav_recorder_test <wav_file>\n");
+ return -1;
+ }
+
+ mbtk_log_init("radio", "MBTK_AUDIO");
+
+ if(mbtk_audio_wav_init()) {
+ printf("mbtk_audio_wav_init() fail.\n");
+ return -1;
+ }
+
+ signal(SIGINT, sig_handler);
+ signal(SIGTERM, sig_handler);
+
+ char cmd[100];
+ bool running = TRUE;
+ while(running)
+ {
+ memset(cmd, 0, 100);
+ int err;
+ printf("1 : Recorder 2 : Pause 3 : Resume 4 : Stop Other : Exit\n");
+ if(fgets(cmd, 100, stdin))
+ {
+ if(cmd[0] == '\r' || cmd[0] == '\n')
+ continue;
+ int state = atoi(cmd);
+ switch(state){
+ case 1:
+ {
+ if(mbtk_audio_wav_recorder_start(argv[1], MBTK_AUDIO_SAMPLE_RATE_8000)) {
+ printf("mbtk_audio_wav_recorder_start() fail.\n");
+ //goto exit;
+ }
+ break;
+ }
+ case 2:
+ {
+ if(mbtk_audio_wav_recorder_pause()) {
+ printf("mbtk_audio_wav_recorder_pause() fail.\n");
+ //goto exit;
+ }
+ break;
+ }
+ case 3:
+ {
+ if(mbtk_audio_wav_recorder_resume()) {
+ printf("mbtk_audio_wav_recorder_resume() fail.\n");
+ //goto exit;
+ }
+ break;
+ }
+ case 4:
+ {
+ if(mbtk_audio_wav_recorder_stop()) {
+ printf("mbtk_audio_wav_recorder_stop() fail.\n");
+ //goto exit;
+ }
+ break;
+ }
+ default:
+ {
+ running = FALSE;
+ break;
+ }
+ }
+ }
+ }
+
+exit:
+ if(mbtk_audio_wav_deinit()) {
+ printf("mbtk_audio_wav_deinit() fail.\n");
+ return -1;
+ }
+
+ printf("Success exit.\n");
+ return 0;
+}
+