Trying to make a very simple speech recognition windows form project - c#

I followed a very basic tutorial (forgot the link) and it all seems straightforward, but I don't seem to be getting the output I want. Here is my main form class:
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.Speech.Recognition;
namespace OralNotes.Alpha
{
public partial class MainForm : Form
{
private SpeechRecognitionEngine recognitionEngine;
public MainForm()
{
InitializeComponent();
recognitionEngine = new SpeechRecognitionEngine();
recognitionEngine.SetInputToDefaultAudioDevice();
recognitionEngine.SpeechRecognized += (s, args) =>
{
foreach (RecognizedWordUnit word in args.Result.Words)
{
if (word.Confidence > 0.8f)
txtNotes.Text += word.Text + " ";
}
txtNotes.Text += Environment.NewLine;
};
recognitionEngine.LoadGrammar(new DictationGrammar());
}
private void btnStart_Click(object sender, EventArgs e)
{
recognitionEngine.RecognizeAsync();
}
private void btnStop_Click(object sender, EventArgs e)
{
recognitionEngine.RecognizeAsyncStop();
}
}
}
And you probably don't need this, but here it is anyway:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace OralNotes.Alpha
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm());
}
}
}
I click start and talk, and nothing happens.
UPDATE*
ok so this is working, if I adjust this line
if (word.Confidence > 0.8f)
to a lower number I get a word or two in. Not transcribing sentences or anything, is there a way to make this have better recognition?

It seems that the engine that you are using returns a confidence value on every word it hears.
I suspect that the confidence values range from 0-1. Which means that if you lower the value from 0.8 you are making the engine more tolerant to words. Maybe it is the quality of your mic?

Related

Enable Microphone using NAudio c#

I have this code that uses NAudio to look for all microphones connected on laptop, andwhen I choose one it shows a meter of the sound in a progressbar.
when I gather all device, I collect them in a dropdwon list, my issue is that when I choose the one microphone from the list it will not be activated and display the sound on a meter unless I start WINDOWS SOUND RECORDER which seems to activate the mic.
how to enable or activate the microphone from the code without starting WINDOWS SOUND RECORDER?
here is the code:
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 NAudio;
using NAudio.CoreAudioApi;
using System.Threading;
using NAudio.Wave;
using NAudio.Mixer;
namespace NaudioVisualizer
{
public partial class Form1 : Form
{
public int a = 1;
public Form1()
{
InitializeComponent();
MMDeviceEnumerator de = new MMDeviceEnumerator();
var device = de.EnumerateAudioEndPoints(DataFlow.All, DeviceState.Active);
comboBox1.Items.AddRange(device.ToArray());
}
private void timer1_Tick(object sender, EventArgs e)
{
if (comboBox1.SelectedItem != null)
{
var device = (MMDevice)comboBox1.SelectedItem;
progressBar1.Value = (int)Math.Round(device.AudioMeterInformation.MasterPeakValue * 100);
}
}
}
}
I had the same problem try to use the following code :
var waveIn = new WaveIn();
waveIn.StartRecording();

C# Textbox properties do not update

