我正在使用 Node.js 的 ffi 和 ref 模块来创建与codec2 C library的绑定。
这是库标题的一部分:
#define CODEC2_SAMPLES_PER_FRAME 160
#define CODEC2_BITS_PER_FRAME 50
void *codec2_create();
void codec2_encode(void *codec2_state, unsigned char * bits, short speech_in[]);
以下是 C ( c2enc.c ) 中编码的示例实现:
#define BITS_SIZE ((CODEC2_BITS_PER_FRAME + 7) / 8)
int main(int argc, char *argv[])
{
void *codec2;
FILE *fin;
FILE *fout;
short buf[CODEC2_SAMPLES_PER_FRAME];
unsigned char bits[BITS_SIZE];
/* ... */
codec2 = codec2_create();
while(fread(buf, sizeof(short), CODEC2_SAMPLES_PER_FRAME, fin) ==
CODEC2_SAMPLES_PER_FRAME) {
codec2_encode(codec2, bits, buf);
fwrite(bits, sizeof(char), BITS_SIZE, fout);
//if this is in a pipeline, we probably don't want the usual
//buffering to occur
if (fout == stdout) fflush(stdout);
if (fin == stdin) fflush(stdin);
}
/* ... */
}
这就是我试图encode
从 JavaScript 代码调用函数的方式:
CODEC2_SAMPLES_PER_FRAME = 160
CODEC2_BITS_PER_FRAME = 50
BITS_SIZE = ((CODEC2_BITS_PER_FRAME + 7) / 8)
var Codec2 = ffi.Library('./libcodec2', {
"codec2_create": [ 'pointer', [] ],
"codec2_destroy": [ "void", [ref.refType('void')] ],
"codec2_encode": [ "void", [
ref.refType('void'),
ref.refType('uchar') ,
ref.refType('short')
]
],
"codec2_decode": [ "void", [
ref.refType('void'),
ref.refType('short'),
ref.refType('uchar')
]
]
});
var codec2 = Codec2.codec2_create();
var buf = ref.alloc('short', CODEC2_SAMPLES_PER_FRAME)
var bits = ref.alloc('uchar', BITS_SIZE)
Codec2.codec2_encode(codec2, bits, buf);
Codec2.codec2_destroy(codec2);
您能否解释一下如何正确分配数组unsigned char* bits
和数组short
?因为我怀疑我做得对。在参考库文档中,我发现可以分配字符串,但没有关于如何创建一些其他数据类型的数组的参考。
顺便说一句,如果这很重要,我正在使用节点 0.10.26。