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();
}
}
Related
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();
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.)
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
I would like to change the gender and age of the voice of System.Speech in c#. For example, a girl of 10 years but can not find any simple example to help me adjust the parameters.
First, check which voices you have installed by enumerating the GetInstalledVoices method of the SpeechSynthesizer class, and then use SelectVoiceByHints to select one of them:
using (SpeechSynthesizer synthesizer = new SpeechSynthesizer())
{
// show installed voices
foreach (var v in synthesizer.GetInstalledVoices().Select(v => v.VoiceInfo))
{
Console.WriteLine("Name:{0}, Gender:{1}, Age:{2}",
v.Description, v.Gender, v.Age);
}
// select male senior (if it exists)
synthesizer.SelectVoiceByHints(VoiceGender.Male, VoiceAge.Senior);
// select audio device
synthesizer.SetOutputToDefaultAudioDevice();
// build and speak a prompt
PromptBuilder builder = new PromptBuilder();
builder.AppendText("Found this on Stack Overflow.");
synthesizer.Speak(builder);
}
http://msdn.microsoft.com/en-us/library/system.speech.synthesis.voiceage.aspx
http://msdn.microsoft.com/en-us/library/system.speech.synthesis.voicegender.aspx
Did you take a look at this ?
first you need to intialise the reference speech using the add reference.
then create an event handler for the speak started then you can edit the paramemters inside that handler.
in the handler is where you can change the voice and age using the
synthesizer.SelectVoiceByHints(VoiceGender.Male , VoiceAge.Adult);
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Speech.Synthesis; // first import this package
namespace textToSpeech
{
public partial class home : Form
{
public string s = "pran"; // storing string (pran) to s
private void home_Load(object sender, EventArgs e)
{
speech(s); // calling the function with a string argument
}
private void speech(string args) // defining the function which will accept a string parameter
{
SpeechSynthesizer synthesizer = new SpeechSynthesizer();
synthesizer.SelectVoiceByHints(VoiceGender.Male , VoiceAge.Adult); // to change VoiceGender and VoiceAge check out those links below
synthesizer.Volume = 100; // (0 - 100)
synthesizer.Rate = 0; // (-10 - 10)
// Synchronous
synthesizer.Speak("Now I'm speaking, no other function'll work");
// Asynchronous
synthesizer.SpeakAsync("Welcome" + args); // here args = pran
}
}
}
It'll be better choice to use "SpeakAsync" because when "Speak" function is executing/running none of other function will work until it finishes it's work (personally recommended)
Change VoiceGender
Change VoiceAge
These age and gender is actually of no use. If you have many voices installed in your windows, then you may call specific voices by these parameters. Otherwise, its simply fake!
I created a Process that is targeted to the Java.exe file. The process should run a Java.jar server file, and continue to run giving output feedback until the server crashed or I forcefully close it down. Everything works fine.. the server is running.. and when I set UseShellExecute to True I can see the black CMD window returning all the output. But when I set it to false and redirect the output... the Window is completely blank ( the server is still running though ) and the OutputDataReceived event doesn't fire at all ( I think I got it to once but that was when I closed the window it seemed the args was empty ) and as far as I can see StandardOutput.ReadToEnd() isnt returning anything either. Why is this Process not returning any Output feedback?? Here is my code:
gameServerProcess = new Process();
gameServerProcess.StartInfo.UseShellExecute = false;
gameServerProcess.StartInfo.RedirectStandardOutput = true;
gameServerProcess.StartInfo.RedirectStandardInput = true;
gameServerProcess.EnableRaisingEvents = true;
gameServerProcess.Exited += new EventHandler(gameServer_WindowExit);
window = new ServerWindow();
gameServerProcess.OutputDataReceived += new DataReceivedEventHandler(window.server_recievedOutputStream);
window.Show();
gameServerProcess.StartInfo.FileName = #"D:\Program Files\Java\jdk1.6.0_12\bin\java.exe";
gameServerProcess.StartInfo.WorkingDirectory = #"D:\Users\Zack\Desktop\ServerFiles\gameserver";
gameServerProcess.StartInfo.Arguments = #"-Xmx1024m -cp ./../libs/*;l2jserver.jar net.sf.l2j.gameserver.GameServer";
gameServerProcess.Start();
gameServerProcess.BeginOutputReadLine();
And my 'window' form class code which should receive the DataReceivedEventArgs with the output data:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
namespace MWRemoteServer
{
public partial class ServerWindow : Form
{
private delegate void WriteOutputDelegate(string output);
private WriteOutputDelegate writeOutput;
public ServerWindow()
{
InitializeComponent();
logBox.BackColor = Color.White;
logBox.ForeColor = Color.Black;
writeOutput = new WriteOutputDelegate(write);
}
public void server_recievedOutputStream(object sender, DataReceivedEventArgs args)
{
MessageBox.Show("Called window output!");
if (args.Data != null)
{
BeginInvoke(writeOutput, new object[] { args.Data.ToString() });
}
}
private void write(string output)
{
logBox.AppendText(output + Environment.NewLine);
}
}
}
Again the process works completely fine and with the UseShellExecute set to true I can see it is providing all information I need to grab. But when I set that to false its blank and I cant seem to grab any output data!
Thank you so much in advance... I've been at this for hours and hours...
Are you sure it writes to stdout? Have you tried checking stderr? (RedirectStandardError/StandardError/ErrorDataReceived/BeginErrorReadLine).
If it writes directly to the display buffer (instead of stderr/stdout) then it might not be possible without a lot of work.