phetsims / wave-interference

"Wave Interference" is an educational simulation in HTML5, by PhET Interactive Simulations.
MIT License
19 stars 5 forks source link

The single wavefront sound pitch should fade gradually instead of instantly cutting off. #386

Closed samreid closed 5 years ago

samreid commented 5 years ago

From this week's sonification meeting:

@Ashton-Morris: The single wavefront sound pitch should fade gradually instead of instantly cutting off. @samreid: Maybe all tone can fade out?

samreid commented 5 years ago

I wonder if this will be addressed by https://github.com/phetsims/wave-interference/issues/384.

samreid commented 5 years ago

@jbphet and I addressed the clicks on oscillator stop in #384, perhaps this is addressed too. @Ashton-Morris can you please listen and let me know if this issue can be closed?

Ashton-Morris commented 5 years ago

@samreid (Chrome) The first time I turn on the Play Tone box after already having it previously turned on once, I do get a pop. After that initial pop I get no more no mater how many times I turn it on and off.

(Safari) Every time I turn off the Play Sound button I get a click/pop.

I'm not sure what your target browsers are? But I test in Chrome and Safari usually

samreid commented 5 years ago

I can reproduce both those problems, thanks! I've been primarily developing on Chrome but we are targeting the platforms listed here: https://phet.colorado.edu/en/help-center/running-sims

samreid commented 5 years ago

I tried adding a delay to the start like so:

this.oscillator.start( this.audioContext.currentTime + soundConstants.LINEAR_GAIN_CHANGE_TIME );

But now it pops every time on chrome instead of only the 2nd time. @jbphet can you help me investigate?

jbphet commented 5 years ago

I set the initial output level of the sound generator to zero and made the amplitude-to-output-level mapping more consistent, and I'm not hearing any popping in Chrome, but I still hear it in Safari. I think this is likely related to https://github.com/phetsims/tambo/issues/61. I've used all my sonification time for this week (and more), so I won't get to this again until next week. @samreid - please let me know if this is a problem.

samreid commented 5 years ago

I tried a minimal test case on Safari, and I am still hearing the pop/click, especially when the sound ends:

<head></head>
<body>
<button id="playButton">Play</button>
<button id="stopButton">Stop</button>
<script>

  // create web audio api context
  var audioCtx = null;

  var oscillators = [];
  document.getElementById( 'playButton' ).addEventListener( 'click', () => {

    audioCtx = audioCtx || ( new ( window.AudioContext || window.webkitAudioContext )() );
    // create Oscillator node
    var oscillator = audioCtx.createOscillator();

    oscillator.type = 'sine';
    oscillator.frequency.setValueAtTime( 440, audioCtx.currentTime ); // value in hertz
    oscillator.connect( audioCtx.destination );
    oscillator.start();
    oscillators.push( oscillator );
  } );

  document.getElementById( 'stopButton' ).addEventListener( 'click', () => {
    oscillators.forEach( oscillator => {
      oscillator.stop();
    } );
    oscillators.length = 0;
  } );

</script>
</body>

Should we try handling this manually via an explicit GainNode that we turn down before stopping the oscillator?

jbphet commented 5 years ago

This issue was due in part to differences between how the browsers implement the linearRampToValueAtTime method for audio parameters. I logged an issue with the Web Audio folks on this topic at https://github.com/WebAudio/web-audio-api/issues/1842, but it is unlikely to be consistent across all browsers any time soon. I added something that sets the gain to the value that it is supposed to be before initiating the linear gain change, and that seems to have fixed the problem. This is because this creates an "event" from which the linear gain change can extrapolate. I also lengthened the time for the linear change. In my subsequent testing, the start and stop of the sound was smooth and pop free on Chrome+Win10, Firefox+Win10, and Safari+MacOS. @samreid - can you try this out and see if it seems fixed for you too?

samreid commented 5 years ago

Sounds great in Mac safari, chrome and firefox. @jbphet can this issue be closed?

jbphet commented 5 years ago

Yep. Closing.