I’ve written quite a bit on this blog about speech interactions in the past and elsewhere like these articles that I wrote for the Windows blog a couple of years ago;
Using speech in your UWP apps- It’s good to talk
which came out of earlier investigations that I did for this blog like this post;
Speech to Text (and more) with Windows 10 UWP & ‘Project Oxford’
and we talked about Speech in our Channel9 show again a couple of years ago now;
and so I won’t rehash the whole topic here of speech recognition and understanding but in the last week I’ve been working on a fairly simple scenario that I thought I would share the code from.
Backdrop – the Scenario
The scenario involved a Unity application built against the “Stable .NET 3.5 Equivalent” scripting runtime which targets both HoloLens and immersive Windows Mixed Reality headsets where there was a need to use natural language instructions inside of the app.
That is, there’s a need to;
- grab audio from the microphone.
- turn the audio into text.
- take the text and derive the user’s intent from the spoken text.
- drive some action inside of the application based on that intent.
It’s fairly generic although the specific application is quite exciting but in order to get this implemented there’s some choices around technologies/APIs and whether functionality happens in the cloud or at the edge.
Choices
When it comes to (2) there’s a couple of choices in that there are layered Unity/UWP APIs that can make this happen and the preference in this scenario would be to use the Unity APIs which are the KeywordRecognizer and the DictationRecognizer for handling short/long chunks of speech respectively.
Those APIs are packaged so as to wait for a reasonable, configurable period of time for some speech to occur before delivering a ‘speech occurred’ type event to the caller passing the text that has been interpreted from the speech.
There’s no cost (beyond on-device resources) to using these APIs and so in a scenario which only went as far as speech-to-text it’d be quite reasonable to have these types of APIs running all the time gathering up text and then having the app decide what to do with it.
However, when it comes to (3), the API of choice is LUIS which can take a piece of text like;
“I’d like to order a large pepperoni pizza please”
and can turn it into something like;
Intent: OrderPizza
Entity: PizzaType (Pepperoni)
Entity: Size (Large)
Confidence: 0.85
and so it’s a very useful thing as it takes the task of fathoming all the intricacies of natural language away from the developer.
This poses a bit of a challenge though for a ‘real time’ app in that it’s not reasonable to take every speech utterance that the user delivers and run it through the LUIS cloud service. There’s a number of reasons for that including;
- The round-trip time from the client to the service is likely to be fairly long and so, without care, the app would have many calls in flight leading to problems with response time and complicating the code and user experience.
- The service has a financial cost.
- The user may not expect or want all of their utterances to be run through the cloud.
Consequently, it seems sensible to have some trigger in an app which signifies that the user is about to say something that is of meaning to the app and which should be sent off to the LUIS service for examination. In short, it’s the;
“Hey, Cortana”
type key phrase that lets the system know that the user has something to say.
This can be achieved in a Unity app targeting .NET 3.5 by having the KeywordRecognizer class work in conjunction with the DictationRecognizer class such that the former listens for the speech keyword (‘hey, Cortana!’) and the latter then springs into life and listens for the dictated phrase that the user wants to pass on to the app.
As an aside, it’s worth flagging that these classes are only supported by Unity on Windows 10 as detailed in the docs and that there is an isSupported flag to let the developer test this at runtime.
There’s another aside to using these two classes together in that the docs here note that different types of recognizer cannot be instantiated at once and that they rely on an underlying PhraseRecognitionSystem and that the system has to be Shutdown in order to switch between one type of recognizer and another.
Later on in the post, I’ll return to the idea of making a different choice around turning speech to text but for the moment, I moved forward with the DictationRecognizer.
Getting Something Built
Some of that took a little while to figure out but once it’s sorted it’s “fairly” easy to write some code in Unity which uses a KeywordRecognizer to switch on/off a DictationRecognizer in an event-driven loop so as to gather dictated text.
I chose to have the notion of a DictationSink which is just something that receives some text from somewhere. It could have been an interface but I thought that I’d bring in MonoBehavior;
using UnityEngine; public class DictationSink : MonoBehaviour { public virtual void OnDictatedText(string text) { } }
and so then I can write a DictationSource which surfaces a few properties from the underlying DictationRecognizer and passes on recognized text to a DictationSink;
using System; using UnityEngine; using UnityEngine.Windows.Speech; public class DictationSource : MonoBehaviour { public event EventHandler DictationStopped; public float initialSilenceSeconds; public float autoSilenceSeconds; public DictationSink dictationSink; // TODO: Think about whether this should be married with the notion of // a focused object rather than just some 'global' entity. void NewRecognizer() { this.recognizer = new DictationRecognizer(); this.recognizer.InitialSilenceTimeoutSeconds = this.initialSilenceSeconds; this.recognizer.AutoSilenceTimeoutSeconds = this.autoSilenceSeconds; this.recognizer.DictationResult += OnDictationResult; this.recognizer.DictationError += OnDictationError; this.recognizer.DictationComplete += OnDictationComplete; this.recognizer.Start(); } public void Listen() { this.NewRecognizer(); } void OnDictationComplete(DictationCompletionCause cause) { this.FireStopped(); } void OnDictationError(string error, int hresult) { this.FireStopped(); } void OnDictationResult(string text, ConfidenceLevel confidence) { this.recognizer.Stop(); if ((confidence == ConfidenceLevel.Medium) || (confidence == ConfidenceLevel.High) && (this.dictationSink != null)) { this.dictationSink.OnDictatedText(text); } } void FireStopped() { this.recognizer.DictationComplete -= this.OnDictationComplete; this.recognizer.DictationError -= this.OnDictationError; this.recognizer.DictationResult -= this.OnDictationResult; this.recognizer = null; // https://docs.microsoft.com/en-us/windows/mixed-reality/voice-input-in-unity // The challenge we have here is that we want to use both a KeywordRecognizer // and a DictationRecognizer at the same time or, at least, we want to stop // one, start the other and so on. // Unity does not like this. It seems that we have to shut down the // PhraseRecognitionSystem that sits underneath them each time but the // challenge then is that this seems to stall the UI thread. // So far (following the doc link above) the best plan seems to be to // not call Stop() on the recognizer or Dispose() it but, instead, to // just tell the system to shutdown completely. PhraseRecognitionSystem.Shutdown(); if (this.DictationStopped != null) { // And tell any friends that we are done. this.DictationStopped(this, EventArgs.Empty); } } DictationRecognizer recognizer; }
notice in that code my attempt to use PhraseRecognitionSystem.Shutdown() to really stop this recognizer when I’ve processed a single speech utterance from it.
I need to switch this recognition on/off in response to a keyword being spoken by the user and so I wrote a simple KeywordDictationSwitch class which tries to do this using KeywordRecognizer with a few keywords;
using System.Linq; using UnityEngine; using UnityEngine.Windows.Speech; public class KeywordDictationSwitch : MonoBehaviour { public string[] keywords = { "ok", "now", "hey", "listen" }; public DictationSource dictationSource; void Start() { this.NewRecognizer(); this.dictationSource.DictationStopped += this.OnDictationStopped; } void NewRecognizer() { this.recognizer = new KeywordRecognizer(this.keywords); this.recognizer.OnPhraseRecognized += this.OnPhraseRecgonized; this.recognizer.Start(); } void OnDictationStopped(object sender, System.EventArgs e) { this.NewRecognizer(); } void OnPhraseRecgonized(PhraseRecognizedEventArgs args) { if (((args.confidence == ConfidenceLevel.Medium) || (args.confidence == ConfidenceLevel.High)) && this.keywords.Contains(args.text.ToLower()) && (this.dictationSource != null)) { this.recognizer.OnPhraseRecognized -= this.OnPhraseRecgonized; this.recognizer = null; // https://docs.microsoft.com/en-us/windows/mixed-reality/voice-input-in-unity // The challenge we have here is that we want to use both a KeywordRecognizer // and a DictationRecognizer at the same time or, at least, we want to stop // one, start the other and so on. // Unity does not like this. It seems that we have to shut down the // PhraseRecognitionSystem that sits underneath them each time but the // challenge then is that this seems to stall the UI thread. // So far (following the doc link above) the best plan seems to be to // not call Stop() on the recognizer or Dispose() it but, instead, to // just tell the system to shutdown completely. PhraseRecognitionSystem.Shutdown(); // And then start up the other system. this.dictationSource.Listen(); } else { Debug.Log(string.Format("Dictation: Listening for keyword {0}, heard {1} with confidence {2}, ignored", this.keywords, args.text, args.confidence)); } } void StartDictation() { this.dictationSource.Listen(); } KeywordRecognizer recognizer; }
and once again I’m going through some steps to try and switch the KeywordRecognizer on/off here so that I can then switch the DictationRecognizer on/off as simply calling Stop() on a recognizer isn’t enough.
With this in place, I can now stack these components in Unity and have them use each other;
and so now I’ve got some code that listens for keywords, switches dictation on, listens for dictation and then passes that on to some DictationSink.
That’s a nice place to implement some LUIS functionality.
In doing so, I ended up writing perhaps more code than I’d liked as I’m not sure whether there is a LUIS library that works from a Unity environment targeting the Stable .NET 3.5 subset. I’ve found this to be a challenge with calling a few Azure services from Unity and LUIS doesn’t seem to be an exception in that there are client libraries on NuGet for most scenarios but I don’t think that they work in Unity (I could be wrong) and there aren’t generally examples/samples for Unity.
So…I rolled some small pieces of my own here which isn’t so hard when the call that we need here with LUIS is just a REST call.
Based on the documentation around the most basic “GET” functionality as detailed in the LUIS docs here, I wrote some classes to represent the LUIS results;
using System; using System.Linq; namespace LUIS.Results { [Serializable] public class QueryResultsIntent { public string intent; public float score; } [Serializable] public class QueryResultsResolution { public string[] values; public string FirstOrDefaultValue() { string value = string.Empty; if (this.values != null) { value = this.values.FirstOrDefault(); } return (value); } } [Serializable] public class QueryResultsEntity { public string entity; public string type; public int startIndex; public int endIndex; public QueryResultsResolution resolution; public string FirstOrDefaultResolvedValue() { var value = string.Empty; if (this.resolution != null) { value = this.resolution.FirstOrDefaultValue(); } return (value); } public string FirstOrDefaultResolvedValueOrEntity() { var value = this.FirstOrDefaultResolvedValue(); if (string.IsNullOrEmpty(value)) { value = this.entity; } return (value); } } [Serializable] public class QueryResults { public string query; public QueryResultsEntity[] entities; public QueryResultsIntent topScoringIntent; } }
and then wrote some code to represent a Query of the LUIS service. I wrote this on top of pieces that I borrowed from my colleague, Dave’s, repo over here in github which provides some Unity compatible REST pieces with JSON serialization etc.
using LUIS.Results; using RESTClient; using System; using System.Collections; namespace LUIS { public class Query { string serviceBaseUrl; string serviceKey; public Query(string serviceBaseUrl, string serviceKey) { this.serviceBaseUrl = serviceBaseUrl; this.serviceKey = serviceKey; } public IEnumerator Get(Action<IRestResponse<QueryResults>> callback) { var request = new RestRequest(this.serviceBaseUrl, Method.GET); request.AddQueryParam("subscription-key", this.serviceKey); request.AddQueryParam("q", this.Utterance); request.AddQueryParam("verbose", this.Verbose.ToString()); request.UpdateRequestUrl(); yield return request.Send(); request.ParseJson<QueryResults>(callback); } public bool Verbose { get;set; } public string Utterance { get;set; } } }
and so now I can Query LUIS and get results back and so it’s fairly easy to put this into a DictationSink which passes the dictated speech in text form off to LUIS;
using LUIS; using LUIS.Results; using System; using System.Linq; using UnityEngine.Events; [Serializable] public class QueryResultsEventType : UnityEvent<QueryResultsEntity[]> { } [Serializable] public class DictationSinkHandler { public string intentName; public QueryResultsEventType intentHandler; } public class LUISDictationSink : DictationSink { public float minimumConfidenceScore = 0.5f; public DictationSinkHandler[] intentHandlers; public string luisApiEndpoint; public string luisApiKey; public override void OnDictatedText(string text) { var query = new Query(this.luisApiEndpoint, this.luisApiKey); query.Utterance = text; StartCoroutine(query.Get( results => { if (!results.IsError) { var data = results.Data; if ((data.topScoringIntent != null) && (data.topScoringIntent.score > this.minimumConfidenceScore)) { var handler = this.intentHandlers.FirstOrDefault( h => h.intentName == data.topScoringIntent.intent); if (handler != null) { handler.intentHandler.Invoke(data.entities); } } } } )); } }
and this is really just a map which takes a look at the confidence score provided by LUIS, makes sure that it is high enough for our purposes and then looks into a map which maps between the names of the LUIS intents and a function which handles that intent set up here as a UnityEvent<T> so that it can be configured in the editor.
So, in use if I have some LUIS model which has intents named Create, DeleteAll and DeleteType then I can configure up an instance of this LUISDictationSink in Unity as below to map these to functions inside of a class named LUISIntentHandlers in this case;
and then a handler for this type of interaction might look something like;
public void OnIntentCreate(LUIS.Results.QueryResultsEntity[] entities) { // We need two pieces of information here - the shape type and // the distance. var entityShapeType = entities.FirstOrDefault(e => e.type == "shapeType"); var entityDistance = entities.FirstOrDefault(e => e.type == "builtin.number"); // ... }
and this all works fine and completes the route that goes from;
keyword recognition –> start dictation –> end dictation –> LUIS –> intent + entities –> handler in code –> action
Returning to Choices – Multi-Language & Dictation in the Cloud
I now have some code that works and it feels like the pieces are in the ‘best’ place in that I’m running as much as possible on the device and hopefully only calling the cloud when I need to. That said, if I could get the capabilities of LUIS offline and run then on the device then I’d like to do that too but it’s not something that I think you can do right now with LUIS.
However, there is one limit to what I’m currently doing which isn’t immediately obvious and it’s that it is limited in terms of offering the possibility of non-English languages and, specifically, on HoloLens where (as far as I know) the recognizer classes only offer English support.
So, to support other languages I’d need to do my speech to text work via some other route – I can’t rely on the DictationRecognizer alone.
As an aside, it’s worth saying that I think multi-language support would need more work than just getting the speech to text to work in another language.
I think it would also require building a LUIS model in another language but that’s something that could be done.
An alternate way of performing speech-to-text that does support multiple languages would be to bring in a cloud powered speech to text API like the Cognitive Service Speech API and I could bring that into my code here by wrapping it up as a new type of DictationSource.
That speech-to-text API has some different ways of working. Specifically it can perform speech to text by;
- Submitting an audio file in a specified format to a REST endpoint and getting back text.
- Opening a websocket and sending chunks of streamed speech data up to the service to get back responses.
Of the two, the second has the advantage that it can be a bit smarter around detecting silence in the stream and it can also offer interim ‘hypotheses’ around what is being said before it delivers its ultimate view of what the utterance was. It can also support longer sections of speech than the file-based method.
So, this feels like a good way to go as an alternate DictationSource for my code.
However, making use of that API requires sending a stream of audio data to the cloud down a websocket in a format that is compatible with the service on the other end of the wire and that’s code I’d like to avoid writing. Ideally, it feels like the sort of code that one developer who was close to the service would write once and everyone would then re-use.
That work is already done if you’re using the service from .NET and you’re in a situation where you can make use of the client library that wrappers up the service access but I don’t think that it’s going to work for me from Unity when targeting the “Stable .NET 3.5 Equivalent” scripting runtime.
So…for this post, I’m going to leave that as a potential ‘future exercise’ that I will try to return back to if time permits and I’ll update the post if I do so.
In the meantime, here’s the code.
Code
If you’re interested in the code then it’s wrapped up in a simple Unity project that’s here on github;
That code is coupled to a LUIS service which has some very basic intents and entities around creating simple Unity game objects (spheres and cubes) at a certain distance in front of the user. It’s very rough.
There are three intents inside of this service. One is intended to create objects with utterances like “I want to create a cube 2 metres away”
and then it’s possible to delete everything that’s been created with a simple utterance;
and lastly it’s possible to get rid of just the spheres/cubes with a different intent such as “get rid of all the cubes”;
If you wanted to make the existing code run then you’d need an API endpoint and a service key for such a service and so I’ve exported the service itself from LUIS as a JSON export into this file in the repo;
so it should be possible to go to the LUIS portal and import that as a service;
and then plug in the endpoint and service key into the code here;