ALSA【一】

ALSA是Advanced Linux Sound Architecture 的缩写,目前已经成为了linux的主流音频体系结构。
在内核设备驱动层,ALSA提供了alsa-driver,同时在应用层,ALSA为我们提供了alsa-lib,应用程序只要调用alsa-lib提供的API,即可以完成对底层音频硬件的控制。
这里写图片描述

实现录音功能代码:

/*This example reads from the default PCM device
and writes to standard output for 5 seconds of data.*/
#include <alsa/asoundlib.h>
#include <math.h>#define BUFFERSIZE 4096
#define PERIOD_SIZE 1024
#define PERIODS 2
#define SAMPLE_RATE 16000
#define CHANNELS 2
#define FSIZE 2*CHANNELS/* Use the newer ALSA API */
#define ALSA_PCM_NEW_HW_PARAMS_APIint main(int argc, char *argv[]) {long loops; //define the record time.int rc;	//return code.int size;snd_pcm_t *handle;snd_pcm_hw_params_t *params;unsigned int val;int dir;snd_pcm_uframes_t frames;char *buffer;int err;char *file;int fd;if(argc ==2){file = argv[1];}else{file = "output.raw";}fd = open(file,O_WRONLY|O_CREAT,0777);if( fd ==-1){printf("open file:%s fail.\n",file);exit(1);}/* Open PCM device for recording (capture). */err = snd_pcm_open(&handle, "default",SND_PCM_STREAM_CAPTURE, 0);if (err < 0) {fprintf(stderr,"unable to open pcm device: %s\n",snd_strerror(err));exit(1);}/* Allocate a hardware parameters object. */snd_pcm_hw_params_alloca(&params);/* Fill it in with default values. */err=snd_pcm_hw_params_any(handle, params);if (err < 0) {fprintf(stderr, "Can not configure this PCM device: %s\n",snd_strerror(err));exit(1);}/* Set the desired hardware parameters. *//* Interleaved mode */err=snd_pcm_hw_params_set_access(handle, params,SND_PCM_ACCESS_RW_INTERLEAVED);if (err < 0) {fprintf(stderr,"Failed to set PCM device to interleaved: %s\n",snd_strerror(err));exit(1);}/* Signed 16-bit little-endian format */err=snd_pcm_hw_params_set_format(handle, params,SND_PCM_FORMAT_S16_LE);if (err < 0) {fprintf(stderr,"Failed to set PCM device to 16-bit signed PCM: %s\n",snd_strerror(err));exit(1);}/* One channels (mono) */err=snd_pcm_hw_params_set_channels(handle, params, CHANNELS);if (err < 0) {fprintf(stderr, "Failed to set PCM device to mono: %s\n",snd_strerror(err));exit(1);}/* 16000 bits/second sampling rate (CD quality) */val = SAMPLE_RATE;err=snd_pcm_hw_params_set_rate_near(handle, params,&val, &dir);if (err < 0) {fprintf(stderr, "Failed to set PCM device to sample rate =%d: %s\n",val,snd_strerror(err));exit(1);}/* Set buffer time 500000. */unsigned int buffer_time,period_time;snd_pcm_hw_params_get_buffer_time_max(params, &buffer_time, 0);if ( buffer_time >500000)buffer_time = 500000;period_time = buffer_time / 4;err = snd_pcm_hw_params_set_buffer_time_near(handle, params, &buffer_time, 0);if (err < 0) {fprintf(stderr, "Failed to set PCM device to buffer time =%d: %s\n",buffer_time,snd_strerror(err));exit(1);}err = snd_pcm_hw_params_set_period_time_near(handle, params, &period_time, 0);if (err < 0) {fprintf(stderr, "Failed to set PCM device to period time =%d: %s\n",period_time,snd_strerror(err));exit(1);}/* Write the parameters to the driver */err = snd_pcm_hw_params(handle, params);if (err < 0) {fprintf(stderr,"unable to set hw parameters: %s\n",snd_strerror(err));exit(1);}/* Use a buffer large enough to hold one period */snd_pcm_hw_params_get_period_size(params,&frames, &dir);size = frames * FSIZE; /* 2 bytes/sample, 1 channels */buffer = (char *) malloc(size);printf("period size = %d frames\n", (int)frames);printf("read buffer size = %d\n",size);/* We want to loop for 5 seconds */snd_pcm_hw_params_get_period_time(params,&val, &dir);printf("period time is: %d\n",val);loops = 100;/*print alsa config parameter*/snd_pcm_hw_params_get_buffer_time(params, &val, &dir);printf("buffer time = %d us\n", val);snd_pcm_hw_params_get_buffer_size(params, (snd_pcm_uframes_t *) &val);printf("buffer size = %d frames\n", val);snd_pcm_hw_params_get_periods(params, &val, &dir);printf("periods per buffer = %d frames\n", val);while (loops > 0) {loops--;rc = snd_pcm_readi(handle, buffer, frames);if (rc == -EPIPE) {// EPIPE means overrun fprintf(stderr, "overrun occurred\n");err=snd_pcm_prepare(handle);if( err <0){fprintf(stderr, "Failed to recover form overrun : %s\n",snd_strerror(err));exit(1);}}else if (rc < 0) {fprintf(stderr,"error from read: %s\n",snd_strerror(rc));exit(1);} else if (rc != (int)frames) {fprintf(stderr, "short read, read %d frames\n", rc);}rc = write(fd, buffer, size);if (rc <0){perror("fail to write to audio file\n");}}close(fd);snd_pcm_drain(handle);snd_pcm_close(handle);free(buffer);return 0;
}

