aboutsummaryrefslogtreecommitdiff
path: root/extension/src/openvic-extension/singletons/SoundSingleton.hpp
diff options
context:
space:
mode:
author Nemrav <>2024-04-22 22:30:21 +0200
committer Nemrav <>2024-08-06 01:40:34 +0200
commit9506f4160f0bd351f0853e6e8263ea927d9ec771 (patch)
tree0a9bd4f52c01315c3b38ce641a78c33bd8562be2 /extension/src/openvic-extension/singletons/SoundSingleton.hpp
parentfde15e554dc9ed458a838683c69d10262764db12 (diff)
Music and Sound Effect loading and playing
almost working music loading music list sent to godot wav loading not working mp3 metadata load wav initial load and play title theme first fix errors not related to nodetools working wav define loading and music playlists fixup error handling and playlist load song chances code style progress switch mp3 metadata addon to MusicMetadata add id3v1 mp3 metadata handling to MusicMetadata remove commented code from id3v1 sounds gd styling fix dataloader conflicts clean up commented code move MusicChance to dataloader remove reference to old addon move sfx defines loader add self to credits feedback on soundSingleton include subfolders in sound singleton sfx map replace space tabs replace std_view_to_godot_string with std_to_godot_string revise singleton files revise gd side sound final revisions
Diffstat (limited to 'extension/src/openvic-extension/singletons/SoundSingleton.hpp')
-rw-r--r--extension/src/openvic-extension/singletons/SoundSingleton.hpp86
1 files changed, 86 insertions, 0 deletions
diff --git a/extension/src/openvic-extension/singletons/SoundSingleton.hpp b/extension/src/openvic-extension/singletons/SoundSingleton.hpp
new file mode 100644
index 0000000..bfa03ea
--- /dev/null
+++ b/extension/src/openvic-extension/singletons/SoundSingleton.hpp
@@ -0,0 +1,86 @@
+#pragma once
+
+#include <godot_cpp/core/class_db.hpp>
+#include <godot_cpp/core/object.hpp>
+#include <godot_cpp/variant/string.hpp>
+#include <godot_cpp/variant/string_name.hpp>
+
+#include <godot_cpp/classes/file_access.hpp>
+#include <godot_cpp/classes/audio_stream.hpp>
+#include <godot_cpp/classes/audio_stream_mp3.hpp>
+#include <godot_cpp/classes/audio_stream_wav.hpp>
+#include <godot_cpp/templates/vector.hpp>
+
+#include <openvic-simulation/types/OrderedContainers.hpp>
+#include <openvic-simulation/types/IdentifierRegistry.hpp>
+#include <openvic-simulation/types/fixed_point/FixedPoint.hpp>
+
+namespace OpenVic {
+
+ class SoundSingleton : public godot::Object {
+
+ GDCLASS(SoundSingleton, godot::Object);
+
+ static inline SoundSingleton* _singleton = nullptr;
+
+ //cache of songs
+ //names will be like "subfolder/songname", with "music/" base folder and the extension (.mp3) being excluded
+ using song_asset_map_t = deque_ordered_map<godot::StringName, godot::Ref<godot::AudioStreamMP3>>;
+ song_asset_map_t tracks;
+
+ //cache of sfx (map file name to an audio stream), only used temporarily until the sfx_define_map is built
+ using sfx_asset_map_t = deque_ordered_map<godot::StringName, godot::Ref<godot::AudioStreamWAV>>;
+ sfx_asset_map_t sfx;
+
+ //define name, stream ref, volume for sound effects so we can get these properties with a simple call in godot
+ struct sound_asset_t {
+ std::optional<godot::Ref<godot::AudioStreamWAV>> audioStream;
+ std::optional<fixed_point_t> volume;
+ };
+ using sfx_define_map_t = deque_ordered_map<godot::StringName,sound_asset_t>;
+ sfx_define_map_t sfx_define;
+
+ static constexpr std::string_view title_theme_name = "thecoronation_titletheme.mp3";
+ static constexpr std::string_view music_folder = "music/";
+ static constexpr std::string_view sound_folder = "sound/";
+
+ //property for gd scripts to access song names
+ godot::Array PROPERTY(song_list);
+ godot::String PROPERTY(title_theme);
+
+ //property for gd scripts to access sound names
+ godot::Array PROPERTY(sound_list);
+
+ public:
+ SoundSingleton();
+ ~SoundSingleton();
+ static SoundSingleton* get_singleton();
+
+ protected:
+ static void _bind_methods();
+
+ godot::String to_define_file_name(godot::String const& path, std::string_view const& base_folder) const;
+ godot::String read_riff_str(godot::Ref<godot::FileAccess> const& file, int size=4) const;
+
+ private:
+ /* Loads AudioStreams (.mp3 or .wav) at runtime using godot's functions*/
+ godot::Ref<godot::AudioStreamMP3> _load_godot_mp3(godot::String const& path) const;
+ godot::Ref<godot::AudioStreamWAV> _load_godot_wav(godot::String const& path) const;
+
+ public:
+ //gets a song from the cache ('tracks' variable), or if not, then from the files using _load_godot_mp3
+ godot::Ref<godot::AudioStreamMP3> get_song(godot::String const& name);
+ godot::Ref<godot::AudioStreamWAV> get_sound(godot::String const& path);
+
+ //load the files into memory
+ bool load_music();
+ bool load_sounds();
+ bool load_title_theme();
+
+ //for sound effects, get the stream and relative volume it should play at from the sfx map
+ godot::Ref<godot::AudioStreamWAV> get_sound_stream(godot::String const& path);
+ float get_sound_base_volume(godot::String const& path);
+
+ };
+
+} \ No newline at end of file