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

Document how to use AudioStreamGenerator #76373

Merged

Conversation

MewPurPur
Copy link
Contributor

@MewPurPur MewPurPur commented Apr 23, 2023

This class doesn't have many (if any) resources online for how to use it. Other than our own, linked under tutorials:

  • The first link is to a demo, which is alright, but I find it overkill since this class works from script without assets or complex node configurations.
  • The second link is Juan's blog post from long ago when he announced the feature. The page itself claims it might be outdated - and indeed it is. The code also doesn't abide by GDScript's style guide, has a typo that results in an error, and... it can be better.

I changed up the description (to remove any confusion that AudioStreamGenerator produces sounds on its own). I also added a simple GDScript example and removed the second link.

@MewPurPur MewPurPur requested a review from a team as a code owner April 23, 2023 15:27
@Chaosus Chaosus added this to the 4.1 milestone Apr 23, 2023
@MewPurPur MewPurPur force-pushed the add-AudioStreamGenerator-example branch from b81775f to 26d0e95 Compare April 23, 2023 15:38
@mieldepoche
Copy link
Contributor

your code snippet is invalid, since you have to call play on the audioStreamPlayer before getting the stream playback (at least when using an audioStreamGenerator). No idea if it's a bug or something, but that's how it is. From what I get this means that you can't pre-fill a buffer? idk.
Here's a snippet that works (in 4.0.2):

@onready var sample_hz: float = $AudioStreamPlayer.stream.mix_rate
var playback: AudioStreamGeneratorPlayback
var pulse_hz := 440.0 # The frequency of the sound wave.


func _ready() -> void:
    $AudioStreamPlayer.play()
    playback = $AudioStreamPlayer.get_stream_playback()
    fill_buffer()


func fill_buffer():
    var phase := 0.0
    var increment := pulse_hz / sample_hz
    var frames_available := playback.get_frames_available()

    for i in range(frames_available):
        playback.push_frame(Vector2.ONE * sin(phase * TAU))
        phase = fmod(phase + increment, 1.0)

@MewPurPur
Copy link
Contributor Author

MewPurPur commented Apr 23, 2023

I think it's a bug. Neither Juan's announcement nor the demo do this. I believe it shouldn't be the case with AudioStreamGenerator, as the announcement moreover claims:

Just call this function before AudioStreamPlayer.play() (to ensure buffer is full before playback begins), and then call it from _process(), to ensure it remains full.

For reference: The error thrown is this:

ERR_FAIL_COND_V_MSG(stream_playbacks.is_empty(), Ref<AudioStreamPlayback>(), "Player is inactive. Call play() before requesting get_stream_playback().");

It was added 3 months ago in #71906

@MewPurPur MewPurPur force-pushed the add-AudioStreamGenerator-example branch from 26d0e95 to 238461f Compare April 30, 2023 00:27
@MewPurPur
Copy link
Contributor Author

Fixed a typo and addressed the concerns above.

@MewPurPur MewPurPur force-pushed the add-AudioStreamGenerator-example branch from 238461f to 9d77caa Compare April 30, 2023 18:19
@mhilbrunner mhilbrunner merged commit 64eeb04 into godotengine:master May 6, 2023
@mhilbrunner
Copy link
Member

Thanks! Merged.

@MewPurPur MewPurPur deleted the add-AudioStreamGenerator-example branch May 6, 2023 15:43
@akien-mga
Copy link
Member

Cherry-picked for 4.0.3.

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.

5 participants