Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error: Cannot create FMOD::Sound instance for clip "" (FMOD error: Error loading file. ) #89

Closed
AmarTrivedi1 opened this issue Aug 26, 2024 · 6 comments
Labels
bug Something isn't working

Comments

@AmarTrivedi1
Copy link
Contributor

AmarTrivedi1 commented Aug 26, 2024

Bug Report

Overview

Error: Cannot create FMOD::Sound instance for clip "" (FMOD error: Error loading file. )
UnityEngine.StackTraceUtility:ExtractStackTrace ()
Utilities.WebRequestRest.Rest/<DownloadAudioClipAsync>d__43:MoveNext () (at ./Library/PackageCache/[email protected]/Runtime/Rest.cs:680)
System.Runtime.CompilerServices.AsyncMethodBuilderCore/MoveNextRunner:Run ()
Utilities.Async.AwaiterExtensions:RunOnUnityScheduler (System.Action) (at ./Library/PackageCache/[email protected]/Runtime/Async/AwaiterExtensions.cs:248)
Utilities.Async.SimpleCoroutineAwaiter:Complete (System.Exception) (at ./Library/PackageCache/[email protected]/Runtime/Async/SimpleCoroutineAwaiter.cs:47)
Utilities.Async.AwaiterExtensions/<ReturnVoid>d__27:MoveNext () (at ./Library/PackageCache/[email protected]/Runtime/Async/AwaiterExtensions.cs:334)
UnityEngine.SetupCoroutine:InvokeMoveNext (System.Collections.IEnumerator,intptr)

To Reproduce

Steps to reproduce the behavior:
I simply made a normal Text to Speech request using the library.

The first request I make works just fine, but any continued requests throw this error. When I stop and start the game again, it works the first time.

@AmarTrivedi1 AmarTrivedi1 added the bug Something isn't working label Aug 26, 2024
@StephenHodgson
Copy link
Member

Need a code snippet for repro steps

@StephenHodgson
Copy link
Member

first request I make works just fine, but any continued requests throw this error. When I stop and start the game again, it works the first time.

It is generating mp3 or PCM?

@AmarTrivedi1
Copy link
Contributor Author

AmarTrivedi1 commented Aug 26, 2024

This is my ElevenLabsManager.cs script:

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using ElevenLabs;
using System.Threading.Tasks;
using System.Threading;
using Utilities.Async;

public class ElevenLabsManager : MonoBehaviour
{

    // Singleton instance
    public static ElevenLabsManager Instance { get; private set; }

    private string elevenLabsAPIKey;

    private ElevenLabsClient api;

    private void Awake()
    {
        // Ensure there's only one instance of AIManager
        if (Instance == null)
        {
            Instance = this;
            //DontDestroyOnLoad(gameObject); // Optional: Keep this instance between scene loads
        }
        else
        {
            Destroy(gameObject);
        }
    }

    void Start()
    {
        
        // Get the API key set in the user's environment variables
        elevenLabsAPIKey = Environment.GetEnvironmentVariable("ELEVEN_LABS_API_KEY", EnvironmentVariableTarget.User);

        Debug.Log(elevenLabsAPIKey);

        if (!string.IsNullOrEmpty(elevenLabsAPIKey))
        {
            Debug.Log("API Key found: " + elevenLabsAPIKey);
            // Use the API key in your requests or other logic
        }
        else
        {
            Debug.LogError("API Key not found!");
        }
        

        // Initialize the ElevenLabs API Client
        api = new ElevenLabsClient(elevenLabsAPIKey);

        if (api == null)
        {
            Debug.LogError("ElevenLabsClient could not be initialized.");
        }

    }

    // Function to generate an audio clip from text and voiceId
    public async Task<AudioClip> GenerateAudioClip(string text, string voiceId)
    {
        try
        {
            // Fetch the voice using the voiceId
            var voice = await api.VoicesEndpoint.GetVoiceAsync(voiceId);
            if (voice == null)
            {
                Debug.LogError("Voice not found with the given voiceId.");
                return null;
            }

            // Fetch the default voice settings
            var defaultVoiceSettings = await api.VoicesEndpoint.GetDefaultVoiceSettingsAsync();

            if (defaultVoiceSettings == null)
            {
                Debug.LogError("Default voice settings could not be retrieved.");
                return null;
            }

            // Generate the AudioClip using the TextToSpeech API
            var voiceClip = await api.TextToSpeechEndpoint.TextToSpeechAsync(text, voice, defaultVoiceSettings);

            return voiceClip.AudioClip;
        }
        catch (System.Exception ex)
        {
            Debug.LogError($"Error generating audio clip: {ex.Message}");
            return null;
        }
    }



}

I am making calls to it like so:

AIUser.cs

// Text to speech- I accidently named this function SpeechToText. Will need to fix this, but inform others to change their function name in their scripts.
    public async void SpeechToText(string text)
    {
        AudioClip clip;

        if (characterData.usingOnlineTTS == false)
        {
            clip = await AIManager.Instance.GetTextToSpeech(text, characterData.speakerID);
        }
        else
        {
            clip = await AIManager.Instance.GetElevenLabsAudioClip(text, characterData.elevenLabsModelID);
        }
        
        audioSource.clip = clip;
        
        audioSource.loop = false;
        audioSource.Play();
    }

AIManager.cs

// *** ONLINE TEXT TO SPEECH FUNCTIONS ***
    
    // Function to get audio clip from ElevenLabs using ElevenLabsManager
    public async Task<AudioClip> GetElevenLabsAudioClip(string text, string voiceId)
    {
        try
        {
            // Use the ElevenLabsManager's GenerateAudioClip function to get the audio clip
            AudioClip clip = await ElevenLabsManager.Instance.GenerateAudioClip(text, voiceId);
            return clip;
        }
        catch (System.Exception ex)
        {
            Debug.LogError($"Error in GetElevenLabsAudioClip: {ex.Message}");
            return null;
        }
    }

AIUser -> AIManager -> ElevenLabsManager

I greatly appreciate you responding so quickly!

@StephenHodgson
Copy link
Member

What happens when you run the sample scene?

@StephenHodgson
Copy link
Member

Usually this should only happen in this context #35

@AmarTrivedi1
Copy link
Contributor Author

AmarTrivedi1 commented Aug 26, 2024

The sample scene seems to run with no issues.

I noticed you used audioSource.PlayOneShot(), and so I changed the end of my AIUser function to this:

// Text to speech- I accidently named this function SpeechToText. Will need to fix this, but inform others to change their function name in their scripts.
    public async void SpeechToText(string text)
    {
        AudioClip clip;

        if (characterData.usingOnlineTTS == false)
        {
            clip = await AIManager.Instance.GetTextToSpeech(text, characterData.speakerID);
        }
        else
        {
            clip = await AIManager.Instance.GetElevenLabsAudioClip(text, characterData.elevenLabsModelID);
        }
        
        //audioSource.clip = clip;
        
        //audioSource.loop = false;
        audioSource.PlayOneShot(clip);
    }

And I seem to be getting no errors now. Could that possibly be it!? I only changed the last three lines of that function.

@RageAgainstThePixel RageAgainstThePixel locked and limited conversation to collaborators Aug 26, 2024
@StephenHodgson StephenHodgson converted this issue into discussion #90 Aug 26, 2024

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

7 participants
@StephenHodgson @AmarTrivedi1 and others