Ubuntu12.04下pulseaudio的安装以及API使用 pulseaudio

一、Ubuntu12.04下安装音频库客户端软件libpulse-dev

Package libpulse-dev:PulseAudio client development headers and libraries

使用如下命令:

sudo apt-get install libpulse-dev

安装时会提示依赖于下面这几个软件,一并安装即可:

libavahi-client-dev libavahi-common-dev libpulse-mainloop-glib0 libpulse0

这个也可以上packages.ubuntu.com官方网址查询libpulse-dev软件的依赖关系,相关链接如下:

http://packages.ubuntu.com/raring/libpulse-dev

如下图所示:



二、pulseaudio库的使用(同步simple API)

pulseaudio官网有关于pulseaudio的API doxygen使用手册,网址如下:

http://freedesktop.org/software/pulseaudio/doxygen/

1、播音sample

A simple playback tool using the simple API

[cpp] view plaincopy

//<span>pacat-simple.c</span>

/***

ThisfileispartofPulseAudio.

PulseAudioisfreesoftware;youcanredistributeitand/ormodify

itunderthetermsoftheGNULesserGeneralPublicLicenseaspublished

bytheFreeSoftwareFoundation;eitherversion2.1oftheLicense,

or(atyouroption)anylaterversion.

PulseAudioisdistributedinthehopethatitwillbeuseful,but

WITHOUTANYWARRANTY;withouteventheimpliedwarrantyof

MERCHANTABILITYorFITNESSFORAPARTICULARPURPOSE.SeetheGNU

GeneralPublicLicenseformoredetails.

YoushouldhavereceivedacopyoftheGNULesserGeneralPublicLicense

alongwithPulseAudio;ifnot,writetotheFreeSoftware

Foundation,Inc.,59TemplePlace,Suite330,Boston,MA02111-1307

USA.

***/

#ifdefHAVE_CONFIG_H

#include<config.h>

#endif

#include<stdio.h>

#include<unistd.h>

#include<string.h>

#include<errno.h>

#include<fcntl.h>

#include<pulse/simple.h>

#include<pulse/error.h>

#defineBUFSIZE1024

intmain(intargc,char*argv[]){

/*TheSampleformattouse*/

staticconstpa_sample_specss={

.format=PA_SAMPLE_S16LE,

.rate=44100,

.channels=2

};

pa_simple*s=NULL;

intret=1;

interror;

/*replaceSTDINwiththespecifiedfileifneeded*/

if(argc>1){

intfd;

if((fd=open(argv[1],O_RDONLY))<0){

fprintf(stderr,__FILE__":open()failed:%sn",strerror(errno));

gotofinish;

}

if(dup2(fd,STDIN_FILENO)<0){

fprintf(stderr,__FILE__":dup2()failed:%sn",strerror(errno));

gotofinish;

}

close(fd);

}

/*Createanewplaybackstream*/

if(!(s=pa_simple_new(NULL,argv[0],PA_STREAM_PLAYBACK,NULL,"playback",&ss,NULL,NULL,&error))){

fprintf(stderr,__FILE__":pa_simple_new()failed:%sn",pa_strerror(error));

gotofinish;

}

for(;;){

uint8_tbuf[BUFSIZE];

ssize_tr;

#if0

pa_usec_tlatency;

if((latency=pa_simple_get_latency(s,&error))==(pa_usec_t)-1){

fprintf(stderr,__FILE__":pa_simple_get_latency()failed:%sn",pa_strerror(error));

gotofinish;

}

fprintf(stderr,"%0.0fusecr",(float)latency);

#endif

/*Readsomedata...*/

if((r=read(STDIN_FILENO,buf,sizeof(buf)))<=0){

if(r==0)/*EOF*/

break;

fprintf(stderr,__FILE__":read()failed:%sn",strerror(errno));

gotofinish;

}

/*...andplayit*/

if(pa_simple_write(s,buf,(size_t)r,&error)<0){

fprintf(stderr,__FILE__":pa_simple_write()failed:%sn",pa_strerror(error));

gotofinish;

}

}

/*Makesurethateverysinglesamplewasplayed*/

if(pa_simple_drain(s,&error)<0){

fprintf(stderr,__FILE__":pa_simple_drain()failed:%sn",pa_strerror(error));

gotofinish;

}

ret=0;

finish:

if(s)

pa_simple_free(s);

returnret;

}

2、录音sample

A simple recording tool using the simple API

[cpp] view plaincopy

//parec-simple.c

/***

ThisfileispartofPulseAudio.

PulseAudioisfreesoftware;youcanredistributeitand/ormodify

itunderthetermsoftheGNULesserGeneralPublicLicenseaspublished

bytheFreeSoftwareFoundation;eitherversion2.1oftheLicense,

or(atyouroption)anylaterversion.

PulseAudioisdistributedinthehopethatitwillbeuseful,but

WITHOUTANYWARRANTY;withouteventheimpliedwarrantyof

MERCHANTABILITYorFITNESSFORAPARTICULARPURPOSE.SeetheGNU

GeneralPublicLicenseformoredetails.

YoushouldhavereceivedacopyoftheGNULesserGeneralPublicLicense

alongwithPulseAudio;ifnot,writetotheFreeSoftware

Foundation,Inc.,59TemplePlace,Suite330,Boston,MA02111-1307

USA.

***/

