我设法使用Embind val 音译来创建 Web Audio APIAudioContext
并在函数中使用它。但我不明白如何将它存储起来以供以后在 C++ 类实例字段 ( mContext
) 中使用。当我编译以下代码时,我得到
SC_WebAudio.cpp:62:20: error: call to deleted constructor of 'emscripten::val'
SC_WebAudioDriver::SC_WebAudioDriver(struct World* inWorld): SC_AudioDriver(inWorld) {}
^
#include <emscripten.h>
#include <emscripten/val.h>
using namespace emscripten;
class SC_WebAudioDriver : public SC_AudioDriver {
val mContext; // this doesn't work
protected:
virtual bool DriverSetup(int* outNumSamplesPerCallback, double* outSampleRate);
virtual bool DriverStart();
virtual bool DriverStop();
public:
SC_WebAudioDriver(struct World* inWorld);
virtual ~SC_WebAudioDriver();
};
SC_AudioDriver* SC_NewAudioDriver(struct World* inWorld) { return new SC_WebAudioDriver(inWorld); }
SC_WebAudioDriver::SC_WebAudioDriver(struct World* inWorld): SC_AudioDriver(inWorld) {}
SC_WebAudioDriver::~SC_WebAudioDriver() {
}
bool SC_WebAudioDriver::DriverSetup(int* outNumSamples, double* outSampleRate) {
scprintf("SC_WebAudio: DriverSetup.\n");
// this uses the experimental emscripten Embind 'val' transliteration,
// see https://emscripten.org/docs/api_reference/val.h.html
val AudioContext = val::global("AudioContext");
if (!AudioContext.as<bool>()) {
AudioContext = val::global("webkitAudioContext");
if (!AudioContext.as<bool>()) {
scprintf("SC_WebAudioDriver: could not get hold of AudioContext\n");
return false;
}
}
val context = AudioContext.new_();
mContext = context; // how to do this?
double sr = context["sampleRate"].as<double>();
val proc = context.call<val>("createScriptProcessor", 0, 0, 2);
int bufSize = proc["bufferSize"].as<int>();
*outNumSamples = bufSize;
*outSampleRate = sr;
return true;
}