blob: f783f5f84f0929a15d2483c79b964b7b9ca2fb71 [file] [log] [blame]
/**
* \file lynq_audio_api.c
* \brief A Documented file.
*
* Detailed description
* \Author: luojian
* \Version: 1.0.0
* \Date: 2022-10-27
*/
#include <fcntl.h>
#include <stdint.h>
#include <limits.h>
#include <termios.h>
#include <stdarg.h>
#include <dirent.h>
#include <sys/stat.h>
#include <sys/statfs.h>
#include <sys/types.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <time.h>
#include <sys/ioctl.h>
#include <stdio.h>
#include <signal.h>
#include <errno.h>
#include <stdlib.h>
#include <pthread.h>
#include "mbtk_log.h"
#include "mbtk_type.h"
#ifdef MBTK_PLATFORM_ASR1803
#include "mbtk_audio.h"
static int rec_fd = 0;
static int play_fd = 0;
static mbtk_audio_handle play_hdl = NULL;
static mbtk_audio_handle record_hdl = NULL;
int volume_size = 0;
pthread_t paly_thread;
void dtmf_cb(char dtmf)
{
printf("%s:%c\n", __FUNCTION__, dtmf);
}
void audio_volume_cb(int volume)
{
volume_size = volume;
printf("%s:%d\n", __FUNCTION__, volume);
}
int lynq_memscpy
(
void *dst,
int dst_size,
const void *src,
int src_size
)
{
if(dst_size == 0 || src_size == 0 || dst == NULL || src == NULL)
{
return 0;
}
else
{
return memcpy( dst, src,src_size);
}
} /* dsatutil_free_memory() */
static int lynq_get_path_name ( const char* path_name,char* path )
{
int i=0;
int last = -1;
int len = strlen ( path_name );
if ( len > 0 )
{
for ( i=len - 1; i >= 0; i-- )
{
if ( path_name[i] == '/' )
{
last = i;
break;
}
}
if ( i != -1 )
{
lynq_memscpy ( path, ( i + 1 ) * sizeof ( char ), path_name, ( i + 1 ) * sizeof ( char ) );
printf ( "mbtk_get_path %s", path );
return 0;
}
}
return -1;
}
int lynq_create_audio_dir(const char *dirname)
{
DIR *p_dir;
int res = -1, i = 0;;
char str[512];
strncpy(str, dirname, 512);
int len=strlen(str);
if(NULL == (p_dir = opendir((const char *)dirname)))
{
for(i=0; i<len; i++ )
{
if( str[i]=='/' )
{
str[i] = '\0';
if( access(str,0)!=0 )
{
if(mkdir(dirname, 0777) == 0)
{
res = 0;
}
else
{
fprintf(stderr, "create audio dir error \n");
res = -1;
}
}
str[i]='/';
}
}
if( len>0 && access(str,0)!=0 )
{
if(mkdir(dirname, 0777) == 0)
{
res = 0;
}
else
{
fprintf(stderr, "create audio dir error \n");
res = -1;
}
}
}
else
{
closedir(p_dir);
res = 0;
}
return res;
}
void lynq_record_cb_func(int cb_result, char* databuf, unsigned int len)
{
int rc;
// printf("lynq_record_cb_func() len:%d, rec_fd:%d\n", len, rec_fd);
if(NULL == databuf)
{
printf("NULL == databuf\n");
}
if(NULL != databuf && len > 0 && rec_fd > 0)
{
//for debug:save into file
rc = write(rec_fd, databuf, len);
if (rc < 0) {
printf("%s: error writing to file!\n", __FUNCTION__);
} else if (rc < len) {
printf("%s: wrote less the buffer size!\n", __FUNCTION__);
}
}
}
#else
#endif
int lynq_media_rec_audio(const char *path)
{
#ifdef MBTK_PLATFORM_ASR1803
int ret = 0;
char audio_dir[50] ={0};
char audio_wav[10] = {0};
lynq_get_path_name(path, audio_dir);
printf("path:%s, audio_dir:%s\n", path, audio_dir);
record_hdl = mbtk_audio_open(MBTK_AUTIO_TYPE_IN, 1, 8000, NULL);
if (record_hdl == 0)
{
printf("AudRecorder open error\n");
return -1;
}
lynq_create_audio_dir(audio_dir);
rec_fd = open(path, O_RDWR|O_CREAT|O_TRUNC, 0644);
if (rec_fd <= 0)
{
printf("file open error\n");
goto err;
}
if(-1 == mbtk_audio_record(record_hdl, lynq_record_cb_func, NULL))
{
printf("file write error\n");
goto err;
}
return 0;
// sleep(10);
err:
// Ql_AudRecorder_Close();
if(rec_fd > 0)
{
close(rec_fd);
rec_fd = 0;
}
return -1;
#else
return -1;
#endif
}
//停止录制音频文件
void lynq_media_rec_stop_audio(void)
{
#ifdef MBTK_PLATFORM_ASR1803
// sleep(10);
mbtk_audio_close(record_hdl);
if(rec_fd > 0)
{
close(rec_fd);
rec_fd = 0;
}
#else
#endif
}
#ifdef MBTK_PLATFORM_ASR1803
//播放音频文件
int lynq_media_play_audio_thread_handle(void *argv)
{
char databuf[1024];
int size;
char *path = (char *)argv;
printf("lynq_media_play_audio() start \npath:%s\n",path);
LOGI("%s %d", __FUNCTION__, __LINE__);
play_hdl = mbtk_audio_open(MBTK_AUTIO_TYPE_OUT, 1, 8000, NULL);
if(NULL == play_hdl)
printf("mbtk_audio_open fail\n");
play_fd = open(path, O_RDWR);
if (play_fd <= 0)
{
printf("file open error\n");
goto err;
}
memset(databuf, 0, sizeof(databuf));
while(0 < (size = read(play_fd, databuf, sizeof(databuf))))
{
if(-1 == mbtk_audio_play_stream(play_hdl, databuf, size))
break;
}
printf("aplay Stream end \n");
err:
if(play_fd > 0)
{
close(play_fd);
play_fd = 0;
}
pthread_exit(&paly_thread);
mbtk_audio_close(play_hdl);
return 0;
}
#endif
//创建线程播放音频文件
int lynq_media_play_audio(const char *path)
{
#ifdef MBTK_PLATFORM_ASR1803
int ret = pthread_create(&paly_thread, NULL, lynq_media_play_audio_thread_handle, (void *)path);
if (ret != 0) {
printf("create thread failed!\n");
return -1;
}
pthread_detach(paly_thread);
return 0;
#else
return 0;
#endif
}
//停止播放音频文件
void lynq_media_stop_audio(void)
{
#ifdef MBTK_PLATFORM_ASR1803
printf("lynq_media_stop_audio()----\n");
if(play_fd > 0)
{
int ret = pthread_cancel(paly_thread);
if (ret != 0) {
printf("cancle paly_thread fail\n");
return ;
}
close(play_fd);
play_fd = 0;
}
mbtk_audio_close(play_hdl);
#else
#endif
}
#ifdef MBTK_PLATFORM_ASR1803
int lynq_audio_ubus_client_init(mbtk_audio_client_handle_type *ph_audio, mbtk_dtmf_cb cb)
{
if(rec_fd > 0 || play_fd > 0)
{
printf("rec or play need close\n");
return -1;
}
return mbtk_audio_ubus_client_init(ph_audio, cb);
}
int lynq_audio_ubus_client_deinit(mbtk_audio_client_handle_type h_audio)
{
if(rec_fd > 0 || play_fd > 0)
{
printf("rec or play need close\n");
return -1;
}
return mbtk_audio_ubus_client_deinit(h_audio);
}
#endif
int lynq_get_spk_volume(int * volume)
{
#ifdef MBTK_PLATFORM_ASR1803
mbtk_audio_ubus_volume_get(audio_volume_cb);
*volume = volume_size;
return 0;
#else
return 0;
#endif
}
int lynq_set_spk_volume(const int volume)
{
#ifdef MBTK_PLATFORM_ASR1803
mbtk_audio_ubus_volume_set(volume);
return 0;
#else
return 0;
#endif
}