2021-01-20 03:26:30 +00:00
|
|
|
using System;
|
|
|
|
using System.IO;
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
|
|
|
namespace MoonWorks.Audio
|
|
|
|
{
|
2021-01-20 20:39:31 +00:00
|
|
|
public class StaticSound : IDisposable
|
2021-01-20 03:26:30 +00:00
|
|
|
{
|
2021-01-20 20:39:31 +00:00
|
|
|
internal AudioDevice Device { get; }
|
2021-01-20 03:26:30 +00:00
|
|
|
internal FAudio.FAudioBuffer Handle;
|
2021-01-20 20:39:31 +00:00
|
|
|
public ushort Channels { get; }
|
|
|
|
public uint SamplesPerSecond { get; }
|
2021-01-20 03:26:30 +00:00
|
|
|
|
|
|
|
public uint LoopStart { get; set; } = 0;
|
|
|
|
public uint LoopLength { get; set; } = 0;
|
|
|
|
|
2021-01-20 05:33:25 +00:00
|
|
|
private bool IsDisposed;
|
|
|
|
|
|
|
|
public static StaticSound LoadOgg(AudioDevice device, FileInfo fileInfo)
|
2021-01-20 03:26:30 +00:00
|
|
|
{
|
|
|
|
var filePointer = FAudio.stb_vorbis_open_filename(fileInfo.FullName, out var error, IntPtr.Zero);
|
|
|
|
|
|
|
|
if (error != 0)
|
|
|
|
{
|
|
|
|
throw new AudioLoadException("Error loading file!");
|
|
|
|
}
|
|
|
|
var info = FAudio.stb_vorbis_get_info(filePointer);
|
2021-01-20 19:03:26 +00:00
|
|
|
var bufferSize = FAudio.stb_vorbis_stream_length_in_samples(filePointer) * info.channels;
|
2021-01-20 03:26:30 +00:00
|
|
|
var buffer = new float[bufferSize];
|
|
|
|
|
2021-01-20 05:33:25 +00:00
|
|
|
FAudio.stb_vorbis_get_samples_float_interleaved(
|
|
|
|
filePointer,
|
|
|
|
info.channels,
|
|
|
|
buffer,
|
|
|
|
(int) bufferSize
|
|
|
|
);
|
|
|
|
|
2021-01-20 03:26:30 +00:00
|
|
|
FAudio.stb_vorbis_close(filePointer);
|
|
|
|
|
|
|
|
return new StaticSound(
|
2021-01-20 05:33:25 +00:00
|
|
|
device,
|
2021-01-20 20:39:31 +00:00
|
|
|
(ushort) info.channels,
|
|
|
|
info.sample_rate,
|
2021-01-20 03:26:30 +00:00
|
|
|
buffer,
|
|
|
|
0,
|
2021-01-20 20:39:31 +00:00
|
|
|
(uint) buffer.Length
|
2021-01-20 03:26:30 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public StaticSound(
|
2021-01-20 05:33:25 +00:00
|
|
|
AudioDevice device,
|
2021-01-20 20:39:31 +00:00
|
|
|
ushort channels,
|
|
|
|
uint samplesPerSecond,
|
2021-01-20 03:26:30 +00:00
|
|
|
float[] buffer,
|
2021-01-20 19:13:55 +00:00
|
|
|
uint bufferOffset, /* in floats */
|
2021-01-20 20:39:31 +00:00
|
|
|
uint bufferLength /* in floats */
|
|
|
|
) {
|
|
|
|
Device = device;
|
2021-01-20 05:33:25 +00:00
|
|
|
|
2021-01-20 20:39:31 +00:00
|
|
|
Channels = channels;
|
|
|
|
SamplesPerSecond = samplesPerSecond;
|
2021-01-20 05:33:25 +00:00
|
|
|
|
2021-01-20 19:13:55 +00:00
|
|
|
var bufferLengthInBytes = (int) (bufferLength * sizeof(float));
|
2021-01-20 03:26:30 +00:00
|
|
|
Handle = new FAudio.FAudioBuffer();
|
|
|
|
Handle.Flags = FAudio.FAUDIO_END_OF_STREAM;
|
|
|
|
Handle.pContext = IntPtr.Zero;
|
2021-01-20 05:33:25 +00:00
|
|
|
Handle.AudioBytes = (uint) bufferLengthInBytes;
|
|
|
|
Handle.pAudioData = Marshal.AllocHGlobal(bufferLengthInBytes);
|
2021-01-20 19:13:55 +00:00
|
|
|
Marshal.Copy(buffer, (int) bufferOffset, Handle.pAudioData, (int) bufferLength);
|
2021-01-20 03:26:30 +00:00
|
|
|
Handle.PlayBegin = 0;
|
2021-01-20 05:33:25 +00:00
|
|
|
Handle.PlayLength = 0;
|
|
|
|
|
|
|
|
LoopStart = 0;
|
|
|
|
LoopLength = 0;
|
|
|
|
}
|
|
|
|
|
2021-01-20 18:55:15 +00:00
|
|
|
public StaticSoundInstance CreateInstance(bool loop = false)
|
2021-01-20 05:33:25 +00:00
|
|
|
{
|
2021-01-20 18:55:15 +00:00
|
|
|
return new StaticSoundInstance(Device, this, false, loop);
|
2021-01-20 03:26:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected virtual void Dispose(bool disposing)
|
|
|
|
{
|
|
|
|
if (!IsDisposed)
|
|
|
|
{
|
|
|
|
if (disposing)
|
|
|
|
{
|
|
|
|
// dispose managed state (managed objects)
|
|
|
|
}
|
|
|
|
|
|
|
|
Marshal.FreeHGlobal(Handle.pAudioData);
|
|
|
|
IsDisposed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// override finalizer only if 'Dispose(bool disposing)' has code to free unmanaged resources
|
|
|
|
~StaticSound()
|
|
|
|
{
|
|
|
|
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
|
|
|
|
Dispose(disposing: false);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
{
|
|
|
|
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
|
|
|
|
Dispose(disposing: true);
|
|
|
|
GC.SuppressFinalize(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|