Skip to content

Commit

Permalink
Remove the need for a sub_stream. (closes #24)
Browse files Browse the repository at this point in the history
  • Loading branch information
stechyo committed Jan 12, 2024
1 parent 974836b commit aa97bf0
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 38 deletions.
14 changes: 5 additions & 9 deletions project/scenes/demo.tscn
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[gd_scene load_steps=38 format=3 uid="uid://bilxmr83dwsgw"]
[gd_scene load_steps=36 format=3 uid="uid://bilxmr83dwsgw"]

[ext_resource type="Script" path="res://src/player.gd" id="1_c2adp"]
[ext_resource type="SteamAudioMaterial" uid="uid://dbai50r63k346" path="res://addons/steamaudio/materials/rock_material.tres" id="1_wtcfy"]
Expand Down Expand Up @@ -33,8 +33,6 @@ data = PackedVector3Array(1, 0, 1, -1, 0, 1, 1, 0, -1, -1, 0, 1, -1, 0, -1, 1, 0

[sub_resource type="CapsuleShape3D" id="CapsuleShape3D_j0ktp"]

[sub_resource type="SteamAudioStream" id="SteamAudioStream_vraar"]

[sub_resource type="StandardMaterial3D" id="StandardMaterial3D_dg8l6"]
albedo_color = Color(0.976471, 0.133333, 0.286275, 1)
emission_enabled = true
Expand All @@ -50,8 +48,6 @@ stream_0/weight = 1.0
stream_1/stream = ExtResource("6_dw5dv")
stream_1/weight = 1.0

[sub_resource type="SteamAudioStream" id="SteamAudioStream_fkje5"]

[sub_resource type="StandardMaterial3D" id="StandardMaterial3D_pyn86"]
albedo_color = Color(0.976471, 0.133333, 0.286275, 1)
emission_enabled = true
Expand Down Expand Up @@ -201,7 +197,7 @@ sub_stream = ExtResource("3_0iivb")
loop_sub_stream = true
reflection = true
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0.0999999)
stream = SubResource("SteamAudioStream_vraar")
stream = ExtResource("3_0iivb")
volume_db = 20.0
autoplay = true
panning_strength = 0.0
Expand All @@ -226,7 +222,7 @@ sub_stream = SubResource("AudioStreamRandomizer_w72gs")
min_attenuation_distance = 5.0
reflection = true
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0)
stream = SubResource("SteamAudioStream_fkje5")
stream = SubResource("AudioStreamRandomizer_w72gs")
volume_db = 20.0
panning_strength = 0.0
script = ExtResource("7_gx8pe")
Expand Down Expand Up @@ -298,7 +294,7 @@ distance_attenuation = true
min_attenuation_distance = 5.0
reflection = true
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0.0999999)
stream = SubResource("SteamAudioStream_vraar")
stream = ExtResource("3_0iivb")
attenuation_model = 3
volume_db = 20.0
autoplay = true
Expand All @@ -324,7 +320,7 @@ sub_stream = ExtResource("3_0iivb")
loop_sub_stream = true
min_attenuation_distance = 5.0
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -2, 1, 1)
stream = SubResource("SteamAudioStream_vraar")
stream = ExtResource("3_0iivb")
volume_db = 20.0
autoplay = true
panning_strength = 0.0
Expand Down
2 changes: 1 addition & 1 deletion project/sound/2.mp3.import
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ dest_files=["res://.godot/imported/2.mp3-3055b0866e8cbdb3783e580633ac5a8a.mp3str

[params]

loop=false
loop=true
loop_offset=0
bpm=0
beat_count=0
Expand Down
60 changes: 32 additions & 28 deletions src/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,21 @@ void SteamAudioPlayer::_bind_methods() {
SteamAudioPlayer::SteamAudioPlayer() {
is_local_state_init.store(false);
can_load_local_state.store(true);

if (Engine::get_singleton()->is_editor_hint()) {
return;
}

auto str = dynamic_cast<SteamAudioStream *>(get_stream().ptr());
if (str == nullptr) {
Ref<SteamAudioStream> new_stream;
new_stream.instantiate();
new_stream->parent = this;
if (get_stream().ptr() != nullptr) {
new_stream->set_stream(get_stream());
}
this->set_stream(new_stream);
}
}
SteamAudioPlayer::~SteamAudioPlayer() {
SteamAudio::log(SteamAudio::log_debug, "destroying player");
Expand Down Expand Up @@ -149,28 +164,25 @@ void SteamAudioPlayer::_ready() {
set_attenuation_model(ATTENUATION_DISABLED);
}

auto str = dynamic_cast<SteamAudioStream *>(get_stream().ptr());
if (str == nullptr) {
UtilityFunctions::push_warning("You cannot use a stream that isn't SteamAudioStream for SteamAudioPlayer. Setting the stream to a new SteamAudioStream.");
Ref<SteamAudioStream> new_stream;
new_stream.instantiate();
set_stream(new_stream);
}

if (Engine::get_singleton()->is_editor_hint()) {
return;
}

if (loop_sub_stream && sub_stream->get("loop").get_type() != Variant::NIL) {
sub_stream->set("loop", true);
}

str = dynamic_cast<SteamAudioStream *>(get_stream().ptr());
auto str = dynamic_cast<SteamAudioStream *>(get_stream().ptr());
if (str == nullptr) {
UtilityFunctions::push_error(" \
The stream of a SteamAudioPlayer must be a \
SteamAudioStream. No sound will be played.");
queue_free();
if (is_autoplay_enabled()) {
stop();
}
Ref<SteamAudioStream> new_stream;
new_stream.instantiate();
if (get_stream().ptr() != nullptr) {
new_stream->set_stream(get_stream());
}
set_stream(new_stream);
str = new_stream.ptr();
if (is_autoplay_enabled()) {
play();
}
}

if (cfg.ambisonics_order > SteamAudioConfig::max_ambisonics_order) {
Expand All @@ -180,7 +192,6 @@ void SteamAudioPlayer::_ready() {
cfg.occ_samples = SteamAudioConfig::max_num_occ_samples;
}

str->set_stream(sub_stream);
str->parent = this;

// TODO: do this properly, if it can be done
Expand All @@ -202,12 +213,8 @@ void SteamAudioPlayer::_process(double delta) {
UtilityFunctions::push_warning("You cannot enable Godot's and SteamAudio's distance attenuation features at the same time. Disable SteamAudio's attenuation before adjusting Godot's.");
set_attenuation_model(ATTENUATION_DISABLED);
}
auto str = dynamic_cast<SteamAudioStream *>(get_stream().ptr());
if (str == nullptr) {
UtilityFunctions::push_warning("You cannot use a stream that isn't SteamAudioStream for SteamAudioPlayer. Setting the stream to a new SteamAudioStream.");
Ref<SteamAudioStream> new_stream;
new_stream.instantiate();
set_stream(new_stream);
if (Engine::get_singleton()->is_editor_hint() && sub_stream.ptr() != get_stream().ptr()) {
set_stream(sub_stream);
}
}

Expand All @@ -216,6 +223,7 @@ void SteamAudioPlayer::set_sub_stream(Ref<AudioStream> p_sub_stream) {
sub_stream = p_sub_stream;
auto str = dynamic_cast<SteamAudioStream *>(get_stream().ptr());
if (str == nullptr) {
set_stream(p_sub_stream);
return;
}
str->set_stream(sub_stream);
Expand Down Expand Up @@ -248,9 +256,5 @@ void SteamAudioPlayer::set_occlusion_on(bool p_occlusion_on) { cfg.is_occlusion_

PackedStringArray SteamAudioPlayer::_get_configuration_warnings() const {
PackedStringArray res;
auto str = dynamic_cast<SteamAudioStream *>(get_stream().ptr());
if (str == nullptr) {
res.push_back("This player's stream is either not set or not a SteamAudioStream. Please set it to a SteamAudioStream and use the sub stream property to set the audio stream you want to play.");
}
return res;
}

0 comments on commit aa97bf0

Please sign in to comment.