286 lines
8.0 KiB
C
Executable File
286 lines
8.0 KiB
C
Executable File
#include "PrjInc.h"
|
|
#if (VOICE_MODE == ENABLE)
|
|
//global debug level: PRJ_DBG_LVL
|
|
//local debug level: THIS_DBGLVL
|
|
#define THIS_DBGLVL 2 // 0=FATAL, 1=ERR, 2=WRN, 3=UNIT, 4=FUNC, 5=IND, 6=MSG, 7=VALUE, 8=USER
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
#define __MODULE__ UiAppVoiceCmd
|
|
#define __DBGLVL__ ((THIS_DBGLVL>=PRJ_DBG_LVL)?THIS_DBGLVL:PRJ_DBG_LVL)
|
|
#define __DBGFLT__ "*" //*=All, [mark]=CustomClass
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
#include <kwrap/cmdsys.h>
|
|
#include <kwrap/debug.h>
|
|
#include <kwrap/stdio.h>
|
|
#include <kwrap/sxcmd.h>
|
|
#include <kwrap/task.h>
|
|
#include "GxTime.h"
|
|
|
|
static UINT32 uivoice_writefile_tsk_run, uivoice_is_writefile_tsk_running;
|
|
static char uivoice_record_filename[128];
|
|
static THREAD_HANDLE uivoice_writefile_tsk_id;
|
|
static UINT32 uivoice_is_recording = FALSE;
|
|
static UINT32 uivoice_abuf_pa = 0, uivoice_abuf_va = 0, uivoice_abuf_size = 0;
|
|
static FST_FILE uivoice_play_fd = NULL;
|
|
|
|
|
|
#define PRI_UIVOICE_WFILE 10
|
|
#define STKSIZE_UIVOICE_WFILE 4096
|
|
|
|
THREAD_RETTYPE _UIAppVoice_WriteFileTsk(void)
|
|
{
|
|
FST_FILE filehdl;
|
|
UINT32 size;
|
|
UINT32 open_flag = FST_CREATE_ALWAYS | FST_OPEN_WRITE;
|
|
|
|
WAV_HEADER header;
|
|
header.riff.ChunkID = MAKEFOURCC('R', 'I', 'F', 'F');
|
|
header.riff.ChunkSize = 36;
|
|
header.riff.Format = MAKEFOURCC('W', 'A', 'V', 'E');
|
|
header.fmt.Subchunk1ID = MAKEFOURCC('f', 'm', 't', ' ');
|
|
header.fmt.Subchunk1Size = 16; // 16 for PCM
|
|
header.fmt.AudioFormat = 1; // PCM = 1
|
|
header.fmt.NumChannels = UIVoice_Audio_Info.aud_ch_num;
|
|
header.fmt.SampleRate = UIVoice_Audio_Info.aud_rate;
|
|
header.fmt.BitsPerSample = 16;
|
|
header.fmt.ByteRate = header.fmt.SampleRate * header.fmt.NumChannels * header.fmt.BitsPerSample / 8;
|
|
header.fmt.BlockAlign = header.fmt.NumChannels * header.fmt.BitsPerSample / 8;
|
|
header.data.Subchunk2ID = MAKEFOURCC('d', 'a', 't', 'a');
|
|
header.data.Subchunk2Size = 0;
|
|
|
|
THREAD_ENTRY();
|
|
|
|
uivoice_is_writefile_tsk_running = TRUE;
|
|
|
|
if (uivoice_abuf_size == 0) {
|
|
DBG_ERR("No audio frame buffer\r\n");
|
|
goto WRITETSK_EXIT;
|
|
}
|
|
|
|
if ((filehdl = FileSys_OpenFile(uivoice_record_filename, open_flag)) == NULL) {
|
|
DBG_ERR("Open file:%s failed.\r\n", uivoice_record_filename);
|
|
goto WRITETSK_EXIT;
|
|
}
|
|
|
|
size = sizeof(header);
|
|
FileSys_WriteFile(filehdl, (UINT8 *)&header, &size, 0, NULL);
|
|
|
|
while (uivoice_writefile_tsk_run) {
|
|
size = 4096;
|
|
if (ImageApp_Voice_AudCapGetFrame(0, (UINT8 *)uivoice_abuf_va, &size, 500) == E_OK) {
|
|
FileSys_WriteFile(filehdl, (UINT8 *)uivoice_abuf_va, &size, 0, NULL);
|
|
header.riff.ChunkSize += size;
|
|
header.data.Subchunk2Size += size;
|
|
}
|
|
}
|
|
|
|
// update wav header
|
|
size = sizeof(header);
|
|
FileSys_SeekFile(filehdl, 0, FST_SEEK_SET);
|
|
FileSys_WriteFile(filehdl, (UINT8 *)&header, &size, 0, NULL);
|
|
|
|
// flush and cose file
|
|
FileSys_FlushFile(filehdl);
|
|
FileSys_CloseFile(filehdl);
|
|
|
|
WRITETSK_EXIT:
|
|
uivoice_is_writefile_tsk_running = FALSE;
|
|
|
|
THREAD_RETURN(0);
|
|
}
|
|
|
|
static BOOL Cmd_voice_recstart(unsigned char argc, char **argv)
|
|
{
|
|
HD_RESULT hd_ret;
|
|
HD_COMMON_MEM_DDR_ID ddr_id = DDR_ID0;
|
|
UINT32 pa;
|
|
void *va;
|
|
struct tm CurDateTime;
|
|
|
|
if (uivoice_is_recording == TRUE) {
|
|
DBG_ERR("Already in record state.\r\n");
|
|
return TRUE;
|
|
}
|
|
|
|
// create data pool
|
|
uivoice_abuf_size = 4096;
|
|
if ((hd_ret = hd_common_mem_alloc("aframebuf", &pa, (void **)&va, uivoice_abuf_size, ddr_id)) != HD_OK) {
|
|
DBG_ERR("hd_common_mem_alloc aframebuf failed, size=%d, ret=%d\r\n", uivoice_abuf_size, hd_ret);
|
|
uivoice_abuf_va = 0;
|
|
uivoice_abuf_pa = 0;
|
|
uivoice_abuf_size = 0;
|
|
} else {
|
|
uivoice_abuf_va = (UINT32)va;
|
|
uivoice_abuf_pa = (UINT32)pa;
|
|
}
|
|
|
|
GxTime_GetTime(&CurDateTime);
|
|
snprintf(uivoice_record_filename, 127, "A:\\%04d%02d%02d%02d%02d%02d.wav",
|
|
CurDateTime.tm_year, CurDateTime.tm_mon, CurDateTime.tm_mday,
|
|
CurDateTime.tm_hour, CurDateTime.tm_min, CurDateTime.tm_sec);
|
|
|
|
if ((uivoice_writefile_tsk_id = vos_task_create(_UIAppVoice_WriteFileTsk, 0, "UIVoice_WFileTsk", PRI_UIVOICE_WFILE, STKSIZE_UIVOICE_WFILE)) == 0) {
|
|
DBG_ERR("UIVoice_WFileTsk create failed.\r\n");
|
|
} else {
|
|
uivoice_writefile_tsk_run = TRUE;
|
|
vos_task_resume(uivoice_writefile_tsk_id);
|
|
}
|
|
|
|
ImageApp_Voice_AudCapStart(0);
|
|
|
|
DBG_DUMP("UIVoice record start...\r\n");
|
|
DBG_DUMP("File name: %s\r\n", uivoice_record_filename);
|
|
|
|
uivoice_is_recording = TRUE;
|
|
|
|
return TRUE;
|
|
}
|
|
|
|
static BOOL Cmd_voice_recstop(unsigned char argc, char **argv)
|
|
{
|
|
HD_RESULT hd_ret;
|
|
|
|
if (uivoice_is_recording == FALSE) {
|
|
DBG_ERR("Already in idle state.\r\n");
|
|
return TRUE;
|
|
}
|
|
|
|
uivoice_writefile_tsk_run = FALSE;
|
|
ImageApp_Voice_AudCapStop(0);
|
|
|
|
// wait file write done
|
|
while(uivoice_is_writefile_tsk_running) {
|
|
vos_util_delay_ms(100);
|
|
}
|
|
|
|
// free data buffer
|
|
if (uivoice_abuf_va) {
|
|
if ((hd_ret = hd_common_mem_free(uivoice_abuf_pa, (void *)uivoice_abuf_va)) != HD_OK) {
|
|
DBG_ERR("hd_common_mem_free failed(%d)\r\n", hd_ret);
|
|
}
|
|
uivoice_abuf_pa = 0;
|
|
uivoice_abuf_va = 0;
|
|
uivoice_abuf_size = 0;
|
|
}
|
|
|
|
DBG_DUMP("UIVoice record stop\r\n");
|
|
|
|
uivoice_is_recording = FALSE;
|
|
|
|
return TRUE;
|
|
}
|
|
|
|
static BOOL Cmd_voice_playstart(unsigned char argc, char **argv)
|
|
{
|
|
IAVOICE_AUDIO_PLAY_INFO play_info = {0};
|
|
char file_name[80] = {0};
|
|
|
|
if (argc) {
|
|
strncpy(file_name, argv[0], 79);
|
|
} else {
|
|
DBG_ERR("No file path.\r\n");
|
|
return TRUE;
|
|
}
|
|
|
|
// Open file
|
|
if ((uivoice_play_fd = FileSys_OpenFile(file_name, FST_OPEN_READ)) == 0) {
|
|
DBG_ERR("UIFlowWndPlay_OnKeySelect: Can't open Video file (%s)\r\n", file_name);
|
|
return TRUE;
|
|
}
|
|
play_info.fd = uivoice_play_fd;
|
|
play_info.format = _IAVOICE_FILE_FORMAT_WAV;
|
|
play_info.aud_rate = 16000;
|
|
play_info.aud_ch_num = 1;
|
|
|
|
Ux_SendEvent(0, NVTEVT_VOICE_PLAY, 1, (UINT32)&play_info);
|
|
DBG_DUMP("Voice play start:%s\r\n", file_name);
|
|
|
|
return TRUE;
|
|
}
|
|
|
|
static BOOL Cmd_voice_playstop(unsigned char argc, char **argv)
|
|
{
|
|
Ux_SendEvent(0, NVTEVT_VOICE_PLAYSTOP, 0);
|
|
FileSys_CloseFile(uivoice_play_fd);
|
|
DBG_DUMP("Voice play stop\r\n");
|
|
|
|
return TRUE;
|
|
}
|
|
|
|
static BOOL Cmd_voice_playpause(unsigned char argc, char **argv)
|
|
{
|
|
Ux_SendEvent(0, NVTEVT_VOICE_PLAYPAUSE, 0);
|
|
DBG_DUMP("Voice play pause\r\n");
|
|
|
|
return TRUE;
|
|
}
|
|
|
|
static BOOL Cmd_voice_playresume(unsigned char argc, char **argv)
|
|
{
|
|
Ux_SendEvent(0, NVTEVT_VOICE_PLAYRESUME, 0);
|
|
DBG_DUMP("Voice play resume\r\n");
|
|
|
|
return TRUE;
|
|
}
|
|
|
|
static BOOL Cmd_voice_playvolume(unsigned char argc, char **argv)
|
|
{
|
|
UINT32 volume = 0;
|
|
|
|
if (argc) {
|
|
sscanf_s(argv[0],"%d", &volume);
|
|
Ux_SendEvent(0, NVTEVT_VOICE_PLAYVOLUME, 1, volume);
|
|
DBG_DUMP("Voice play set aout volume to %d\r\n", volume);
|
|
}
|
|
return TRUE;
|
|
}
|
|
|
|
static SXCMD_BEGIN(uivoice_cmd_tbl, "uivoice command")
|
|
SXCMD_ITEM("recstart", Cmd_voice_recstart, "audio record start")
|
|
SXCMD_ITEM("recstop", Cmd_voice_recstop, "audio record stop")
|
|
SXCMD_ITEM("playstart", Cmd_voice_playstart, "audio play start")
|
|
SXCMD_ITEM("playstop", Cmd_voice_playstop, "audio play stop")
|
|
SXCMD_ITEM("playpause", Cmd_voice_playpause, "audio play pause")
|
|
SXCMD_ITEM("playresume", Cmd_voice_playresume, "audio play resume")
|
|
SXCMD_ITEM("playvolume %", Cmd_voice_playvolume, "audio play volume")
|
|
SXCMD_END()
|
|
|
|
static int uivoice_cmd_showhelp(int (*dump)(const char *fmt, ...))
|
|
{
|
|
UINT32 cmd_num = SXCMD_NUM(uivoice_cmd_tbl);
|
|
UINT32 loop = 1;
|
|
|
|
dump("---------------------------------------------------------------------\r\n");
|
|
dump(" %s\n", "uivoice");
|
|
dump("---------------------------------------------------------------------\r\n");
|
|
|
|
for (loop = 1 ; loop <= cmd_num ; loop++) {
|
|
dump("%15s : %s\r\n", uivoice_cmd_tbl[loop].p_name, uivoice_cmd_tbl[loop].p_desc);
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
MAINFUNC_ENTRY(uivoice, argc, argv)
|
|
{
|
|
UINT32 cmd_num = SXCMD_NUM(uivoice_cmd_tbl);
|
|
UINT32 loop;
|
|
int ret;
|
|
|
|
if (argc < 2) {
|
|
return -1;
|
|
}
|
|
if (strncmp(argv[1], "?", 2) == 0) {
|
|
uivoice_cmd_showhelp(vk_printk);
|
|
return 0;
|
|
}
|
|
for (loop = 1 ; loop <= cmd_num ; loop++) {
|
|
if (strncmp(argv[1], uivoice_cmd_tbl[loop].p_name, strlen(argv[1])) == 0) {
|
|
ret = uivoice_cmd_tbl[loop].p_func(argc-2, &argv[2]);
|
|
return ret;
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
#endif
|
|
|