实现播放声音代码:

#define ALSA_PCM_NEW_HW_PARAMS_API
//#include "alsahead.h"
#include <alsa/asoundlib.h>
#include <math.h>#define BUFFERSIZE 4096
#define PERIOD_SIZE 1024
#define PERIODS 2
#define SAMPLE_RATE 16000
#define CHANNELS 2
#define FSIZE 2*CHANNELS/*
*Usage: play audio.raw
*
*/int main(int argc, char *argv[]) {long loops;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;char *inFile;int fd;int err;if(argc ==2){inFile = argv[1];}else{inFile = "output.raw";}fd = open(inFile,O_RDONLY);rc = snd_pcm_open(&handle, "default",SND_PCM_STREAM_PLAYBACK, 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(&params);/* Fill it in with default values. */err=snd_pcm_hw_params_any(handle, params);if (err < 0) {fprintf(stderr, "Can not configure this PCM device: %s\n",snd_strerror(err));exit(1);}/* Set the desired hardware parameters. *//* Interleaved mode */err=snd_pcm_hw_params_set_access(handle, params,SND_PCM_ACCESS_RW_INTERLEAVED);if (err < 0) {fprintf(stderr,"Failed to set PCM device to interleaved: %s\n",snd_strerror(err));exit(1);}/* Signed 16-bit little-endian format */err=snd_pcm_hw_params_set_format(handle, params,SND_PCM_FORMAT_S16_LE);if (err < 0) {fprintf(stderr,"Failed to set PCM device to 16-bit signed PCM: %s\n",snd_strerror(err));exit(1);}/* One channels (mono) */err=snd_pcm_hw_params_set_channels(handle, params, CHANNELS);if (err < 0) {fprintf(stderr, "Failed to set PCM device to mono: %s\n",snd_strerror(err));exit(1);}/* 16000 bits/second sampling rate (CD quality) */val = SAMPLE_RATE;err=snd_pcm_hw_params_set_rate_near(handle, params,&val, &dir);if (err < 0) {fprintf(stderr, "Failed to set PCM device to sample rate =%d: %s\n",val,snd_strerror(err));exit(1);}/*frames = PERIOD_SIZE;snd_pcm_hw_params_set_period_size_near(handle,params, &frames, &dir);*//*snd_pcm_hw_params_get_buffer_time_max(params, &buffer_time, 0);period_time = buffer_time / 4;err=snd_pcm_hw_params_set_period_time_near(handle, params,&period_time, 0);if (err < 0) {fprintf(stderr,"Failed to set PCM device to period time =%u: %s\n",period_time,snd_strerror(err));exit(1);}*/unsigned int buffer_time,period_time;snd_pcm_hw_params_get_buffer_time_max(params, &buffer_time, 0);if ( buffer_time >500000)buffer_time = 500000;period_time = buffer_time / 4;err = snd_pcm_hw_params_set_buffer_time_near(handle, params, &buffer_time, 0);if (err < 0) {fprintf(stderr, "Failed to set PCM device to buffer time =%d: %s\n",buffer_time,snd_strerror(err));exit(1);}err = snd_pcm_hw_params_set_period_time_near(handle, params, &period_time, 0);if (err < 0) {fprintf(stderr, "Failed to set PCM device to period time =%d: %s\n",period_time,snd_strerror(err));exit(1);}rc = snd_pcm_hw_params(handle, params);if (rc < 0) {fprintf(stderr,"unable to set hw parameters: %s\n",snd_strerror(rc));exit(1);}snd_pcm_hw_params_get_period_size(params, &frames,&dir);size = frames * FSIZE;buffer = (char *) malloc(size);printf("period size = %d frames\n", (int)frames);printf("read buffer size = %d\n",size);snd_pcm_hw_params_get_period_time(params,&val, &dir);printf("period time is: %d\n",val);loops = 100;snd_pcm_hw_params_get_buffer_time(params, &val, &dir);printf("buffer time = %d us\n", val);snd_pcm_hw_params_get_buffer_size(params,(snd_pcm_uframes_t *) &val);printf("buffer size = %d frames\n", val);snd_pcm_hw_params_get_periods(params, &val, &dir);printf("periods per buffer = %d frames\n", val);while (loops > 0) {loops--;rc = read(fd, buffer, size);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) {fprintf(stderr, "underrun occurred\n");err=snd_pcm_prepare(handle);if( err <0){fprintf(stderr, "can not recover from underrun: %s\n",snd_strerror(err));}} 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);}}snd_pcm_drain(handle);snd_pcm_close(handle);free(buffer);close(fd);return 0;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/385664.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

