Search code examples
c++emscriptenembind

How to store an Embind 'val' in a C++ class - error: call to deleted constructor


I managed to use the Embind val transliteration to create a Web Audio API AudioContext and use it inside a function. But I do not understand how I can store it for later use in a C++ class instance field (mContext). When I compile the following code, I get

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;
}

Solution

  • val has no default constructor. Judging from the implementation, you might be able to initialize it with null or undefined as part of the initializer list:

    class SC_WebAudioDriver : public SC_AudioDriver : mContext(val::undefined()) {...}
    

    Alternatively, you can set a default value in the class definition:

    class SC_WebAudioDriver : public SC_AudioDriver {
        val mContext = val::undefined();