Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

COM Objects, C#, and the Microsoft Speech API


00:

Useful C# Tricks

C# includes a string method that I've missed in C++ for ages, the Replace method, which lets your program do simple text replacements in single lines of code. For example, for your computer to respond by speaking the time of day, include "%time%" in the response string ("The time is now %time%"). Now add a line:

sResponse = sResponse.Replace(

"%time%",DateTime.Now.ToString("h m"));

which gets the time of day from the system, formats it as a string, and replaces it in the sReponse string. The example program includes similar functionality for the user's name, date, day of week, and so on.

It is useful to be able to direct a browser with voice commands. Windows includes a ShellExecute function that does the trick by opening any file with its appropriately registered application. So if you give it a file name with an http:// prefix, it will open a browser to open the file. Since ShellExecute is not part of the C# library, you will need to declare it explicitly like this:

[DllImport("Shell32.dll")]

public static extern int ShellExecuteA(

int hwnd,

string lpOperation,

string lpFile,

string lpParameters,

string lpDirectory,

int nShowCmd);

before you call it like this:

int ProcID = ShellExecuteA(

0,"open",sFileName,null,"",1);

A returned ProcID of less than 32 indicates an error. You can also save the ProcID and pass it to other system functions to close the program (for example).

Scraping Data From Web Sites

One of the common uses for speech-enabled programs is information presentation — you ask your computer a question and it speaks the answer. Many of the answers are on web pages in relatively predictable locations, and C# provides all the tools to retrieve web-page information and present it verbally. Because web pages are copyrighted, I would suggest getting permission from the web site owner before distributing a program that performs this type of function.

WebRequest myRequest =

WebRequest.Create(sURL);

WebResponse myResponse =

myRequest.GetResponse();

Stream myStream =

myResponse.GetResponseStream();

string sHtml="";

System.Text.Encoding encode =

System.Text.Encoding.GetEncoding("utf-8");

StreamReader readStream =

new StreamReader( myStream, encode );

Char[] read = new Char[256];

int count =

readStream.Read( read, 0, 256 );

while (count > 0)

{

string str =

new string(read, 0, count);

sHtml += str;

count =

readStream.Read(read, 0, 256);

}

myStream.Close();

With this code, you end up with the string sHttpResponse containing the HTML for the page at sURL. Now you can parse the string to find the information you'd like to speak.


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.