blob: c9fa9364f49fbb8c5176d2ff1fc768bd40b36af9 [file] [log] [blame]
#include "mbtk_type.h"
#include <fcntl.h>
#include <stdint.h>
#include <limits.h>
#include <termios.h>
#include <stdarg.h>
// #include "ql_at.h"
#include "ql/ql_audio.h"
// #include "mopen_tts.h"
#define MBTK_AUD_DEMO_WAV "/data/demo.wav"
#define MBTK_AUD_DEMO_WAV1 "/data/demo1.wav"
int play_hdl = 0;
static int record_fd = 0;
int Ql_cb_playback(int hdl, int result)
{
printf("%s: hdl=%d, result=%d\n\r", __func__, hdl, result);
if (result == AUD_PLAYER_FINISHED || result == AUD_PLAYER_NODATA)
{
printf("%s: play finished\n\r", __func__);
}
return 0;
}
void record_cb_func(int cb_result, char* databuf, unsigned int len)
{
int rc;
if(NULL != databuf && len > 0 && record_fd > 0)
{
//for debug:save into file
rc = write(record_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__);
}
}
}
int MBTK_wav_pcm16Le_check(int fd)
{
struct wav_header hdr;
if (fd <= 0)
return -1;
if (read(fd, &hdr, sizeof(hdr)) != sizeof(hdr))
{
printf("\n%s: cannot read header\n", __FUNCTION__);
return -1;
}
printf("hdr.riff_id:%X, hdr.riff_fmt:%X, hdr.fmt_id:%X", hdr.riff_id, hdr.riff_fmt, hdr.fmt_id);
if ((hdr.riff_id != ID_RIFF)
|| (hdr.riff_fmt != ID_WAVE)
|| (hdr.fmt_id != ID_FMT))
{
printf("\n%s: is not a riff/wave file\n", __FUNCTION__);
return -1;
}
if ((hdr.audio_format != FORMAT_PCM) || (hdr.fmt_sz != 16)) {
printf("\n%s: is not pcm format\n", __FUNCTION__);
return -1;
}
if (hdr.bits_per_sample != 16) {
printf("\n%s: is not 16bit per sample\n", __FUNCTION__);
return -1;
}
printf("audio_format: %d,num_channels: %d,sample_rate: %d,byte_rate: %d,bits_per_sample: %d data_sz: %d\n",
hdr.audio_format, hdr.num_channels, hdr.sample_rate, hdr.byte_rate, hdr.bits_per_sample, hdr.data_sz);
return hdr.data_sz;
}
int MBTK_wav_pcm16Le_set(int fd)
{
struct wav_header hdr;
if (fd <= 0)
return -1;
memset(&hdr, 0, sizeof(struct wav_header));
hdr.riff_id = ID_RIFF;
hdr.riff_fmt = ID_WAVE;
hdr.fmt_id = ID_FMT;
hdr.fmt_sz = 16;
hdr.audio_format = FORMAT_PCM;
hdr.num_channels = 1;
hdr.sample_rate = 8000;
hdr.bits_per_sample = 16;
hdr.byte_rate = (8000 * 1 * hdr.bits_per_sample) / 8;
hdr.block_align = (hdr.bits_per_sample * 1) / 8;
hdr.data_id = ID_DATA;
hdr.data_sz = 0;
hdr.riff_sz = hdr.data_sz + 44 - 8;
if (write(fd, &hdr, sizeof(hdr)) != sizeof(hdr)) {
return -1;
}
return 0;
}
unsigned char* handle_file1(char *audioFilePath)
{
long filesize;
unsigned char *pcmBuf = NULL;
FILE *fp = NULL;
int size;
// 处理文件格式
fp = fopen(audioFilePath, "rb");
if (!fp) {
printf("%s:fopen failed",__func__);
}
fseek(fp,0,SEEK_END);
filesize=ftell(fp);
printf("%s:filesize:%d\n", __func__, filesize);
fseek(fp,44,SEEK_SET);
pcmBuf =(unsigned char *)malloc(sizeof(char)*filesize);
memset(pcmBuf,0,sizeof(char)*filesize);
// fread(pcmBuf, filesize-44,1, fp);
fread(pcmBuf, 1, filesize-44, fp);
fclose(fp);
printf("strlen(pcmBuf):%d\n", strlen(pcmBuf));
return pcmBuf;
}
void *audio_thread(void *arg)
{
printf("audio_thread(1)----------\n");
Ql_Playback_Samprate_Set(1);
int play_hdl = 0;
int fd = 0;
int ret;
int file_size =0;
int file_size1 =0;
unsigned char *pcmBuf = NULL;
unsigned char *pcmBuf1 = NULL;
play_hdl = Ql_AudPlayer_Open(NULL, Ql_cb_playback);
if(0 == play_hdl)
{
printf("Ql_AudPlayer_Open fail\n");
}
fd = open(MBTK_AUD_DEMO_WAV, O_RDWR);
if (fd <= 0)
{
printf("Open fail\n");
}
int fd1 = open(MBTK_AUD_DEMO_WAV1, O_RDWR);
printf("fd1:%d\n", fd1);
if (fd1 <= 0)
{
printf("Open fail\n");
}
file_size = MBTK_wav_pcm16Le_check(fd);
file_size1 = MBTK_wav_pcm16Le_check(fd1);
printf("file_size:%d, file_size1:%d\n", file_size, file_size1);
pcmBuf = handle_file1(MBTK_AUD_DEMO_WAV);
pcmBuf1 = handle_file1(MBTK_AUD_DEMO_WAV1);
if(file_size > 0 && file_size1 > 0 )
{
if(pcmBuf != NULL)
{
ret = Ql_AudPlayer_Play(play_hdl, pcmBuf, file_size);
printf("ret:%d\n", ret);
}
Ql_Rxgain_Set(3);
if(pcmBuf1 != NULL)
{
ret = Ql_AudPlayer_Play(play_hdl, pcmBuf1, file_size1);
printf("ret:%d\n", ret);
}
}
Ql_AudPlayer_Close(play_hdl);
close(fd);
close(fd1);
return 0;
}
void *audio_play_file_thread(void *arg)
{
char operator[10];
int fd = open(MBTK_AUD_DEMO_WAV, O_RDWR);
if (fd <= 0)
return 0;
int fd1 = open(MBTK_AUD_DEMO_WAV1, O_RDWR);
if (fd1 <= 0)
return 0;
Ql_AudPlayer_PlayFrmFile(play_hdl, fd, 0);
Ql_Rxgain_Set(3);
Ql_AudPlayer_PlayFrmFile(play_hdl, fd1, 0);
close(fd);
close(fd1);
return 0;
}
int aplay_thread(char *audioFilePath)
{
int res;
pthread_t play_thread;
res = pthread_create(&play_thread, NULL, audio_thread, audioFilePath);
if (res != 0) {
printf("%s:pthread_create failed",__func__);
return 0;
}
return 0;
}
int aplay_file_thread(int play_hdl)
{
int res;
pthread_t play_thread;
int hdl = play_hdl;
res = pthread_create(&play_thread, NULL, audio_play_file_thread, &hdl);
if (res != 0) {
printf("%s:pthread_create failed",__func__);
return 0;
}
return 0;
}
void aplay(void)
{
char operator[10];
char databuf[1024];
int opt = 0;
int fd = 0;
int size = 0;
int state;
int handler = 0;
int file_size = 0 ;
while(1)
{
printf("=========aplay========2\n"
"\t 0 Open PCM\n"
"\t 1 Play Stream\n"
"\t 2 Play file\n"
"\t 3 Close\n"
"\t 4 play stream thread\n"
"\t 5 pause\n"
"\t 6 repause\n"
"\t 7 play file thread\n"
"\t 8 play mp3\n"
"\t others exit\n\n"
"operator >> ");
fflush(stdin);
fgets(operator, sizeof(operator), stdin);
opt = atoi(operator);
switch (opt)
{
case 0:
Ql_Playback_Samprate_Set(1);
play_hdl = Ql_AudPlayer_Open(NULL, Ql_cb_playback);
if(0 == play_hdl)
printf("Ql_AudPlayer_Open fail\n");
printf("\nplay_hdl:%d\n", play_hdl);
break;
case 1:
if(0 == play_hdl)
continue;
fd = open(MBTK_AUD_DEMO_WAV, O_RDWR);
if (fd <= 0)
continue;
file_size = MBTK_wav_pcm16Le_check(fd);
printf("file_size:%d\n", file_size);
if(file_size > 0 )
{
unsigned char *pcmBuf = NULL;
char *p1 = pcmBuf;
pcmBuf = handle_file1(MBTK_AUD_DEMO_WAV);
if(pcmBuf != NULL)
{
if(-1 == Ql_AudPlayer_Play(play_hdl, pcmBuf, file_size))
printf("\n------\n");
}
}
// Ql_Rxgain_Set(5);
close(fd);
break;
case 2:
if(0 == play_hdl)
continue;
fd = open(MBTK_AUD_DEMO_WAV, O_RDWR);
if (fd <= 0)
continue;
if(1)
{
Ql_AudPlayer_PlayFrmFile(play_hdl, fd, 44);
}
else
{
printf("aplay file type error\n");
}
close(fd);
break;
case 3:
if(0 == play_hdl)
continue;
Ql_AudPlayer_Close(play_hdl);
break;
case 4:
aplay_thread(MBTK_AUD_DEMO_WAV);
break;
case 5:
if(0 == play_hdl)
continue;
Ql_AudPlayer_Pause(play_hdl);
break;
case 6:
if(0 == play_hdl)
continue;
Ql_AudPlayer_Resume(play_hdl);
break;
case 7:
if(0 == play_hdl)
continue;
aplay_file_thread(play_hdl);
break;
case 8:
// Ql_Mp3_To_Wav("/data/demo.wav", "/data/mp3demo.mp3", play_hdl);
Ql_Mp3_To_Play("/data/mp3demo.mp3", play_hdl, 0);
break;
default:
return;
}
sleep(1);
}
printf("aplay exit\n");
return ;
}
void arec(void)
{
int ret;
char operator[10];
int opt;
int hdl = 0;
while(1)
{
printf("=======arec======\n"
"\t 0 Open PCM\n"
"\t 1 Start Record\n"
"\t 2 Get state\n"
"\t 3 Pause\n"
"\t 4 Resume\n"
"\t 5 Stop\n"
"\t 6 Close\n"
"\t others exit\n\n"
"operator >> ");
fflush(stdin);
fgets(operator, sizeof(operator), stdin);
opt = atoi(operator);
switch (opt)
{
case 0:
Ql_Playback_Samprate_Set(0);
hdl = Ql_AudRecorder_Open(NULL, record_cb_func);
if (hdl == 0)
return ;
break;
case 1:
if(0 == hdl)
{
printf("audio is not initialized yet.\n");
continue;
}
if(0 != record_fd)
{
printf("audio It's already being recorded.\n");
continue;
}
record_fd = open(MBTK_AUD_DEMO_WAV, O_RDWR|O_CREAT|O_TRUNC, 0644);
if (record_fd <= 0)
printf("file open error\n");
if(0 == MBTK_wav_pcm16Le_set(record_fd))
{
ret = Ql_AudRecorder_StartRecord();
if(0 != ret)
{
printf("audio record error: %d\n", ret);
close(record_fd);
record_fd = 0;
}
}
else
{
printf("arec set file header error\n");
close(record_fd);
record_fd = 0;
}
break;
case 2:
// printf("arec state : %d\n", state);
break;
case 3:
break;
case 4:
break;
case 5:
break;
case 6:
Ql_AudRecorder_Close();
if(record_fd > 0)
{
close(record_fd);
record_fd = 0;
}
break;
default:
return;
}
sleep(1);
}
printf("arec exit\n");
return ;
}
int main(void)
{
char operator[10];
int opt;
// printf("Ql_Mp3_To_Wav()\n");
// mbtk_audio_mp3_to_wav("/data/demo.wav", "/data/mp3demo.mp3");
// Ql_Mp3_To_Wav("/data/demo.wav", "/data/mp3demo.mp3");
while(1)
{
printf("=========audio main=========\n"
"\t0 exit\n"
"\t1 aplay\n"
"\t2 arec\n"
"\t3 set speaker Volume\n"
"\t4 get speaker Volume\n"
"\t5 set mic Volume\n"
"\t6 get mic Volume\n"
"\t7 tts\n"
"\t8 tone\n"
"operator: >> ");
fgets(operator, sizeof(operator), stdin);
fflush(stdin);
opt = atoi(operator);
switch (opt)
{
case 0:
printf("main exit\n");
return 0;
case 1:
aplay();
break;
case 2:
arec();
break;
case 3:
mbtk_at_rec(NULL);
break;
case 4:
mbtk_at_play(NULL);
break;
case 5:
break;
case 6:
break;
case 7:
break;
case 8:
break;
default:
break;
}
sleep(1);
}
return 0;
}