【发布时间】:2012-01-22 19:23:44
【问题描述】:
我尝试在 Python 中进行 PCM 声音,但我尝试过的所有包都太多或没有文档或死了,所以我决定用 libao 做一个简单的。
我使用 xiph.org 的起始点 this source code 播放 440Hz 1 秒,我用 gcc -o ao_example ao_example.c -lao -ldl -lm 编译它,我成功运行了这段代码,立即在两个通道中听到 440Hz 正弦波 1 秒。
到目前为止,一切都很好。
所以我 $ cp ao_exemple.c mySoundAo.c 和我编辑了 mySoundAo.c 以编译为 Python 模块。完整代码如下:
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <ao/ao.h>
#include <Python.h>
#define BUF_SIZE 4096
static PyObject* py_soundAo(PyObject* self, PyObject* args)
{
ao_device *device;
ao_sample_format format;
int default_driver;
char *buffer;
int buf_size;
int sample;
float freq = 440.0;
int i;
/* -- Initialize -- */
fprintf(stderr, "libao example program\n");
ao_initialize();
/* -- Setup for default driver -- */
default_driver = ao_default_driver_id();
memset(&format, 0, sizeof(format));
format.bits = 16;
format.channels = 2;
format.rate = 44100;
format.byte_format = AO_FMT_LITTLE;
/* -- Open driver -- */
device = ao_open_live(default_driver, &format, NULL /* no options */);
if (device == NULL) {
fprintf(stderr, "Error opening device.\n");
return Py_BuildValue("", 0);
}
/* -- Play some stuff -- */
buf_size = format.bits/8 * format.channels * format.rate;
buffer = calloc(buf_size,
sizeof(char));
for (i = 0; i < format.rate; i++) {
sample = (int)(0.75 * 32768.0 * sin(2 * M_PI * freq * ((float) i/format.rate)));
/* Put the same stuff in left and right channel */
buffer[4*i] = buffer[4*i+2] = sample & 0xff;
buffer[4*i+1] = buffer[4*i+3] = (sample >> 8) & 0xff;
}
ao_play(device, buffer, buf_size);
/* -- Close and shutdown -- */
ao_close(device);
ao_shutdown();
return Py_BuildValue("", 0);
}
static PyMethodDef mySoundAo_methods[] = {
{"soundAo", py_soundAo, METH_VARARGS},
{NULL, NULL}
};
void initmySoundAo()
{
(void) Py_InitModule("mySoundAo", mySoundAo_methods);
}
所以我编译为gcc -shared -I/usr/include/python2.7/ -o mySoundAo.so mySoundAo.c -lpython2.7 -lm -lsndfile -lao -ldl,我收到了这个警告:
In file included from /usr/include/python2.7/Python.h:8:0,
from mySoundAo.c:5:
/usr/include/python2.7/pyconfig.h:1158:0: warning: "_POSIX_C_SOURCE" redefined [enabled by default]
/usr/include/features.h:214:0: note: this is the location of the previous definition
听起来并不危险,所以我继续前进。
在 python 中,我做了以下操作:
$ python
Python 2.7.2+ (default, Oct 4 2011, 20:03:08)
[GCC 4.6.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import mySoundAo
>>> mySoundAo.soundAo()
libao example program
Error opening device.
>>>
没有声音。稍微检查一下代码,我发现函数 ao_initialize(); 挂起大约 4 秒,下面的行 default_driver = ao_default_driver_id(); 将此变量设置为 -1(错误)。
这种行为很奇怪,因为它几乎是相同的代码。
那么,有什么想法可以完成这项工作吗?
谢谢!
【问题讨论】:
-
很好的问题。如果在
~/.libao.conf中设置debug,可以看到/usr/lib/ao/plugins-4/libalsa.so在Python上下文中加载失败。 4 秒延迟似乎是尝试连接 nas 时的超时。你不使用pyao 有什么原因吗? -
是的,@phihag 我在这里设置了调试,我看到了。有趣...好吧,我不使用 pyao 因为它看起来像死了,因为我没有找到例子。但是再看一遍,我发现代码相对较短,所以我想我可以对其进行逆向工程以制作一个工作示例,谢谢!但问题仍然存在:为什么上面的代码不起作用?
标签: python audio gcc pcm python-module