Skip to content

Commit

Permalink
Better logging (#48)
Browse files Browse the repository at this point in the history
* Better logging

* Actually need this
  • Loading branch information
Macoron committed Aug 10, 2023
1 parent c428e31 commit ca485db
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 42 deletions.
13 changes: 12 additions & 1 deletion Assets/Samples/5 - Streaming/5 - Streaming.unity
Original file line number Diff line number Diff line change
Expand Up @@ -546,14 +546,15 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: d9370225a2ca94276b870d5f87b0db55, type: 3}
m_Name:
m_EditorClassIdentifier:
LogLevel: 1
modelPath: Whisper/ggml-tiny.bin
isModelPathInStreamingAssets: 1
initOnAwake: 1
language: en
translateToEnglish: 0
strategy: 0
noContext: 1
singleSegment: 0
singleSegment: 1
enableTokens: 0
initialPrompt:
stepSec: 3
Expand Down Expand Up @@ -1799,6 +1800,16 @@ MonoBehaviour:
frequency: 16000
chunksLengthSec: 0.2
echo: 1
useVad: 1
vadUpdateRateSec: 0.1
vadContextSec: 10
vadLastSec: 1.25
vadThd: 0.6
vadFreqThd: 100
vadIndicatorImage: {fileID: 0}
vadStop: 0
dropVadPart: 1
vadStopTime: 3
microphoneDropdown: {fileID: 948466958}
microphoneDefaultLabel: Default mic
--- !u!114 &1337591124
Expand Down
52 changes: 52 additions & 0 deletions Packages/com.whisper.unity/Runtime/Utils/LogUtils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using System;
using UnityEngine;

namespace Whisper.Utils
{
public enum LogLevel
{
Verbose,
Log,
Warning,
Error,
}

/// <summary>
/// Wrapper for Unity logger that can be configured by log level.
/// </summary>
public static class LogUtils
{
public static LogLevel Level = LogLevel.Verbose;

public static void Exception(Exception msg)
{
Debug.LogException(msg);
}

public static void Error(string msg)
{
Debug.LogError(msg);
}

public static void Warning(string msg)
{
if (Level > LogLevel.Warning)
return;
Debug.LogWarning(msg);
}

public static void Log(string msg)
{
if (Level > LogLevel.Log)
return;
Debug.Log(msg);
}

public static void Verbose(string msg)
{
if (Level > LogLevel.Verbose)
return;
Debug.Log(msg);
}
}
}
3 changes: 3 additions & 0 deletions Packages/com.whisper.unity/Runtime/Utils/LogUtils.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 22 additions & 12 deletions Packages/com.whisper.unity/Runtime/WhisperManager.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
using System;
using System.IO;
using System.Threading.Tasks;
using JetBrains.Annotations;
using UnityEngine;
using UnityEngine.Serialization;
using Whisper.Native;
using Whisper.Utils;