I have a simple form with a text box, a command button and a couple of timers. The only purpose of the form is to advise the user what is happening. The program executes all the code as required EXCEPT for the textbox changes. I know the code to implement the textbox changes is executed because the form and the command button properties change as required.
I have added this.refresh and this.textbox1.refresh to no avail.
I am new to C# and most of the time I do not have Visual Studios available, so your assistance would be most appreciated. I have read other posts on this topic and probably the answer has already been given, but I have not understood the solution.
The simplified code is given below:
//PROGRAM.CS
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Windows.Forms;
using WindowsFormsApplication1;
namespace PostBinaryFile
{
static class Program
{
/// The main entry point for the application.
[STAThread]
static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1(args));
}
}
}
//FORM1.CS
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.IO;
using System.Web;
using System.Net;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
string sUrl;
string sFileName;
string sCorNo;
public Form1(string[] args)
{
sUrl = args[0];
sFileName = args[1];
sCorNo = args[2];
InitializeComponent();
timer1.Enabled = true;
timer1.Start();
timer2.Enabled = true;
timer2.Start();
}
public void PostCode()
{
InitializeComponent();
string sToken;
string sPath;
const string boundary = "----WebKitFormBoundaryePkpFF7tjBAqx29L";
try
{
//Do all general code work here.
//Alter form to show successful post to web
this.button1.Visible = true;
this.button1.Enabled = true;
this.BackColor = System.Drawing.Color.FromArgb(189,194,241);
this.textBox1.Text = sCorNo + " Outlook file saved to FuseDMS."; // this code is executed but is not reflected on the Form
this.textBox1.BackColor= System.Drawing.Color.FromArgb(189,194,241); // this code is executed but is not reflected on the Form
}
private void timer1_Tick(object sender, EventArgs e)
{
timer1.Stop();
timer1.Enabled = false;
PostCode();
}
private void timer2_Tick(object sender, EventArgs e)
{
timer2.Stop();
timer2.Enabled = false;
this.textBox1.Text = "Saving Message " + sCorNo + ".";
}
private void button1_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
}
As #DavidG pointed out, you should not call InitializeComponent() periodically or even more then once, do it as the first thing in the constructor.
This is because any controls and properties that you add/set from the designer are created and initialized in this method.
Another thing to point out is Timer.Enabled = true and Timer.Start() effectively do the same thing
From: System.Windows.Forms.Timer.Enabled
Calling the Start method is the same as setting Enabled to true. Likewise, calling the Stop method is the same as setting Enabled to false.
Both timers namely timer1 and timer2 fire asynchronously and run on separate threads which are completely independent of each other. Even if timer2's tick event would be setting/refreshing the text appropriately through below code:
this.textBox1.Text = "Saving Message " + sCorNo + ".";
you can never say with guarantee that it will happen only after timer1's tick event has completed the execution of its callback method. In all likelyhood your above code is setting the text property of a dangling text box instance as your InitializeComponent function (being called from timer1's tick event) must be reinstantiating a new instance of all the form controls.
Your call to InitializeComponent function in PostCode method which gets called from tick event of timer1's tick event isn't right as it resets all the instances of form controls to new ones. It should be called only once in the constructor of the form. Just remove that piece of code and you should be good. Your PostCode function should actually look like this after you get rid of that piece of code:
public void PostCode()
{
string sToken;
string sPath;
const string boundary = "----WebKitFormBoundaryePkpFF7tjBAqx29L";
try
{
//Do all general code work here.
//Alter form to show successful post to web
this.button1.Visible = true;
this.button1.Enabled = true;
this.BackColor = System.Drawing.Color.FromArgb(189,194,241);
this.textBox1.Text = sCorNo + " Outlook file saved to FuseDMS."; // this code is executed but is not reflected on the Form
this.textBox1.BackColor= System.Drawing.Color.FromArgb(189,194,241); // this code is executed but is not reflected on the Form
}

Closing all Windows in a C# WPF application

I'm creating a little WPF app in VS2013Express and I've come across a little problem.
You see, there are three windows, MainWindow, LatAndLongDialog, TimeCitiesDialog.
`LatAndLongDialog` and `TimeCitiesDialog` are opened from MainWindow (with the click of a button).
I want all the other windows to close when the `Closed()` event is called on `MainWindow`.
The code on MainWindow.xaml.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace GlobalTime_ELITE_for_WPF
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
UserDescText.Content =
"Select a TimeCity or enter the latitude and longitude in \n" +
"to view the World Time there. Or, select another one of the\n" +
"options below to do that. Go to Help by clicking on the link\n" +
"on the upper-right corner of the window to view everything you\n" +
"can do.";
this.Closed += CloseOff;
}
private void OpenTimeCitiesDialog(Object Sender, EventArgs E)
{
TimeCitiesDialog ObjectReference = new TimeCitiesDialog();
ObjectReference.Show();
}
private void OpenLatAndLongDialog(Object Sender, EventArgs E)
{
LatAndLongDialog ObjectReference = new LatAndLongDialog();
ObjectReference.Show();
}
private void CloseOff(Object Sender, EventArgs E)
{
this.Close();
TimeCitiesDialog tcdor = new TimeCitiesDialog();
LatAndLongDialog laldor = new LatAndLongDialog();
}
}
}
How can I close them all? Please help!
The proper way to shutdown a WPF app is to use Application.Current.Shutdown() . This will close all open Windows, raise some events so that cleanup code can be run, and it can't be canceled.
Environment.Exit() terminates the application immediately even if other threads are executing.
You should also consider setting the Owner on non-main Windows. The behavior will likely be more like what you would expect in regards to Z-order, minimizing, and maximizing. As an added bonus, the owned windows will automatically close when the owner Window closes.
private void CloseAllWindows()
{
for (int intCounter = App.Current.Windows.Count - 1; intCounter >= 0; intCounter--)
{
App.Current.Windows[intCounter].Close();
}
}
Close all opened current windows.
Use this instead this.Close()
Environment.Exit(0);
this will force everything to close
If you keep track of the Dialogs outside of the scope of the methods you use to open them, you can call which ever methods on those Dialogs you wish from anywhere within the Class. Here I have them as Class variables and they are instantiated there but not shown until you press the buttons. You can also create "Close" buttons for those specific windows and call their .Close() methods when ever you wish. That will allow you to open and close them at will. You can also call their .Close() methods when the main form closes.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace GlobalTime_ELITE_for_WPF
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
TimeCitiesDialog tcDialog = new TimeCitiesDialog();
LatAndLongDialog lalDialog = new LatAndLongDialog();
public MainWindow()
{
InitializeComponent();
UserDescText.Content = "Select a TimeCity or enter the latitude and longitude in \n" +
"to view the World Time there. Or, select another one of the\n" +
"options below to do that. Go to Help by clicking on the link\n" +
"on the upper-right corner of the window to view everything you\n" +
"can do.";
this.Closed += CloseOff;
}
private void OpenTimeCitiesDialog(Object Sender, EventArgs E)
{
tcDialog.Show();
}
private void OpenLatAndLongDialog(Object Sender, EventArgs E)
{
lalDialog.Show();
}
private void CloseOff(Object Sender, EventArgs E)
{
// Close the dialogs first, then allow this method
// to end which will finish the this.Close() process.
tcDialog.Close();
lalDialog.Close();
}
}
}

