linux下编写本地录音和播放demo测试代码:
1. 通过读.wav格式的音频流,将音频流写入DMA缓存区,实现语音播放;
2. 通过读DMA缓存区的音频流,实现录音。
首先要配置硬件参数,包括设置采样位数、通道数、采样率等,然后向DMA缓存区写或者读,实现播放和录音,具体见下文:
/*************************************************************************
> File Name: local_player.c
> Desc. : Read from file and playback, or capture and write to file.
> Author : horistudio@163.com
> Date : 2020/05/26
************************************************************************/
/* Use the newer ALSA API */
#define ALSA_PCM_NEW_HW_PARAMS_API
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <alsa/asoundlib.h>
int main(int argc, char *argv[]) {
#if 1
int rc;
int size;
snd_pcm_t *handle;
snd_pcm_hw_params_t *params;
unsigned int val;
int dir;
snd_pcm_uframes_t frames;
char *buffer;
#if 0
/* Open PCM device for playback. */
rc = snd_pcm_open(&handle, "default",
SND_PCM_STREAM_PLAYBACK, 0);
#endif
/* Open PCM device for capture. */
rc = snd_pcm_open(&handle, "default",
SND_PCM_STREAM_CAPTURE, 0);
if (rc < 0) {
fprintf(stderr,
"unable to open pcm device: %s/n",
snd_strerror(rc));
exit(1);
}
/* Allocate a hardware parameters object. */
snd_pcm_hw_params_alloca(¶ms);
/* Fill it in with default values. */
snd_pcm_hw_params_any(handle, params);
/* Set the desired hardware parameters. */
/* Interleaved mode */
snd_pcm_hw_params_set_access(handle, params,
SND_PCM_ACCESS_RW_INTERLEAVED);
/* Signed 16-bit little-endian format */
snd_pcm_hw_params_set_format(handle, params,
SND_PCM_FORMAT_S16_LE);
/* Two channels (stereo) */
snd_pcm_hw_params_set_channels(handle, params, 8);
/* 44100 bits/second sampling rate (CD quality) */
val = 48000;
snd_pcm_hw_params_set_rate_near(handle, params,
&val, &dir);
/* Set period size to 32 frames. */
frames = 32;
snd_pcm_hw_params_set_period_size_near(handle,
params, &frames, &dir);
/* Write the parameters to the driver */
rc = snd_pcm_hw_params(handle, params);
if (rc < 0) {
fprintf(stderr,
"unable to set hw parameters: %s/n",
snd_strerror(rc));
exit(1);
}
/* Use a buffer large enough to hold one period */
snd_pcm_hw_params_get_period_size(params, &frames,
&dir);
size = frames * 4; /* 2 bytes/sample, 2 channels */
buffer = (char *) malloc(size);
printf("------>frames:%d, size:%d\n",frames,size);
#endif
#if 1
//************************[3.文件操作]*******************************
#if 0
/*通过主函数传参,传入.wav格式文件名*/
FILE *fp_r = NULL;
int ret = 0;
fp_r = fopen(argv[1], "rb");
if(fp_r == NULL) {
printf("open file fail!\n");
exit(1);
}
#endif
/* 打开一个文件,将录音音频流写入文件 */
FILE *fp = NULL;
int file_len = 0;
fp = fopen("record_dump.raw", "a+");
if(fp == NULL) {
printf("open file fail!\n");
exit(1);
}
printf("Configurations are finished......\n");
while (1) {
/* 1.read from file and playback */
#if 0
//read audio file
memset(buffer,0,size);
rc = fread(buffer, 1, size, fp_r);
if (rc == 0) {
fprintf(stderr, "end of file on input/n");
break;
} else if (rc != size) {
fprintf(stderr,
"short read: read %d bytes/n", rc);
}
rc = snd_pcm_writei(handle, buffer, frames);
if (rc == -EPIPE) {
/* EPIPE means underrun */
fprintf(stderr, "underrun occurred/n");
snd_pcm_prepare(handle);
} else if (rc < 0) {
fprintf(stderr,
"error from writei: %s/n",
snd_strerror(rc));
} else if (rc != (int)frames) {
fprintf(stderr,
"short write, write %d frames/n", rc);
}
#endif
/* 2.capture and write to file */
#if 1
/* capture */
memset(buffer, 0, size);
rc = snd_pcm_readi(handle, buffer, frames);
printf("snd_pcm_readi,frames:%d, buffer:%s\n",frames, buffer);
if (rc == -EPIPE)
{
/* EPIPE means overrun */
fprintf(stderr, "overrun occurred\n");
snd_pcm_prepare(handle);
} else if (rc < 0) {
fprintf(stderr,
"error from read: %s\n",
snd_strerror(rc));
} else if (rc != (int)frames) {
fprintf(stderr,
"short read, read %d frames\n", rc);
}
if (fp) {
file_len = fwrite(buffer, 1, size, fp);
printf("fwrite:%d bytes \n", file_len);
} else {
printf("%s[%d] file open fail\n",__func__,__LINE__);
}
#endif
}
#endif
fclose(fp);
//close(fd_f);
snd_pcm_drain(handle);
snd_pcm_close(handle);
free(buffer);
return 0;
}
*上述代码,可以根据需要解开注释或者打上注释,分别测试录音或者播放
编译时,需要引入alsa库,-L指定具体的alsa-utils-x.x.x类库的asound等,编译方法方法:
gcc -o local_player local_player.c -L XXX/alsa-utils-1.1.5 -lasound -lm -ldl
测试方法:
播放 通过喇叭是否正常播放音频文件,确认是否播放成功;
录音 通过dump出来的文件,用audiocity或其他软件打开并播放,确认录音是否正常
* 上文代码的意义是可以问语音通话做准备,读取打开的.wav音频流其实是模拟读取网络发来的音频流buffer,将录音音频流写入文件是模拟向网络发送buffer写入