blob: edfe45cb73256a46af8992088a064dc5d09c1bb5 [file] [log] [blame]
/******************************************************************************
*(C) Copyright 2019 ASR.
* All Rights Reserved
******************************************************************************/
/* -------------------------------------------------------------------------------------------------------------------
*
* Filename:gen_play_wav.c
*
* Authors:
*
* Description: The DEMO will generate the special wav and play.
*
*
* HISTORY:
*
*
*
* Notes:
*
******************************************************************************/
/******************************************************************************
* Include files
******************************************************************************/
#include "audio_if_types.h"
#include "audio_if_ubus.h"
#include "audio_if_parameter.h"
#include "audio_if.h"
#include "audio_if_api.h"
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "telatparamdef_ubus.h"
#include <libubox/blobmsg_json.h>
#include "libubus.h"
#include <string.h>
#include "audio_if_audio_hw_mrvl.h"
#include "utlEventHandler.h"
#include "udev_monitor.h"
#include "audio_hw_mrvl.h"
#include <stdlib.h>
#include <stdio.h>
#include <cutils/str_parms.h>
#include "vcm.h"
extern void* audio_hal_install(void);
extern void audio_hal_uninstall(void);
extern void configure_vcm(unsigned int data[]);
/******************************************************************************
* Globals
******************************************************************************/
static audio_hw_device_t *gen_play_wav_ahw_dev_ubus;
struct audio_stream_out *stream_out = NULL;
bool go_on_generate_play_wav = true;
unsigned int pcm_record_size = 0; //320:NB, 640:WB
unsigned int pcm_playback_size = 0; //320:NB, 640:WB
struct wav_parameters {
char Byte0;
char Byte1;
char *filename;
};
/////////////////////////////////////////////////////////////////////////////
static int config_parameters(int in_out)
{
unsigned int direction = 0xFF, type, srcdst, priority, dest;
char kvpair[128];
struct str_parms *param = NULL;
int data[5];
const char *key = NULL;
bool update_vcm = false;
direction = in_out;/* 0-play, 1-record */
type = 0;/* 0:PCM_NB_BUF_SIZE, 1:PCM_WB_BUF_SIZE */
srcdst = 1;/* 0-None, 1-Near end, 2-Far end, 3-Both ends */
priority = 1;/* 0-Do not combine(override), 1-Combine */
dest = 0;/* 0-Near codec, 1-Near Vocoder */
if(direction == 0){//output
if(type == 0)
pcm_playback_size = PCM_NB_BUF_SIZE;
else
pcm_playback_size = PCM_WB_BUF_SIZE;
printf("config playback parameters.\n");
}
else if(direction == 1){//input
if(type == 0)
pcm_record_size = PCM_NB_BUF_SIZE;
else
pcm_record_size = PCM_WB_BUF_SIZE;
printf("config record parameters.\n");
}
memset(kvpair, 0x00, sizeof(kvpair));
sprintf(kvpair, "%s=%d;%s=%d;%s=%d;%s=%d;%s=%d", VCM_CONFIG_DIRECTION, direction,
VCM_CONFIG_TYPE, type, VCM_CONFIG_SRC_DST, srcdst,
VCM_CONFIG_PRIORITY, priority, VCM_CONFIG_DEST, dest);
printf("%s: config information kvpair is %s.\n", __FUNCTION__, kvpair);
//extract the parameter and config from string
param = str_parms_create_str(kvpair);
if (!param) {
printf("%s: param create str is null!", __FUNCTION__);
return -1;
}
//set vcm configurations
key = VCM_CONFIG_DIRECTION;
if (str_parms_get_int(param, key, &data[0]) == 0) {
update_vcm = true;
str_parms_del(param, key);
}
key = VCM_CONFIG_TYPE;
if (str_parms_get_int(param, key, &data[1]) == 0) {
update_vcm = true;
str_parms_del(param, key);
}
key = VCM_CONFIG_SRC_DST;
if (str_parms_get_int(param, key, &data[2]) == 0) {
update_vcm = true;
str_parms_del(param, key);
}
key = VCM_CONFIG_PRIORITY;
if (str_parms_get_int(param, key, &data[3]) == 0) {
update_vcm = true;
str_parms_del(param, key);
}
key = VCM_CONFIG_DEST;
if (str_parms_get_int(param, key, &data[4]) == 0) {
update_vcm = true;
str_parms_del(param, key);
}
//printf("Direction is %d, Type is %d, Src_Dst is %d, Priority is %d, Dest is %d. \n",data[0], data[1], data[2], data[3], data[4]);
if (update_vcm) {
configure_vcm(data); /*TODO check if all inputs got all values successfully*/
}
return 0;
}
/*******************************************************************************\
* Function: generate_wav_thread
* Description:This function will main thread to generate special wav file and play
*
*
* NOTE:
sample rate is 8000
channel number is 1
bit of sample is 16
Due to modem limited, only support 8k/16k sample rate
* Returns: void
\*******************************************************************************/
static void generate_wav_thread(void *arg)
{
int rc, fd;
char buffer[PCM_WB_BUF_SIZE] = {0};
unsigned int frames = 0;
struct wav_header header;
char *path = NULL;
unsigned int i = 0;
struct wav_parameters *input_wav_info = NULL;
char output_buffer[1024] = {0};//The largest len is 1024.
input_wav_info = (struct wav_parameters *)arg;
printf("input_wav_info is 0x%02x 0x%02x %s.\n",
input_wav_info->Byte0, input_wav_info->Byte1, input_wav_info->filename);
path = input_wav_info->filename;
header.riff_id = ID_RIFF;
header.riff_sz = 0;
header.riff_fmt = ID_WAVE;
header.fmt_id = ID_FMT;
header.fmt_sz = 16;
header.audio_format = 1; //FORMAT_PCM;
header.num_channels = 1; //Modem ONLY support mono recording
header.sample_rate = (pcm_record_size == PCM_NB_BUF_SIZE ? 8000 : 16000);
header.bits_per_sample = 16; //PCM_SAMPLEBITS_S16_LE;
header.byte_rate = (header.bits_per_sample / 8) * header.num_channels * header.sample_rate;
header.block_align = header.num_channels * (header.bits_per_sample / 8);
header.data_id = ID_DATA;
printf("enter generate_wav_thread.\n");
//Must set vcm_configure before pcmloopback
if((pcm_record_size != PCM_NB_BUF_SIZE) && (pcm_record_size != PCM_WB_BUF_SIZE)){
printf("%s: Please use vcm_configure to set pcm_record_size!!\n", __FUNCTION__);
return;
}
//Must set vcm_configure before playback
if((pcm_playback_size != PCM_NB_BUF_SIZE) && (pcm_playback_size != PCM_WB_BUF_SIZE)){
printf("%s: Please use vcm_configure to set pcm_playback_size!!\n", __FUNCTION__);
return;
}
//Must set vcm_configure before playback
if(pcm_record_size != pcm_playback_size){
printf("%s: Please configure pcm_record_size = pcm_playback_size!!\n", __FUNCTION__);
return;
}
i = 0;
//initiate the buffer with pointed data.
while(i < pcm_record_size) {
buffer[i] = input_wav_info->Byte0;
buffer[i+1] = input_wav_info->Byte1;
i += 2;
}
if (PCM_NB_BUF_SIZE == pcm_record_size) {
/* NB */
for (i=0; i<pcm_record_size; i++) {
snprintf(output_buffer, sizeof(output_buffer), "%s %02x", output_buffer, *(buffer+i));
}
printf("NB data is: %s.\n", output_buffer);
} else if ( PCM_WB_BUF_SIZE == pcm_record_size) {
/* if WB, len=640, the string len is bigger than 1024, so need to output two times. */
/* The first segment */
for (i=0; i<pcm_record_size/2; i++) {
snprintf(output_buffer, sizeof(output_buffer), "%s %02x", output_buffer, *(buffer+i));
}
printf("WB data 1 is: %s.\n", output_buffer);
/* The second segment */
memset(output_buffer, 0x00, sizeof(output_buffer));
for (i=0; i<pcm_record_size/2; i++) {
snprintf(output_buffer, sizeof(output_buffer), "%s %02x", output_buffer, *(buffer+320+i));
}
printf("WB data 2 is: %s.\n", output_buffer);
}
fd = open(path, O_WRONLY | O_CREAT);
if (fd < 0) {
printf("%s: error opening file %s!\n", __FUNCTION__, path);
return;
}
//leave enough room for header
lseek(fd, sizeof(struct wav_header), SEEK_SET);
//open the audiostub_ctl, prepare for record and playback
VCMInit();
//open playback stream
rc = gen_play_wav_ahw_dev_ubus->open_output_stream(gen_play_wav_ahw_dev_ubus, 0,
gen_play_wav_ahw_dev_ubus->get_supported_devices(gen_play_wav_ahw_dev_ubus),
AUDIO_OUTPUT_FLAG_DIRECT, NULL, &stream_out, 0);
if (rc < 0) {
printf("%s: error opening output device. rc = %d!\n", __FUNCTION__, rc);
goto bad_stream;
}
printf("%s: starting generate wav %d bytes every 20ms!\n", __FUNCTION__, pcm_record_size);
go_on_generate_play_wav = true;
while (go_on_generate_play_wav) {
//for debug:save into file
rc = write(fd, buffer, pcm_record_size);
if (rc < 0) {
printf("%s: error writing to file!\n", __FUNCTION__);
goto end_generate_wav;
} else if (rc < (int)pcm_record_size) {
printf("%s: wrote less the buffer size!\n", __FUNCTION__);
goto end_generate_wav;
}
//playback the needed format stream to device.
//only write pcm stream, no send command.
rc = stream_out->write(stream_out, buffer, pcm_record_size);
if (rc < 0) {
printf("%s: error writing (child)!\n", __FUNCTION__);
goto end_generate_wav;
} else if (rc < (int)pcm_record_size) {
printf("%s: wrote less than buffer size!\n", __FUNCTION__);
goto end_generate_wav;
}
// printf("%s: playback to Dev len is %d.\n", __FUNCTION__, rc);
printf("%s: No.%d frame loopback!\n", __FUNCTION__, ++frames);
}
end_generate_wav:
//write header now all information is known
header.data_sz = pcm_record_size * frames;
header.riff_sz = header.data_sz + sizeof(header) - 8;
lseek(fd, 0, SEEK_SET);
write(fd, &header, sizeof(struct wav_header));
printf("%s: save wave file:%s. sample_rate = %d, num_channels = %d!\n", __FUNCTION__, path, header.sample_rate, header.num_channels);
stream_out->common.standby(&stream_out->common);
gen_play_wav_ahw_dev_ubus->close_output_stream(gen_play_wav_ahw_dev_ubus, stream_out);
VCMDeinit();//close the fd of audiostub_ctl when exit the thread.
go_on_generate_play_wav = false;
bad_stream:
if (close(fd))
printf("%s: error closing file!\n", __FUNCTION__);
printf("%s: finished!\n", __FUNCTION__);
printf("exit generate_wav_thread!\n");
return;
}
static void sig_handler(int sig)
{
go_on_generate_play_wav = false;
printf("output signal number: %d.\n", sig);
}
/*******************************************************************************\
* Function: main
\*******************************************************************************/
int main(int argc, char **argv)
{
pthread_t thread_start;
int rc = 0;
struct wav_parameters wav_info;
int first_byte = 0;
int second_byte = 0;
if (argc < 4) {
fprintf(stderr, "Usage: %s 16_smaple_bits_first_BYTE 16_smaple_bits_second_BYTE file.wav.\n", argv[0]);
printf("For example: %s 0xAA 0x55 AA55.wav.\n", argv[0]);
return 1;
}
printf("command line is: %s %s %s %s.\n",
argv[0], argv[1], argv[2], argv[3]);
first_byte = strtol(argv[1], NULL, 16);
second_byte = strtol(argv[2], NULL, 16);
printf("first_byte=0x%02x, second_byte=0x%02x.\n", first_byte, second_byte);
memset(&wav_info, 0x00, sizeof(struct wav_parameters));
wav_info.Byte0 = first_byte;
wav_info.Byte1 = second_byte;
wav_info.filename = argv[3];
printf("wav_info is 0x%02x 0x%02x %s.\n",
wav_info.Byte0, wav_info.Byte1, wav_info.filename);
/* install signal handler and begin to capture signal for close */
signal(SIGINT, sig_handler);
signal(SIGTERM, sig_handler);
//init global variables
gen_play_wav_ahw_dev_ubus = audio_hal_install();
if (gen_play_wav_ahw_dev_ubus == NULL) {
printf("%s: audio_hal_install failed!\n", __FUNCTION__);
exit (-1);
}
//The following config parameters are needed for main thread.
//config record parameters.
config_parameters(1);
//config playback parameters.
config_parameters(0);
rc = pthread_create(&thread_start, NULL, (void *)&generate_wav_thread, &wav_info);
if (rc < 0) {
printf("%s: error creating thread_start!\n", __FUNCTION__);
rc = UBUS_STATUS_PERMISSION_DENIED;
}
if (pthread_join(thread_start, NULL)){
printf("error join thread!\n");
abort();
}
audio_hal_uninstall();
return 0;
}