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

Audio Detune (now wide browser support) #135

Closed
2 tasks done
F3der1co opened this issue Feb 15, 2024 · 5 comments
Closed
2 tasks done

Audio Detune (now wide browser support) #135

F3der1co opened this issue Feb 15, 2024 · 5 comments
Labels
declined Feature requests that will not be acted upon

Comments

@F3der1co
Copy link

F3der1co commented Feb 15, 2024

Reviewed guidelines

  • I have read and understand the suggestion guidelines

Checked for duplicate suggestions

  • I checked for existing similar suggestions

Summary

Add the web audio API detune. This is useful to change pitch without changing length of the sound.

Possible workarounds or alternatives

Import sound pitch shifted, download size and maintainability suboptimal.
Change playback rate, but that changes length of the sound.

Proposed solution

Add the web audio API detune, as it now has wide browser support according to https://developer.mozilla.org/en-US/docs/Web/API/AudioBufferSourceNode/detune

Why is this idea important?

Change pitch of a sound without changing its length, useful for adding variance to sound effects and games with music elements (i.e. you can play a piano in game)

Additional remarks

No response

@F3der1co
Copy link
Author

F3der1co commented Feb 15, 2024

edit: I am not sure anymore if detune doesn't affect playback rate as well, that would make it less useful. Would have to be tested

@Jase0000
Copy link

Deeply interested in this, I'm praying it's a trivial thing to add, but understood if not.

Iirc depending on if you have audio in Sounds or Music folder, it will do different effects when using the PlaybackRate, I believe it time-stretches with no pitch shift for Music, and pitches up/down for Sounds.

I believe this was the case, as I opted for putting my music in the Sounds folder for this very reason (wanted pitch shifting), which I do understand could be a very bad idea, especially if music is very long and gets loaded into memory.

@Wackytoaster
Copy link

Wackytoaster commented Apr 6, 2024

Yes please :) I have just recently added some dynamic sounds and my other option would be to add like 30 manually pitch-shifted versions. Changing the playback rate has obvious unwanted sideeffects for that purpose.

@AshleyScirra AshleyScirra added the declined Feature requests that will not be acted upon label Jun 13, 2024
@AshleyScirra
Copy link
Member

I prototyped this but quickly found out that the Web Audio API actually does not support pitch shifting at all at the moment. The detune parameter works the same as playbackRate - it just works in different units (cents instead of a ratio). So unfortunately the proposal is incorrect in stating that there is now a broadly supported API that can do pitch shifting. A further complication is that Construct plays music tracks with HTMLAudioElement instead of the Web Audio API, and that interface does not provide a detune parameter, so even if it did work as expected with the Web Audio API, it would only be supported for sounds and not for music - which I believe would be a new inconsistency, as IIRC all existing audio features work equally for sounds and music.

HTMLAudioElement does have a preservesPitch property which allows choosing whether altering the playback rate affects pitch or keeps the same pitch. However this does not allow an arbitrary pitch shift, and the Web Audio API does not appear to have any equivalent.

Pitch shifting audio appears to be a complex algorithm (granular synthesis came up in my research, and I think there are other approaches). It is probably possible to implement a custom pitch shifting algorithm with audio worklets, but I'd prefer not to build in such an approach, as it means integrating a complex library, and then has long-term compatibility implications if browsers do add the necessary feature but with a different approach. So I would prefer to only make this built-in if it's provided by a built-in browser API. Meanwhile it's possible to explore the custom audio processing approach with Construct's JavaScript coding feature, as you can access all the same browser APIs with that.

For this to be implementable I think there are two approaches that could do it:

  • both HTMLAudioElement and Web Audio API add support for a parameter that does actually do pitch shifting (which detune currently does not), perhaps both including a preservesPitch setting
  • the Web Audio API adds support for a pitch-shifting node (which can cover HTMLAudioElement as its audio output can be piped in to the Web Audio API)

Either way this will need a change to the specification and support added by browsers. The relevant spec issues appear to be:
WebAudio/web-audio-api#2443
WebAudio/web-audio-api#2487

Therefore I'm afraid this is not something we will consider implementing until the necessary web APIs are in place, so closing this request as declined. If the necessary web APIs are added, you can re-post this and we will reconsider.

(On a side note: this is a classic example of how a simple-looking feature ends up being complicated, and it also illustrates how much prototyping and research work an apparently simple-looking feature request can end up taking, which is why it is difficult for us to respond to feature requests given we receive hundreds of them.)

@F3der1co
Copy link
Author

F3der1co commented Jun 13, 2024

The description on the MDN web docs made it sound like it does just do pitch shifting, but apparently that's not the case.
Thanks for the update and prototyping it. I hope this comes to browsers one day.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
declined Feature requests that will not be acted upon
Projects
None yet
Development

No branches or pull requests

4 participants