在最近的項目中,須要完成這樣一個功能,要求app能與硬件進行語音交流,不須要實時,僅僅實現相似微信的按住說話的功能就能夠。爲了減小網絡流量,考慮將音頻文件壓縮傳送。成熟的壓縮方式有speex、opus。目前speex已再也不維護。考慮到本次須要的功能簡單以及網上關於speex的文檔較多的狀況下,咱們決定使用speex完成語音壓縮功能。 咱們經過node調用c++模塊的方法,來實現speex的壓縮(僅實現壓縮,關於音頻格式轉換往後再碼)。javascript
ps: 先水上他一篇 :)java
githubnode
咱們選擇speex的最新穩定版本進行下載:speex-1.2.0.tar.gzpython
{
'variables': { 'target_arch%': 'x64' },
'target_defaults': {
'default_configuration': 'Debug',
'configuration': {
'Debug': {
'defines': [ 'DEBUG', '_DEBUG' ],
'msvs_settings': {
'VSSLCompilerTool': {
'RuntimeLibrary': 1, #static debug
},
},
},
'Release': {
'defines': [ 'NODEBUG' ],
'msvs_settings': {
'VSSLCompilerTool': {
'RuntimeLibrary': 0, #static release
},
},
},
},
'msvs_settings': {
'VCLinkerTool': {
'GenerateDebugInformation': 'true',
},
},
},
'targets': [
{
'target_name': 'libspeexdsp',
'type': 'static_library',
'sources': [
'speex-1.2.0/libspeex/bits.c',
'speex-1.2.0/libspeex/cb_search.c',
'speex-1.2.0/libspeex/exc_5_64_table.c',
'speex-1.2.0/libspeex/exc_5_256_table.c',
'speex-1.2.0/libspeex/exc_8_128_table.c',
'speex-1.2.0/libspeex/exc_10_16_table.c',
'speex-1.2.0/libspeex/exc_10_32_table.c',
'speex-1.2.0/libspeex/exc_20_32_table.c',
'speex-1.2.0/libspeex/filters.c',
'speex-1.2.0/libspeex/gain_table.c',
'speex-1.2.0/libspeex/gain_table_lbr.c',
'speex-1.2.0/libspeex/hexc_10_32_table.c',
'speex-1.2.0/libspeex/hexc_table.c',
'speex-1.2.0/libspeex/high_lsp_tables.c',
'speex-1.2.0/libspeex/kiss_fft.c',
'speex-1.2.0/libspeex/kiss_fftr.c',
'speex-1.2.0/libspeex/lpc.c',
'speex-1.2.0/libspeex/lsp.c',
'speex-1.2.0/libspeex/lsp_tables_nb.c',
'speex-1.2.0/libspeex/ltp.c',
'speex-1.2.0/libspeex/modes.c',
'speex-1.2.0/libspeex/modes_wb.c',
'speex-1.2.0/libspeex/nb_celp.c',
'speex-1.2.0/libspeex/quant_lsp.c',
'speex-1.2.0/libspeex/smallft.c',
'speex-1.2.0/libspeex/speex.c',
'speex-1.2.0/libspeex/speex_callbacks.c',
'speex-1.2.0/libspeex/speex_header.c',
'speex-1.2.0/libspeex/stereo.c',
'speex-1.2.0/libspeex/vbr.c',
'speex-1.2.0/libspeex/vq.c',
'speex-1.2.0/libspeex/window.c'
],
'cflags': [
'-fvisibility=hidden',
'-W',
'-Wstrict-prototypes',
'-Wno-parentheses',
'-Wno-unused-parameter',
'-Wno-sign-compare',
'-Wno-unused-variable',
],
'include_dirs': [
'config/speex-1.2.0/<(OS)/<(target_arch)',
'speex-1.2.0/include',
],
'defines': [
'PIC',
'HAVE_CONFIG_H',
'_USE_MATH_DEFINES',
]
}
]
}
複製代碼
#include <node.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
複製代碼
#ifdef __cplusplus
extern "C"
{
#endif
#include <speex/speex.h>
#include <speex/speex_callbacks.h>
#ifdef __cplusplus
}
#endif
複製代碼
#ifdef FIXED_DEBUG
extern long long spx_mips;
#endif
複製代碼
#define FRAME_SIZE 160
#ifdef FIXED_DEBUG
extern long long spx_mips;
#endif
複製代碼
using namespace v8;
複製代碼
void Add(const FunctionCallbackInfo<Value>& args) {
複製代碼
Isolate* isolate = Isolate::GetCurrent();
HandleScope scope(isolate);
void *st;
FILE *fin,*fout;
short in[FRAME_SIZE];
float input[FRAME_SIZE];
char cbits[200];
int nbBytes;
SpeexBits bits;
int i,tmp;
st = speex_encoder_init(&speex_nb_mode);
tmp = 8;
speex_encoder_ctl(st,SPEEX_SET_QUALITY,&tmp);
Local<Value> in_addr = args[0];
Local<String> ss = in_addr->ToString();
String::Utf8Value value(ss);
char* c = *value;
fin = fopen(c,"rb");
複製代碼
Local<Value> out_addr = args[1];
Local<String> out_addr_str = out_addr->ToString();
String::Utf8Value value1(out_addr_str);
char* o = *value1;
fout = fopen(o,"wb+");
speex_bits_init(&bits);
while(1){
fread(in,sizeof(short),FRAME_SIZE,fin);
if(feof(fin))
break;
for(i = 0;i<FRAME_SIZE;i++){
input[i] = in[i];
}
speex_bits_reset(&bits);
speex_encode(st,input,&bits);
nbBytes = speex_bits_write(&bits,cbits,200);
fwrite(&nbBytes, sizeof(int), 1, fout);
fwrite(cbits, 1, nbBytes, fout);
}
複製代碼
speex_encoder_destroy(st);
/*Destroy the bit-packing struct*/
speex_bits_destroy(&bits);
fclose(fin);
}
void Init(Handle<Object> exports) {
NODE_SET_METHOD(exports, "add", Add);
}
NODE_MODULE(hello, Init)
複製代碼
binding.gyp內容以下:c++
{
"targets": [
{
"target_name": "hello",
"sources": ["./c++/hello.cc", ],
"dependencies":[
"deps/binding.gyp:libspeexdsp"
],
'cflags': [
'-pthread',
'-fno-exceptions',
'-fno-strict-aliasing',
'-Wall',
'-Wno-unused-parameter',
'-Wno-missing-field-initializers',
'-Wextra',
'-pipe',
'-fno-ident',
'-fdata-sections',
'-ffunction-sections',
'-fPIC'
],
'defines': [
'LARGEFILE_SOURCE',
'_FILE_OFFSET_BITS=64',
'WEBRTC_TARGET_PC',
'WEBRTC_LINUX',
'WEBRTC_THREAD_RR',
'EXPAT_RELATIVE_PATH',
'GTEST_RELATIVE_PATH',
'JSONCPP_RELATIVE_PATH',
'WEBRTC_RELATIVE_PATH',
'POSIX',
'__STDC_FORMAT_MACROS',
'DYNAMIC_ANNOTATIONS_ENABLED=0'
],
'include_dirs': [
'deps/speex-1.2.0/include',
'deps/config/speex-1.2.0/<(OS)/<(target_arch)',
],
}
]
}
複製代碼
在項目根目錄下運行命令git
node-gyp configure
node-gyp build
複製代碼
如未報錯而且根目錄下生成build目錄: github
說明編譯成功。若爲成功,請詳細檢查 兩個binding.gyp中的目錄是否對應正確,注:使用node-gyp須要 python27環境,必須爲此版本新建hello.js 文件內容以下json
const hello = require('./build/Release/hello');
hello.add('a.pcm', "bloom.encode");
複製代碼
文件運行後會將male.wav文件壓縮,並生成male.test文件 效果以下:bash