[英]python selenium example doesn't work, says no module named Keys
[英]libao example doesn't work when compiled as python module
我在Python中尝试使用PCM声音,但我尝试过的所有软件包都太多或者没有文档或死了,所以我决定用libao做一个简单的软件包。
我用xiph.org的源代码作为起始点,播放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(错误)。
这种行为很奇怪,因为它几乎是相同的代码。
那么,任何使这项工作的想法?
谢谢!
你得到的警告是无害的,只需将#include <Python.h>
移动到顶部就应该让标准库正确识别已定义的宏。
问题可能是由于编译错误/usr/lib/ao/plugins-4/libalsa.so
引起的(如果在~/.libao.conf
设置debug
,则会提到此文件)。 由于ao的alsa插件无法加载,ao会尝试所有其他选项,并耗尽4秒的nas超时(这是延迟的原因)。
要检查错误libalsa.so
(或错误链接) libalsa.so
是否是问题,请运行
$ ldd -r /usr/lib/ao/plugins-4/libalsa.so > /dev/null
undefined symbol: ao_is_big_endian (/usr/lib/ao/plugins-4/libalsa.so)
输出中的错误应指向符号的问题。 您可以自己下载libao,并在libao-*/src/plugins/alsa/ao_alsa.c
修补该行,或者从ao_is_big_endian
复制定义,或修复链接。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.