From 06814a678e6505ff5ce874d2ec9d215ad0a7b865 Mon Sep 17 00:00:00 2001 From: dnesov Date: Thu, 22 Aug 2024 22:07:27 +0200 Subject: [PATCH] SoundInstance changes, small updates in Sound and Texture2d loaders. --- TestGame/TestGame.cs | 10 ++++++++++ Voile/Source/Audio/AudioBackend.cs | 4 ++-- Voile/Source/Audio/DummyAudioBackend.cs | 2 +- Voile/Source/Audio/FmodAudioBackend.cs | 10 +++++++++- Voile/Source/Audio/SoundInstance.cs | 8 ++------ Voile/Source/Resources/Loaders/SoundLoader.cs | 10 ++++++---- Voile/Source/Resources/Loaders/Texture2dLoader.cs | 8 +++++--- Voile/Source/Resources/Resource.cs | 4 ++++ 8 files changed, 39 insertions(+), 17 deletions(-) diff --git a/TestGame/TestGame.cs b/TestGame/TestGame.cs index 4401d70..3f613b8 100644 --- a/TestGame/TestGame.cs +++ b/TestGame/TestGame.cs @@ -47,6 +47,8 @@ public class TestGame : Game _uiLayer = new UiLayer(); _worldLayer = new EntityLayer(); + _testSoundInstance = _audioBackend!.CreateInstance(_testSound!); + Input.AddInputMapping("play", new InputAction[] { new KeyInputAction(KeyboardKey.Spacebar) }); Input.AddInputMapping("sprint", new InputAction[] { new KeyInputAction(KeyboardKey.LeftShift) }); Input.AddInputMapping("toggle_fullscreen", new InputAction[] { new KeyInputAction(KeyboardKey.F11) }); @@ -64,6 +66,13 @@ public class TestGame : Game { while (_scene!.ShouldRun) { + if (Input.IsActionPressed("play")) + { + _testSoundInstance!.PitchVariation(0.9f, 1.1f) + .VolumeVariation(0.98f, 1.02f); + + _testSoundInstance!.Play(); + } _scene.Update(); _scene.BeginDraw(); _scene.EndDraw(); @@ -77,6 +86,7 @@ public class TestGame : Game } private Sound? _testSound; + private SoundInstance? _testSoundInstance; private Font? _font; private FmodAudioBackend? _audioBackend; private Scene? _scene; diff --git a/Voile/Source/Audio/AudioBackend.cs b/Voile/Source/Audio/AudioBackend.cs index 410e8a9..15771a8 100644 --- a/Voile/Source/Audio/AudioBackend.cs +++ b/Voile/Source/Audio/AudioBackend.cs @@ -11,8 +11,8 @@ namespace Voile.Audio public abstract float GetBusVolume(string busName); // SOUND - public abstract void PlaySound(Sound sound, string bus = "Master", float pitch = 1.0f, float volume = 1.0f); - public void PlaySound(Sound sound, string bus = "Master") => PlaySound(sound, bus, default, default); + public abstract void PlaySound(Sound sound, float pitch, float volume, string bus = "Master"); + public void PlaySound(Sound sound, string bus = "Master") => PlaySound(sound, 1.0f, 1.0f, bus); public SoundInstance CreateInstance(Sound sound) { diff --git a/Voile/Source/Audio/DummyAudioBackend.cs b/Voile/Source/Audio/DummyAudioBackend.cs index 3a4b172..6c8b918 100644 --- a/Voile/Source/Audio/DummyAudioBackend.cs +++ b/Voile/Source/Audio/DummyAudioBackend.cs @@ -37,7 +37,7 @@ namespace Voile.Audio return; } - public override void PlaySound(Sound sound, string bus = "Master", float pitch = 1, float volume = 1) + public override void PlaySound(Sound sound, float pitch, float volume, string bus = "Master") { return; } diff --git a/Voile/Source/Audio/FmodAudioBackend.cs b/Voile/Source/Audio/FmodAudioBackend.cs index 58bd888..2e8cd0a 100644 --- a/Voile/Source/Audio/FmodAudioBackend.cs +++ b/Voile/Source/Audio/FmodAudioBackend.cs @@ -1,5 +1,6 @@ using FMOD; using System.Runtime.InteropServices; +using Voile.Utils; namespace Voile.Audio { @@ -15,8 +16,14 @@ namespace Voile.Audio } public override void Update() => _system.update(); - public override void PlaySound(Sound sound, string bus = "Master", float pitch = 1, float volume = 1) + public override void PlaySound(Sound sound, float pitch, float volume, string bus = "Master") { + if (sound is null) + { + _logger.Error("Sound is null!"); + return; + } + int channels = 0; if (sound.Format == SoundFormat.Mono) @@ -127,6 +134,7 @@ namespace Voile.Audio return _channelGroups[busName]; } + private Logger _logger = new(nameof(FmodAudioBackend)); private FMOD.System _system; // TODO: use a different key for the dictionary, paths are not good :( (waste of memory lol) diff --git a/Voile/Source/Audio/SoundInstance.cs b/Voile/Source/Audio/SoundInstance.cs index 1c28569..006e377 100644 --- a/Voile/Source/Audio/SoundInstance.cs +++ b/Voile/Source/Audio/SoundInstance.cs @@ -2,6 +2,7 @@ namespace Voile.Audio { public class SoundInstance { + protected virtual Sound Sound => _sound; public SoundInstance(AudioBackend backend, Sound sound) { _backend = backend; @@ -30,12 +31,7 @@ namespace Voile.Audio public void Play() { - _backend.PlaySound(GetSound(), _bus, _pitch, _volume); - } - - protected virtual Sound GetSound() - { - return _sound; + _backend.PlaySound(Sound, _pitch, _volume, _bus); } private readonly AudioBackend _backend; diff --git a/Voile/Source/Resources/Loaders/SoundLoader.cs b/Voile/Source/Resources/Loaders/SoundLoader.cs index c532264..294390a 100644 --- a/Voile/Source/Resources/Loaders/SoundLoader.cs +++ b/Voile/Source/Resources/Loaders/SoundLoader.cs @@ -40,10 +40,12 @@ namespace Voile.Resources audioData[i * 2 + 1] = b1; } - result = new Sound(path, audioData); - result.Format = (SoundFormat)vorbis.Channels - 1; - result.SampleRate = vorbis.SampleRate; - result.BufferSize = length; + result = new Sound(path, audioData) + { + Format = (SoundFormat)vorbis.Channels - 1, + SampleRate = vorbis.SampleRate, + BufferSize = length + }; vorbis.Dispose(); return result; diff --git a/Voile/Source/Resources/Loaders/Texture2dLoader.cs b/Voile/Source/Resources/Loaders/Texture2dLoader.cs index 46a49ac..7715942 100644 --- a/Voile/Source/Resources/Loaders/Texture2dLoader.cs +++ b/Voile/Source/Resources/Loaders/Texture2dLoader.cs @@ -20,9 +20,11 @@ namespace Voile image = ImageResult.FromStream(stream, ColorComponents.RedGreenBlueAlpha); } - Texture2d result = new Texture2d(path, image.Data); - result.Width = image.Width; - result.Height = image.Height; + Texture2d result = new(path, image.Data) + { + Width = image.Width, + Height = image.Height + }; return result; } diff --git a/Voile/Source/Resources/Resource.cs b/Voile/Source/Resources/Resource.cs index 01877fc..32d829c 100644 --- a/Voile/Source/Resources/Resource.cs +++ b/Voile/Source/Resources/Resource.cs @@ -4,6 +4,8 @@ namespace Voile { public abstract class Resource : IDisposable { + public Guid Guid { get; set; } = Guid.NewGuid(); + public string? Path { get => _path; set => _path = value; } [JsonIgnore] @@ -16,6 +18,8 @@ namespace Voile { _path = path; _buffer = buffer; + + BufferSize = buffer.Length; } public void Dispose()