1
0
forked from cgvr/DeltaVR

remove audio loopback functionality

This commit is contained in:
2026-03-23 09:22:20 +02:00
parent ee629e2593
commit 50002adb61
6 changed files with 21 additions and 60 deletions

View File

@@ -4,9 +4,9 @@ using System.Runtime.InteropServices;
using UnityEngine;
using FMOD;
using FMODUnity;
using Whisper; // WhisperManager, WhisperStream, WhisperResult
using Whisper;
using Whisper.Utils;
using Debug = UnityEngine.Debug; // AudioChunk
using Debug = UnityEngine.Debug;
/// <summary>
/// FMOD mic is initialized once (Start) and runs continuously in a ring buffer.
@@ -28,12 +28,6 @@ public class FMODWhisperBridge : MonoBehaviour
public int channels = 1;
[Range(1, 10)] public int bufferLengthSec = 5;
[Header("Loopback (monitor your voice)")]
public bool playLoopback = true;
[Tooltip("If true, loopback plays only while active; otherwise its always on.")]
public bool loopbackOnlyWhenActive = true;
[Range(0f, 2f)] public float loopbackVolume = 1.0f;
public delegate void OnWhisperSegmentUpdatedDelegate(string result);
public event OnWhisperSegmentUpdatedDelegate OnWhisperSegmentUpdated;
@@ -44,7 +38,6 @@ public class FMODWhisperBridge : MonoBehaviour
private FMOD.System _core;
private Sound _recSound;
private Channel _playChannel;
private ChannelGroup _masterGroup;
private uint _soundPcmLength; // in samples
private int _nativeRate;
private int _nativeChannels;
@@ -138,24 +131,6 @@ public class FMODWhisperBridge : MonoBehaviour
_core.getRecordPosition(recordDriverId, out _lastRecordPos);
Debug.Log("[FMOD→Whisper] Recording started.");
// Loopback channel (optional). Start once; pause when inactive if desired.
_core.getMasterChannelGroup(out _masterGroup);
if (playLoopback)
{
res = _core.playSound(_recSound, _masterGroup, false, out _playChannel);
if (res == RESULT.OK && _playChannel.hasHandle())
{
_playChannel.setMode(MODE._2D);
_playChannel.setVolume(loopbackVolume);
if (loopbackOnlyWhenActive) _playChannel.setPaused(true); // keep muted until Activate
Debug.Log("[FMOD→Whisper] Loopback playback ready.");
}
else
{
Debug.LogWarning($"[FMOD→Whisper] playSound failed or channel invalid: {res}");
}
}
// No Whisper stream here. It will be created on ActivateRecording().
await System.Threading.Tasks.Task.Yield();
}
@@ -217,11 +192,6 @@ public class FMODWhisperBridge : MonoBehaviour
_streamStarted = true;
// --- 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();
@@ -231,13 +201,8 @@ public class FMODWhisperBridge : MonoBehaviour
// 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 (buffer cleared; reading from current head).");
}
/// <summary>
@@ -249,10 +214,6 @@ public class FMODWhisperBridge : MonoBehaviour
return;
isRecordingActivated = false;
// Pause loopback if it should only be active during recording
if (playLoopback && loopbackOnlyWhenActive && _playChannel.hasHandle())
_playChannel.setPaused(true);
}
/// <summary>
@@ -277,7 +238,6 @@ public class FMODWhisperBridge : MonoBehaviour
return Mathf.Clamp01(Mathf.InverseLerp(-60f, -15f, db));
}
private void Update()
{
// Always tick FMOD
@@ -285,8 +245,7 @@ public class FMODWhisperBridge : MonoBehaviour
if (!_recSound.hasHandle()) return;
// Compute how many samples recorded since last frame.
uint recPos;
_core.getRecordPosition(recordDriverId, out recPos);
_core.getRecordPosition(recordDriverId, out uint recPos);
uint deltaSamples = (recPos >= _lastRecordPos)
? (recPos - _lastRecordPos)
@@ -353,7 +312,6 @@ public class FMODWhisperBridge : MonoBehaviour
}
}
// If skipping, we just discard this frame to ensure no stale data leaks.
}
finally
@@ -364,7 +322,6 @@ public class FMODWhisperBridge : MonoBehaviour
if (_skipOneFeedFrame) _skipOneFeedFrame = false;
_lastRecordPos = recPos;
}
private string PostProcessInput(string input)