java接口的定义与实现,学习路线+知识点梳理

Spring框架自诞生以来一直备受开发者青睐&#xff0c;有人亲切的称之为&#xff1a;Spring 全家桶。Spring更是避免了重复造轮子的工作并跟随着互联网行业的发展做出不断的更新&#xff0c;很多研发人员把spring看作心目中最好的Java项目&#xff0c;没有之一。 **可以毫不夸张…

第3章 文件IO | 001 文件描述符

概述 在Linux系统中一切皆可以看成是文件&#xff0c;文件又可分为&#xff1a;普通文件、目录文件、链接文件和设备文件。文件描述符&#xff08;file descriptor&#xff09;是内核为了高效管理已被打开的文件所创建的索引&#xff0c;其是一个非负整数&#xff08;通常是小整…

java提取图片中的文字,深入分析

第一个暴击&#xff1a;Spring 上一份Spring的手绘思维脑图&#xff08;就像是个知识大纲总结&#xff09;&#xff0c;预览一下Spring的知识点&#xff0c;心里有个谱。不过这边我是采用的截图方式&#xff0c;为了把全部的内容都截取出来&#xff0c;所以整个就比较小&#…

java基础入门传智播客答案,GitHub已标星16k

选择 在现在这个浮躁而又拜金的社会&#xff0c;我相信很多人做技术并非出于热爱&#xff0c;只是被互联网的高薪吸引&#xff0c;毕竟技术岗位非常枯燥&#xff0c;不仅要面对奇奇怪怪的需求&#xff0c;还要不停的充实自己避免被淘汰。所以想要吃好技术这碗饭并不容易。 我…

java基础入门第二版二手,细节爆炸

一面&#xff1a;70分钟 突击电话面试 正思考着项目功能模块&#xff0c;阿里面试官打来了电话&#xff0c;开始了阿里一面。 阿里面试官自我介绍&#xff0c;介绍了5分钟左右&#xff0c;部门的情况&#xff0c;主要的业务 提问开始 会哪些操作系统 Linux会一点说一下操作指…

进程通讯:管道

管道&#xff0c;通常指无名管道&#xff0c;是 UNIX 系统IPC最古老的形式。 1、特点&#xff1a; 它是半双工的&#xff08;即数据只能在一个方向上流动&#xff09;&#xff0c;具有固定的读端和写端。它只能用于具有亲缘关系的进程之间的通信&#xff08;也是父子进程或者兄…

java基础入门答案谭晓芳,原理+实战讲解

One&#xff1a;JVM实践思维图&#xff08;完整版&#xff09; Two&#xff1a; 走近Java 概述 Java技术体系Java发展史Java虚拟机家族&#xff1a;&#xff08;Sun Classic/Exact VM、HotSpot VM、Mobile/Embedded VM、BEA JRockit/IBM J9 VM、BEA Liquid VM/Azul VM、Apache…

java基础常问面试题,面试必问

一、首先本职工作一定要做好做精 本人之前在干兼职的时候&#xff0c;也忽视过本职工作&#xff0c;从而导致自己落后平均技术水平&#xff0c;虽然之后迎头赶上&#xff0c;但这不能不算是个遗憾。前在接一些活的时候就感觉技术的重要性了&#xff0c;如果当年我技术再好些&a…

java基础教程哪个好,吐血整理

RabbitMQ&#xff1a; 优点&#xff1a;轻量&#xff0c;迅捷&#xff0c;容易部署和使用&#xff0c;拥有灵活的路由配置 缺点&#xff1a;性能和吞吐量较差&#xff0c;不易进行二次开发 RocketMQ&#xff1a; 优点&#xff1a;性能好&#xff0c;稳定可靠&#xff0c;有活…

