Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify usage of tambo from phetlib #175

Open
jbphet opened this issue Feb 22, 2023 · 0 comments
Open

Simplify usage of tambo from phetlib #175

jbphet opened this issue Feb 22, 2023 · 0 comments

Comments

@jbphet
Copy link
Contributor

jbphet commented Feb 22, 2023

I am currently using tambo out of phetlib in the paper programming project (the repo for this is called papyrus at the moment, but there is talk of changing it). It was a bit challenging to get it working, and I think we could probably make it easier, but I'm not sure how much of a priority that is.

For reference, here is the code that was needed to initialize the sound manager and enable sound generation:

// Initialize sound production.
const TRUE_PROPERTY = new axon.BooleanProperty( true );
const FALSE_PROPERTY = new axon.BooleanProperty( false );
tambo.soundManager.enabledProperty.value = true;
tambo.soundManager.initialize( TRUE_PROPERTY, TRUE_PROPERTY, TRUE_PROPERTY, TRUE_PROPERTY, FALSE_PROPERTY );

This code is basically faking out all of the mechanisms that prevent tambo from producing sound when we don't want it to, like when the user has turned sound off, or the tab isn't showing, and so forth.

Also, it is a bit tricky to use a sound clip and other types that rely on pre-recorded sounds (generally MP3 files) because we use something called a WrappedAudioBuffer to handle the asynchronous decode and loading of audio samples. The code to make this work looked like this:

/**
 * Create an instance of WrappedAudioBuffer and return it, and start the process of decoding the audio file from the
 * provided path and load it into the buffer when complete.  Instances of WrappedAudioBuffer are often needed for
 * creating sounds using the tambo library.
 * @param {string} pathToAudioFile
 */
const createAndLoadWrappedAudioBuffer = pathToAudioFile => {
  const wrappedAudioBuffer = new tambo.WrappedAudioBuffer();

  window.fetch( pathToAudioFile )
    .then( response => response.arrayBuffer() )
    .then( arrayBuffer => tambo.phetAudioContext.decodeAudioData( arrayBuffer ) )
    .then( audioBuffer => {
      wrappedAudioBuffer.audioBufferProperty.value = audioBuffer;
    } );

  return wrappedAudioBuffer;
};

const testSoundClip = new SoundClip( createAndLoadWrappedAudioBuffer( someFile.mp3 );

const onButtonClicked = () => { testSoundClip.play(); };

We could probably add some sort of method like initializeForLibraryUse that creates the stub properties, and we could potentially provide the createAndLoadWrappedAudioBuffer function, and there may some other improvements as well.

I'll put this up for discussion at the next developer meeting since, as mentioned above, I have no idea if this is important enough to spend time on.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant