From 13a1ef64910de1babdf8d92c215142d069b3fa9d Mon Sep 17 00:00:00 2001 From: cosmonaut Date: Wed, 20 Apr 2022 14:31:46 -0700 Subject: [PATCH] add audio Seek functionality --- src/SoundInstance.cs | 2 ++ src/StaticSoundInstance.cs | 25 +++++++++++++++++++++++++ src/StreamingSoundOgg.cs | 29 +++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+) diff --git a/src/SoundInstance.cs b/src/SoundInstance.cs index ee1550f..2b9cc96 100644 --- a/src/SoundInstance.cs +++ b/src/SoundInstance.cs @@ -241,6 +241,8 @@ namespace FineAudio public abstract void Play(bool loop); public abstract void Pause(); public abstract void Stop(bool immediate); + public abstract void Seek(float seconds); + public abstract void Seek(uint sampleFrame); private void InitDSPSettings(uint srcChannels) { diff --git a/src/StaticSoundInstance.cs b/src/StaticSoundInstance.cs index 47fcc48..e6f95aa 100644 --- a/src/StaticSoundInstance.cs +++ b/src/StaticSoundInstance.cs @@ -93,6 +93,31 @@ namespace FineAudio } } + private void PerformSeek(uint sampleFrame) + { + if (State == SoundState.Playing) + { + FAudio.FAudioSourceVoice_Stop(Handle, 0, 0); + FAudio.FAudioSourceVoice_FlushSourceBuffers(Handle); + } + + Parent.Handle.PlayBegin = sampleFrame; + Play(); + } + + public override void Seek(float seconds) + { + uint sampleFrame = + (uint) (Parent.SamplesPerSecond * seconds); + + PerformSeek(sampleFrame); + } + + public override void Seek(uint sampleFrame) + { + PerformSeek(sampleFrame); + } + public void Free() { Parent.FreeInstance(this); diff --git a/src/StreamingSoundOgg.cs b/src/StreamingSoundOgg.cs index b11be92..ed7ec97 100644 --- a/src/StreamingSoundOgg.cs +++ b/src/StreamingSoundOgg.cs @@ -63,6 +63,35 @@ namespace FineAudio device.AddDynamicSoundInstance(this); } + private void PerformSeek(uint sampleFrame) + { + if (State == SoundState.Playing) + { + FAudio.FAudioSourceVoice_Stop(Handle, 0, 0); + } + + FAudio.FAudioSourceVoice_FlushSourceBuffers(Handle); + ClearBuffers(); + FAudio.stb_vorbis_seek(VorbisHandle, sampleFrame); + QueueBuffers(); + + if (State == SoundState.Playing) + { + Play(); + } + } + + public override void Seek(float seconds) + { + uint sampleFrame = (uint) (Info.sample_rate * seconds); + PerformSeek(sampleFrame); + } + + public override void Seek(uint sampleFrame) + { + PerformSeek(sampleFrame); + } + protected override void AddBuffer( out float[] buffer, out uint bufferOffset,