blob: 740f15264f1384114be01cb651cebcf41c9c818a [file] [log] [blame]
#include "mbtk_type.h"
#include <fcntl.h>
#include <stdint.h>
#include <limits.h>
#include <termios.h>
#include <stdarg.h>
#include <errno.h>
#include <signal.h>
// #include "ql_at.h"
#include "ql/ql_audio.h"
// #include "mopen_tts.h"
#define MBTK_PLAY_PA_CONTROL 1
#define MBTK_RECV_PA_CONTROL 1
static int record_fd = -1;
static int play_hdl = 0;
static int gpio33_fd = -1;
static int gpio34_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 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;
}
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;
}
static void audio_pa_open()
{
if(gpio34_fd > 0) {
write(gpio34_fd, "1", 1);
}
if(gpio33_fd > 0) {
write(gpio33_fd, "0", 1);
}
}
static void audio_pa_close()
{
if(gpio34_fd > 0) {
write(gpio34_fd, "0", 1);
}
if(gpio33_fd > 0) {
write(gpio33_fd, "1", 1);
}
}
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 int play(const char *path)
{
int fd = open(path, O_RDONLY);
if(fd < 0) {
printf("open(%s) fail:%d\n", path, errno);
return -1;
}
#if MBTK_PLAY_PA_CONTROL
audio_pa_close();
#endif
play_hdl = Ql_AudPlayer_Open(NULL, Ql_cb_playback);
if(0 == play_hdl) {
printf("Ql_AudPlayer_Open fail\n");
close(fd);
return -1;
}
if(MBTK_wav_pcm16Le_check(fd)) {
printf("MBTK_wav_pcm16Le_check() fail.\n");
goto fail;
}
#if MBTK_PLAY_PA_CONTROL
audio_pa_open();
#endif
if(Ql_AudPlayer_PlayFrmFile(play_hdl, fd, 0)) {
printf("Ql_AudPlayer_PlayFrmFile() fail.\n");
goto fail;
}
if(play_hdl) {
#if MBTK_PLAY_PA_CONTROL
audio_pa_close();
#endif
printf("Play complete.\n");
Ql_AudPlayer_Close(play_hdl);
#if MBTK_PLAY_PA_CONTROL
audio_pa_open();
#endif
play_hdl = 0;
}
close(fd);
return 0;
fail:
Ql_AudPlayer_Close(play_hdl);
close(fd);
return -1;
}
static int recv(const char *path)
{
record_fd = open(path, O_RDWR|O_CREAT|O_TRUNC, 0644);
if(record_fd < 0) {
printf("open(%s) fail:%d\n", path, errno);
return -1;
}
#if MBTK_RECV_PA_CONTROL
audio_pa_close();
#endif
int hdl = Ql_AudRecorder_Open(NULL, record_cb_func);
if(0 == hdl) {
printf("Ql_AudRecorder_Open fail\n");
close(record_fd);
record_fd = -1;
return -1;
}
if(MBTK_wav_pcm16Le_set(record_fd)) {
printf("MBTK_wav_pcm16Le_set() fail.\n");
goto fail;
}
#if MBTK_RECV_PA_CONTROL
audio_pa_open();
#endif
if(Ql_AudRecorder_StartRecord()) {
printf("Ql_AudRecorder_StartRecord() fail.\n");
goto fail;
}
sleep(10);
#if MBTK_RECV_PA_CONTROL
audio_pa_close();
#endif
Ql_AudRecorder_Close();
#if MBTK_RECV_PA_CONTROL
audio_pa_open();
#endif
printf("Recorder complete.\n");
close(record_fd);
record_fd = -1;
return 0;
fail:
Ql_AudRecorder_Close();
close(record_fd);
record_fd = -1;
return -1;
}
static void sig_handler(int sig)
{
printf("output signal number: %d.\n", sig);
if(play_hdl) {
Ql_AudPlayer_Stop(play_hdl);
}
}
static void help()
{
printf("yx_audio_test play/recv file_name\n");
}
int main(int argc, char *argv[])
{
if(argc != 3) {
help();
return -1;
}
signal(SIGINT, sig_handler);
signal(SIGTERM, sig_handler);
signal(SIGTSTP, sig_handler);
if(access("/sys/class/gpio/gpio33/value", F_OK)) {
system("echo 33 > /sys/class/gpio/export");
system("echo out > /sys/class/gpio/gpio33/direction");
}
gpio33_fd = open("/sys/class/gpio/gpio33/value", O_RDWR);
if(access("/sys/class/gpio/gpio34/value", F_OK)) {
system("echo 34 > /sys/class/gpio/export");
system("echo out > /sys/class/gpio/gpio34/direction");
}
gpio34_fd = open("/sys/class/gpio/gpio34/value", O_RDWR);
int ret = -1;
if(!strcmp(argv[1], "play")) {
ret = play(argv[2]);
} else if(!strcmp(argv[1], "recv")) {
ret = recv(argv[2]);
} else {
help();
}
if(gpio34_fd > 0) {
close(gpio34_fd);
}
if(gpio33_fd > 0) {
close(gpio33_fd);
}
return ret;
}