打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

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

一、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

  1. //<span style="font-size:14px;">pacat-simple.c</span>  
  2. /*** 
  3. This file is part of PulseAudio. 
  4. PulseAudio is free software; you can redistribute it and/or modify 
  5. it under the terms of the GNU Lesser General Public License as published 
  6. by the Free Software Foundation; either version 2.1 of the License, 
  7. or (at your option) any later version. 
  8. PulseAudio is distributed in the hope that it will be useful, but 
  9. WITHOUT ANY WARRANTY; without even the implied warranty of 
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 
  11. General Public License for more details. 
  12. You should have received a copy of the GNU Lesser General Public License 
  13. along with PulseAudio; if not, write to the Free Software 
  14. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 
  15. USA. 
  16. ***/  
  17. #ifdef HAVE_CONFIG_H  
  18. #include <config.h>  
  19. #endif  
  20. #include <stdio.h>  
  21. #include <unistd.h>  
  22. #include <string.h>  
  23. #include <errno.h>  
  24. #include <fcntl.h>  
  25. #include <pulse/simple.h>  
  26. #include <pulse/error.h>  
  27. #define BUFSIZE 1024  
  28. int main(int argc, char*argv[]) {  
  29. /* The Sample format to use */  
  30. static const pa_sample_spec ss = {  
  31. .format = PA_SAMPLE_S16LE,  
  32. .rate = 44100,  
  33. .channels = 2  
  34. };  
  35. pa_simple *s = NULL;  
  36. int ret = 1;  
  37. int error;  
  38. /* replace STDIN with the specified file if needed */  
  39. if (argc > 1) {  
  40. int fd;  
  41. if ((fd = open(argv[1], O_RDONLY)) < 0) {  
  42. fprintf(stderr, __FILE__": open() failed: %s\n", strerror(errno));  
  43. goto finish;  
  44. }  
  45. if (dup2(fd, STDIN_FILENO) < 0) {  
  46. fprintf(stderr, __FILE__": dup2() failed: %s\n", strerror(errno));  
  47. goto finish;  
  48. }  
  49. close(fd);  
  50. }  
  51. /* Create a new playback stream */  
  52. if (!(s = pa_simple_new(NULL, argv[0], PA_STREAM_PLAYBACK, NULL, "playback", &ss, NULL, NULL, &error))) {  
  53. fprintf(stderr, __FILE__": pa_simple_new() failed: %s\n", pa_strerror(error));  
  54. goto finish;  
  55. }  
  56. for (;;) {  
  57. uint8_t buf[BUFSIZE];  
  58. ssize_t r;  
  59. #if 0  
  60. pa_usec_t latency;  
  61. if ((latency = pa_simple_get_latency(s, &error)) == (pa_usec_t) -1) {  
  62. fprintf(stderr, __FILE__": pa_simple_get_latency() failed: %s\n", pa_strerror(error));  
  63. goto finish;  
  64. }  
  65. fprintf(stderr, "%0.0f usec \r", (float)latency);  
  66. #endif  
  67. /* Read some data ... */  
  68. if ((r = read(STDIN_FILENO, buf, sizeof(buf))) <= 0) {  
  69. if (r == 0) /* EOF */  
  70. break;  
  71. fprintf(stderr, __FILE__": read() failed: %s\n", strerror(errno));  
  72. goto finish;  
  73. }  
  74. /* ... and play it */  
  75. if (pa_simple_write(s, buf, (size_t) r, &error) < 0) {  
  76. fprintf(stderr, __FILE__": pa_simple_write() failed: %s\n", pa_strerror(error));  
  77. goto finish;  
  78. }  
  79. }  
  80. /* Make sure that every single sample was played */  
  81. if (pa_simple_drain(s, &error) < 0) {  
  82. fprintf(stderr, __FILE__": pa_simple_drain() failed: %s\n", pa_strerror(error));  
  83. goto finish;  
  84. }  
  85. ret = 0;  
  86. finish:  
  87. if (s)  
  88. pa_simple_free(s);  
  89. return ret;  
  90. }  

2、录音sample

A simple recording tool using the simple API

  1. //parec-simple.c  
  2. /*** 
  3. This file is part of PulseAudio. 
  4. PulseAudio is free software; you can redistribute it and/or modify 
  5. it under the terms of the GNU Lesser General Public License as published 
  6. by the Free Software Foundation; either version 2.1 of the License, 
  7. or (at your option) any later version. 
  8. PulseAudio is distributed in the hope that it will be useful, but 
  9. WITHOUT ANY WARRANTY; without even the implied warranty of 
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 
  11. General Public License for more details. 
  12. You should have received a copy of the GNU Lesser General Public License 
  13. along with PulseAudio; if not, write to the Free Software 
  14. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 
  15. USA. 
  16. ***/  
  17. #ifdef HAVE_CONFIG_H  
  18. #include <config.h>  
  19. #endif  
  20. #include <stdio.h>  
  21. #include <unistd.h>  
  22. #include <string.h>  
  23. #include <errno.h>  
  24. #include <pulse/simple.h>  
  25. #include <pulse/error.h>  
  26. #define BUFSIZE 1024  
  27. /* A simple routine calling UNIX write() in a loop */  
  28. static ssize_t loop_write(int fd, const void*data, size_t size) {  
  29. ssize_t ret = 0;  
  30. while (size > 0) {  
  31. ssize_t r;  
  32. if ((r = write(fd, data, size)) < 0)  
  33. return r;  
  34. if (r == 0)  
  35. break;  
  36. ret += r;  
  37. data = (const uint8_t*) data + r;  
  38. size -= (size_t) r;  
  39. }  
  40. return ret;  
  41. }  
  42. int main(int argc, char*argv[]) {  
  43. /* The sample type to use */  
  44. static const pa_sample_spec ss = {  
  45. .format = PA_SAMPLE_S16LE,  
  46. .rate = 44100,  
  47. .channels = 2  
  48. };  
  49. pa_simple *s = NULL;  
  50. int ret = 1;  
  51. int error;  
  52. /* Create the recording stream */  
  53. if (!(s = pa_simple_new(NULL, argv[0], PA_STREAM_RECORD, NULL, "record", &ss, NULL, NULL, &error))) {  
  54. fprintf(stderr, __FILE__": pa_simple_new() failed: %s\n", pa_strerror(error));  
  55. goto finish;  
  56. }  
  57. for (;;) {  
  58. uint8_t buf[BUFSIZE];  
  59. /* Record some data ... */  
  60. if (pa_simple_read(s, buf, sizeof(buf), &error) < 0) {  
  61. fprintf(stderr, __FILE__": pa_simple_read() failed: %s\n", pa_strerror(error));  
  62. goto finish;  
  63. }  
  64. /* And write it to STDOUT */  
  65. if (loop_write(STDOUT_FILENO, buf, sizeof(buf)) != sizeof(buf)) {  
  66. fprintf(stderr, __FILE__": write() failed: %s\n", strerror(errno));  
  67. goto finish;  
  68. }  
  69. }  
  70. ret = 0;  
  71. finish:  
  72. if (s)  
  73. pa_simple_free(s);  
  74. return ret;  
  75. }  


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



本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
C语言的宏中定义可变参数
C/C++宏定义的可变参数 | Vimer的程序世界
深入 了解 C宏
使用C语言操作MySQL数据库
solaris 系统日常维护
用C语言操作MySQL数据库,进行连接、插入、修改、删除等操作
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服