williamngan / pts

A library for visualization and creative-coding
https://ptsjs.org
Apache License 2.0
5.17k stars 182 forks source link

Sound.start and Sound.stop do not account for nodes added with Sound.connect #133

Closed prkirby closed 3 years ago

prkirby commented 3 years ago

Currently, Sound.connect is provided to add an AudioNode to the node generated. However, Sound.start and Sound.stop do not account for this, and always connect or disconnect the Sound's _node directly to the AudioContext.destination.

This can result in adding something simple like a GainNode to a loaded audio track, and banging your head against the desk trying to figure out why its not doing anything. I suggest creating a function that allows you to update the Sound instances 'output node', (last AudioNode in the chain that you've connected), and reference that in the start and stop functions.

@williamngan would you like me to open a PR for this? It shouldn't be too hard to fix. lmk.

Code that was failing for reference:

    Sound.load(SONG).then((song) => {
      const ctx = song.ctx
      const gainNode = ctx.createGain()
      song.connect(gainNode)
      gainNode.gain.value = 0.1
      song.start()
    })

Hacky workaround:

    Sound.load(SONG).then((song) => {
      const ctx = song.ctx
      const gainNode = ctx.createGain()
      song.connect(gainNode)
      gainNode.gain.value = 0.1
      ctx.resume().then(() => {
        song.start()
        song.node.disconnect(ctx.destination)
        gainNode.connect(ctx.destination)
      })
    })
williamngan commented 3 years ago

Hi @prkirby -- thanks for reporting this and sending a PR. I'm review and test it this weekend. Cheers!

williamngan commented 3 years ago

I've merged the PR, and will update the docs and guide. This fix should be in the next release (v0.10) in a few days. Thanks again!

prkirby commented 3 years ago

@williamngan Awesome! Thanks for the quick response. Let me know if you want help with any other issues, I'm really impressed with this library. I just stumbled across it a few days ago, but its been a pleasure to learn. I'm pretty interested in potentially using a library like this in a node environment to control some midi or serial output to connect to other interfaces. Would be more than happy to help collab.

williamngan commented 3 years ago

Thank you so much @prkirby. I'm glad to hear that you're enjoying the library. If you encounter bugs or have questions, please continue to file issues here.

Would love to hear your thoughts on using Pts in node environment to connect to midi devices. I don't have enough knowledge in midi and procedural audio so I'm not sure if I can give you relevant feedbacks. But if you've created a sound library that works with Pts, let me know and I'll be happy to add it to Pts website.

There's currently a bug that prevented using Pts in node which should be fixed in next release. Thanks again!

prkirby commented 3 years ago

Awesome, will do. I was thinking in a more generic midi output interface, to connect with hardware or other software. However that would likely need to be supported by another 3rd party library. I will look into it, see what creating another Space abstraction would look like for something like that. Thanks again.