gopxl / beep

A little package that brings sound to any Go application. Suitable for playback and audio-processing.
MIT License
283 stars 13 forks source link

Can't compile to wasm #77

Open duysqubix opened 1 year ago

duysqubix commented 1 year ago

Hi ! I've been toying a little with Beep under Mac OS with success. Great lib !

Then I tried to compile my little hello world for wasm with

GOOS=js GOARCH=wasm go build -o tstbeep

and I got these errors

/Users/chris/go/pkg/mod/github.com/gopherjs/gopherwasm@v1.0.0/js/js_wasm.go:38:17: undefined: js.Callback
/Users/chris/go/pkg/mod/github.com/gopherjs/gopherwasm@v1.0.0/js/js_wasm.go:40:26: undefined: js.EventCallbackFlag
/Users/chris/go/pkg/mod/github.com/gopherjs/gopherwasm@v1.0.0/js/js_wasm.go:43:29: undefined: js.PreventDefault
/Users/chris/go/pkg/mod/github.com/gopherjs/gopherwasm@v1.0.0/js/js_wasm.go:44:29: undefined: js.StopPropagation
/Users/chris/go/pkg/mod/github.com/gopherjs/gopherwasm@v1.0.0/js/js_wasm.go:45:29: undefined: js.StopImmediatePropagation
/Users/chris/go/pkg/mod/github.com/gopherjs/gopherwasm@v1.0.0/js/js_wasm.go:49:9: undefined: js.NewCallback
/Users/chris/go/pkg/mod/github.com/gopherjs/gopherwasm@v1.0.0/js/js_wasm.go:53:9: undefined: js.NewEventCallback
/Users/chris/go/pkg/mod/github.com/gopherjs/gopherwasm@v1.0.0/js/js_wasm.go:64:19: undefined: js.TypedArray
/Users/chris/go/pkg/mod/github.com/gopherjs/gopherwasm@v1.0.0/js/js_wasm.go:67:9: undefined: js.TypedArrayOf

Most, if not all, of these functions have been deprecated for a while.

At first I thought it could be a problem related with Oto, so I compiled https://github.com/hajimehoshi/oto/blob/master/example/main.go to wasm and go it working (at least for Firefox, Chrome is more restrictive but that's not the point)

My tstbeep go.mod is simple as

module tstbeep

go 1.16

require (
    github.com/faiface/beep v1.0.2 // indirect
    github.com/hajimehoshi/oto v0.3.1 // indirect
)

I really don't see where gopherjs comes into the dance. Oto doesn't seems to use it, neither Beep...

So, because I had installed (and never used) gopherjs : rm -rf gopherjs ~/go/bin directory and rm -rf gopherjs ~/go/pkg/mod/github.com/gopherjs

And issuing a GOOS=js GOARCH=wasm go build -o tstbeep

resurrected ~/go/pkg/mod/github.com/gopherjs

Any clue ?

Original issue: https://github.com/faiface/beep/issues/113

justgook commented 2 months ago

I made it compile and work under wasm - main issue is bufferSize - it must be 0 or a power of two between 256 and 16384.

I didn't investigate what happens under hood, but by brute force managed to make it work.

if you setup buffer size to 8194 (that then in browser becomes 4096, everything works (or at least "hello beep")

 speaker.Init(format.SampleRate, 8194)