问题描述
我正在尝试使用Electron创建音频软件(DAW),以创建窗口,并使用c ++播放音频/生成音频/应用音频效果。
我一直在寻找一个简单,功能强大且跨平台的库来播放和处理音频,我发现The Synthesis Toolkit
对此感到非常满意。
这是代码(来自STK演示程序):
#include "BeeThree.h"
#include "RtAudio.h"
using namespace stk;
// The TickData structure holds all the class instances and data that
// are shared by the varIoUs processing functions.
struct TickData {
Instrmnt *instrument;
StkFloat frequency;
StkFloat scaler;
long counter;
bool done;
// Default constructor.
TickData()
: instrument(0),scaler(1.0),counter(0),done( false ) {}
};
// This tick() function handles sample computation only. It will be
// called automatically when the system needs a new buffer of audio
// samples.
int tick( void *outputBuffer,void *inputBuffer,unsigned int nBufferFrames,double streamTime,RtAudioStreamStatus status,void *userData )
{
TickData *data = (TickData *) userData;
register StkFloat *samples = (StkFloat *) outputBuffer;
for ( unsigned int i=0; i<nBufferFrames; i++ ) {
*samples++ = data->instrument->tick();
if ( ++data->counter % 2000 == 0 ) {
data->scaler += 0.025;
data->instrument->setFrequency( data->frequency * data->scaler );
}
}
if ( data->counter > 80000 )
data->done = true;
return 0;
}
int main()
{
// Set the global sample rate and rawwave path before creating class instances.
Stk::setSampleRate( 44100.0 );
Stk::setRawwavePath("./engine/rawwaves/");
TickData data;
RtAudio dac;
// figure out how many bytes in an StkFloat and setup the RtAudio stream.
RtAudio::StreamParameters parameters;
parameters.deviceid = dac.getDefaultOutputDevice();
parameters.nChannels = 1;
RtAudioFormat format = ( sizeof(StkFloat) == 8 ) ? RTAUdio_FLOAT64 : RTAUdio_FLOAT32;
unsigned int bufferFrames = RT_BUFFER_SIZE;
try {
dac.openStream( ¶meters,NULL,format,(unsigned int)Stk::sampleRate(),&bufferFrames,&tick,(void *)&data );
}
catch ( RtAudioError& error ) {
error.printMessage();
goto cleanup;
}
try {
// Define and load the BeeThree instrument
data.instrument = new BeeThree();
}
catch ( StkError & ) {
goto cleanup;
}
data.frequency = 220.0;
data.instrument->noteOn( data.frequency,0.5 );
try {
dac.startStream();
}
catch ( RtAudioError &error ) {
error.printMessage();
goto cleanup;
}
// Block waiting until callback signals done.
std::cin.get();
data.scaler = 0.025;
std::cin.get();
data.scaler = -1;
std::cin.get();
// Shut down the callback and output stream.
try {
dac.closeStream();
}
catch ( RtAudioError &error ) {
error.printMessage();
}
cleanup:
delete data.instrument;
return 0;
}
我设法使用以下命令用g ++编译了这个简单的演示程序:
g++ -D__LITTLE_ENDIAN__ -D__LINUX_ALSA__ ./engine/engine.cpp -o ./engine/engi -I./engine/include/ -L./engine/lib/ -lstk -lpthread -lasound -lm
但是随后我尝试使用 node-gyp 将其编译为engine.node
文件,出现此错误:
paulux@Paulux-Laptop:~/Documents/Code/FyneWav$ node-gyp build
/usr/bin/ld : can't find -lstk
collect2: error: ld returned 1 exit status
这是我的binding.gyp
文件:
{
"targets": [
{
"target_name": "engine","sources": ["./engine/engine.cpp"],"cflags_cc" :["-fexceptions"],"include_dirs": [
"./engine/include/"
],'link_settings': {
"libraries": [
"-lpthread","-lasound","-lm","-L./engine/lib/","-lstk"
],},"defines": [
"__LITTLE_ENDIAN__","__LINUX_ALSA__"
]
}
]
}
我的文件夹如下:
root
|- package-lock.json
|- package.json
|- README.md
|- binding.gyp
|- 10.1.4 (includes for v8 NodeJS addon)
|- engine
|- engine.cpp
|- include (all include files from *STK/include* archive)
|- lib
|- libstk.a (lib from the *STK/src/Release* archive)
我尝试不链接stk
文件中的binding.gyp
,但随后将engine.node
模块加载到电子中,我得到了:
Uncaught Error: /home/paulux/Documents/Code/FyneWav/build/Release/engine.node:
undefined symbol: _ZN3stk3Stk17sampleRateChangedEdd
因此,问题是:
如何在node-gyp中链接stk
,如果不能,如何使用其他编译器(例如g ++)来制作engine.node
文件?
解决方法
最后!我一个人发现了!
答案真的很愚蠢:在我的binding.gyp
文件中,我只需要替换
-L./engine/include
由
-L/home/paulux/Documents/Code/fynewav/engine/include
。
我只需要从相对路径更改为绝对路径...
花了我一天的时间才解决...
我讨厌自己=)