第一次写C++程序,完成使用Alsa采集linux音频

利用alsa 完成linux下音频采集,第一次用C++写程序,很菜,很水,记录下当时的水平,以后肯定会有提高,算是一个纪念吧

1、运行时前先装库,sudo apt-get install libalsa
2遍tab自动提示出库文件,选择库,alsa-ocaml-dev,最终的命令行为:sudo apt-get install libalsa-ocaml-dev
2、编译执行脚本,gcc -o main main.c -lasound;
3、执行 ./main
以下是代码`#define LSA_PCM_NEW_HW_PARAMS_API
#include <stdlib.h>
#include <stdio.h>
#include <alsa/asoundlib.h>
#include <signal.h>
static int recording;

void stop_record(int param)
{
recording = 0;
}
void InitCapture(snd_pcm_t ** handle,snd_pcm_hw_params_t ** params,snd_pcm_uframes_t* frames,char ** buffer,int* size)
{
int ret;
unsigned int val;
int dir;
//打开设备
ret = snd_pcm_open(handle, “default”, SND_PCM_STREAM_CAPTURE, 0);
printf(“after open file\n”);
if (ret < 0)
{
fprintf(stderr, “unable to open device:%s\n”, snd_strerror(ret));
exit(1);
}
//分配一个硬件参数结构体
snd_pcm_hw_params_alloca(params);

//使用默认参数
snd_pcm_hw_params_any(*handle, *params);

//翻译
    snd_pcm_hw_params_set_access(*handle, *params, SND_PCM_ACCESS_RW_INTERLEAVED);

//S16小端
snd_pcm_hw_params_set_format(*handle, *params, SND_PCM_FORMAT_S16_LE);

//双通道,立体声
snd_pcm_hw_params_set_channels(*handle, *params, 2);

//采样率
val = 44100;
snd_pcm_hw_params_set_rate_near(*handle, *params,&val,&dir);

*frames = 32;
snd_pcm_hw_params_set_period_size_near(*handle, *params,frames,&dir);

//参数生效
ret = snd_pcm_hw_params(*handle, *params);
if (ret<0)
{
	fprintf(stderr, "unable to set hw parameters:%s\n", snd_strerror(ret));
		exit(1);
}
//得到一个周期的数据大小
snd_pcm_hw_params_get_period_size(*params, frames, &dir);

//16位双通道,16位为2字节,2字节*2通道=4,假如frames=1024,则size是1024*4 = 4096
*size = *frames * 4;
*buffer = (char*)malloc(*size); 

//设置一个周期的时间长度
snd_pcm_hw_params_get_period_time(*params, &val, &dir);

}

void CaptureAudio(snd_pcm_t ** handle,snd_pcm_uframes_t* frames,char ** buffer,int* size,FILE ** pFile)
{
int ret;

recording = 1;
while (recording)
{
	ret = snd_pcm_readi(*handle, *buffer,*frames);
	if (ret == -EPIPE )
	{
		fprintf(stderr, "overrun occurred\n");
		snd_pcm_prepare(*handle);
	}
	else if (ret < 0)
	{
		fprintf(stderr, "error from read\n");
		snd_strerror(ret);
	}
	else if (ret != (int)(*frames))
	{
		fprintf(stderr, "short read %d frames\n",ret);
	}
	printf("write to file......%d\n", *size);
	//写到标准输出中去
	ret = fwrite(*buffer, sizeof(char), *size, *pFile);
	if (ret != *size)
	{
		fprintf(stderr, "short write :write %d bytes\n", ret);
	}
	if (signal(SIGINT, stop_record)==SIG_ERR)
	{
		fprintf(stderr, "signal failed\n");
	}
}

}

void CloseCaptureDevice(FILE ** pFile,snd_pcm_t ** handle,char ** buffer)
{
printf(“write file exit\n”);
snd_pcm_drain(*handle);
snd_pcm_close(*handle);
free(*buffer);
fclose(*pFile);
}

int main()
{
FILE * pFile;
pFile = fopen(“test.pcm”, “wb”);
int size;
//给文件操作分配一个句柄
snd_pcm_t * handle;
//硬件参数
snd_pcm_hw_params_t * params;
//
snd_pcm_uframes_t frames;
char * buffer;
printf(“before open file\n”);
InitCapture(&handle,&params,&frames,&buffer,&size);
CaptureAudio(&handle,&frames,&buffer,&size,&pFile);
CloseCaptureDevice(&pFile,&handle,&buffer);
printf(“write file exit\n”);

return 0;

}
`

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用alsa-lib编一个音频采集程序,需要以下步骤: 1. 包含alsa/asoundlib.h头文件。 2. 打开ALSA音频设备,使用snd_pcm_open函数打开音频设备。 3. 配置音频设备,使用snd_pcm_hw_params函数配置音频设备参数,例如采样率、通道数、采样格式等。 4. 分配音频缓冲区,使用snd_pcm_hw_params_get_period_size函数获取音频缓冲区大小,使用snd_pcm_malloc_frames函数分配音频缓冲区。 5. 循环读取音频数据,使用snd_pcm_readi函数从音频设备读取音频数据,将数据存储到音频缓冲区中。 6. 处理音频数据,对音频数据进行处理。 7. 关闭音频设备,使用snd_pcm_close函数关闭音频设备。 以下是一个简单的音频采集程序示例: ```c #include <stdio.h> #include <stdlib.h> #include <alsa/asoundlib.h> #define SAMPLE_RATE 44100 #define CHANNELS 2 #define SAMPLE_SIZE 2 int main() { int err; snd_pcm_t *capture_handle; snd_pcm_hw_params_t *hw_params; snd_pcm_format_t format = SND_PCM_FORMAT_S16_LE; // 打开音频设备 if ((err = snd_pcm_open(&capture_handle, "default", SND_PCM_STREAM_CAPTURE, 0)) < 0) { printf("Unable to open PCM device: %s\n", snd_strerror(err)); exit(1); } // 配置音频设备 snd_pcm_hw_params_malloc(&hw_params); snd_pcm_hw_params_any(capture_handle, hw_params); snd_pcm_hw_params_set_access(capture_handle, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED); snd_pcm_hw_params_set_format(capture_handle, hw_params, format); snd_pcm_hw_params_set_channels(capture_handle, hw_params, CHANNELS); unsigned int rate = SAMPLE_RATE; snd_pcm_hw_params_set_rate_near(capture_handle, hw_params, &rate, 0); snd_pcm_hw_params_set_period_size(capture_handle, hw_params, 1024, 0); snd_pcm_hw_params(capture_handle, hw_params); snd_pcm_hw_params_free(hw_params); // 分配音频缓冲区 snd_pcm_uframes_t frames = 1024; char *buffer = malloc(frames * CHANNELS * SAMPLE_SIZE); // 循环读取音频数据 while (1) { if ((err = snd_pcm_readi(capture_handle, buffer, frames)) != frames) { printf("Read error: %s\n", snd_strerror(err)); } // 处理音频数据 // ... // 输出音频数据 fwrite(buffer, 1, frames * CHANNELS * SAMPLE_SIZE, stdout); } // 关闭音频设备 snd_pcm_close(capture_handle); free(buffer); return 0; } ``` 在上面的示例中,使用snd_pcm_open函数打开默认的PCM音频设备,使用snd_pcm_hw_params函数配置音频设备参数,使用snd_pcm_readi函数读取音频数据,将音频数据入stdout。在实际使用中,可以根据需要修改音频设备参数和处理音频数据的代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值