【问题标题】:Can't compile STK (The Synthesis Toolkit) with node-gyp (C++)无法使用 node-gyp (C++) 编译 STK (The Synthesis Toolkit)
【发布时间】:2021-02-07 09:42:54
【问题描述】:

我正在尝试使用 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( &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文件中链接stk,但后来我在电子中加载了engine.node模块,我得到了:

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

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

【问题讨论】:

    标签: c++ node.js electron linker-errors node-gyp


    【解决方案1】:

    终于!我自己找到的!

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

    -L./engine/includeby

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

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

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

    我讨厌自己 =)

    【讨论】:

      猜你喜欢
      • 2015-03-15
      • 2020-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-01
      • 1970-01-01
      相关资源
      最近更新 更多