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();
_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;

View File

@@ -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)
{

View File

@@ -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;
}

View File

@@ -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)

View File

@@ -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;

View File

@@ -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;

View File

@@ -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;
}

View File

@@ -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()