How to Integrate Azure AI Speech in Unity: Step-by-Step Guide by info.odysseyx@gmail.com October 3, 2024 written by info.odysseyx@gmail.com October 3, 2024 0 comment 7 views 7 Have you ever planned to create a virtual assistant for your games? Or is it something like the real world where players can talk to NPCs? Well, if you want to integrate the power of artificial intelligence within your game, the possibilities can be endless, and luckily, Azure AI Services provides a variety of tools to help you: utilize Within the Unity game engine. In this blog sore We’ll walk you through using Azure AI Speech in Unity by creating a simple speech-to-text function. Prerequisites: Unity 2020.3 or later. Your subscription key for the Azure Speech service. (You can try Azure for free.) A working microphone and access to it. Create an Azure AI Speech resource: Go to Azure AI Speech Studio Sign in with your Microsoft account. Select Settings, and then select Create resource. Configure with the following settings: New resource name: Please enter a unique name. application: Azure subscription. region: Please select a supported region. Pricing tiers: Free FO (if available, otherwise select standard S0). Resource group: Select or create a resource group with a unique name. Select Create resource. Wait for the resource to be created, then select Use Resource. The audio welcome page is displayed. You can explore Speech Studio and check out the different tools, but for now: Real-time voice to text. check it out Microsoft Learn – Azure AI Voice To explore more. Copy region and resource key Click the settings icon in the top right corner. Copy “Region” and “Resource Key”. Unity project settings Create a new 3D project in Unity. Right click on the Hierrarchy panel and go to UI “Text – TextMeshPro”. Right click on the Hierrarchy panel and go to UI “Button – TextMeshPro”. Place these two components in the Scene View. “text” Used to display recognized speech. “button” It is used to record your speech. Import the Speech SDK into your Unity project Download the Speech SDK from: here. After downloading, select Next to get the Speech SDK. asset > import package > custom package (Alternatively, you can double-click the downloaded package and open it in your current project.) Make sure all files are selected and click Import. Create a script Right-click in the Assets folder and create a C# script called “Speech.cs”. Copy and paste the code below – using UnityEngine; using UnityEngine.UI; using Microsoft.CognitiveServices.Speech; using TMPro; #if PLATFORM_ANDROID using UnityEngine.Android; #endif #if PLATFORM_IOS using UnityEngine.iOS; using System.Collections; #endif public class HelloWorld : MonoBehaviour { // Hook up the two properties below with a Text and Button object in your UI. public TextMeshProUGUI outputText; public Button startRecoButton; private object threadLocker = new object(); private bool waitingForReco; private string message; private bool micPermissionGranted = false; #if PLATFORM_ANDROID || PLATFORM_IOS // Required to manifest microphone permission, cf. // https://docs.unity3d.com/Manual/android-manifest.html private Microphone mic; #endif public async void ButtonClick() { // Creates an instance of a speech config with specified subscription key and service region. // Replace with your own subscription key and service region (e.g., "westus"). var config = SpeechConfig.FromSubscription("", ""); // Make sure to dispose the recognizer after use! using (var recognizer = new SpeechRecognizer(config)) { lock (threadLocker) { waitingForReco = true; } // Starts speech recognition, and returns after a single utterance is recognized. The end of a // single utterance is determined by listening for silence at the end or until a maximum of 15 // seconds of audio is processed. The task returns the recognition text as result. // Note: Since RecognizeOnceAsync() returns only a single utterance, it is suitable only for single // shot recognition like command or query. // For long-running multi-utterance recognition, use StartContinuousRecognitionAsync() instead. var result = await recognizer.RecognizeOnceAsync().ConfigureAwait(false); // Checks result. string newMessage = string.Empty; if (result.Reason == ResultReason.RecognizedSpeech) { newMessage = result.Text; } else if (result.Reason == ResultReason.NoMatch) { newMessage = "NOMATCH: Speech could not be recognized."; } else if (result.Reason == ResultReason.Canceled) { var cancellation = CancellationDetails.FromResult(result); newMessage = $"CANCELED: Reason={cancellation.Reason} ErrorDetails={cancellation.ErrorDetails}"; } lock (threadLocker) { message = newMessage; waitingForReco = false; } } } void Start() { if (outputText == null) { UnityEngine.Debug.LogError("outputText property is null! Assign a UI Text element to it."); } else if (startRecoButton == null) { message = "startRecoButton property is null! Assign a UI Button to it."; UnityEngine.Debug.LogError(message); } else { // Continue with normal initialization, Text and Button objects are present. #if PLATFORM_ANDROID // Request to use the microphone, cf. // https://docs.unity3d.com/Manual/android-RequestingPermissions.html message = "Waiting for mic permission"; if (!Permission.HasUserAuthorizedPermission(Permission.Microphone)) { Permission.RequestUserPermission(Permission.Microphone); } #elif PLATFORM_IOS if (!Application.HasUserAuthorization(UserAuthorization.Microphone)) { Application.RequestUserAuthorization(UserAuthorization.Microphone); } #else micPermissionGranted = true; message = "Click button to recognize speech"; #endif startRecoButton.onClick.AddListener(ButtonClick); } } void Update() { #if PLATFORM_ANDROID if (!micPermissionGranted && Permission.HasUserAuthorizedPermission(Permission.Microphone)) { micPermissionGranted = true; message = "Click button to recognize speech"; } #elif PLATFORM_IOS if (!micPermissionGranted && Application.HasUserAuthorization(UserAuthorization.Microphone)) { micPermissionGranted = true; message = "Click button to recognize speech"; } #endif lock (threadLocker) { if (startRecoButton != null) { startRecoButton.interactable = !waitingForReco && micPermissionGranted; } if (outputText != null) { outputText.text = message; } } } } // Please enter the copied content. “Subscription Key” and “region” to line 36 Save the script and return to Unity. 3. Script Canvas GameObject. 4. Instead “Output Text” and “Start recording button” drag text and button Each component created in the scene. test Now click the play button in Unity to enter game mode. in game mode button And speak into the microphone. You will see your voice converted to text. In this way, you can easily integrate Azure AI Speech and use it for Speech to Text. you can do more modify You can make your imagination a reality according to your own needs.. Learn more about the different use cases for Azure AI Speech. text to speech It’s about understanding its full potential. Resources include: Source link Share 0 FacebookTwitterPinterestEmail info.odysseyx@gmail.com previous post Discover Exciting Marketing Job Opportunities in Mumbai with WinMagic Toys Private Limited Careers next post Dominate your industry and boost performance using Azure AI tools and skilling You may also like 7 Disturbing Tech Trends of 2024 December 19, 2024 AI on phones fails to impress Apple, Samsung users: Survey December 18, 2024 Standout technology products of 2024 December 16, 2024 Is Intel Equivalent to Tech Industry 2024 NY Giant? December 12, 2024 Google’s Willow chip marks breakthrough in quantum computing December 11, 2024 Job seekers are targeted in mobile phishing campaigns December 10, 2024 Leave a Comment Cancel Reply Save my name, email, and website in this browser for the next time I comment.