libao示例在编译为Python模块时不工作

3 投票
1 回答
1066 浏览
提问于 2025-04-17 10:51

我在用Python尝试做PCM音效的时候,发现我试过的所有包都太复杂了,要么没有文档,要么已经不维护了,所以我决定用libao自己简单做一个。

我以这个源代码为起点,代码来自xiph.org,它可以播放440Hz的声音,持续1秒。我用命令gcc -o ao_example ao_example.c -lao -ldl -lm编译了这个代码,成功运行后,立刻听到了1秒的440Hz正弦波声,左右声道都有。

到这里为止,一切都很好。

接着我用$ 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(表示出错)。

这个情况很奇怪,因为代码几乎是一样的。

所以,有什么想法可以让这个工作吗?

谢谢!

1 个回答

2

你看到的警告其实没什么大不了的,只需要把 #include <Python.h> 移到最上面,这样标准库就能正确识别这个宏已经定义过了。

问题可能是因为 /usr/lib/ao/plugins-4/libalsa.so 这个文件编译错了(如果你在 ~/.libao.conf 里设置了 debug,就会提到这个文件)。由于 ao 的 alsa 插件加载失败,ao 就会尝试其他所有选项,结果导致了 4 秒的超时(这就是延迟的原因)。

要检查这个编译错的(或者链接错的) 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 复制定义,或者修复链接。

撰写回答