Source Viewer: PronounceWord.cs
Font Size:
1
2
3
4
5
6
7
MorphArray.cs
Morpho.cs
MyWebClient.cs
PronounceWord.cs
syslog.cs
SyslogTraceListener.cs
MQ
..
// PronounceWord.cs // ------------------------------------------------------------------ // // Lookup the .wav file for a word from the m-w.com website, then Use the // PlaySound Windows API from .NET via P/Invoke to play the wav file. // // Author: Dinoch // built on host: DINOCH-2 // Created Thu Apr 23 06:48:42 2009 // // last saved: // Time-stamp: <2009-June-10 21:15:21> // ------------------------------------------------------------------ // // Copyright (c) 2009 by Dino Chiesa // All rights reserved! // // ------------------------------------------------------------------ using System; using System.Net; using System.Text.RegularExpressions; using System.Runtime.InteropServices; namespace Cheeso.Sounds { public class WordPronouncer { public static void Pronounce(string word) { // can implement a MRU cache here byte[] pronunciation = GetPronunciationWav(word); Console.WriteLine("playing wav data..."); WavPlayer.Play(pronunciation, WavPlayer.SND_SYNC); } public static byte[] GetPronunciationWav(string word) { // These are the steps: // a. get the M-W.com dictionary page for the word. // b. get the pronunciation page for the word embedded in that page // c. get the wav URI embedded in that page. // d. get the bytestream for that URI Console.WriteLine("looking up '{0}'...", word); string dictionaryPage = GetMwDictionaryPage(word); Console.WriteLine("dictionary page: {0}", dictionaryPage); string dictionaryPageMarkup = GetPageMarkup(dictionaryPage); Console.WriteLine("got dictionary page markup, {0} chars...", dictionaryPageMarkup.Length); Console.WriteLine("getting pronunciation uri..."); string pronunciationPageUri= GetPronunciationPageUri(dictionaryPageMarkup); Console.WriteLine("got uri: '{0}'...", pronunciationPageUri); Console.WriteLine("getting page markup..."); string pronunciationPageMarkup = GetPageMarkup(pronunciationPageUri); Console.WriteLine("got pronunciation page markup, {0} chars...", pronunciationPageMarkup.Length); Console.WriteLine("getting wav uri..."); string wavUri = GetWavUri(pronunciationPageMarkup); Console.WriteLine("got wav uri: '{0}'...", wavUri); Console.WriteLine("getting wav data..."); byte[] wavData = GetBinaryData(wavUri); Console.WriteLine("got wav data, {0} bytes...", wavData.Length); return wavData; } private static byte[] GetBinaryData(string uri) { byte[] bytes= null; using (WebClient client = new WebClient ()) { bytes = client.DownloadData(uri); } return bytes; } private static string GetWavUri(string pronunciationPageMarkup) { var re0 = new Regex("
[
...]"); } public static void Main(string[] args) { try { for (int i=0; i < args.Length; i++) { switch(args[i]) { case "-?": throw new ArgumentException(args[i]); default: Console.Write("pronouncing {0}...", args[i]); WordPronouncer.Pronounce(args[i]); break; } } } catch (System.Exception exc1) { Console.WriteLine("Exception: {0}", exc1.ToString()); Usage(); } } } }
The srcview page has been enjoyed 291584 times since 18 September 2003