blob: a954734ade23d1a1fe8653e102b126cdf6fca6f7 [file] [log] [blame]
#include "mbtk_type.h"
#include <fcntl.h>
#include <stdint.h>
#include <limits.h>
#include <termios.h>
#include <stdarg.h>
#include <pthread.h>
#include <sys/stat.h>
#include <unistd.h>
#include "ql/ql_audio.h"
static pthread_t play_thread_play;
int play_hdl = 0;
int handler = 0;
int fd = -1;
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 dtmf_cb1(char dtmf)
{
printf("%s:%c\n", __FUNCTION__, dtmf);
}
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;
}
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;
}
return 0;
}
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;
}
static void audio_play_thread(void *arg)
{
Ql_AudPlayer_PlayFrmFile(play_hdl, fd, 0);
}
int main(int argc, char* argv[])
{
if(argc != 2) {
printf("audio_play_test <wav_file>\n");
return -1;
}
struct stat st;
const char *path = (const char *)argv[1];
// mbtk_log_init("radio", "MBTK_AUDIO");
/* 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;
}
printf("1\n");
mbtk_audio_ubus_client_init(&handler, dtmf_cb1);
printf("2\n");
mbtk_audio_switch_pcm(1);
printf("3\n");
mbtk_audio_mode_set(2);
printf("4\n");
play_hdl = Ql_AudPlayer_Open(NULL, Ql_cb_playback);
printf("5\n");
if(0 == play_hdl) {
printf("Ql_AudPlayer_Open fail\n");
return -1;
}
char cmd[100];
bool running = TRUE;
printf("6\n");
while(running)
{
memset(cmd, 0, 100);
int err;
printf("1 : Play 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(0 == play_hdl)
break;
fd = open(argv[1], O_RDONLY);
if (fd <= 0) {
printf("Open file fail.\n");
goto exit;
}
mbtk_audio_path_enable(1);
if(0 == MBTK_wav_pcm16Le_check(fd))
{
pthread_attr_t thread_attr;
pthread_attr_init(&thread_attr);
if(pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED))
{
printf("pthread_attr_setdetachstate() fail.\n");
goto exit;
}
if (pthread_create(&play_thread_play, NULL, (void *)&audio_play_thread, NULL) < 0) {
printf("%s: error creating thread_play!\n", __FUNCTION__);
goto exit;
}
printf("Start play audio...\n");
}
else
{
printf("aplay file type error\n");
goto exit;
}
break;
}
default:
{
if(0 == play_hdl)
break;
Ql_AudPlayer_Close(play_hdl);
running = FALSE;
break;
}
}
}
}
exit:
if(fd > 0) {
close(fd);
}
mbtk_audio_path_enable(0);
mbtk_audio_mode_set(0);
mbtk_audio_switch_pcm(0);
printf("Success exit.\n");
return 0;
}