SoundInstance changes, small updates in Sound and Texture2d loaders.

This commit is contained in:
2024-08-22 22:07:27 +02:00
parent 912de8a5ef
commit 06814a678e
8 changed files with 39 additions and 17 deletions

View File

@@ -47,6 +47,8 @@ public class TestGame : Game
_uiLayer = new UiLayer(); _uiLayer = new UiLayer();
_worldLayer = new EntityLayer(); _worldLayer = new EntityLayer();
_testSoundInstance = _audioBackend!.CreateInstance(_testSound!);
Input.AddInputMapping("play", new InputAction[] { new KeyInputAction(KeyboardKey.Spacebar) }); Input.AddInputMapping("play", new InputAction[] { new KeyInputAction(KeyboardKey.Spacebar) });
Input.AddInputMapping("sprint", new InputAction[] { new KeyInputAction(KeyboardKey.LeftShift) }); Input.AddInputMapping("sprint", new InputAction[] { new KeyInputAction(KeyboardKey.LeftShift) });
Input.AddInputMapping("toggle_fullscreen", new InputAction[] { new KeyInputAction(KeyboardKey.F11) }); Input.AddInputMapping("toggle_fullscreen", new InputAction[] { new KeyInputAction(KeyboardKey.F11) });
@@ -64,6 +66,13 @@ public class TestGame : Game
{ {
while (_scene!.ShouldRun) while (_scene!.ShouldRun)
{ {
if (Input.IsActionPressed("play"))
{
_testSoundInstance!.PitchVariation(0.9f, 1.1f)
.VolumeVariation(0.98f, 1.02f);
_testSoundInstance!.Play();
}
_scene.Update(); _scene.Update();
_scene.BeginDraw(); _scene.BeginDraw();
_scene.EndDraw(); _scene.EndDraw();
@@ -77,6 +86,7 @@ public class TestGame : Game
} }
private Sound? _testSound; private Sound? _testSound;
private SoundInstance? _testSoundInstance;
private Font? _font; private Font? _font;
private FmodAudioBackend? _audioBackend; private FmodAudioBackend? _audioBackend;
private Scene? _scene; private Scene? _scene;

View File

@@ -11,8 +11,8 @@ namespace Voile.Audio
public abstract float GetBusVolume(string busName); public abstract float GetBusVolume(string busName);
// SOUND // SOUND
public abstract void PlaySound(Sound sound, string bus = "Master", float pitch = 1.0f, float volume = 1.0f); public abstract void PlaySound(Sound sound, float pitch, float volume, string bus = "Master");
public void PlaySound(Sound sound, string bus = "Master") => PlaySound(sound, bus, default, default); public void PlaySound(Sound sound, string bus = "Master") => PlaySound(sound, 1.0f, 1.0f, bus);
public SoundInstance CreateInstance(Sound sound) public SoundInstance CreateInstance(Sound sound)
{ {

View File

@@ -37,7 +37,7 @@ namespace Voile.Audio
return; 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; return;
} }

View File

@@ -1,5 +1,6 @@
using FMOD; using FMOD;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using Voile.Utils;
namespace Voile.Audio namespace Voile.Audio
{ {
@@ -15,8 +16,14 @@ namespace Voile.Audio
} }
public override void Update() => _system.update(); 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; int channels = 0;
if (sound.Format == SoundFormat.Mono) if (sound.Format == SoundFormat.Mono)
@@ -127,6 +134,7 @@ namespace Voile.Audio
return _channelGroups[busName]; return _channelGroups[busName];
} }
private Logger _logger = new(nameof(FmodAudioBackend));
private FMOD.System _system; private FMOD.System _system;
// TODO: use a different key for the dictionary, paths are not good :( (waste of memory lol) // TODO: use a different key for the dictionary, paths are not good :( (waste of memory lol)

View File

@@ -2,6 +2,7 @@ namespace Voile.Audio
{ {
public class SoundInstance public class SoundInstance
{ {
protected virtual Sound Sound => _sound;
public SoundInstance(AudioBackend backend, Sound sound) public SoundInstance(AudioBackend backend, Sound sound)
{ {
_backend = backend; _backend = backend;
@@ -30,12 +31,7 @@ namespace Voile.Audio
public void Play() public void Play()
{ {
_backend.PlaySound(GetSound(), _bus, _pitch, _volume); _backend.PlaySound(Sound, _pitch, _volume, _bus);
}
protected virtual Sound GetSound()
{
return _sound;
} }
private readonly AudioBackend _backend; private readonly AudioBackend _backend;

View File

@@ -40,10 +40,12 @@ namespace Voile.Resources
audioData[i * 2 + 1] = b1; audioData[i * 2 + 1] = b1;
} }
result = new Sound(path, audioData); result = new Sound(path, audioData)
result.Format = (SoundFormat)vorbis.Channels - 1; {
result.SampleRate = vorbis.SampleRate; Format = (SoundFormat)vorbis.Channels - 1,
result.BufferSize = length; SampleRate = vorbis.SampleRate,
BufferSize = length
};
vorbis.Dispose(); vorbis.Dispose();
return result; return result;

View File

@@ -20,9 +20,11 @@ namespace Voile
image = ImageResult.FromStream(stream, ColorComponents.RedGreenBlueAlpha); image = ImageResult.FromStream(stream, ColorComponents.RedGreenBlueAlpha);
} }
Texture2d result = new Texture2d(path, image.Data); Texture2d result = new(path, image.Data)
result.Width = image.Width; {
result.Height = image.Height; Width = image.Width,
Height = image.Height
};
return result; return result;
} }

View File

@@ -4,6 +4,8 @@ namespace Voile
{ {
public abstract class Resource : IDisposable public abstract class Resource : IDisposable
{ {
public Guid Guid { get; set; } = Guid.NewGuid();
public string? Path { get => _path; set => _path = value; } public string? Path { get => _path; set => _path = value; }
[JsonIgnore] [JsonIgnore]
@@ -16,6 +18,8 @@ namespace Voile
{ {
_path = path; _path = path;
_buffer = buffer; _buffer = buffer;
BufferSize = buffer.Length;
} }
public void Dispose() public void Dispose()