0

I am trying to add voice to my calculator project. I was able to add the voice. But i am facing some problem. When i press 13, it first says "One" then says "Thirteen". And one more thing i am unable to add voice to equal(=) sign.

private void NumberButtons(object sender, EventArgs e)
{
    Button b = sender as Button;
    if ((b == null) || (b.Text == "0" && buffer.Length == 0))
        return;
    buffer += b.Text;
    txtOutput.Text = buffer;
    if (txtOutput.Text != "")
    {
        SpVoice voice = new SpVoice();
        voice.Volume = 100;
        voice.Speak(txtOutput.Text, SpeechVoiceSpeakFlags.SVSFlagsAsync);
    }
}

here's the method.

private void Equal(object sender, EventArgs e) 
{ 
    if (buffer.Length != 0) 
        operand[1] = Double.Parse(buffer); 
    switch (op) 
    { 
        case '+': result = operand[0] + operand[1]; break; 
        case '-': result = operand[0] - operand[1]; break; 
        case '*': result = operand[0] * operand[1]; break;
        case '/': result = operand[0] / operand[1]; break; 
    } 
    txtOutput.Text = result.ToString();
    if (txtOutput.Text != "")
    {
        SpVoice voice = new SpVoice();
        voice.Volume = 100;
        // voice.Speak("The Result Is"+ SpeechVoiceSpeakFlags.SVSFlagsAsync);
        voice.Speak(txtOutput.Text, SpeechVoiceSpeakFlags.SVSFlagsAsync);
    }
    step = 1; 
    buffer = ""; 
}

here's the equal method.

CDspace
  • 2,639
  • 18
  • 30
  • 36
Atib
  • 39
  • 3
  • 1
    I don't understand your problem with "=" but you can solve the one about digits using a delay. Don't "speak" after each typing. Start a timer and if nothing changed after 500 msec (or 1 sec) then speak. Reset timer after each character. Voice will start only when user pauses (of course you can't be sure when he finished typing but pause will handle most common cases). – Adriano Repetti Apr 11 '14 at 20:32

3 Answers3

1

You have to convert the numbers to words, otherwise it'll say 13 as one three. Here's a post that shows you how to do that.

Also, for equal you need to add it manually as well to the text you want to be spoken.

Basically you need to build the text like this, where operationText will be "plus" or "minus", etc.:

  var textToBeSpoken =  input1ToWord + operationText + input2ToWord + "equals" + resultToWord
Community
  • 1
  • 1
AD.Net
  • 13,352
  • 2
  • 28
  • 47
  • 1
    He is saying you need to get a package such as [Humanizer](http://humanizr.net/) that will convert numbers and symbols to English words. – Icemanind Apr 11 '14 at 20:48
1

I had a quick look at http://msdn.microsoft.com/en-us/library/ms723609(v=vs.85).aspx and I could not see anything about speaking numbers.

As I read in the comments you could adapt your code to speak the numbers in words.

For a quick code change

Install Humanizer nuget in Visual studio http://www.nuget.org/packages/humanizer

Then change your code to

// using Humanizer;    
voice.Speak(result.ToWords(), SpeechVoiceSpeakFlags.SVSFlagsAsync);

ToWords() is the extension method which translate your number to the word equivalent, e.g. 13 to "Thirteen"

Kunukn
  • 2,136
  • 16
  • 16
0

I could just solve it using Tasks and Cancelation tokens, code as follows:

I Added a List to the class:

List<CancellationTokenSource> toCancel = new List<CancellationTokenSource>();

And the NumbersButtons event:

private void NumberButtons(object sender, EventArgs e)
{
    Button b = sender as Button;
    if ((b == null) || (b.Text == "0" && buffer.Length == 0))
        return;
    buffer += b.Text;
    txtOutput.Text = buffer;

    if (txtOutput.Text != "")
    {
        if (toCancel.Count > 0)
        {
            foreach (var tc in toCancel)
            {
                tc.Cancel();
            }
        }

        CancellationTokenSource ts = new CancellationTokenSource();
        CancellationToken ct;

        ct = ts.Token;

        toCancel.Add(ts);

        Task.Factory.StartNew(() =>
        {
            Thread.Sleep(1000);

            if (ct.IsCancellationRequested == false)
            {
                SpVoice voice = new SpVoice();
                voice.Volume = 100;
                voice.Speak(txtOutput.Text, SpeechVoiceSpeakFlags.SVSFlagsAsync);
            }
        }, ct);
    }
}
RMalke
  • 4,048
  • 29
  • 42