From aaafd163b29180090161c0169c70ab2772dfdcd7 Mon Sep 17 00:00:00 2001 From: Adam Scott Date: Tue, 20 Aug 2024 10:29:32 -0400 Subject: [PATCH] Honor `pitch_scale` value before playing audio sample --- platform/web/audio_driver_web.cpp | 1 + platform/web/godot_audio.h | 2 +- platform/web/js/libs/library_godot_audio.js | 10 +++++++--- scene/audio/audio_stream_player_internal.cpp | 1 + servers/audio/audio_stream.h | 1 + 5 files changed, 11 insertions(+), 4 deletions(-) diff --git a/platform/web/audio_driver_web.cpp b/platform/web/audio_driver_web.cpp index 5e046d7050d..0108f407262 100644 --- a/platform/web/audio_driver_web.cpp +++ b/platform/web/audio_driver_web.cpp @@ -294,6 +294,7 @@ void AudioDriverWeb::start_sample_playback(const Ref &p_pla itos(p_playback->stream->get_instance_id()).utf8().get_data(), AudioServer::get_singleton()->get_bus_index(p_playback->bus), p_playback->offset, + p_playback->pitch_scale, volume_ptrw); } diff --git a/platform/web/godot_audio.h b/platform/web/godot_audio.h index 4961ebd2bb7..f5a2a856052 100644 --- a/platform/web/godot_audio.h +++ b/platform/web/godot_audio.h @@ -51,7 +51,7 @@ extern void godot_audio_input_stop(); extern int godot_audio_sample_stream_is_registered(const char *p_stream_object_id); extern void godot_audio_sample_register_stream(const char *p_stream_object_id, float *p_frames_buf, int p_frames_total, const char *p_loop_mode, int p_loop_begin, int p_loop_end); extern void godot_audio_sample_unregister_stream(const char *p_stream_object_id); -extern void godot_audio_sample_start(const char *p_playback_object_id, const char *p_stream_object_id, int p_bus_index, float p_offset, float *p_volume_ptr); +extern void godot_audio_sample_start(const char *p_playback_object_id, const char *p_stream_object_id, int p_bus_index, float p_offset, float p_pitch_scale, float *p_volume_ptr); extern void godot_audio_sample_stop(const char *p_playback_object_id); extern void godot_audio_sample_set_pause(const char *p_playback_object_id, bool p_pause); extern int godot_audio_sample_is_active(const char *p_playback_object_id); diff --git a/platform/web/js/libs/library_godot_audio.js b/platform/web/js/libs/library_godot_audio.js index 0ba6eed4640..40fb0c356ca 100644 --- a/platform/web/js/libs/library_godot_audio.js +++ b/platform/web/js/libs/library_godot_audio.js @@ -328,6 +328,7 @@ class SampleNodeBus { * offset?: number * playbackRate?: number * startTime?: number + * pitchScale?: number * loopMode?: LoopMode * volume?: Float32Array * start?: boolean @@ -438,7 +439,7 @@ class SampleNode { /** @type {LoopMode} */ this.loopMode = options.loopMode ?? this.getSample().loopMode ?? 'disabled'; /** @type {number} */ - this._pitchScale = 1; + this._pitchScale = options.pitchScale ?? 1; /** @type {number} */ this._sourceStartTime = 0; /** @type {Map} */ @@ -1648,13 +1649,14 @@ const _GodotAudio = { }, godot_audio_sample_start__proxy: 'sync', - godot_audio_sample_start__sig: 'viiiii', + godot_audio_sample_start__sig: 'viiiifi', /** * Starts a sample. * @param {number} playbackObjectIdStrPtr Playback object id pointer * @param {number} streamObjectIdStrPtr Stream object id pointer * @param {number} busIndex Bus index * @param {number} offset Sample offset + * @param {number} pitchScale Pitch scale * @param {number} volumePtr Volume pointer * @returns {void} */ @@ -1663,6 +1665,7 @@ const _GodotAudio = { streamObjectIdStrPtr, busIndex, offset, + pitchScale, volumePtr ) { /** @type {string} */ @@ -1671,11 +1674,12 @@ const _GodotAudio = { const streamObjectId = GodotRuntime.parseString(streamObjectIdStrPtr); /** @type {Float32Array} */ const volume = GodotRuntime.heapSub(HEAPF32, volumePtr, 8); - /** @type {SampleNodeConstructorOptions} */ + /** @type {SampleNodeOptions} */ const startOptions = { offset, volume, playbackRate: 1, + pitchScale, start: true, }; GodotAudio.start_sample( diff --git a/scene/audio/audio_stream_player_internal.cpp b/scene/audio/audio_stream_player_internal.cpp index 206408e3a77..7d1ed56ca8e 100644 --- a/scene/audio/audio_stream_player_internal.cpp +++ b/scene/audio/audio_stream_player_internal.cpp @@ -152,6 +152,7 @@ Ref AudioStreamPlayerInternal::play_basic() { Ref sample_playback; sample_playback.instantiate(); sample_playback->stream = stream; + sample_playback->pitch_scale = pitch_scale; stream_playback->set_sample_playback(sample_playback); } } else if (!stream->is_meta_stream()) { diff --git a/servers/audio/audio_stream.h b/servers/audio/audio_stream.h index d02dc6aae7d..c41545aeba4 100644 --- a/servers/audio/audio_stream.h +++ b/servers/audio/audio_stream.h @@ -50,6 +50,7 @@ public: Ref stream; float offset = 0.0f; + float pitch_scale = 1.0; Vector volume_vector; StringName bus; };