Skip to content
This repository has been archived by the owner on Jul 19, 2024. It is now read-only.

Commit

Permalink
Fixed high CPU usage
Browse files Browse the repository at this point in the history
  • Loading branch information
demonixis committed Dec 23, 2022
1 parent 7a85248 commit b81319d
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 51 deletions.
3 changes: 2 additions & 1 deletion Assets/Scenes/Main.unity
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ MonoBehaviour:
_maxAlternatives: 0
_autoLoadModel: 0
_autoStart: 0
_audioChunkSize: 4096
_audioChunkSize: 1024
_sampleRate: 16000
_audioClipBufferSeconds: 1
_listeningDevice:
Expand All @@ -502,6 +502,7 @@ MonoBehaviour:
_logPartialResults: 0
_logModelLoad: 0
_logRecording: 0
_checkTime: 0.25
--- !u!114 &115404856
MonoBehaviour:
m_ObjectHideFlags: 0
Expand Down
2 changes: 2 additions & 0 deletions Assets/Scripts/Robot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ private void Awake()

private void Start()
{
Application.targetFrameRate = 30;

if (_autoStartRobot)
InitializeRobot();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
Expand Down Expand Up @@ -53,6 +54,8 @@ [SerializeField] [Header("Audio Settings")]
[SerializeField] [Header("Misc.")]
private VoskListenerDebugOptions _debugOptions = VoskListenerDebugOptions.CreateAllDisabled();

[SerializeField] private float _checkTime = 0.25f;

#pragma warning restore CS0649

private VoskRecognizer _voskRecognizer;
Expand Down Expand Up @@ -145,69 +148,78 @@ protected virtual void Start()
{
StartListening();
}

StartCoroutine(UpdateLoop());
}

private void Update()
private IEnumerator UpdateLoop()
{
if (IsListening && IsReady)
var wait = new WaitForSeconds(_checkTime);

while (true)
{
int micPos = Microphone.GetPosition(_listeningDevice);

int sampleDelta = micPos >= _previousMicPosition
? (micPos - _previousMicPosition)
: (_microphoneAudio.samples * _microphoneAudio.channels - (_previousMicPosition - micPos));

if (sampleDelta * FLOAT_BYTE_SIZE >= AudioChunkSize)
if (IsListening && IsReady)
{
byte[] waveData = _microphoneAudio.GetWavData(sampleDelta, _previousMicPosition);
int micPos = Microphone.GetPosition(_listeningDevice);

int bufferCount = Mathf.CeilToInt(waveData.Length / (float) AudioChunkSize);
int sampleDelta = micPos >= _previousMicPosition
? (micPos - _previousMicPosition)
: (_microphoneAudio.samples * _microphoneAudio.channels - (_previousMicPosition - micPos));

bool cancel = false;

for (int i = 0; i < bufferCount; i++)
if (sampleDelta * FLOAT_BYTE_SIZE >= AudioChunkSize)
{
byte[] buffer = waveData.Skip(i * AudioChunkSize).Take(AudioChunkSize).ToArray();
byte[] waveData = _microphoneAudio.GetWavData(sampleDelta, _previousMicPosition);

int bufferCount = Mathf.CeilToInt(waveData.Length / (float) AudioChunkSize);

string result = string.Empty;
bool cancel = false;

if (_voskRecognizer.AcceptWaveform(buffer, buffer.Length))
for (int i = 0; i < bufferCount; i++)
{
_previousPartialResult = null;
byte[] buffer = waveData.Skip(i * AudioChunkSize).Take(AudioChunkSize).ToArray();

result = _voskRecognizer.Result();
string result = string.Empty;

_resultDeserializer.UseAlternatives = MaxAlternatives > 0;
VoskResult voskResult = _resultDeserializer.Deserialize(result);
if (_voskRecognizer.AcceptWaveform(buffer, buffer.Length))
{
_previousPartialResult = null;

OnResultFound(voskResult);
}
else
{
result = _voskRecognizer.PartialResult();
result = _voskRecognizer.Result();

VoskPartialResult partialResult = _partialResultDeserializer.Deserialize(result);
_resultDeserializer.UseAlternatives = MaxAlternatives > 0;
VoskResult voskResult = _resultDeserializer.Deserialize(result);

if (partialResult != null && partialResult.Text != _previousPartialResult?.Text)
OnResultFound(voskResult);
}
else
{
cancel = OnPartialResultFound(partialResult);
_previousPartialResult = partialResult;
result = _voskRecognizer.PartialResult();

if (cancel)
VoskPartialResult partialResult = _partialResultDeserializer.Deserialize(result);

if (partialResult != null && partialResult.Text != _previousPartialResult?.Text)
{
result = _voskRecognizer.FinalResult();
cancel = OnPartialResultFound(partialResult);
_previousPartialResult = partialResult;

if (cancel)
{
result = _voskRecognizer.FinalResult();

_resultDeserializer.UseAlternatives = MaxAlternatives > 0;
VoskResult voskResult = _resultDeserializer.Deserialize(result);
_resultDeserializer.UseAlternatives = MaxAlternatives > 0;
VoskResult voskResult = _resultDeserializer.Deserialize(result);

OnResultFound(voskResult);
OnResultFound(voskResult);
}
}
}
}
}

_previousMicPosition = micPos;
_previousMicPosition = micPos;
}
}

yield return wait;
}
}

