Pepper.SFX
All Guidance pages

Guidance / Cookbook

Performance & Debugging

Recipe: Voice Count Display

📋 What: Real-time voice statistics HUD

🎯 Use Case: Development, profiling, debugging

💻 Code:

using UnityEngine;
using AudioSystem;

public class AudioDebugHUD : MonoBehaviour
{
    [SerializeField] private bool showDebug = true;
    [SerializeField] private KeyCode toggleKey = KeyCode.F1;

    private GUIStyle style;

    void Start()
    {
        style = new GUIStyle();
        style.normal.textColor = Color.white;
        style.fontSize = 14;
        style.padding = new RectOffset(10, 10, 10, 10);
    }

    void Update()
    {
        if (Input.GetKeyDown(toggleKey))
        {
            showDebug = !showDebug;
        }
    }

    void OnGUI()
    {
        if (!showDebug || AudioManager.Instance == null) return;

        var stats = AudioManager.Instance.GetStatistics();

        int y = 10;
        int lineHeight = 20;

        GUI.Label(new Rect(10, y, 500, lineHeight),
            $"<color=yellow>AUDIO SYSTEM DEBUG (F1 to toggle)</color>",
            style);
        y += lineHeight + 5;

        GUI.Label(new Rect(10, y, 500, lineHeight),
            $"Active Voices: {stats.activeVoices} / {stats.totalVoices}",
            style);
        y += lineHeight;

        GUI.Label(new Rect(10, y, 500, lineHeight),
            $"Available Voices: {stats.availableVoices}",
            style);
        y += lineHeight;

        GUI.Label(new Rect(10, y, 500, lineHeight),
            $"Active Loops: {stats.activeLoops}",
            style);
        y += lineHeight;

        // Voice usage bar
        float usagePercent = (float)stats.activeVoices / stats.totalVoices;
        DrawUsageBar(new Rect(10, y, 300, 20), usagePercent);
    }

    void DrawUsageBar(Rect rect, float percent)
    {
        // Background
        GUI.color = Color.black;
        GUI.DrawTexture(rect, Texture2D.whiteTexture);

        // Fill
        Rect fillRect = new Rect(rect.x, rect.y, rect.width * percent, rect.height);

        if (percent < 0.7f)
            GUI.color = Color.green;
        else if (percent < 0.9f)
            GUI.color = Color.yellow;
        else
            GUI.color = Color.red;

        GUI.DrawTexture(fillRect, Texture2D.whiteTexture);

        GUI.color = Color.white;
    }
}

Setup:

  1. Attach to any GameObject
  2. Press F1 to toggle
  3. Monitor voice usage in real-time

✅ Result: Development HUD showing audio system stats!


Recipe: Event Callbacks for Debugging

📋 What: Log when sounds start/finish

🎯 Use Case: Debugging event flow, testing

💻 Code:

using UnityEngine;
using AudioSystem;

public class AudioEventLogger : MonoBehaviour
{
    [SerializeField] private AudioEvent eventToMonitor;
    [SerializeField] private bool logToConsole = true;
    [SerializeField] private bool logToScreen = false;

    private string lastLog = "";
    private float logDisplayTime = 0f;

    void Start()
    {
        TestEvent();
    }

    void TestEvent()
    {
        if (eventToMonitor == null) return;

        AudioHandle handle = eventToMonitor.Post(gameObject, transform.position);

        if (handle != null)
        {
            handle.OnStarted += () => Log($"[{handle.EventName}] STARTED");
            // (OnLoop exists but isn't raised in this version, so it's left out here.)
            handle.OnFinished += () => Log($"[{handle.EventName}] FINISHED");
        }
    }

    void Log(string message)
    {
        if (logToConsole)
        {
            Debug.Log($"<color=cyan>{message}</color>");
        }

        if (logToScreen)
        {
            lastLog = message;
            logDisplayTime = Time.time;
        }
    }

    void OnGUI()
    {
        if (logToScreen && Time.time - logDisplayTime < 3f)
        {
            GUI.Label(new Rect(10, Screen.height - 30, 500, 20), lastLog);
        }
    }
}

✅ Result: Detailed event lifecycle logging!

From the Pepper.SFX 3.1 guides (USER_GUIDE.md). Something wrong or unclear? Tell me.