AIDevKit - AI Suite for Unity
API ReferencesDiscordGlitch9
  • Introduction
    • AI Dev Kit 3.7.0
    • Troubleshooting
    • FAQ
    • Update Logs
  • Provider Setup
    • OpenAI
    • Google Gemini
    • ElevenLabs
    • Ollama
    • OpenRouter
  • Editor Tools
    • Introduction
    • Editor Chat
    • Model Library
    • Voice Library
  • GEN Tasks
    • Prefixes
    • Response
    • Chat
    • Image
    • Video
    • SoundFX
    • Speech
    • Transcript
    • Voice Change
    • Audio Isolation
  • Advanced API (Pro)
    • Assistants
      • How it works
      • Creating custom functions
      • Creating assistants API
    • Realtime
  • Legacy API
    • OpenAI
      • 💬Chat completions
      • 🖼️Image operations
      • 🗣️Text to speech
      • 🎙️Speech to text
        • Recording real-time in Unity
      • 💾Files
      • 🔎Embeddings
      • 🛡️Moderations
      • ⚙️Fine-tuning
    • Google Gemini
      • 📝System instructions
      • 💬Text generation
      • ⚙️Fine-tuning
      • ▶️Fucntion calling
      • 🔎Embeddings
      • 🛡️Safety
      • 💻Code execution
  • Legacy Documents
    • AI Dev Kit 1.0
      • Preperation
      • Scriptable Toolkits
        • Chat Streamer
        • Image Generator
        • Voice Transcriber
        • Voice Generator
      • Editor Tools
      • Troubleshooting (Legacy)
        • ❗Build Error: The name 'UnityMenu' does not exist in the current context
        • ❗The type or namespace name 'AndroidJavaObject' could not be found
        • ❗The type or namaspace name 'Plastic' does not exist
        • ❗Build Error: The name 'Asset Database' does not exist in the current context
        • ❗'ModelData.Create(Provider, string, UnixTime?, string)': not all code paths return a value
      • Code Generators
        • C# Script Generator
        • Unity Component Generator
    • AI Dev Kit 2.0
      • Event Handlers
      • Editor Chat
      • Editor Vision (TTI, ITI)
      • Editor Speech (TTS)
      • Management Tools
        • Prompt History Viewer
        • AI Model Manager
        • TTS Voice Manager
        • OpenAI File Manager
        • OpenAI Assistant Manager
        • ElevenLabs Voice Library
Powered by GitBook
On this page
  1. Legacy API
  2. OpenAI
  3. Speech to text

Recording real-time in Unity

To record voice in real-time within your Unity project, you can utilize the AudioRecorder class to accomplish this task.

var recorder = new AudioRecorder();
recorder.StartRecording(); // Start recording the voice

// Wait until the recording is done

var audioClip = recorder.StopRecording(); // Stop recording and get the audio clip

// Now you can use the audio clip to request transcription or translation
// For example:

var request = new TranscriptionRequest.Builder()
    .SetModel(WhisperModel.Whisper1)
    .SetFile(audioClip)
    .Build();

var result = await request.ExecuteAsync();

Debug.Log(result.Text);
Debug.Log(result.Language);
Debug.Log(result.Duration);

MonoBehaviour Example

using UnityEngine;
using System;
using System.IO;
using System.Net.Http;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;

public class OpenAISpeechToText : MonoBehaviour
{
    private AudioRecorder _audioRecorder;

    private void Start()
    {
        _audioRecorder = new AudioRecorder();
    }

    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.R))
        {
            _audioRecorder.StartRecording();
        }

        if (Input.GetKeyDown(KeyCode.S))
        {
            _audioRecorder.StopRecording();
        }

        if (Input.GetKeyDown(KeyCode.P))
        {
            _audioRecorder.PlayRecording(gameObject);
        }

        if (Input.GetKeyDown(KeyCode.T))
        {
            StartCoroutine(TranscribeAudio());
        }
    }

    private async UniTask TranscribeAudio()
    {
        AudioClip audioClip = _audioRecorder.GetRecording();
        if (audioClip == null)
        {
            Debug.LogWarning("No audio clip found for transcription.");
            return;
        }

       var request = new TranscriptionRequest.Builder()
            .SetModel(WhisperModel.Whisper1)
            .SetFile(audioClip)
            .Build();

        var result = await request.ExecuteAsync();

        Debug.Log(result.Text);
        Debug.Log(result.Language);
        Debug.Log(result.Duration);
    }
}
PreviousSpeech to textNextFiles

Last updated 1 month ago

🎙️