2012-01-22 3 views
2

私はPythonで少し音PCMにしようとしていたが、私が試したすべてのパッケージがあまりなかったか、文書化されていないか、死んでいたので、私は単純に作ることにしました1つは、のpythonモジュールとしてコンパイルした場合の例では動作しませんlibao

Iは、1秒間440Hzのを果たしxiph.orgによって始点this source codeとして使用し、私は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 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. 
>>> 

はPythonで、私は次のようでした。コードを少し検査、Iは-1(エラー)この変数関数をao_initialize();約4秒間ハングアップし、次の行default_driver = ao_default_driver_id();セットを発見しました。それはほとんど同じコードだから

この動作は、奇妙です。

だから、この作品を作るための任意のアイデア?

ありがとうございました!

+1

優秀な質問です。 '〜/ .libao.conf'に' debug'を設定すると、 '/ usr/lib/ao/plugins-4/libalsa.so'がPythonコンテキストに読み込まれないことがわかります。 4秒間の遅延は、NASに接続しようとするとタイムアウトになるようです。 [pyao](http://ekyo.nerim.net/software/pyogg/index.html)を使用していない理由はありますか? – phihag

+0

はい、@phihagここにデバッグを設定して見ました。面白い...まあ、私はpyaoを使いませんでした。なぜなら、それは死んだように見えました。しかし、もう少し見ていると、コードが比較的短いことが分かります。そのため、リバースエンジニアリングして実用的な例を作ることができます。ありがとう! しかし、上記のコードが機能しないのはなぜですか? – LucasBr

答えて

2

あなたが得る警告は無害で、単にトップに#include <Python.h>を移動する標準ライブラリが正しくすでに定義されたマクロを認識させる必要があります。

この問題はおそらく、コンパイルされた/usr/lib/ao/plugins-4/libalsa.soが原因で発生している可能性があります(このファイルはdebug~/.libao.confに設定した場合に記載されています)。 aoのalsaプラグインはロードできないので、他のすべてのオプションが試行され、4秒のタイムアウトが発生します(これが遅延の原因です)。正しくコンパイル(またはmislinked)かどうかをチェックする

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から定義をコピー、またはリンクを修正することができます。

+1

ao_is_big_endian関数をao_alsa.cファイルにコピーして再コンパイルしました。ありがとう! – LucasBr

関連する問題