diff --git a/cplusplus/level1_single_api/6_media/1_audio/audio_gitee/sample_audio.c b/cplusplus/level1_single_api/6_media/1_audio/audio_gitee/sample_audio.c index 86960a03bfba1d6800f614bbf9b627ba7d50b7b1..ee6828c9efd28cc90cfb9d4404464182728925d2 100644 --- a/cplusplus/level1_single_api/6_media/1_audio/audio_gitee/sample_audio.c +++ b/cplusplus/level1_single_api/6_media/1_audio/audio_gitee/sample_audio.c @@ -17,6 +17,7 @@ #include #include #include +#include #include "sample_comm_audio.h" #define sample_dbg(ret) \ @@ -54,12 +55,12 @@ hi_s32 sample_comm_sys_init(void) static hi_void sample_audio_aio_init_param(hi_aio_attr *aio_attr) { - aio_attr->chn_cnt = 1; + aio_attr->chn_cnt = 2; aio_attr->point_num_per_frame = 960; aio_attr->expand_flag = 0; aio_attr->frame_num = 30; aio_attr->clk_share = 1; - aio_attr->snd_mode = HI_AUDIO_SOUND_MODE_MONO; + aio_attr->snd_mode = HI_AUDIO_SOUND_MODE_STEREO; aio_attr->sample_rate = HI_AUDIO_SAMPLE_RATE_48000; aio_attr->bit_width = HI_AUDIO_BIT_WIDTH_16; aio_attr->work_mode = HI_AIO_MODE_I2S_MASTER; @@ -118,11 +119,30 @@ hi_s32 sample_comm_audio_start_ai(hi_audio_dev ai_dev_id, hi_aio_attr *aio_attr, return ret; } - ret = hi_mpi_ai_enable_chn(ai_dev_id, 0); - if (ret) { - printf("%s: hi_mpi_ai_enable_chn(%d,%d) failed with %#x\n", __FUNCTION__, ai_dev_id, 0, ret); - return ret; + hi_s32 chn_cnt; + int i; + chn_cnt = aio_attr->chn_cnt >> ((hi_u32)aio_attr->snd_mode); + for (i = 0; i < (hi_s32)chn_cnt; i++) { + ret = hi_mpi_ai_enable_chn(ai_dev_id, i); + if (ret) { + printf("%s: hi_mpi_ai_enable_chn(%d,%d) failed with %#x\n", __FUNCTION__, ai_dev_id, i, ret); + return ret; + } } + + + // ret = hi_mpi_ai_enable_chn(ai_dev_id, 0); + // if (ret) { + // printf("%s: hi_mpi_ai_enable_chn(%d,%d) failed with %#x\n", __FUNCTION__, ai_dev_id, 0, ret); + // return ret; + // } + + // printf("aio_attr= ==================%d\n", aio_attr->chn_cnt); + // ret = hi_mpi_ai_enable_chn(ai_dev_id, 1); + // if (ret) { + // printf("%s: hi_mpi_ai_enable_chn(%d,%d) failed with %#x\n", __FUNCTION__, ai_dev_id, 1, ret); + // return ret; + // } return HI_SUCCESS; } @@ -225,7 +245,7 @@ static hi_s32 sample_audio_ai_init() return ret; } -static hi_s32 audio_ai_get_frame_and_send(sample_ai *ai_ctl, FILE *capture_fd) +static hi_s32 audio_ai_get_frame_and_send(sample_ai *ai_ctl, FILE *capture_fd1, FILE *capture_fd2) { hi_s32 ret; hi_audio_frame frame = {0}; @@ -238,7 +258,8 @@ static hi_s32 audio_ai_get_frame_and_send(sample_ai *ai_ctl, FILE *capture_fd) return HI_SUCCESS; } - fwrite(frame.virt_addr[0], 1, frame.len, capture_fd); + fwrite(frame.virt_addr[0], 1, frame.len, capture_fd1); + fwrite(frame.virt_addr[1], 1, frame.len, capture_fd2); /* finally you must release the stream */ ret = hi_mpi_ai_release_frame(ai_ctl->ai_dev, ai_ctl->ai_chn, &frame, &aec_frm); @@ -314,13 +335,52 @@ static hi_void sample_audio_play_thread(void *parg) ao_ctl->start = HI_FALSE; pthread_kill(&ao_ctl->ao_pid, 0); } +static hi_void sample_audio_combin_interwave(FILE *cap_fd1, FILE *cap_fd2) +{ + struct stat filestat; + int filesize1, filesize2, i; + fstat(fileno(cap_fd1), &filestat); + filesize1 = filestat.st_size; + + fstat(fileno(cap_fd2), &filestat); + filesize2 = filestat.st_size; + + hi_u8 *src_buf1 = (hi_u8 *)malloc(sizeof(char) *filesize1); + hi_u8 *src_buf2 = (hi_u8 *)malloc(sizeof(char) *filesize2); + + int len1 = fread(src_buf1, 1 ,filesize1, cap_fd1); + int len2 = fread(src_buf2, 1 ,filesize2, cap_fd2); + int len = len2 < len1 ? len2 : len1; + + hi_u8 *dest_buf = (hi_u8 *)malloc(sizeof(char) *len * 2 + 1); + + //16bit + hi_u32 *dest = (hi_u32 *)dest_buf; + hi_u16 *src1 = (hi_u16 *)src_buf1; + hi_u16 *src2 = (hi_u16 *)src_buf2; + + printf("len = %d\n", len); + for (i = 0; i < len / 2; i++) { + dest[i] = (hi_u32)(((hi_u32)(src1[i] << 16U)) | src2[i]); + // printf("dest[i] = 0x%u, src1 = 0x%u, src2 = 0x%x \n", dest[i], src_buf1[i], src_buf2[i]); + } + + + rewind(cap_fd1); + fwrite(dest_buf, 1, len * 2, cap_fd1); + + free(dest_buf); + free(src_buf1); + free(src_buf2); +} static hi_void sample_audio_capture_thread(void *parg) { hi_s32 ret; sample_ai *ai_ctl = (sample_ai *)parg; hi_char capture_file_name[FILE_NAME_LEN] = {0}; - FILE *cap_fd; + FILE *cap_fd1; + FILE *cap_fd2; ret = snprintf(capture_file_name, FILE_NAME_LEN, ai_ctl->file_name); if (ret < 0) { @@ -328,21 +388,32 @@ static hi_void sample_audio_capture_thread(void *parg) return NULL; } - cap_fd = fopen(capture_file_name, "w+"); - if (cap_fd == NULL) { + cap_fd1 = fopen(capture_file_name, "w+"); + if (cap_fd1 == NULL) { + printf("capture save file open failed!\n"); + return NULL; + } + + cap_fd2 = fopen("2.pcm", "w+"); + if (cap_fd2 == NULL) { printf("capture save file open failed!\n"); return NULL; } while (g_start == 1) { - ret = audio_ai_get_frame_and_send(ai_ctl, cap_fd); + ret = audio_ai_get_frame_and_send(ai_ctl, cap_fd1, cap_fd2); if (ret != HI_SUCCESS) { break; } } + rewind(cap_fd1); + rewind(cap_fd2); + sample_audio_combin_interwave(cap_fd1, cap_fd2); - fclose(cap_fd); - cap_fd = NULL; + fclose(cap_fd1); + cap_fd1 = NULL; + fclose(cap_fd2); + cap_fd2 = NULL; ai_ctl->start = HI_FALSE; @@ -450,4 +521,4 @@ hi_s32 main(int argc, char *argv[]) sample_comm_sys_exit(); printf("main finish!\n"); return ret; -} \ No newline at end of file +}