namespace Whisper
{
public class WhisperManager : MonoBehaviour
{
[SerializeField] [Tooltip("Path to model weights file")]
private string modelPath = "Whisper/ggml-base.bin";
[Tooltip("Log level for whisper loading and inference")]
public LogLevel logLevel = LogLevel.Log;

[Header("Model")]
[SerializeField]
[Tooltip("Path to model weights file")]
private string modelPath = "Whisper/ggml-tiny.bin";

public string ModelPath
{
Expand Down Expand Up @@ -90,7 +93,7 @@ public bool IsModelPathInStreamingAssets

[Tooltip("[EXPERIMENTAL] Speed-up the audio by 2x using Phase Vocoder. " +
"These can significantly reduce the quality of the output.")]
public bool speedUp = false;
public bool speedUp;

[Tooltip("[EXPERIMENTAL] Overwrite the audio context size (0 = use default). " +
"These can significantly reduce the quality of the output.")]
Expand All @@ -116,11 +119,18 @@ public bool IsModelPathInStreamingAssets

private async void Awake()
{
LogUtils.Level = logLevel;

if (!initOnAwake)
return;
await InitModel();
}

private void OnValidate()
{
LogUtils.Level = logLevel;
}

private void Update()
{
_dispatcher.Update();
Expand All @@ -134,13 +144,13 @@ public async Task InitModel()
// check if model is already loaded or actively loading
if (IsLoaded)
{
Debug.LogWarning("Whisper model is already loaded and ready for use!");
LogUtils.Warning("Whisper model is already loaded and ready for use!");
return;
}

if (IsLoading)
{
Debug.LogWarning("Whisper model is already loading!");
LogUtils.Warning("Whisper model is already loading!");
return;
}

Expand All @@ -160,7 +170,7 @@ public async Task InitModel()
}
catch (Exception e)
{
Debug.LogException(e);
LogUtils.Exception(e);
}

IsLoading = false;
Expand All @@ -170,7 +180,7 @@ public bool IsMultilingual()
{
if (!IsLoaded)
{
Debug.LogError("Whisper model isn't loaded! Init Whisper model first!");
LogUtils.Error("Whisper model isn't loaded! Init Whisper model first!");
return false;
}

Expand Down Expand Up @@ -210,7 +220,7 @@ public async Task<WhisperStream> CreateStream(int frequency, int channels)
var isLoaded = await CheckIfLoaded();
if (!isLoaded)
{
Debug.LogError("Model weights aren't loaded! Load model first!");
LogUtils.Error("Model weights aren't loaded! Load model first!");
return null;
}

Expand All @@ -226,7 +236,7 @@ public async Task<WhisperStream> CreateStream(MicrophoneRecord microphone)
var isLoaded = await CheckIfLoaded();
if (!isLoaded)
{
Debug.LogError("Model weights aren't loaded! Load model first!");
LogUtils.Error("Model weights aren't loaded! Load model first!");
return null;
}

Expand Down Expand Up @@ -257,7 +267,7 @@ private async Task<bool> CheckIfLoaded()
{
if (!IsLoaded && !IsLoading)
{
Debug.LogError("Whisper model isn't loaded! Init Whisper model first!");
LogUtils.Error("Whisper model isn't loaded! Init Whisper model first!");
return false;
}

Expand Down
6 changes: 3 additions & 3 deletions Packages/com.whisper.unity/Runtime/WhisperParams.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using System.Runtime.InteropServices;
using UnityEngine;
using Whisper.Native;
using Whisper.Utils;

namespace Whisper
{
Expand Down Expand Up @@ -351,9 +351,9 @@ private void FreeInitialPromptString()
public static WhisperParams GetDefaultParams(WhisperSamplingStrategy strategy =
WhisperSamplingStrategy.WHISPER_SAMPLING_GREEDY)
{
Debug.Log($"Requesting default Whisper params for strategy {strategy}...");
LogUtils.Verbose($"Requesting default Whisper params for strategy {strategy}...");
var nativeParams = WhisperNative.whisper_full_default_params(strategy);
Debug.Log("Default params generated!");
LogUtils.Verbose("Default params generated!");

var param = new WhisperParams(nativeParams)
{
Expand Down
8 changes: 4 additions & 4 deletions Packages/com.whisper.unity/Runtime/WhisperStream.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using UnityEngine;
using Whisper.Utils;
// ReSharper disable once RedundantUsingDirective
using System.Linq;

namespace Whisper
Expand Down Expand Up @@ -135,7 +135,7 @@ public void StartStream()
{
if (_isStreaming)
{
Debug.LogWarning("Stream is already working!");
LogUtils.Warning("Stream is already working!");
return;
}
_isStreaming = true;
Expand All @@ -152,7 +152,7 @@ public void AddToStream(float[] samples)
{
if (!_isStreaming)
{
Debug.LogWarning("Start streaming first!");
LogUtils.Warning("Start streaming first!");
return;
}

Expand All @@ -167,7 +167,7 @@ public async void StopStream()
{
if (!_isStreaming)
{
Debug.LogWarning("Start streaming first!");
LogUtils.Warning("Start streaming first!");
return;
}
_isStreaming = false;
Expand Down
Loading

0 comments on commit ca485db

Please sign in to comment.