Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Uncaught TypeError: Module.cwrap is not a function #3

Open
amrsobhy opened this issue Apr 30, 2020 · 3 comments
Open

Uncaught TypeError: Module.cwrap is not a function #3

amrsobhy opened this issue Apr 30, 2020 · 3 comments

Comments

@amrsobhy
Copy link

Many thanks! I have followed the instructions carefully however, when I go to docs and run a python server, this is the error I'm getting

image

Any thoughts?

@chrisspen
Copy link

This solution had the answer.

When I added -s EXTRA_EXPORTED_RUNTIME_METHODS=["cwrap"] to the index.js build command in Makefile, that fixed it for me.

However, now I get the new error:

bundle.js:104 Assertion failed: native function `Chromagram_constructor` called before runtime initialization

I think this is being thrown from worker.js where the Chromagram instance is initialized.

And I can't find any solution for that. The FAQ suggests that error is from trying to use a C function before the WASM is loaded and initialized, but all the example code for waiting for initialization doesn't work with the browserify bundle.js and/or the web-worker, which seems to disable the Module['onRuntimeInitialized']() call.

I tried a ton of different things, to hold off initializing until the WASM can initialize, but nothing's worked.

@chrisspen
Copy link

It looks like something in the toolchain that builds bundle.js is creating buggy code.

The line:

fetch(wasmBinaryFile, { credentials: 'same-origin' }).then(function (response) {

was throwing a fetch error, so I printed out the value of wasmBinaryFile, and turns out it gets called twice.

Once with the value http://localhost:36275/docs/index.wasm, which succeeds, and once with index.wasm, which fails with:

Failed to execute 'fetch' on 'WorkerGlobalScope': Failed to parse URL from index.wasm

So fetch() is so poorly implemented, it can't understand relative URIs. I'm not sure how to workaround this. I tried hacking it to check for the missing "http://" and prepending window.location.href, but the window object doesn't exist yet.

If I hard-code it to my current URI, then that error is fixed.

I then rewrote worker.js into:

let cd = require("../index.js")

let chromagram;
let chordDetector;

cd.Module.onRuntimeInitialized = function() {
  console.log('onRuntimeInitialized')
  chromagram = new cd.Chromagram(1024, 44100)
  chordDetector = new cd.ChordDetector()
}

module.exports = function (self) {
  self.firstSampleArrivedAt = null;
  self.addEventListener('message', function (ev) {
    if (!ev.data.hasOwnProperty('audioData')) {
      console.log("expected audioData")
    }
    chromagram.processAudioFrame(ev.data.audioData)

    if (self.firstSampleArrivedAt == null) {
      self.firstSampleArrivedAt = ev.data.sentAt
    }

    if (!chromagram.isReady()) return

    currentChroma = chromagram.getChromagram()
    chordDetector.detectChord(currentChroma)
    self.postMessage({
      receivedAt: self.firstSampleArrivedAt,
      currentChroma: currentChroma,
      rootNote: chordDetector.rootNote(),
      quality: chordDetector.quality(),
      intervals: chordDetector.intervals()
    })
    self.firstSampleArrivedAt = null
  })
}

Which required modifying the bottom of chord_detector.js to export Module, so it looks like:

module.exports = {
  Chromagram: Chromagram,
  ChordDetector: ChordDetector,
  Module: Module
}

That fixed the remaining called before runtime initialization error.

However, the detector doesn't work. It doesn't produce any errors but it also doesn't seem to access the detector.

Any ideas what I'm doing wrong?

@chrisspen
Copy link

Seems I spoke too soon. I had commented out some code in demo.js, which I'd forgotten to re-enable and add back to bundle.js. When I re-enabled that, now the detector works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants