如何在 cython 中编写带有嵌套括号的#define 表达式?

How to write #define expressions with nested brackets in cython?

C 表达式:

#define EFX_REVERB_PRESET_GENERIC \
    { 1.0000f, 1.0000f, 0.3162f, 0.8913f, 1.0000f, 1.4900f, 0.8300f, 1.0000f, 0.0500f, 0.0070f, { 0.0000f, 0.0000f, 0.0000f }, 1.2589f, 0.0110f, { 0.0000f, 0.0000f, 0.0000f }, 0.2500f, 0.0000f, 0.2500f, 0.0000f, 0.9943f, 5000.0000f, 250.0000f, 0.0000f, 0x1 }

我想在 .pxd 文件.

中定义这个表达式

我必须将此表达式作为参数传递给某些 C 函数。所以我不将它用于Python

来源:OpenAL-Soft:https://github.com/kcat/openal-soft/blob/master/include/AL/efx-presets.h#L37

值得注意的是,并非所有内容都可以直接从 C 语言转换为 Cython。在这种情况下 EFX_REVERB_PRESET_GENERIC 不能真正使用类型定义,因为它不是类型 - 它只是括号和数字的集合。这些括号和数字只在少数地方有效:

 void other_func(WhateverTheStructIsCalled s);

 void func() {
     WhateverTheStructIsCalled s = EFX_REVERB_PRESET_GENERIC; // OK
     s = EFX_REVERB_PRESET_GENERIC; // wrong - not an initialization
     other_func(EFX_REVERB_PRESET_GENERIC); // also doesn't work
 }

因此它不适合 Cython 的模型,所以你不能直接包装它。

我要做的是自己写一个小的 C 包装器。您可以使用 Cython 的“内联 C 代码”功能来执行此操作:

cdef extern from *:
    """
    WhateverTheStructIsCalled get_EFX_REVERB_PRESET_GENERIC(void) {
        WhateverTheStructIsCalled s = EFX_REVERB_PRESET_GENERIC;
        return s;
    }
    """
    WhateverTheStructIsCalled get_EFX_REVERB_PRESET_GENERIC()

然后用get_EFX_REVERB_PRESET_GENERIC()调用这个函数,得到相关的初始化结构