Sunday, March 9, 2014

Anonymous Method & Speech Translator

This is used to create a method, without any name.
Syntax:
delegate()
{
 //some code
}

PROGRAM OF ANONYMOUS METHOD
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace AnonymousMethodsDemo
{
class ThreadingDemo
  {
   public void Display(string name)
    {
      Thread th1 = new Thread(delegate()
      {
       while (true)
        {
          Console.WriteLine("Welcome to " + name);
          Thread.Sleep(500);
         }
     });
    th1.Start();
   }
 }

  class Program
  {
   static void Main(string[] args)
   {
     string name;
     Console.WriteLine("Enter your name:");
      name = Console.ReadLine();
      ThreadingDemo td = new ThreadingDemo();
      td.Display(name);
      Console.Read();
    }
  }
} 

SPEECH TRANSLATION
  1. This is to translate the text as speech.
  2. This makes us to listen a voice from the speakers that reads some specified text.
  3. Library: System.Speech.Synthesis.SpeechSynthesizer
  4. This class used to speak the required text through the speakers.
Implementation:
  1. Create the object of SpeechSynthesizer class:
  2. SpeechSynthesizer ss = new SpeechSynthesizer();
  3. Set the volume (1 to 100):
  4. ss.Volume = n;
  5. Set the speed of speaking (-10 to +10):
  6. ss.Rate = n;
  7. Change the voice gender and age:
  8. ss.SelectVoiceByHints(VoiceGender.xxxx, VoiceAge.xxxx);
  9. Speak the text:
  10. ss.SpeakAsync(―message‖);

PROGRAM OF SPEECH TRANSLATOR
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Speech.Synthesis;
namespace SpeechTranslationDemo
{
class Program
 {
  static void Main(string[] args)
  {
   Console.WriteLine("Enter text to speak:");
   string TextToSpeak = Console.ReadLine();
   SpeechSynthesizer ss = new SpeechSynthesizer();
   ss.Volume = 100; //1 to 100 
   ss.Rate = -3; // -10 to +10 
   ss.SelectVoiceByHints(VoiceGender.Female, VoiceAge.Child);
   ss.SpeakAsync(TextToSpeak);
   Console.Read();
  }
 }
}