Share via


How to use Google.Apis.Translate and voice recognition with .Net

Google offers services and features available from Apis Web available to your application whatever the technology: Microsoft Windows, Apple or Android system.
We will also be interested in voice recognition mechanisms: Cortana for Microsoft, Google Assistant for Android or SIRI for Apple systems.

For Googles services, you must use Apis Google.Apis.Services, Apis web are web services remotely accessible from remote calls (here asynchronous):
an internet access is required as well as the registration with a Google account developer.

Here are the different services available by Google:

Google Maps Directions API
Google Maps Distance Matrix API
Google Maps Elevation API
Google Maps Geocoding API
Google Maps Geolocation API
Google Maps Roads API
Google Maps Time Zone API
Google Places API Web Service

For voice recognition, there is the Xamarin cross-platform feature TextToSpeech running on different vendors.
For the Microsoft platform, you have the namespace: Windows.Media.SpeechRecognition and Windows.Media.SpeechSynthesis.

https://developer.xamarin.com/guides/xamarin-forms/application-fundamentals/dependency-service/text-to-speech/Images/tts_diagram.png

To implement the Xamarin functionality TextToSpeech, you need this interface :

public interface ITextToSpeech
{
    void Speak (string text);
}

By exemple, for a Windows device you can use this code :

https://www.genuisoft.com/s/cc_images/cache_48859161.png?t=1505312641

public class TextToSpeechImplementation : ITextToSpeech
{
    public async void Speak(string text)
    {
        var mediaElement = new MediaElement();
        var synth = new Windows.Media.SpeechSynthesis.SpeechSynthesizer();
        var stream = await synth.SynthesizeTextToStreamAsync(text);
 
        mediaElement.SetSource(stream, stream.ContentType);
        mediaElement.Play();
    }
}

For Android plateform :

https://www.genuisoft.com/s/cc_images/cache_48859064.jpg?t=1505312498

      public class TextToSpeechImplementation :  ITextToSpeech, TextToSpeech.IOnInitListener
    {
        TextToSpeech speaker;
        string toSpeak;
 
        public void Speak(string text)
        {
            toSpeak = text;
            if (speaker == null)
            {
                speaker = new TextToSpeech(Forms.Context, this);
            }
            else
            {
                speaker.Speak(toSpeak, QueueMode.Flush, null, null);
            }
        }
 
        public void OnInit(OperationResult status)
        {
            if (status.Equals(OperationResult.Success))
            {
                speaker.Speak(toSpeak, QueueMode.Flush, null, null);
            }
        }
    }

For IoS device, use this code :
**
https://www.genuisoft.com/s/cc_images/cache_48859147.png?t=1505312557
**

      public class TextToSpeechImplementation : ITextToSpeech
    {
        public TextToSpeechImplementation() { }
 
        public void Speak(string text)
        {
            var speechSynthesizer = new AVSpeechSynthesizer();
            var speechUtterance = new AVSpeechUtterance(text)
            {
                Rate = AVSpeechUtterance.MaximumSpeechRate / 4,
                Voice = AVSpeechSynthesisVoice.FromLanguage("en-US"),
                Volume = 0.7f,
                PitchMultiplier = 1.0f
            };
 
            speechSynthesizer.SpeakUtterance(speechUtterance);
        }
    }

In this article, we will be interested in the Google feature of translations from Google.Apis.Translate:
To do this after creating a Google Developer Account, you must have a key or token to use
any Google service: here the key to Apis.

https://developers.google.com/_static/d28730e87a/images/redesign-14/lockup-color-knockout.png?hl=fr

using Google.Apis.Services;
using Google.Apis.Translate.v2;
using Google.Apis.Translate.v2.Data;
using System.Collections.Generic;
using TranslationsResource = Google.Apis.Translate.v2.Data.TranslationsResource;


namespace GenuisTranslateSpeech
{
    public sealed class TranslationManager
     {
    private static TranslationManager mInstance = null;
    private static object mSyncObj = new object ();
 
    public static TranslationManager Instance
    {
        get
        {
            if (mInstance == null)
            {
                lock (mSyncObj)
                {
                    mInstance = new TranslationManager ();
                }
            }
            return mInstance;
        }
    }
 
    private TranslationManager ()
    {
 
    }
 
    private string GetApiKey ()
    {
        return "<Private ID Key>";
    }


     public KeyValuePair<string, string> Translate(string srcText, string target_language = "en")
    {
        string[] text = new string[1] { srcText };
         KeyValuePair<string, string> translation = Translate(text, target_language);
        return translation;
    }
 
