Skip to content

Commit

Permalink
Add stop_callable to AudioStreamPlayerInternal
Browse files Browse the repository at this point in the history
  • Loading branch information
kus04e4ek committed Aug 11, 2024
1 parent 88f3b5f commit 8c7dd3c
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 14 deletions.
4 changes: 2 additions & 2 deletions scene/2d/audio_stream_player_2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ void AudioStreamPlayer2D::seek(float p_seconds) {

void AudioStreamPlayer2D::stop() {
setplay.set(-1);
internal->stop();
internal->stop_basic();
}

bool AudioStreamPlayer2D::is_playing() const {
Expand Down Expand Up @@ -430,7 +430,7 @@ void AudioStreamPlayer2D::_bind_methods() {
}

AudioStreamPlayer2D::AudioStreamPlayer2D() {
internal = memnew(AudioStreamPlayerInternal(this, callable_mp(this, &AudioStreamPlayer2D::play), true));
internal = memnew(AudioStreamPlayerInternal(this, callable_mp(this, &AudioStreamPlayer2D::play), callable_mp(this, &AudioStreamPlayer2D::stop), true));
cached_global_panning_strength = GLOBAL_GET("audio/general/2d_panning_strength");
set_hide_clip_children(true);
}
Expand Down
4 changes: 2 additions & 2 deletions scene/3d/audio_stream_player_3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,7 @@ void AudioStreamPlayer3D::seek(float p_seconds) {

void AudioStreamPlayer3D::stop() {
setplay.set(-1);
internal->stop();
internal->stop_basic();
}

bool AudioStreamPlayer3D::is_playing() const {
Expand Down Expand Up @@ -862,7 +862,7 @@ void AudioStreamPlayer3D::_bind_methods() {
}

AudioStreamPlayer3D::AudioStreamPlayer3D() {
internal = memnew(AudioStreamPlayerInternal(this, callable_mp(this, &AudioStreamPlayer3D::play), true));
internal = memnew(AudioStreamPlayerInternal(this, callable_mp(this, &AudioStreamPlayer3D::play), callable_mp(this, &AudioStreamPlayer3D::stop), true));
velocity_tracker.instantiate();
set_disable_scale(true);
cached_global_panning_strength = GLOBAL_GET("audio/general/3d_panning_strength");
Expand Down
4 changes: 2 additions & 2 deletions scene/audio/audio_stream_player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ void AudioStreamPlayer::seek(float p_seconds) {
}

void AudioStreamPlayer::stop() {
internal->stop();
internal->stop_basic();
}

bool AudioStreamPlayer::is_playing() const {
Expand Down Expand Up @@ -283,7 +283,7 @@ void AudioStreamPlayer::_bind_methods() {
}

AudioStreamPlayer::AudioStreamPlayer() {
internal = memnew(AudioStreamPlayerInternal(this, callable_mp(this, &AudioStreamPlayer::play), false));
internal = memnew(AudioStreamPlayerInternal(this, callable_mp(this, &AudioStreamPlayer::play), callable_mp(this, &AudioStreamPlayer::stop), false));
}

AudioStreamPlayer::~AudioStreamPlayer() {
Expand Down
13 changes: 7 additions & 6 deletions scene/audio/audio_stream_player_internal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ Ref<AudioStreamPlayback> AudioStreamPlayerInternal::play_basic() {
}
ERR_FAIL_COND_V_MSG(!node->is_inside_tree(), stream_playback, "Playback can only happen when a node is inside the scene tree");
if (stream->is_monophonic() && is_playing()) {
stop();
stop_callable.call();
}
stream_playback = stream->instantiate_playback();
ERR_FAIL_COND_V_MSG(stream_playback.is_null(), stream_playback, "Failed to instantiate playback.");
Expand Down Expand Up @@ -242,7 +242,7 @@ void AudioStreamPlayerInternal::set_stream(Ref<AudioStream> p_stream) {
if (stream.is_valid()) {
stream->disconnect(SNAME("parameter_list_changed"), callable_mp(this, &AudioStreamPlayerInternal::_update_stream_parameters));
}
stop();
stop_callable.call();
stream = p_stream;
_update_stream_parameters();
if (stream.is_valid()) {
Expand All @@ -253,12 +253,12 @@ void AudioStreamPlayerInternal::set_stream(Ref<AudioStream> p_stream) {

void AudioStreamPlayerInternal::seek(float p_seconds) {
if (is_playing()) {
stop();
stop_callable.call();
play_callable.call(p_seconds);
}
}

void AudioStreamPlayerInternal::stop() {
void AudioStreamPlayerInternal::stop_basic() {
for (Ref<AudioStreamPlayback> &playback : stream_playbacks) {
AudioServer::get_singleton()->stop_playback_stream(playback);
}
Expand Down Expand Up @@ -289,7 +289,7 @@ void AudioStreamPlayerInternal::set_playing(bool p_enable) {
if (p_enable) {
play_callable.call(0.0);
} else {
stop();
stop_callable.call();
}
}

Expand Down Expand Up @@ -339,9 +339,10 @@ StringName AudioStreamPlayerInternal::get_bus() const {
return SceneStringName(Master);
}

AudioStreamPlayerInternal::AudioStreamPlayerInternal(Node *p_node, const Callable &p_play_callable, bool p_physical) {
AudioStreamPlayerInternal::AudioStreamPlayerInternal(Node *p_node, const Callable &p_play_callable, const Callable &p_stop_callable, bool p_physical) {
node = p_node;
play_callable = p_play_callable;
stop_callable = p_stop_callable;
physical = p_physical;
bus = SceneStringName(Master);

Expand Down
5 changes: 3 additions & 2 deletions scene/audio/audio_stream_player_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class AudioStreamPlayerInternal : public Object {

Node *node = nullptr;
Callable play_callable;
Callable stop_callable;
bool physical = false;
AudioServer::PlaybackType playback_type = AudioServer::PlaybackType::PLAYBACK_TYPE_DEFAULT;

Expand Down Expand Up @@ -94,7 +95,7 @@ class AudioStreamPlayerInternal : public Object {

Ref<AudioStreamPlayback> play_basic();
void seek(float p_seconds);
void stop();
void stop_basic();
bool is_playing() const;
float get_playback_position();

Expand All @@ -110,7 +111,7 @@ class AudioStreamPlayerInternal : public Object {
void set_playback_type(AudioServer::PlaybackType p_playback_type);
AudioServer::PlaybackType get_playback_type() const;

AudioStreamPlayerInternal(Node *p_node, const Callable &p_play_callable, bool p_physical);
AudioStreamPlayerInternal(Node *p_node, const Callable &p_play_callable, const Callable &p_stop_callable, bool p_physical);
};

#endif // AUDIO_STREAM_PLAYER_INTERNAL_H

0 comments on commit 8c7dd3c

Please sign in to comment.