You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

331 lines
8.1 KiB

/*
* Copyright (c) Hisilicon Technologies Co., Ltd. 2019-2020. All rights reserved.
* Description: mix engine sample
* Author: audio
* Create: 2019-05-19
*/
#include <stdio.h>
#include <string.h>
#include <pthread.h>
#include <unistd.h>
#include "uapi_sound.h"
#include "securec.h"
#include "uapi_system.h"
#include "adp_common_ext.h"
#define RATIO 1000
#define INPUT_CMD_LENGTH 32
typedef struct {
uapi_snd snd;
td_bool geq_enable;
td_u32 select_band;
uapi_snd_geq_attr geq_attr;
td_s32 argc;
td_char *argv[INPUT_CMD_LENGTH];
} sample_geq_ctx;
static td_void sample_geq_print_attr(sample_geq_ctx *geq_ctx)
{
td_u32 i;
td_s32 ret;
ret = uapi_snd_get_geq_enable(geq_ctx->snd, &geq_ctx->geq_enable);
if (ret != TD_SUCCESS) {
printf("call uapi_snd_get_geq_enable failed(0x%x)\n", ret);
return;
}
ret = uapi_snd_get_geq_attr(geq_ctx->snd, &geq_ctx->geq_attr);
if (ret != TD_SUCCESS) {
printf("call uapi_snd_get_geq_attr failed(0x%x)\n", ret);
return;
}
printf("==========================================\n");
printf("-geq_enable: %s\n", (geq_ctx->geq_enable == TD_TRUE) ? "yes" : "no");
printf("-band_num: %d\n", geq_ctx->geq_attr.band_num);
printf("%4s | %-6s | %-5s |\n",
"band", "freq", "gain");
for (i = 0; i < geq_ctx->geq_attr.band_num; i++) {
printf("%4d | %-6d %-5d |\n",
i,
geq_ctx->geq_attr.param[i].freq,
geq_ctx->geq_attr.param[i].gain);
}
printf("-select_band: %d\n", geq_ctx->select_band);
printf("\n==========================================\n");
}
static td_void sample_geq_deinit(const sample_geq_ctx *geq_ctx)
{
td_s32 ret;
(td_void)geq_ctx;
ret = uapi_snd_deinit();
if (ret != TD_SUCCESS) {
printf("call uapi_snd_deinit failed(0x%x)\n", ret);
return;
}
}
static td_s32 sample_geq_init(sample_geq_ctx *geq_ctx)
{
td_s32 ret;
ret = memset_s(geq_ctx, sizeof(*geq_ctx), 0, sizeof(sample_geq_ctx));
if (ret != EOK) {
printf("call memset_s failed(0x%x)\n", ret);
return ret;
}
geq_ctx->snd = UAPI_SND_0;
ret = uapi_snd_init();
if (ret != TD_SUCCESS) {
printf("call uapi_snd_init failed(0x%x)\n", ret);
return ret;
}
ret = uapi_snd_get_geq_enable(geq_ctx->snd, &geq_ctx->geq_enable);
if (ret != TD_SUCCESS) {
printf("call uapi_snd_get_geq_enable failed(0x%x)\n", ret);
goto out;
}
ret = uapi_snd_get_geq_attr(geq_ctx->snd, &geq_ctx->geq_attr);
if (ret != TD_SUCCESS) {
printf("call uapi_snd_get_geq_attr failed(0x%x)\n", ret);
goto out;
}
return TD_SUCCESS;
out:
sample_geq_deinit(geq_ctx);
return ret;
}
static td_void sample_geq_set_attr(sample_geq_ctx *geq_ctx)
{
td_s32 ret;
ret = uapi_snd_set_geq_enable(geq_ctx->snd, geq_ctx->geq_enable);
if (ret != TD_SUCCESS) {
printf("call uapi_snd_set_geq_enable failed(0x%x)\n", ret);
}
ret = uapi_snd_set_geq_attr(geq_ctx->snd, &geq_ctx->geq_attr);
if (ret != TD_SUCCESS) {
printf("call uapi_snd_set_geq_attr failed(0x%x)\n", ret);
}
sample_geq_print_attr(geq_ctx);
}
static td_void sample_geq_print_memu(td_void)
{
printf("==========================================\n");
printf("input q to quit\n");
printf("support option:\n");
printf(
"\t-p: print geq attr\n"
"\t-e: enable geq\n"
"\t-d: disable geq\n"
"\t-b: set total num of banks [1, 10]\n"
"\t-n: select bank to change attr [0, 9]\n"
"\t-f: freq\n"
"\t-g: gain(-15000,15000,step by 125)\n"
);
printf("==========================================\n");
}
static td_s32 sample_geq_parse_attr(sample_geq_ctx *geq_ctx, td_char opt, td_char *opt_arg)
{
if (opt_arg == TD_NULL) {
printf("invalid option -- '%c'\n", opt);
sample_geq_print_memu();
return TD_FAILURE;
}
if (geq_ctx->geq_attr.band_num > UAPI_SND_EQ_BAND_MAX) {
printf("invalid band_num -- '%d'\n", geq_ctx->geq_attr.band_num);
sample_geq_print_memu();
return TD_FAILURE;
}
if (geq_ctx->select_band >= UAPI_SND_EQ_BAND_MAX) {
printf("invalid select_band -- '%d'\n", geq_ctx->select_band);
sample_geq_print_memu();
return TD_FAILURE;
}
switch (opt) {
case 'b':
geq_ctx->geq_attr.band_num = (td_u32)strtol(opt_arg, NULL, 10); /* 10 is Dec */
break;
case 'n':
geq_ctx->select_band = (td_u32)strtol(opt_arg, NULL, 10); /* 10 is Dec */
break;
case 'f':
geq_ctx->geq_attr.param[geq_ctx->select_band].freq = (td_u32)strtol(opt_arg, NULL, 10); /* 10 is Dec */
break;
case 'g':
geq_ctx->geq_attr.param[geq_ctx->select_band].gain = (td_u32)strtol(opt_arg, NULL, 10); /* 10 is Dec */
break;
default:
printf("invalid option -- '%c'\n", opt);
sample_geq_print_memu();
return TD_FAILURE;
}
return TD_SUCCESS;
}
static td_u32 sample_geq_parse_opt(sample_geq_ctx *geq_ctx, td_char opt, td_char *opt_arg)
{
td_s32 ret;
switch (opt) {
case 'h' | 'H':
sample_geq_print_memu();
return INPUT_CMD_LENGTH;
case 'p' | 'P':
sample_geq_print_attr(geq_ctx);
return INPUT_CMD_LENGTH;
case 'e':
geq_ctx->geq_enable = TD_TRUE;
return 1;
case 'd':
geq_ctx->geq_enable = TD_FALSE;
return 1;
default:
break;
}
ret = sample_geq_parse_attr(geq_ctx, opt, opt_arg);
if (ret != TD_SUCCESS) {
return INPUT_CMD_LENGTH;
}
return 2; /* consume 2 args */
}
static td_void sample_geq_cmd_line_to_arg(sample_geq_ctx *geq_ctx, td_char *cmd, td_u32 cmd_size)
{
td_u32 i;
geq_ctx->argc = 0;
geq_ctx->argv[0] = TD_NULL;
if ((*cmd == '\0') || (*cmd == '\n')) {
sample_geq_print_memu();
return;
}
for (i = 0; i < cmd_size; i++) {
if ((cmd[i] == ' ') || (cmd[i] == '\n')) {
cmd[i] = '\0';
}
}
for (i = 0; i < cmd_size; i++) {
if ((i == 0) && (cmd[i] != '\0')) {
geq_ctx->argv[geq_ctx->argc] = cmd + i;
geq_ctx->argc++;
}
if ((i > 0) && (cmd[i - 1] == '\0') && (cmd[i] != '\0')) {
geq_ctx->argv[geq_ctx->argc] = cmd + i;
geq_ctx->argc++;
}
}
}
static td_void sample_geq_parse_cmd_line(sample_geq_ctx *geq_ctx, td_char *cmd, td_u32 cmd_size)
{
td_s32 i;
td_u32 j;
geq_ctx->argc = 0;
sample_geq_cmd_line_to_arg(geq_ctx, cmd, cmd_size);
if (geq_ctx->argc == 0) {
return;
}
i = 0;
while (1) {
if (i >= geq_ctx->argc) {
break;
}
j = sample_geq_parse_opt(geq_ctx, *(geq_ctx->argv[i]), geq_ctx->argv[i + 1]);
if (j == INPUT_CMD_LENGTH) {
return;
}
i += j;
}
sample_geq_set_attr(geq_ctx);
}
td_s32 main(td_s32 argc, td_char *argv[])
{
td_s32 ret;
sample_geq_ctx geq_ctx;
td_char input_cmd[INPUT_CMD_LENGTH];
(td_void)argc;
(td_void)argv;
ret = uapi_sys_init();
if (ret != TD_SUCCESS) {
printf("call uapi_sys_init failed(0x%x)\n", ret);
return ret;
}
ret = sample_geq_init(&geq_ctx);
if (ret != TD_SUCCESS) {
printf("call sample_geq_init failed(0x%x)\n", ret);
goto out;
}
sample_geq_print_attr(&geq_ctx);
sample_geq_print_memu();
while (1) {
if (memset_s(input_cmd, sizeof(input_cmd),
0, INPUT_CMD_LENGTH) != EOK) {
break;
}
(td_void)fgets((char *)(input_cmd), (sizeof(input_cmd) - 1), stdin);
if (input_cmd[0] == 'q') {
printf("Prepare to quit!\n");
break;
}
input_cmd[INPUT_CMD_LENGTH - 1] = '\0';
sample_geq_parse_cmd_line(&geq_ctx, input_cmd, INPUT_CMD_LENGTH);
}
sample_geq_deinit(&geq_ctx);
out:
ret = uapi_sys_deinit();
if (ret != TD_SUCCESS) {
printf("call uapi_sys_deinit failed(0x%x)\n", ret);
return ret;
}
return 0;
}