java基础教程哪个好,面试必会

如何才可以进大厂&#xff1f; 答案其实也很简单&#xff0c;能力学历。不知道大家有没有发现&#xff0c;大厂的一些部门对于学历要求已经放低了&#xff0c;阿里的一些部门同样也招大专学历的程序员&#xff0c;当然肯定也是因为他的能力足够出色。 对于准备秋招的你来说&a…

java基础教程第三版耿祥义,后台开发JAVA岗

Java虚拟机内存模型 Java虚拟机内存模型中定义的访问操作与物理计算机处理的基本一致&#xff01; Java中通过多线程机制使得多个任务同时执行处理&#xff0c;所有的线程共享JVM内存区域main memory&#xff0c;而每个线程又单独的有自己的工作内存&#xff0c;当线程与内存区…

java基础案例教程黑马程序员案例答案,真香

掌握核心知识 1、90%几率面试被问&#xff0c;吃透原理&#xff0c;面试不慌&#xff08;Spring原理&#xff09; 2、大厂必问Redis&#xff0c;赶紧码起来&#xff08;Redis核心原理&#xff09; 3、MySQL从入门到实战都在这篇&#xff0c;面试笑谈优化 当然核心知识不止这三…

java基础的三个框架,进阶学习资料!

阿里巴巴一面 自我介绍这个就不说了&#xff0c;开头必问的说一下StringBuilder 和 StringBufferSpring bean加载&#xff0c;实例化的过程Spring AOP源码看过吗java内存模型说一下如果给你一个map&#xff0c;里面有很多很多对象&#xff0c;那么这个map存放在哪了解GC算法吗…

实验3 | 由遍历序列构造二叉树

二叉树构造定理&#xff1a; 定理7.1&#xff1a;任何n&#xff08;n>0&#xff09;个不同结点的二又树&#xff0c;都可由它的中序序列和先序序列唯一地确定。 定理7.2&#xff1a;任何n&#xff08;n&#xff1e;0&#xff09;个不同结点的二又树&#xff0c;都可由它的…

万字总结!java让字符串反转

Java基础 JDK 和 JRE 有什么区别&#xff1f; 和 equals 的区别是什么&#xff1f;两个对象的 hashCode()相同&#xff0c;则 equals()也一定为 true&#xff0c;对吗&#xff1f;final 在 java 中有什么作用&#xff1f;java 中的 Math.round(-1.5) 等于多少&#xff1f;Stri…

万字总结!springcloud分布式限流

正文 作为后端开发&#xff0c;日常操作数据库最常用的是写操作和读操作。读操作我们下边会讲&#xff0c;这个分类里我们主要来看看写操作时为什么会导致 SQL 变慢。 刷脏页 脏页的定义是这样的&#xff1a;内存数据页和磁盘数据页不一致时&#xff0c;那么称这个内存数据页…

万字长文!java读取json文件数据给对象

Java基础核心笔记总结 由于篇幅限制&#xff0c;我就只以截图展示目录内容以及部分笔记内容&#xff0c;获取完整版王者级核心宝典只需要点击点赞关注即可获取领取方式&#xff01; 在这个部分我们总结了Java的基础知识&#xff0c;涵盖了&#xff1a;概述、开发环境、开发环境…

三年Java开发,java基础常问面试题

一、首先本职工作一定要做好做精 本人之前在干兼职的时候&#xff0c;也忽视过本职工作&#xff0c;从而导致自己落后平均技术水平&#xff0c;虽然之后迎头赶上&#xff0c;但这不能不算是个遗憾。前在接一些活的时候就感觉技术的重要性了&#xff0c;如果当年我技术再好些&a…

三年Java开发,尚学堂java马士兵全套

基于 Servlet 容器的 Web MVC 身为 Java 开发者&#xff0c;对于 Spring 框架并不陌生。它起源于 2002 年、Rod Johnson 著作《Expert One-on-One J2EE Design and Development》中的 Interface 21 框架&#xff0c;到了 2004 年&#xff0c;推出 Spring 1.0&#xff0c;从 XM…

三年经验java工资,含爱奇艺,小米,腾讯,阿里

1、PTP模型 Point-to-Point&#xff0c;点对点通信模型。PTP是基于队列(Queue)的&#xff0c;一个队列可以有多个生产者&#xff0c;和多个消费者。消息服务器按照收到消息的先后顺序&#xff0c;将消息放到队列中。队列中的每一条消息&#xff0c;只能由一个消费者进行消费&a…