linking Microsoft.Speech with xamarin - c#

I'm trying to link a microphone in a mobile phone which uses android and trying to achieve voice recognition and tts. I want to use Microsot.Speech library, the thing is that how can I access the microphone.
please have a look at this sample code from microsoft's website:
using System;
using Microsoft.Speech.Recognition;
namespace SpeechRecognitionApp
{
class Program
{
static void Main(string[] args)
{
// Create a SpeechRecognitionEngine object for the default recognizer in the en-US locale.
using (
SpeechRecognitionEngine recognizer =
new SpeechRecognitionEngine(
new System.Globalization.CultureInfo("en-US")))
{
// Create a grammar for finding services in different cities.
Choices services = new Choices(new string[] { "restaurants", "hotels", "gas stations" });
Choices cities = new Choices(new string[] { "Seattle", "Boston", "Dallas" });
GrammarBuilder findServices = new GrammarBuilder("Find");
findServices.Append(services);
findServices.Append("near");
findServices.Append(cities);
// Create a Grammar object from the GrammarBuilder and load it to the recognizer.
Grammar servicesGrammar = new Grammar(findServices);
recognizer.LoadGrammarAsync(servicesGrammar);
// Add a handler for the speech recognized event.
recognizer.SpeechRecognized +=
new EventHandler<SpeechRecognizedEventArgs>(recognizer_SpeechRecognized);
// Configure the input to the speech recognizer.
recognizer.SetInputToDefaultAudioDevice();
// Start asynchronous, continuous speech recognition.
recognizer.RecognizeAsync(RecognizeMode.Multiple);
// Keep the console window open.
while (true)
{
Console.ReadLine();
}
}
}
// Handle the SpeechRecognized event.
static void recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
Console.WriteLine("Recognized text: " + e.Result.Text);
}}}
I think that the line of interest here is:
recognizer.SetInputToDefaultAudioDevice();
how can I change input to be device microphone?
many thanks.
note: I copied the code above from: https://msdn.microsoft.com/en-us/library/office/microsoft.speech.recognition.speechrecognitionengine(v=office.14).aspx

Related

MAUI Blazor App Microsoft.CognitiveServices.Speech keyword recognition opening custom table error

I was trying to create simple service that listens for keyword from speech. I was using Microsoft.CognitiveServices.Speech ver 1.24.1 and proceeding with official documentation but got error while trying to read KeywordRecognitionodel from file, file is accessible from that path. Also tried relative and absolute path.
using Microsoft.CognitiveServices.Speech;
using Microsoft.CognitiveServices.Speech.Audio;
namespace HandyCook.Services
{
internal class KeyWordRecognizerService
{
public KeyWordRecognizerService()
{
StartRecognizing();
}
private async void StartRecognizing()
{
var audioConfig = AudioConfig.FromDefaultMicrophoneInput();
var recognizer = new KeywordRecognizer(audioConfig);
recognizer.Recognized += (s, e) =>
{
Console.WriteLine($"{e.Result.Text} DETECTED");
};
var stream = await FileSystem.Current.OpenAppPackageFileAsync("XXX.table"); //accessible
//Exception with an error code: 0x8 (SPXERR_FILE_OPEN_FAILED)
var keywordModel = KeywordRecognitionModel.FromFile(Path.Combine(FileSystem.Current.AppDataDirectory, "XXX.table"));
var result = recognizer.RecognizeOnceAsync(keywordModel);
result.Wait();
}
}
}
To reproduce the error just simply create new MAUI Blazor App, add nuget package 'Microsoft.CognitiveServices.Speech 1.24.1', add service as singleton in MauiProgram then inject in Index.razor. I'm pasting also table file and solution explorer view.
table file: https://sendanywhe.re/18U3PXF8

LibVLCSharp: how to stop the application when player closes

