rserota / wad

Web Audio DAW. Use the Web Audio API for dynamic sound synthesis. It's like jQuery for your ears.
MIT License
1.88k stars 160 forks source link

import Wad from 'web-audio-daw' throws ReferenceError: window is not defined #152

Closed whitaiji closed 10 months ago

whitaiji commented 11 months ago

Hi! I have just installed your library, as soon as I import Wad from 'web-audio-daw'

I get a "ReferenceError: window is not defined" in node_modules/web-audio-daw/build/wad.js (10:4)

I am using NextJs v 12.1.0. Any idea what it could be?

thank you for your help

Mike

Screenshot 2023-08-22 at 00 11 56
whitaiji commented 11 months ago

I have found a work around, looking around the net. I first tried importing with next/dynamic

const Wad = dynamic(() => import('web-audio-daw'), { ssr: false })

but would not import as a constructor, no luck there.

So after a few more searches, I managed to make it work this way, without importing Wad at the top:

const MusicPlayer = () => {

  const [wad, setWad] = useState()

 useEffect(()=>{
    const init = async () => {

    const WadConstructor = (await import('web-audio-daw')).default;
    const wads = {
        saw: new WadConstructor({source : 'sawtooth'})
    }
    setWad(wads)

    }
    init()
 },[])

  const testFunction = () => {
    console.log(wad)
    wad.saw.play()
  }
  return (
    <div>
      <button onClick={testFunction}>test button music player</button>
    </div>
  )
}

export default MusicPlayer

Sound played, so far so good. But of course a bug fix would be much appreciated great work by the way, love it!

all the best

Mike