1
0
forked from cgvr/DeltaVR

cafe waiter npc and mic detect speech more

This commit is contained in:
2026-02-21 22:06:23 +02:00
parent dbd2cdca0d
commit 8a3f10a9a2
5 changed files with 113 additions and 25 deletions

View File

@@ -62,6 +62,7 @@ public class FMODWhisperBridge : MonoBehaviour
// activation flag
private bool isRecordingActivated = false;
private bool _skipOneFeedFrame = false;
private void Awake()
{
@@ -188,13 +189,15 @@ public class FMODWhisperBridge : MonoBehaviour
// Wire events
_stream.OnSegmentUpdated += (seg) =>
{
if (IsSpeechMeaningful(seg.Result))
OnWhisperSegmentUpdated?.Invoke(seg.Result);
string cleanedText = PostProcessInput(seg.Result);
if (!string.IsNullOrEmpty(cleanedText))
OnWhisperSegmentUpdated?.Invoke(cleanedText);
};
_stream.OnSegmentFinished += (seg) =>
{
if (IsSpeechMeaningful(seg.Result))
OnWhisperSegmentFinished?.Invoke(seg.Result);
string cleanedText = PostProcessInput(seg.Result);
if (!string.IsNullOrEmpty(cleanedText))
OnWhisperSegmentFinished?.Invoke(cleanedText);
};
whisper.useVad = useVadInStream;
@@ -202,15 +205,28 @@ public class FMODWhisperBridge : MonoBehaviour
_stream.StartStream();
_streamStarted = true;
// Unpause loopback if it's meant to play only while active
if (playLoopback && loopbackOnlyWhenActive && _playChannel.hasHandle())
_playChannel.setPaused(false);
// Prepare temp arrays roughly 100ms of audio
EnsureTmpCapacity((rate / 10) * _nativeChannels);
// --- NEW: Clear the ring buffer and reset read pointer ---
// Pause loopback while we clear (optional, but avoids clicks)
if (playLoopback && _playChannel.hasHandle())
_playChannel.setPaused(true);
// Clear buffer bytes
ClearRecordRingBuffer();
// Reset our read pointer to the current write head
_core.getRecordPosition(recordDriverId, out _lastRecordPos);
// Well skip feeding for one frame to guarantee a clean start
_skipOneFeedFrame = true;
// Unpause loopback if we want it active during recording
if (playLoopback && _playChannel.hasHandle() && (!loopbackOnlyWhenActive || isRecordingActivated))
_playChannel.setPaused(loopbackOnlyWhenActive ? false : _playChannel.getPaused(out var paused) == FMOD.RESULT.OK && paused ? false : false);
isRecordingActivated = true;
Debug.Log("[FMOD→Whisper] Stream activated (Whisper started; FMOD was already recording).");
Debug.Log("[FMOD→Whisper] Stream activated (buffer cleared; reading from current head).");
}
/// <summary>
@@ -269,36 +285,37 @@ public class FMODWhisperBridge : MonoBehaviour
IntPtr p1, p2;
uint len1, len2;
var r = _recSound.@lock(startBytes, bytesToRead, out p1, out p2, out len1, out len2);
if (r != RESULT.OK)
{
// If lock fails, still advance last position to avoid spin
_lastRecordPos = recPos;
return;
}
try
{
if (shouldFeed)
if (shouldFeed && !_skipOneFeedFrame)
{
if (len1 > 0) CopyPcm16ToFloatAndFeed(p1, len1);
if (len2 > 0) CopyPcm16ToFloatAndFeed(p2, len2);
}
// else: just discard; were only keeping the ring fresh.
// If skipping, we just discard this frame to ensure no stale data leaks.
}
finally
{
_recSound.unlock(p1, p2, len1, len2);
}
if (_skipOneFeedFrame) _skipOneFeedFrame = false;
_lastRecordPos = recPos;
}
private bool IsSpeechMeaningful(string userText)
private string PostProcessInput(string input)
{
return !string.IsNullOrEmpty(userText)
&& !userText.Contains("BLANK_AUDIO")
&& !userText.Trim().Equals("[ Silence ]");
return input.Replace("[silence]", "").Replace("[ Silence ]", "").Replace("BLANK_AUDIO", "").Replace("[", "").Replace("]", "").Trim();
}
private void CopyPcm16ToFloatAndFeed(IntPtr src, uint byteLen)
@@ -359,4 +376,55 @@ public class FMODWhisperBridge : MonoBehaviour
_recSound.clearHandle();
}
}
private void ClearRecordRingBuffer()
{
if (!_recSound.hasHandle() || _soundPcmLength == 0) return;
uint totalBytes = _soundPcmLength * (uint)_nativeChannels * 2; // PCM16
IntPtr p1, p2;
uint len1, len2;
// Lock the whole buffer (start=0, length=totalBytes)
var r = _recSound.@lock(0, totalBytes, out p1, out p2, out len1, out len2);
if (r != FMOD.RESULT.OK)
{
Debug.LogWarning($"[FMOD→Whisper] Could not lock ring buffer to clear: {r}");
return;
}
try
{
if (len1 > 0)
{
// zero p1
// Well reuse a static zero array to avoid allocating huge buffers repeatedly
ZeroMem(p1, (int)len1);
}
if (len2 > 0)
{
ZeroMem(p2, (int)len2);
}
}
finally
{
_recSound.unlock(p1, p2, len1, len2);
}
}
// cheap zeroing helper (avoids allocating len-sized arrays each time)
private static readonly byte[] _zeroChunk = new byte[16 * 1024]; // 16 KB
private static void ZeroMem(IntPtr dst, int byteLen)
{
int offset = 0;
while (byteLen > 0)
{
int n = Math.Min(_zeroChunk.Length, byteLen);
Marshal.Copy(_zeroChunk, 0, dst + offset, n);
offset += n;
byteLen -= n;
}
}
}