Expand Down
2 changes: 1 addition & 1 deletion Packages/packages-lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"url": "https://packages.unity.com"
},
"com.unity.burst": {
"version": "1.7.3",
"version": "1.7.4",
"depth": 1,
"source": "registry",
"dependencies": {
Expand Down
5 changes: 3 additions & 2 deletions ProjectSettings/ProjectSettings.asset
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ PlayerSettings:
m_SubKind:
m_BuildTargetBatching:
- m_BuildTarget: Standalone
m_StaticBatching: 0
m_StaticBatching: 1
m_DynamicBatching: 0
- m_BuildTarget: tvOS
m_StaticBatching: 1
Expand Down Expand Up @@ -636,6 +636,7 @@ PlayerSettings:
switchNetworkInterfaceManagerInitializeEnabled: 1
switchPlayerConnectionEnabled: 1
switchUseNewStyleFilepaths: 0
switchUseLegacyFmodPriorities: 0
switchUseMicroSleepForYield: 1
switchEnableRamDiskSupport: 0
switchMicroSleepForYieldTime: 25
Expand Down Expand Up @@ -761,7 +762,7 @@ PlayerSettings:
platformArchitecture: {}
scriptingBackend:
Android: 1
Standalone: 1
Standalone: 0
il2cppCompilerConfiguration:
Standalone: 2
il2cppCodeGeneration: {}
Expand Down
4 changes: 2 additions & 2 deletions ProjectSettings/ProjectVersion.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
m_EditorVersion: 2022.2.0f1
m_EditorVersionWithRevision: 2022.2.0f1 (35dcd44975df)
m_EditorVersion: 2022.2.1f1
m_EditorVersionWithRevision: 2022.2.1f1 (4fead5835099)
29 changes: 21 additions & 8 deletions ProjectSettings/QualitySettings.asset
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,32 @@ QualitySettings:
serializedVersion: 5
m_CurrentQuality: 0
m_QualitySettings:
- serializedVersion: 2
- serializedVersion: 3
name: High
pixelLightCount: 2
pixelLightCount: 1
shadows: 2
shadowResolution: 1
shadowProjection: 1
shadowCascades: 2
shadowCascades: 1
shadowDistance: 40
shadowNearPlaneOffset: 3
shadowCascade2Split: 0.33333334
shadowCascade4Split: {x: 0.06666667, y: 0.2, z: 0.46666667}
shadowmaskMode: 0
skinWeights: 2
textureQuality: 0
skinWeights: 4
globalTextureMipmapLimit: 0
textureMipmapLimitSettings: []
anisotropicTextures: 1
antiAliasing: 0
softParticles: 0
softVegetation: 1
realtimeReflectionProbes: 1
billboardsFaceCameraPosition: 1
vSyncCount: 0
realtimeReflectionProbes: 0
billboardsFaceCameraPosition: 0
useLegacyDetailDistribution: 0
vSyncCount: 1
lodBias: 1
maximumLODLevel: 0
enableLODCrossFade: 0
streamingMipmapsActive: 0
streamingMipmapsAddAllCameras: 1
streamingMipmapsMemoryBudget: 512
Expand All @@ -41,7 +44,17 @@ QualitySettings:
asyncUploadPersistentBuffer: 1
resolutionScalingFixedDPIFactor: 1
customRenderPipeline: {fileID: 0}
terrainQualityOverrides: 0
terrainPixelError: 1
terrainDetailDensityScale: 1
terrainBasemapDistance: 1000
terrainDetailDistance: 80
terrainTreeDistance: 5000
terrainBillboardStart: 50
terrainFadeLength: 5
terrainMaxTrees: 50
excludedTargetPlatforms: []
m_TextureMipmapLimitGroupNames: []
m_PerPlatformDefaultQuality:
Android: 0
Lumin: 0
Expand Down

0 comments on commit b81319d

Please sign in to comment.