FineAudio/StreamingSoundOgg.cs

97 lines
2.0 KiB
C#
Raw Normal View History

2022-02-23 05:14:32 +00:00
using System;
using System.IO;
namespace MoonWorks.Audio
{
2022-02-23 05:14:32 +00:00
public class StreamingSoundOgg : StreamingSound
{
// FIXME: what should this value be?
public const int BUFFER_SIZE = 1024 * 128;
2022-02-23 05:14:32 +00:00
internal IntPtr FileHandle { get; }
internal FAudio.stb_vorbis_info Info { get; }
2022-02-23 05:14:32 +00:00
private readonly float[] buffer;
2022-02-23 05:14:32 +00:00
public override SoundState State { get; protected set; }
2022-02-23 05:14:32 +00:00
public static StreamingSoundOgg Load(
AudioDevice device,
string filePath,
2022-04-05 23:05:42 +00:00
bool is3D = false
2022-02-23 05:14:32 +00:00
)
{
var fileHandle = FAudio.stb_vorbis_open_filename(filePath, out var error, IntPtr.Zero);
if (error != 0)
{
Logger.LogError("Error opening OGG file!");
throw new AudioLoadException("Error opening OGG file!");
}
2022-02-23 05:14:32 +00:00
var info = FAudio.stb_vorbis_get_info(fileHandle);
2022-02-23 05:14:32 +00:00
return new StreamingSoundOgg(
device,
fileHandle,
info,
2022-04-05 23:05:42 +00:00
is3D
2022-02-23 05:14:32 +00:00
);
}
2022-02-23 05:14:32 +00:00
internal StreamingSoundOgg(
AudioDevice device,
IntPtr fileHandle,
FAudio.stb_vorbis_info info,
2022-04-05 23:05:42 +00:00
bool is3D
) : base(
device,
3, /* float type */
32, /* size of float */
(ushort) (4 * info.channels),
(ushort) info.channels,
info.sample_rate,
2022-04-05 23:05:42 +00:00
is3D
)
2022-02-23 05:14:32 +00:00
{
FileHandle = fileHandle;
Info = info;
buffer = new float[BUFFER_SIZE];
2022-02-23 05:14:32 +00:00
device.AddDynamicSoundInstance(this);
}
2022-02-23 05:14:32 +00:00
protected override void AddBuffer(
out float[] buffer,
out uint bufferOffset,
out uint bufferLength,
out bool reachedEnd
)
{
buffer = this.buffer;
2022-02-23 05:14:32 +00:00
/* NOTE: this function returns samples per channel, not total samples */
var samples = FAudio.stb_vorbis_get_samples_float_interleaved(
FileHandle,
Info.channels,
buffer,
buffer.Length
);
2022-02-23 05:14:32 +00:00
var sampleCount = samples * Info.channels;
bufferOffset = 0;
bufferLength = (uint) sampleCount;
reachedEnd = sampleCount < buffer.Length;
}
2022-02-23 05:14:32 +00:00
protected override void SeekStart()
{
FAudio.stb_vorbis_seek_start(FileHandle);
}
2022-02-23 05:14:32 +00:00
protected override void Destroy()
{
FAudio.stb_vorbis_close(FileHandle);
}
}
}