Form freezing when loaded from a DLL that is imported at runtime in a console application

Continuing from my previous question: Trying to show a Windows Form using a DLL that is imported at the runtime in a console application
The form I am showing using the code given in the answer to my previous question by #Ksv3n is freezing (showing a wait cursor over it). For code please check out above link.
Try to use a BackgroundWorker, like this:
(I dind't compiled the code, this is an example for the approach)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;
namespace DLLTest
{
class Program
{
static void Main(string[] args)
{
BackgroundWorker m_oWorker;
m_oWorker = new BackgroundWorker();
m_oWorker.DoWork += new DoWorkEventHandler(m_oWorker_DoWork);
m_oWorker.RunWorkerAsync();
Console.ReadLine();
}
static void m_oWorker_DoWork(object sender, DoWorkEventArgs e)
{
string DLLPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + "\\TestLib.dll";
var DLL = Assembly.LoadFile(DLLPath);
foreach (Type type in DLL.GetExportedTypes())
{
dynamic c = Activator.CreateInstance(type);
c.test();
}
}
}
}
Online Resource:Background Worker for Beginners

C# geckoFX (AutoJSContext not Wοrking)

I'm new here hoping for some help with geckoFX in C#
So I just downloaded the geckoFX and did the following.
Downloaded: geckofx.dll
downloaded: XULRunner
I added the geckofx browser successfully and works fine but when I try to run this code to add JavaScript to the page I get an error.
The error I'm getting is: skybound.geckoFX.AutoJSContext does not contain a definition for evaluate script and jscontext.
Also I don't know if this helps but AutoJSContext and EvaluateScript are not hightlighting.
Here is my code
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using Skybound.Gecko;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Skybound.Gecko.Xpcom.Initialize(#"C:\Program Files\xulrunner");
}
private void geckoWebBrowser1_DocumentCompleted(object sender, EventArgs e)
{
string outString = "";
using (AutoJSContext java = new AutoJSContext(geckoWebBrowser1.Window.JSContext))
{
java.EvaluateScript(#"window.alert('alert')", out outString);
}
}
}
You should call EvaluateScript like so:
java.EvaluateScript(#"window.alert('alert')", (nsISupports)geckoWebBrowser1.Window.DomWindow, out result);

Categories

Resources