#ifdefHAVE_CONFIG_H

#include<config.h>

#endif

#include<stdio.h>

#include<unistd.h>

#include<string.h>

#include<errno.h>

Ubuntu12.04下pulseaudio的安装以及API使用 pulseaudio
#include<pulse/simple.h>

#include<pulse/error.h>

#defineBUFSIZE1024

/*AsimpleroutinecallingUNIXwrite()inaloop*/

staticssize_tloop_write(intfd,constvoid*data,size_tsize){

ssize_tret=0;

while(size>0){

ssize_tr;

if((r=write(fd,data,size))<0)

returnr;

if(r==0)

break;

ret+=r;

data=(constuint8_t*)data+r;

size-=(size_t)r;

}

returnret;

}

intmain(intargc,char*argv[]){

/*Thesampletypetouse*/

staticconstpa_sample_specss={

.format=PA_SAMPLE_S16LE,

.rate=44100,

.channels=2

};

pa_simple*s=NULL;

intret=1;

interror;

/*Createtherecordingstream*/

if(!(s=pa_simple_new(NULL,argv[0],PA_STREAM_RECORD,NULL,"record",&ss,NULL,NULL,&error))){

fprintf(stderr,__FILE__":pa_simple_new()failed:%sn",pa_strerror(error));

gotofinish;

}

for(;;){

uint8_tbuf[BUFSIZE];

/*Recordsomedata...*/

if(pa_simple_read(s,buf,sizeof(buf),&error)<0){

fprintf(stderr,__FILE__":pa_simple_read()failed:%sn",pa_strerror(error));

gotofinish;

}

/*AndwriteittoSTDOUT*/

if(loop_write(STDOUT_FILENO,buf,sizeof(buf))!=sizeof(buf)){

fprintf(stderr,__FILE__":write()failed:%sn",strerror(errno));

gotofinish;

}

}

ret=0;

finish:

if(s)

pa_simple_free(s);

returnret;

}

3、编译、运行含有pulseaudio库的时候,需要添加pulseaudio的动态链接库-libpulse等,

(可以在/usr/lib/i386-linux-gnu/目录下找到pulse的动态库

/usr/lib/i386-linux-gnu/libpulsecommon-1.1.so

/usr/lib/i386-linux-gnu/libpulsedsp.so

/usr/lib/i386-linux-gnu/libpulse-mainloop-glib.so

/usr/lib/i386-linux-gnu/libpulse-mainloop-glib.so.0

/usr/lib/i386-linux-gnu/libpulse-mainloop-glib.so.0.0.4

/usr/lib/i386-linux-gnu/libpulse-simple.so

/usr/lib/i386-linux-gnu/libpulse-simple.so.0

/usr/lib/i386-linux-gnu/libpulse-simple.so.0.0.3

/usr/lib/i386-linux-gnu/libpulse.so

/usr/lib/i386-linux-gnu/libpulse.so.0

/usr/lib/i386-linux-gnu/libpulse.so.0.13.5)

例如,以pacat-simple.c程序为例:

(1)编译命令如下:

gcc -o pacat-simple pacat-simple.c -lpulse -lpulsecommon-1.1 -lpulse-simple

(2)运行上一步生成的pacat-simple可执行程序

./pacat-simple 飞鸟.wav

(注意:有一个命令行参数,是音频文件名,上网搜索一个wav格式的音频文件,放在当前目录,然后执行。)

三、ALSA音频库

ALSA全称为:Advanced Linux Sound Architecture

一个网址为:http://www.alsa-project.org/main/index.php/Main_Page

  

爱华网本文地址 » http://www.413yy.cn/a/25101011/52039.html

更多阅读

智能手机速度变慢的原因以及解决办法 下载速度慢的解决办法

智能手机速度变慢的原因以及解决办法——简介人手一机智能手机,不管是高端的还是低端的,但是在买来以后用着用着,我们就发现自己的手机越来越不智能,速度也是变得越来越慢,这是什么原因呢?如何解决这个问题呢?智能手机速度变慢的原因以及

Win7下applocale的安装方法 微软applocale win7

Win7下applocale的安装方法——简介对于国内普遍使用简体中文win7系统的玩家来说,有时候遇上几款台湾的或者日本汉化的繁体游戏,有时候会出现乱码,这个时候就需要用到applocale也就是apploc.msi软件。通常在Windows XP下安装毫无压力,不

IIS的安装与配置详细图解教程。 简单粘土教程详细图解

IIS的安装与配置详细图解教程。——简介一、这里讲的是在WINDOWS 2003下的IIS组件的安装,至于WINDOWS XP,请看这里: IIS的安装与配置详细图解教程。——工具/原料IIS组件一个(百度下载)IIS的安装与配置详细图解教程。——方法/步骤IIS

ant的安装,配置及验证 ant 1.9.6 安装 配置

ant的安装,配置及验证——简介Ant是一个Apache基金会下的跨平台的构件工具,在实际开发项目中经常会用到,可以实现项目的自动构建和部署等功能。在本文中,主要熟悉怎样将Ant安装成功。ant的安装,配置及验证——方法/步骤ant的安装,配置及

声明:《Ubuntu12.04下pulseaudio的安装以及API使用 pulseaudio》为网友尘心欲尽何曾尽分享!如侵犯到您的合法权益请联系我们删除