I am trying to write a simple player application using LibVLCSharp and I am wondering how to stop the app when player closes. Currently, it just freezes and doesn't stop the app even though I added SetExitHandler callback.
using System;
using System.Threading;
using LibVLCSharp.Shared;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
Core.Initialize();
using var libVlc = new LibVLC();
using var mediaPlayer = new MediaPlayer(libVlc);
libVlc.SetExitHandler(() =>
{
Environment.Exit(1);
});
var media = new Media(libVlc, "v4l2:///dev/video0", FromType.FromLocation);
media.AddOption(":v4l2-standard=ALL :live-caching=300");
mediaPlayer.Play(media);
Thread.Sleep(TimeSpan.FromSeconds(10));
}
}
}
Log after I close the window:
[00007fa164004790] gl gl: Initialized libplacebo v2.72.0 (API v72)
[00007fa17400a7c0] main decoder error: buffer deadlock prevented
[00007fa1600429a0] xcb_window window error: X server failure
The following code example from LibVLCSharp GitHub Page shows how to play the video in a console application.
Core.Initialize();
using var libvlc = new LibVLC(enableDebugLogs: true);
using var media = new Media(libvlc, new Uri(#"C:\tmp\big_buck_bunny.mp4"));
using var mediaplayer = new MediaPlayer(media);
mediaplayer.Play();
Console.ReadKey();
Note the use of Console.ReadKey() to wait for the user to press a key before closing the application and subsequently closing the player.
To exit the application automatically when the video ends, you can use MediaPlayer.EndReached event as shown here:
Core.Initialize();
using var libvlc = new LibVLC(enableDebugLogs: true);
using var media = new Media(libvlc, new Uri(#"C:\tmp\big_buck_bunny.mp4"));
using var mediaplayer = new MediaPlayer(media);
mediaplayer.EndReached += (s, e) => Environment.Exit(0);
mediaplayer.Play();
Console.ReadKey();

Out to set audio file in AudioConfig function from Azure cognitive services

I would like to load an audio wav file in My Xamarin forms project.
The audio file is in SpeechApp=>Data=>audio.wav
I am using the Azure AudioConfig function like this:
var taskCompleteionSource = new TaskCompletionSource<int>();
var config = SpeechConfig.FromSubscription("xxx", "xx");
var transcriptionStringBuilder = new StringBuilder();
// Replace the language with your language in BCP-47 format, e.g., en-US.
var language = "fr-FR";
config.SpeechRecognitionLanguage = language;
config.OutputFormat = OutputFormat.Detailed;
using (var audioInput = AudioConfig.FromWavFileInput("SpeechApp.Data.audio.wav"))
{
using (var recognizer = new SpeechRecognizer(config, audioInput))
{
// Stops recognition.
await recognizer.StopContinuousRecognitionAsync();
}
}
But It is not working and I have this error : System.DllNotFoundException: Microsoft.CognitiveServices.Speech.core.dll
My error comes from this lines : var config = SpeechConfig.FromSubscription()
I was inspired from this web site :click here
Thanks for your help
Could you please try something like below:
using (var audioInput = AudioConfig.FromWavFileInput(#"SpeechApp.Data.audio.wav"))
{
using (var recognizer = new SpeechRecognizer(config, audioInput))
{
}
You can refer this link for more samples in different languages.
Hope it helps.

I can't use speech recognition with C#. Why?

I'd like to make a speech recognition software, but I always get the same error message, when I try to.
System.PlatformNotSupportedException:
I saw the same problem on stackoverflow, I installed the stuffs, but the problem is still exists. Here's my code below, and this is the stack overflow "solution", what not works for me:
PlatformNotSupportedException Using .NET Speech Recognition
Yes, I installed every mentioned stuffs...
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Speech.Recognition;
using System.Speech.Synthesis;
namespace SpeechExe
{
class ExecuteSpeech
{
SpeechRecognitionEngine sre = new SpeechRecognitionEngine();
Choices choices = new Choices();
GrammarBuilder gb = new GrammarBuilder();
private void MakeCommands()
{
string[] commantList = { "say hello" };
choices.Add(commantList);
gb.Append(choices);
Grammar grammar = new Grammar(gb);
sre.LoadGrammarAsync(grammar);
sre.SetInputToDefaultAudioDevice();
sre.SpeechRecognized += Sre_SpeechRecognized;
}
private void Sre_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
switch(e.Result.Text)
{
case "say hello":
Console.WriteLine("Hello!");
break;
}
}
public void Start()
{
this.MakeCommands();
sre.RecognizeAsync(RecognizeMode.Multiple);
}
}
}
I changed the windows's language to english, and I don't get this error message. (It was Hungarian.)

RaceOnRCWCleanup when running console app for speech recognition

I don't know either we can create a console app or not for speech recognition(searched for the same but didn't find any answer)and tried this code.
I have this code working in winforms app
but when trying to create this app in console visual studio is giving a very strange error mscorelib.pdb not found.And transferring to a page mscorelib.pdb
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Speech;
using System.Speech.Recognition;
using System.Speech.Synthesis;
using System.Threading;
namespace ConsoleApplication2
{
public class Program
{
public static void Main(string[] args)
{
tester tst = new tester();
tst.DoWorks();
}
}
public class tester
{
SpeechSynthesizer ss = new SpeechSynthesizer();
SpeechRecognitionEngine sre = new SpeechRecognitionEngine();
PromptBuilder pb = new PromptBuilder();
Choices clist = new Choices();
public void DoWorks()
{
clist.Add(new string[] { "how are you", "what is the current time", "open chrome", "hello" });
Grammar gr = new Grammar(new GrammarBuilder(clist));
sre.RequestRecognizerUpdate();
sre.LoadGrammar(gr);
sre.SetInputToDefaultAudioDevice();
sre.SpeechRecognized += sre_recognised;
sre.RecognizeAsync(RecognizeMode.Multiple);
}
public void sre_recognised(object sender, SpeechRecognizedEventArgs e)
{
switch (e.Result.Text.ToString())
{
case "hello":ss.SpeakAsync("Hello shekar");
break;
case "how are you": ss.SpeakAsync("I am fine and what about you");
break;
case "what is the time":ss.SpeakAsync("current time is: " + DateTime.Now.ToString());
break;
case "open chrome":System.Diagnostics.Process.Start("chrome", "wwe.google.com");
break;
default: ss.SpeakAsync("thank you");
break;
}
Console.WriteLine(e.Result.Text.ToString());
}
}
}
here is the snapshot of the error page
I also loaded the given option "Microsoft.symbol.Server", but it is still giving the same output.
EDIT
HERE is the output window
Outputs are of big lengths ,so not being able to show all the outputs ,captured some relevant parts (regret).
You're seeing the debugger issuing a RaceOnRCWCleanup. The reason may be that you are instantiating but not properly cleaning up COM objects created under the hood by SpeechSynthesizer and/or SpeechRecognitionEngine.
At the same time, a Console application is not automatically 'kept alive'. You need to specifically add code to prevent it from exiting immediately.
You need to do 2 things:
Ensure your application stays alive long enough (for example, by adding a Console.ReadLine() statement in the Main method
Make sure that resources are properly cleaned up. Both SpeechRecognitionEngine and SpeechSynthesizer implement IDisposable, so they should be disposed when no longer needed. To do this properly, implement IDisposable in your tester class:
Example:
public class Tester
{
SpeechSynthesizer ss = new SpeechSynthesizer();
SpeechRecognitionEngine sre = new SpeechRecognitionEngine();
public void Dispose()
{
ss.Dispose();
sre.Dispose();
}
}

Categories

Resources