声音设备接口
1. 系统环境
2. 安装音频库
ubuntu环境下,旧的/dev/dsp接口没有了. 录/放音乐需要先安装音频库文件. 如下:
3. 声音设备应用举例
例1 : 播放.wav文件
pacat-simple.c
/*** http://www.freedesktop.org/software/pulseaudio/doxygen/pacat-simple_8c-example.html This file is part of PulseAudio. PulseAudio is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. PulseAudio is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with PulseAudio; if not, see <http://www.gnu.org/licenses/>. ***/ #ifdef HAVE_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> #define BUFSIZE 1024 int main(int argc, char* argv[]) { #if 1 /* The Sample format to use */ static const pa_sample_spec ss = { .format = PA_SAMPLE_S16LE, .rate = 44100, .channels = 2 }; pa_simple *s = NULL; int ret = 1; int error; /* replace STDIN with the specified file if needed */ if (argc > 1) { int fd; if ((fd = open(argv[1], O_RDONLY)) < 0) { fprintf(stderr, __FILE__": open() failed: %s\n", strerror(errno)); goto finish; } if (dup2(fd, STDIN_FILENO) < 0) { fprintf(stderr, __FILE__": dup2() failed: %s\n", strerror(errno)); goto finish; } close(fd); } /* Create a new playback stream */ if (!(s = pa_simple_new(NULL, argv[0], PA_STREAM_PLAYBACK, NULL, "playback", &ss, NULL, NULL, &error))) { fprintf(stderr, __FILE__": pa_simple_new() failed: %s\n", pa_strerror(error)); goto finish; } for (;;) { uint8_t buf[BUFSIZE]; ssize_t r; #if 0 pa_usec_t latency; if ((latency = pa_simple_get_latency(s, &error)) == (pa_usec_t) -1) { fprintf(stderr, __FILE__": pa_simple_get_latency() failed: %s\n", pa_strerror(error)); goto finish; } fprintf(stderr, "%0.0f usec \r", (float)latency); #endif /* Read some data ... */ if ((r = read(STDIN_FILENO, buf, sizeof(buf))) <= 0) { if (r == 0) /* EOF */ break; fprintf(stderr, __FILE__": read() failed: %s\n", strerror(errno)); goto finish; } /* ... and play it */ if (pa_simple_write(s, buf, (size_t) r, &error) < 0) { fprintf(stderr, __FILE__": pa_simple_write() failed: %s\n", pa_strerror(error)); goto finish; } } /* Make sure that every single sample was played */ if (pa_simple_drain(s, &error) < 0) { fprintf(stderr, __FILE__": pa_simple_drain() failed: %s\n", pa_strerror(error)); goto finish; } ret = 0; finish: if (s) pa_simple_free(s); return ret; #else return 0; #endif }
编译链接执行, 输出如下:
可以听到扬声器中发出的音乐了.
其中用到多个动态库文件, 上面的命令链接动态库的顺序有要求, 否则会报错:
hr@huarain:/opt/study/homework/alsa$ gcc -g -o pacat-simple -lpulse -lpulsecommon-4.0 -lpulse-simple pacat-simple.c
/tmp/cc2t4mNL.o:在函数‘main’中:
/opt/study/homework/alsa/pacat-simple.c:58:对‘pa_simple_new’未定义的引用
/opt/study/homework/alsa/pacat-simple.c:59:对‘pa_strerror’未定义的引用
/opt/study/homework/alsa/pacat-simple.c:87:对‘pa_simple_drain’未定义的引用
/opt/study/homework/alsa/pacat-simple.c:81:对‘pa_simple_write’未定义的引用
/opt/study/homework/alsa/pacat-simple.c:82:对‘pa_strerror’未定义的引用
/opt/study/homework/alsa/pacat-simple.c:88:对‘pa_strerror’未定义的引用
/opt/study/homework/alsa/pacat-simple.c:94:对‘pa_simple_free’未定义的引用
collect2: error: ld returned 1 exit status
hr@huarain:/opt/study/homework/alsa$
或
hr@huarain:/opt/study/homework/alsa$ gcc -g -o pacat-simple -lpulse -lpulsecommon-4.0 pacat-simple.c -lpulse-simple
/usr/bin/ld: /tmp/cc4aOF4H.o: undefined reference to symbol 'pa_strerror@@PULSE_0'
/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu/libpulse.so: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
hr@huarain:/opt/study/homework/alsa$
例2: 录音
parec-simple.c
/*** http://www.freedesktop.org/software/pulseaudio/doxygen/parec-simple_8c-example.html This file is part of PulseAudio. PulseAudio is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. PulseAudio is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with PulseAudio; if not, see <http://www.gnu.org/licenses/>. ***/ #ifdef HAVE_CONFIG_H #include <config.h> #endif #include <stdio.h> #include <unistd.h> #include <string.h> #include <errno.h> #include <pulse/simple.h> #include <pulse/error.h> #define BUFSIZE 1024 /* A simple routine calling UNIX write() in a loop */ static ssize_t loop_write(int fd, const void*data, size_t size) { ssize_t ret = 0; while (size > 0) { ssize_t r; if ((r = write(fd, data, size)) < 0) return r; if (r == 0) break; ret += r; data = (const uint8_t*) data + r; size -= (size_t) r; } return ret; } int main(int argc, char*argv[])
{ /* The sample type to use */ static const pa_sample_spec ss = { .format = PA_SAMPLE_S16LE, .rate = 44100, .channels = 2 }; pa_simple *s = NULL; int ret = 1; int error; /* Create the recording stream */ if (!(s = pa_simple_new(NULL, argv[0], PA_STREAM_RECORD, NULL, "record", &ss, NULL, NULL, &error))) { fprintf(stderr, __FILE__": pa_simple_new() failed: %s\n", pa_strerror(error)); goto finish; } for (;;) { uint8_t buf[BUFSIZE]; /* Record some data ... */ if (pa_simple_read(s, buf, sizeof(buf), &error) < 0) { fprintf(stderr, __FILE__": pa_simple_read() failed: %s\n", pa_strerror(error)); goto finish; } /* And write it to STDOUT */ if (loop_write(STDOUT_FILENO, buf, sizeof(buf)) != sizeof(buf)) { fprintf(stderr, __FILE__": write() failed: %s\n", strerror(errno)); goto finish; } } ret = 0; finish: if (s) pa_simple_free(s); return ret; }
编译链接运行如下:
暂未验证得到的parec.wav文件内容
4. 参考
http://www.freedesktop.org/software/pulseaudio/doxygen/pacat-simple_8c-example.html
http://www.freedesktop.org/software/pulseaudio/doxygen/parec-simple_8c-example.html
http://www.alsa-project.org/main/index.php/Main_Page