Tonejs / Tone.js

A Web Audio framework for making interactive music in the browser.
https://tonejs.github.io
MIT License
13.4k stars 976 forks source link

start() and stop() not working correctly applied to a panner3d #1057

Closed PrNotGood closed 2 years ago

PrNotGood commented 2 years ago

Describe the bug

Hi, I'm currently try to implement audio generation with ToneJS on a site developed in Aframe (Networked Aframe to be precise), the basic idea was to create sounds when clicking an entity, which seems to work, but when I apply this same concept to an oscillator, the methods start() and stop() don't seem to work properly (sometimes they doen't seem to activate, resulting in multiple audio overlapped or no audio reproduced). I'm using a panner3d in order to give them positionality, it could easily be that I'm using something incorrectly. I created this looking at the source of the example with the 3d Panner

To Reproduce

Unfortunately I cannot provide a direct editor, though here is the part of my code that gives me problem:

/**/
Tone.Transport.start();
now = Tone.now();

/* The variable I use to toggle the audio */ 
riproduciosc = 0;

AFRAME.registerComponent("addpannerosc", {
  init: function(){

    pannerosc = new Tone.Panner3D(this.el.object3D.position.x, this.el.object3D.position.y, this.el.object3D.position.z).toDestination();
    osc = new Tone.Oscillator(220, "sine").sync().connect(pannerosc); 

    this.el.addEventListener("click", function(){
      if(riproduciosc){
        osc.stop();
        riproduciosc = 0;
      }
      else{
        osc.start();
        riproduciosc = 1;
      }
    });
  }
});

<a-entity id="osc1" geometry="primitive:box;" material="color: #4CC3D9" position="-1 0.5 -3" addpannerosc></a-entity>

Expected behavior Just reproduce the sound on click and to stop it on the next click

What I've tried It seems to work correctly without all the panner3d, but it kinda defeat the idea of 3d audio, I couldn't find anything online

tambien commented 2 years ago

try removing sync() from the oscillator, that sync's it to the Tone.Transport which is going to change the behavior of start/stop so that it is aligned with the start/stop of the transport. It essentially schedules the start/stop events to the transport. If you want it to respond to a click handler, you should probably remove it.

PrNotGood commented 2 years ago

Thank you, I was sure that I tried it, but apparently not. It seems that now it is working correctly.