无法使用 node-gyp (C++) 编译 STK(The Synthesis Toolkit)

Can't compile STK (The Synthesis Toolkit) with node-gyp (C++)

我正在尝试制作一个音频软件(DAW),使用 Electron 创建 window 和 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( &parameters, 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)

我尝试不在 binding.gyp 文件中 link stk,但后来我在 electron 中加载了 engine.node 模块,我得到:

Uncaught Error: /home/paulux/Documents/Code/FyneWav/build/Release/engine.node:
undefined symbol: _ZN3stk3Stk17sampleRateChangedEdd

所以,问题是: 我如何在 node-gyp 中 link stk,如果不能,我如何使用其他编译器(例如 g++)制作 engine.node 文件?

最后!我自己找到的!

答案真的很愚蠢:在我的 binding.gyp 文件中,我只需要替换

-L./engine/include 作者

-L/home/paulux/Documents/Code/fynewav/engine/include.

我只需要从相对路径更改为绝对路径...

我花了一天时间才弄明白...

而且我讨厌自己 =)