fix dynamic looping

pull/14/head
cosmonaut 2021-01-20 10:55:15 -08:00
parent ec09a5952b
commit ffc51cca18
5 changed files with 48 additions and 28 deletions

View File

@ -44,9 +44,9 @@ namespace MoonWorks.Audio
};
}
public DynamicSoundInstance CreateInstance()
public DynamicSoundInstance CreateInstance(bool loop = false)
{
var instance = new DynamicSoundInstance(Device, this, false);
var instance = new DynamicSoundInstance(Device, this, false, loop);
Device.AddDynamicSoundInstance(instance);
return instance;
}

View File

@ -19,8 +19,9 @@ namespace MoonWorks.Audio
internal DynamicSoundInstance(
AudioDevice device,
DynamicSound parent,
bool is3D
) : base(device, parent, is3D)
bool is3D,
bool loop
) : base(device, parent, is3D, loop)
{
queuedBuffers = new List<IntPtr>();
queuedSizes = new List<uint>();
@ -32,17 +33,14 @@ namespace MoonWorks.Audio
public void Play()
{
Update();
if (State == SoundState.Playing)
{
return;
}
QueueBuffers();
FAudio.FAudioSourceVoice_Start(Handle, 0, 0);
State = SoundState.Playing;
Update();
FAudio.FAudioSourceVoice_Start(Handle, 0, 0);
}
public void Pause()
@ -54,14 +52,18 @@ namespace MoonWorks.Audio
}
}
public void Stop()
public void Stop(bool immediate = true)
{
if (immediate)
{
FAudio.FAudioSourceVoice_Stop(Handle, 0, 0);
FAudio.FAudioSourceVoice_FlushSourceBuffers(Handle);
State = SoundState.Stopped;
ClearBuffers();
}
State = SoundState.Stopped;
}
internal void Update()
{
if (State != SoundState.Playing)
@ -80,8 +82,9 @@ namespace MoonWorks.Audio
{
Marshal.FreeHGlobal(queuedBuffers[0]);
queuedBuffers.RemoveAt(0);
queuedSizes.RemoveAt(0);
}
QueueBuffers();
}
private void QueueBuffers()
@ -104,7 +107,7 @@ namespace MoonWorks.Audio
Marshal.FreeHGlobal(buf);
}
queuedBuffers.Clear();
queuedBuffers.Clear();
queuedSizes.Clear();
}
}
@ -112,6 +115,7 @@ namespace MoonWorks.Audio
{
var parent = (DynamicSound) Parent;
/* NOTE: this function returns samples per channel, not total samples */
var samples = FAudio.stb_vorbis_get_samples_float_interleaved(
parent.FileHandle,
parent.Info.channels,
@ -119,13 +123,14 @@ namespace MoonWorks.Audio
buffer.Length
);
IntPtr next = Marshal.AllocHGlobal(buffer.Length * sizeof(float));
Marshal.Copy(buffer, 0, next, buffer.Length);
var sampleCount = samples * parent.Info.channels;
var lengthInBytes = (uint) sampleCount * sizeof(float);
IntPtr next = Marshal.AllocHGlobal((int) lengthInBytes);
Marshal.Copy(buffer, 0, next, sampleCount);
lock (queuedBuffers)
{
var lengthInBytes = (uint) buffer.Length * sizeof(float);
queuedBuffers.Add(next);
if (State != SoundState.Stopped)
{
@ -151,6 +156,19 @@ namespace MoonWorks.Audio
queuedSizes.Add(lengthInBytes);
}
}
/* We have reached the end of the file, what do we do? */
if (sampleCount < buffer.Length)
{
if (Loop)
{
FAudio.stb_vorbis_seek_start(parent.FileHandle);
}
else
{
Stop(false);
}
}
}
}
}

View File

@ -8,6 +8,8 @@ namespace MoonWorks.Audio
protected AudioDevice Device { get; }
internal IntPtr Handle { get; }
public Sound Parent { get; }
public bool Loop { get; }
protected FAudio.F3DAUDIO_DSP_SETTINGS dspSettings;
protected bool is3D;
@ -171,7 +173,8 @@ namespace MoonWorks.Audio
public SoundInstance(
AudioDevice device,
Sound parent,
bool is3D
bool is3D,
bool loop
) {
Device = device;
Parent = parent;
@ -203,6 +206,8 @@ namespace MoonWorks.Audio
handle,
ref Device.ReverbSends
);
Loop = loop;
}
private void InitDSPSettings(uint srcChannels)

View File

@ -78,9 +78,9 @@ namespace MoonWorks.Audio
LoopLength = 0;
}
public StaticSoundInstance CreateInstance()
public StaticSoundInstance CreateInstance(bool loop = false)
{
return new StaticSoundInstance(Device, this, false, true);
return new StaticSoundInstance(Device, this, false, loop);
}
protected virtual void Dispose(bool disposing)

View File

@ -4,8 +4,6 @@ namespace MoonWorks.Audio
{
public class StaticSoundInstance : SoundInstance
{
public bool Loop { get; }
private SoundState _state = SoundState.Stopped;
public override SoundState State
{
@ -30,14 +28,13 @@ namespace MoonWorks.Audio
}
}
public StaticSoundInstance(
internal StaticSoundInstance(
AudioDevice device,
StaticSound parent,
bool is3D,
bool loop = false
) : base(device, parent, is3D)
bool loop
) : base(device, parent, is3D, loop)
{
Loop = loop;
State = SoundState.Stopped;
}