    public  KeyValuePair<string, string> Translate(string[] srcText, string target_language = "en")
    {
 
        // Create the service.
        var service = new TranslateService(new BaseClientService.Initializer()
        {
            ApiKey = GetApiKey(),
            ApplicationName = "Genuis Translate Speech"
        });
 
        TranslationsListResponse response = service.Translations.List(srcText, target_language).Execute();
        Dictionary<string, KeyValuePair<string, string>>
                           translations = new Dictionary<string, KeyValuePair<string, string>>();
 
        int counter = 0;
        foreach (TranslationsResource translation in response.Translations)
        {
            translations[srcText[counter]] =
                           new KeyValuePair<string, string>(translation.TranslatedText, translation.DetectedSourceLanguage);
            counter++;
        }
        return translations[srcText[0]];
    }
  }
 }

In your main class, you need to call the Translate method define in your TranslateManager helper class like this:

 

      public async Task<string> TranslateText(string input)
    {
            string target_language = RetreiveTargetLang();
            KeyValuePair<string, string> translation = TranslationManager.Instance.Translate(input, target_language);
            return translation.Key;
    }

You can use an appendix method from a drop-down list to select the culture
to translate our method of translations from our Google service.

 

            private string RetreiveTargetLang ()
        {
            string target_language = "en";
            switch (indexTo)
            {
                case 0:
                    target_language = "en";
                    break;
                case 1:
                    target_language = "en";
                    break;
                case 2:
                    target_language = "it";
                    break;
                case 3:
                    target_language = "es";
                    break;
                case 4:
                    target_language = "de";
                    break;
                case 5:
                    target_language = "da";
                    break;
                 default: 
                     target_language = "en";
                 break;
        }

Cortana functionality of speech recognition
About the Windows method to use Cortana, you can use this method in your application :

In this example, we will use namespaces to use the features
of Cortana with the SpeechSynthesizer type for synthesizing a string of characters for the
rendering in the form of a pronunciation from the vocal features of Cortana.

using Windows.Media.SpeechRecognition;
**using Windows.Media.SpeechSynthesis;

https://www.genuisoft.com/s/cc_images/cache_48859161.png?t=1505312641
**

            public async Task SpeakSample()
        {
            var dispatcher = CoreApplication.MainView.CoreWindow.Dispatcher;
            await dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () =>
            {
                try
                {
                    SR = new SpeechRecognizer();
                    SR.UIOptions.AudiblePrompt = "Hello";
                    SR.UIOptions.ExampleText = "Is it me you're looking for?";
                    var result = await SR.RecognizeWithUIAsync();
 
                    MediaElement mediaElement = new MediaElement();
                    using (SpeechSynthesizer speech = new SpeechSynthesizer())
                    {
                        speech.Voice = SpeechSynthesizer.AllVoices.First
                                         (i => i.Gender == VoiceGender.Female && i.Language.Contains("en"));
 
                        var enText = result.Text;
 
                        var voiceStream = await speech.SynthesizeTextToStreamAsync(enText);
                        mediaElement.SetSource(voiceStream, voiceStream.ContentType);
                        mediaElement.Play();
                    }
                }
                catch (Exception ex) 
                {
                    var error = ex.Message;
                }
            });
        }

          private async Task<string> SpeakVoice(string text)
          {
            try
            {
                MediaElement mediaElement = new MediaElement();
                using (SpeechSynthesizer speech = new SpeechSynthesizer())
                {
                    speech.Voice = SpeechSynthesizer
                                                  .AllVoices.First(i => i.Gender == VoiceGender.Female);
                    var culture = new CultureInfo("en-US");
                    Windows.Globalization.ApplicationLanguages
                                                   .PrimaryLanguageOverride = culture.Name;
                    CultureInfo.DefaultThreadCurrentCulture = culture;
                    CultureInfo.DefaultThreadCurrentUICulture = culture;
                  
                    speech.Voice = SpeechSynthesizer.AllVoices.First(i => i.Gender == VoiceGender.Female);

                    TxtInfo.Text = text;
                    var voiceStream = await speech.SynthesizeTextToStreamAsync(text);

                    mediaElement.SetSource(voiceStream, voiceStream.ContentType);
                    mediaElement.Play();
                }
            }
            catch (Exception exce)
            {

            }
            return text;
        }

     Sample call of SpeakSample():
     
     await SpeakSample();

     Sample call of SpeakVoice(string):

     await SpeakVoice("Welcome to Genuis Speech Translate");

https://www.genuisoft.com/s/cc_images/cache_48377584.png  https://www.genuisoft.com/s/cc_images/cache_48377576.png  https://www.genuisoft.com/s/cc_images/cache_48377577.png https://www.genuisoft.com/s/cc_images/cache_48377582.png

Visit my Portfolio Website : www.genuisoft.com

Daniel Padrosa
Software Engineer senior
**Microsoft Certified Professional 
Microsoft Professional Associate 
Microsoft Certified Technology Specialist
Microsoft Certified Professional Developer
**