Change folder structure, add solution to the root.
This commit is contained in:
40
DaggerFramework/Source/Audio/AudioBackend.cs
Normal file
40
DaggerFramework/Source/Audio/AudioBackend.cs
Normal file
@@ -0,0 +1,40 @@
|
||||
namespace DaggerFramework.Audio
|
||||
{
|
||||
public abstract class AudioBackend : IDisposable
|
||||
{
|
||||
public abstract void Initialize();
|
||||
public abstract void Update();
|
||||
public abstract void Shutdown();
|
||||
// BUS
|
||||
public abstract void CreateBus(string busName);
|
||||
public abstract void SetBusVolume(string busName, float volume);
|
||||
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 void PlaySoundVariation(Sound sound, string bus = "Master", float pitchVariation = 0.1f, float volume = 1.0f)
|
||||
{
|
||||
var maxPitch = 1.0f + pitchVariation;
|
||||
var minPitch = 1.0f - pitchVariation;
|
||||
|
||||
var pitch = (float)_random.NextDouble() * (maxPitch - minPitch) + minPitch;
|
||||
PlaySound(sound, bus, pitch, volume);
|
||||
}
|
||||
|
||||
// EFFECTS
|
||||
public abstract void AddBusEffect<T>(T effect, string bus = "Master") where T : AudioEffect;
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Shutdown();
|
||||
}
|
||||
|
||||
private LehmerRandom _random = new LehmerRandom();
|
||||
}
|
||||
|
||||
public struct SoundInstance
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
7
DaggerFramework/Source/Audio/AudioBus.cs
Normal file
7
DaggerFramework/Source/Audio/AudioBus.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
namespace DaggerFramework
|
||||
{
|
||||
public class AudioBus
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
9
DaggerFramework/Source/Audio/AudioEffect.cs
Normal file
9
DaggerFramework/Source/Audio/AudioEffect.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
namespace DaggerFramework
|
||||
{
|
||||
public class AudioEffect { }
|
||||
|
||||
public class AudioEffectReverb : AudioEffect
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
45
DaggerFramework/Source/Audio/DummyAudioBackend.cs
Normal file
45
DaggerFramework/Source/Audio/DummyAudioBackend.cs
Normal file
@@ -0,0 +1,45 @@
|
||||
namespace DaggerFramework.Audio
|
||||
{
|
||||
public class DummyAudioBackend : AudioBackend
|
||||
{
|
||||
public override void AddBusEffect<T>(T effect, string bus = "Master")
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
public override void CreateBus(string busName)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
public override float GetBusVolume(string busName)
|
||||
{
|
||||
return 0.0f;
|
||||
}
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
public override void SetBusVolume(string busName, float volume)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
public override void Shutdown()
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
public override void Update()
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
public override void PlaySound(Sound sound, string bus = "Master", float pitch = 1, float volume = 1)
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
136
DaggerFramework/Source/Audio/FmodAudioBackend.cs
Normal file
136
DaggerFramework/Source/Audio/FmodAudioBackend.cs
Normal file
@@ -0,0 +1,136 @@
|
||||
using FMOD;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace DaggerFramework.Audio
|
||||
{
|
||||
public class FmodAudioBackend : AudioBackend
|
||||
{
|
||||
public override void Initialize()
|
||||
{
|
||||
CreateSystem();
|
||||
_loadedSounds = new Dictionary<string, FMOD.Sound>();
|
||||
_channelGroups = new Dictionary<string, ChannelGroup>();
|
||||
|
||||
CreateBus("Master");
|
||||
}
|
||||
public override void Update() => _system.update();
|
||||
|
||||
public override void PlaySound(Sound sound, string bus = "Master", float pitch = 1, float volume = 1)
|
||||
{
|
||||
int channels = 0;
|
||||
|
||||
if (sound.Format == SoundFormat.Mono)
|
||||
{
|
||||
channels = 1;
|
||||
}
|
||||
else if (sound.Format == SoundFormat.Stereo)
|
||||
{
|
||||
channels = 2;
|
||||
}
|
||||
|
||||
var channel = PlaySoundFromBuffer(sound.Path, sound.BufferSize, channels, sound.SampleRate, sound.Buffer, GetChannelGroup(bus));
|
||||
channel.setVolume(volume);
|
||||
channel.setPitch(pitch);
|
||||
}
|
||||
|
||||
public override void CreateBus(string busName)
|
||||
{
|
||||
ChannelGroup channelGroup;
|
||||
_system.createChannelGroup(busName, out channelGroup);
|
||||
_channelGroups.Add(busName, channelGroup);
|
||||
}
|
||||
|
||||
public override void SetBusVolume(string busName, float volume)
|
||||
{
|
||||
var channel = GetChannelGroup(busName);
|
||||
channel.setVolume(volume);
|
||||
}
|
||||
|
||||
public override float GetBusVolume(string busName)
|
||||
{
|
||||
float volume;
|
||||
GetChannelGroup(busName).getVolume(out volume);
|
||||
return volume;
|
||||
}
|
||||
|
||||
public override void AddBusEffect<T>(T effect, string bus = "Master")
|
||||
{
|
||||
var channelGroup = GetChannelGroup(bus);
|
||||
DSP dsp;
|
||||
|
||||
switch (effect)
|
||||
{
|
||||
case AudioEffectReverb:
|
||||
dsp = CreateReverbDsp(effect as AudioEffectReverb);
|
||||
break;
|
||||
default:
|
||||
_system.createDSPByType(DSP_TYPE.UNKNOWN, out dsp);
|
||||
break;
|
||||
}
|
||||
|
||||
channelGroup.addDSP(0, dsp);
|
||||
}
|
||||
|
||||
private DSP CreateReverbDsp(AudioEffectReverb effectReverb)
|
||||
{
|
||||
DSP dsp;
|
||||
_system.createDSPByType(DSP_TYPE.SFXREVERB, out dsp);
|
||||
return dsp;
|
||||
}
|
||||
|
||||
private void CreateSystem()
|
||||
{
|
||||
var result = FMOD.Factory.System_Create(out _system);
|
||||
_system.init(128, INITFLAGS.NORMAL, 0);
|
||||
}
|
||||
|
||||
private Channel PlaySoundFromBuffer(string path, int length, int channels, int sampleRate, byte[] buffer, ChannelGroup channelGroup)
|
||||
{
|
||||
Channel fmodChannel;
|
||||
FMOD.Sound fmodSound = IsLoaded(path) ? GetSoundFromLoaded(path) : CreateSound(length, channels, sampleRate, path, buffer);
|
||||
|
||||
_system.playSound(fmodSound, channelGroup, false, out fmodChannel);
|
||||
|
||||
return fmodChannel;
|
||||
}
|
||||
|
||||
|
||||
private FMOD.Sound GetSoundFromLoaded(string path) => _loadedSounds[path];
|
||||
private bool IsLoaded(string path) => _loadedSounds.ContainsKey(path);
|
||||
|
||||
private FMOD.Sound CreateSound(int length, int channels, int sampleRate, string path, byte[] buffer)
|
||||
{
|
||||
FMOD.Sound sound;
|
||||
CREATESOUNDEXINFO info = new CREATESOUNDEXINFO()
|
||||
{
|
||||
numchannels = channels,
|
||||
defaultfrequency = sampleRate,
|
||||
format = SOUND_FORMAT.PCM16,
|
||||
length = (uint)length,
|
||||
cbsize = Marshal.SizeOf(typeof(CREATESOUNDEXINFO))
|
||||
};
|
||||
|
||||
var result = _system.createSound(buffer, FMOD.MODE.OPENMEMORY | FMOD.MODE.OPENRAW | FMOD.MODE.CREATESAMPLE, ref info, out sound);
|
||||
AddToLoaded(path, sound);
|
||||
return sound;
|
||||
}
|
||||
|
||||
private void AddToLoaded(string path, FMOD.Sound sound) => _loadedSounds.Add(path, sound);
|
||||
|
||||
private ChannelGroup GetChannelGroup(string busName)
|
||||
{
|
||||
return _channelGroups[busName];
|
||||
}
|
||||
|
||||
public override void Shutdown()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
private FMOD.System _system;
|
||||
|
||||
// TODO: use a different key for the dictionary, paths are not good :( (waste of memory lol)
|
||||
private Dictionary<string, FMOD.Sound> _loadedSounds;
|
||||
private Dictionary<string, ChannelGroup> _channelGroups;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user