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

Sound.play() ignores existing volume set by Sound.setVolume() #127

Open
michael-dean-haynie opened this issue Dec 12, 2024 · 2 comments
Open

Comments

@michael-dean-haynie
Copy link

michael-dean-haynie commented Dec 12, 2024

The Sound class exposes a method (setVolume()) which tweaks its gainNode.
It seems like the intent of the setVolume() method is to pre-configure the volume of the Sound instance so that subsequent calls to its play() method use the configured volume.

However, the play() method accepts its own volume param which is overwriting the existing gain value that was set during the setVolume() method call.

Here is where the value is being overwritten:

gainNode.gain.value = volume;

Example code:

const sound = new SoundWave('sound.mp3');
sound.setVolume(0.01);
sound.play()

Expected Outcome:
The sound should be played at a volume much lower than the default.

Actual Outcome:
The sound seems to be played at the default volume.

@michael-dean-haynie
Copy link
Author

michael-dean-haynie commented Dec 12, 2024

I'm updating the example code to drastically reduce the volume so that it's more obvious that the setVolume() value is not being used.

TIL that decibels logarithmic, not linear. Scaling the gain by 0.5 might not produce noticeable change.

@KilledByAPixel
Copy link
Owner

This is because setVolume is only really used to change the volume of a sound after it's been played for sounds where you only have one instance of it like music for example. Typically you control the volume by passing it into the play function.

I'm not sure how to make this work in a better way because you need to be able to set the volume when a sound is played but you also need to be able to change that volume afterwards sometimes like if the music volume is changed by the player.

I need to do some thinking on the sound object itself because it is kind of doing double duty by also allowing changes to the most recently played instance of that sound.

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

2 participants