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

Add AudioStreamPolyphonic to simplify sound playback from code #71855

Merged
merged 1 commit into from
Jan 22, 2023

Conversation

reduz
Copy link
Member

@reduz reduz commented Jan 22, 2023

  • This new audio stream allows to play multiple sounds and control them over time from code.
  • It greatly simplifies tasks such as generative music (music generated from code) or audio.

This new type of stream was added with the goal of fixing audio blending in AnimationPlayer and AnimationTree, but can be used by others for their regular audio needs.

Does not fix anything currently, but should help implement #69758 properly.

Some demo code of how to use this:

var player = $SomeNode as AudioStreamPlayer
player.stream = AudioStreamPolyphonic.new()
var playback = player.get_stream_playback() as AudioStreamPlaybackPolyphonic
var id = playback.play_stream(preload("res://Clip1.ogg"))
await get_tree().create_timer(1).timeout
playback.set_stream_volume(id,-6) # Set volume to half after one second
await get_tree().create_timer(2).timeout
var id2 = playback.play_stream(preload("res://Clip2.ogg")) # 2 seconds later, start another clip
await get_tree().create_timer(1).timeout
playback.stop_stream(id) # 1 second later, kill the first clip
playback.set_stream_pitch_scale(id2,1.5) # Make the second clip go 50% faster

@reduz reduz requested a review from a team as a code owner January 22, 2023 12:58
@reduz reduz force-pushed the audio-stream-polyphonic branch 2 times, most recently from 098a537 to ee478b2 Compare January 22, 2023 13:12
@Calinou
Copy link
Member

Calinou commented Jan 22, 2023

Does the AnimationPlayer's audio playback tracks support this new stream? I've tried to use AnimationPlayer with AudioStreamPlayer as part of a demo project recently, but found that it didn't support polyphony (old sounds were replaced by new sounds as they were played, even if Max Polyphony was increased above 1 on the AudioStreamPlayer).

@Calinou Calinou added this to the 4.0 milestone Jan 22, 2023
@reduz reduz force-pushed the audio-stream-polyphonic branch from ee478b2 to b77a533 Compare January 22, 2023 13:16
@reduz
Copy link
Member Author

reduz commented Jan 22, 2023

@Calinou So far this PR does not change animation code, but the idea is that it blends the audio from one animation to another (or lets it finish instead of killing it). Currently only one stream can be played at the same time always.

@TokageItLab
Copy link
Member

TokageItLab commented Jan 22, 2023

Previously, the streams in AudioTrack were set directly to AudioStreamPlayer.

If I understand correctly, after this PR, we can just set AudioStreamPolyphonic which is made by AnimationPlayer/Tree internally to AudioStreamPlayer once. Then AnimationPlayer/Tree will handle the AudioStreamPolyphonic via AudioStreamPolyphonicPlayback to play multiple streams.

@reduz Is it right? If no problem, I will try to work on it later.

@reduz
Copy link
Member Author

reduz commented Jan 22, 2023

@TokageItLab that sounds great!

Copy link
Member

@TokageItLab TokageItLab left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have been explained this implementation at ContributorsChat and I think that it seems there is no missing for solving #69758.

After this is merged, AnimationPlayer and AnimationTree will need to be changed to use this.

…from code

* This new audio stream allows to play multiple sounds and control them over time from code.
* It greatly simplifies tasks such as generative music (music generated from code) or audio.

This new type of stream was added with the goal of fixing audio blending in AnimationPlayer and AnimationTree, but can be used by others for their regular audio needs.

Does not fix anything currently, but should help implement godotengine#69758 properly.

Some demo code of how to use this:

```GDScript

var player = $SomeNode as AudioStreamPlayer
player.stream = AudioStreamPolyphonic.new()
var playback = player.get_stream_playback() as AudioStreamPlaybackPolyphonic
var id = playback.play_stream(preload("res://Clip1.ogg"))
await get_tree().create_timer(1).timeout
playback.set_stream_volume(id,-12) # Set volume to half after one second
await get_tree().create_timer(2).timeout
var id2 = playback.play_stream(preload("res://Clip2.ogg")) # 2 seconds later, start another clip
await get_tree().create_timer(1).timeout
playback.stop_stream(id) # 1 second later, kill the first clip
playback.set_stream_pitch_scale(id2,1.5) # Make the second clip go 50% faster

```
@reduz reduz force-pushed the audio-stream-polyphonic branch from b77a533 to f18d408 Compare January 22, 2023 15:23
@akien-mga akien-mga merged commit 78d45fc into godotengine:master Jan 22, 2023
@akien-mga
Copy link
Member

Thanks!

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

Successfully merging this pull request may close these issues.

4 participants