Source Viewer:  PronounceWord.cs

Font Size:
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("<EMBED SRC=\"([^\"]+)\""); string result= null; if (re0.IsMatch(pronunciationPageMarkup)) { Match m = re0.Match(pronunciationPageMarkup); result = m.Groups[1].ToString(); } return result; } private static string GetPronunciationPageUri(string dictionaryPageMarkup) { var re0 = new Regex("popWin\\('([^']+)'\\)"); string result= null; if (re0.IsMatch(dictionaryPageMarkup)) { Match m = re0.Match(dictionaryPageMarkup); result = String.Format("http://www.merriam-webster.com/{0}", m.Groups[1].ToString()); } return result; } private static string MwDictionaryUriTemplate= "http://www.merriam-webster.com/dictionary/"; private static string GetMwDictionaryPage(string word) { string uriToGet = MwDictionaryUriTemplate + word; return uriToGet; } private static string GetPageMarkup(string uri) { string pageData = null; using (WebClient client = new WebClient()) { pageData = client.DownloadString(uri); } return pageData; } } public class WavPlayer { // from http://www.codeguru.com/Csharp/Csharp/cs_graphics/sound/article.php/c6143/ ? // and http://www.eggheadcafe.com/articles/20030302.asp // For some reason, the 2nd param, hModule, must be of type IntPtr if I am // playing a byte[], and it must be of type long if I am playing a wav // file specified via a string. ?? Ah, the mysteries of P/Invoke. //[DllImport("WinMM.dll", EntryPoint="PlaySound")] //public static extern bool PlaySoundBytes(byte[] wavData, long hModule, UInt32 flags); //[DllImport("Winmm.dll")] [DllImport("WinMM.dll", EntryPoint="PlaySound")] public static extern bool PlaySoundBytes(byte[] data, IntPtr hMod, UInt32 dwFlags); //[System.Runtime.InteropServices.DllImport("WinMM.dll", EntryPoint="PlaySoundW" )] [System.Runtime.InteropServices.DllImport("WinMM.dll", EntryPoint="PlaySound")] public static extern bool PlaySoundFile( string filename, long hModule, UInt32 flags ); // this will not work! [System.Runtime.InteropServices.DllImport("WinMM.dll", EntryPoint="PlaySound")] public static extern bool PlaySoundFile( string filename, IntPtr hModule, UInt32 flags ); // flag values for SoundFlags argument on PlaySound public static uint SND_SYNC = 0x0000; // play synchronously (default) public static uint SND_ASYNC = 0x0001; // play asynchronously public static uint SND_NODEFAULT = 0x0002; // silence (!default) if sound not found public static uint SND_MEMORY = 0x0004; // pszSound points to a memory file public static uint SND_LOOP = 0x0008; // loop the sound until next sndPlaySound public static uint SND_NOSTOP = 0x0010; // don't stop any currently playing sound public static uint SND_NOWAIT = 0x00002000; // don't wait if the driver is busy public static uint SND_ALIAS = 0x00010000; // name is a Registry alias public static uint SND_ALIAS_ID = 0x00110000; // alias is a predefined ID public static uint SND_FILENAME = 0x00020000; // name is file name public static uint SND_RESOURCE = 0x00040004; // name is resource name or atom public static uint SND_PURGE = 0x0040; // purge non-static events for task public static uint SND_APPLICATION = 0x0080; // look for application-specific association public static void Play(byte[] wavData, uint flags) { PlaySoundBytes(wavData, IntPtr.Zero, flags | SND_MEMORY); } public static void StopPlay() { PlaySoundBytes(null, IntPtr.Zero, SND_PURGE); } public static void PlayWavResource(string wav) { // get the namespace string strNameSpace= System.Reflection.Assembly.GetExecutingAssembly().GetName().Name.ToString(); // get the resource into a stream System.IO.Stream str = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream( strNameSpace +"."+ wav ); if ( str == null ) return; // bring stream into a byte array byte[] bStr = new Byte[str.Length]; str.Read(bStr, 0, (int)str.Length); // play the resource PlaySoundBytes(bStr, IntPtr.Zero, SND_ASYNC | SND_MEMORY); } } public class PronounceWord { public PronounceWord () {} public static void Usage() { Console.WriteLine("PronounceWord: pronounces a word by looking up the M-W.com wav file.\n"); Console.WriteLine("Usage:\n PronounceWord <word> [<word>...]"); } 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