PoshCode Logo PowerShell Code Repository

Out-Voice 1.2 by Joel Bennett 4 years ago
View followups from Joel Bennett | embed code: <script type="text/javascript" src="http://PoshCode.org/embed/109"></script>download | new post

Speaks text using the SAPI text-to-speech api
new version has a passthru parameter …

  1. # ---------------------------------------------------------------------------
  2. ## <Script>
  3. ## <Author>
  4. ## Joel "Jaykul" Bennett
  5. ## </Author>
  6. ## <Description>
  7. ## Outputs text as spoken words
  8. ## </Description>
  9. ## <Usage>
  10. ## Out-Speech "Hello World"
  11. ## </Usage>
  12. ## </Script>
  13. # ---------------------------------------------------------------------------
  14.  
  15. param([array]$Collection, [switch]$wait, [switch]$purge, [switch]$readfiles, [switch]$readxml, [switch]$notxml, [switch]$passthru)
  16.  
  17. begin
  18. {  
  19.   if ($args -eq '-?') {
  20. ''
  21. 'Usage: Out-Speech [[-Collection] <array>]'
  22. ''
  23. 'Parameters:'
  24. '    -Collection : A collection of items to speak.'
  25. '    -?          : Display this usage information'
  26. '  Switches:'
  27. '    -wait       : Wait for the machine to read each item (NOT asynchronous).'
  28. '    -purge      : Purge all other speech requests before making this call.'
  29. '    -readfiles  : Read the contents of the text files indicated.'
  30. '    -readxml    : Treat input as speech XML markup.'
  31. '    -notxml     : Do NOT parse XML (if text starts with "<" but is not XML).'
  32. '    -passthru   : Pass on the input as output.'
  33. ''
  34. 'Examples:'
  35. '    PS> Out-Speech "Hello World"'
  36. '    PS> Select-RandomLine quotes.txt | Out-Speech -wait'
  37. '    PS> Out-Speech -readfiles "Hitchhiker''s Guide To The Galaxy.txt"'
  38. ''
  39.       exit
  40.   }
  41.    
  42.   # To override this default, use the other flag values given below.
  43.   $SPF_DEFAULT = 0          # Specifies that the default settings should be used.  
  44.     ## The defaults are:
  45.     #~ * Speak the given text string synchronously
  46.     #~ * Not purge pending speak requests
  47.     #~ * Parse the text as XML only if the first character is a left-angle-bracket (<)
  48.     #~ * Not persist global XML state changes across speak calls
  49.     #~ * Not expand punctuation characters into words.
  50.   $SPF_ASYNC = 1            # Specifies that the Speak call should be asynchronous.
  51.   $SPF_PURGEBEFORESPEAK = 2 # Purges all pending speak requests prior to this speak call.
  52.   $SPF_IS_FILENAME = 4      # The string passed is a file name, and the file text should be spoken.
  53.   $SPF_IS_XML = 8           # The input text will be parsed for XML markup.
  54.   $SPF_IS_NOT_XML= 16       # The input text will not be parsed for XML markup.
  55.  
  56.  
  57.   $SPF = $SPF_DEFAULT
  58.   if(!$wait){ $SPF += $SPF_ASYNC }
  59.   if($purge){ $SPF += $SPF_PURGEBEFORESPEAK }
  60.   if($readfiles){ $SPF += $SPF_IS_FILENAME }
  61.   if($readxml){ $SPF += $SPF_IS_XML }
  62.   if($notxml){ $SPF += $SPF_IS_NOT_XML }
  63.  
  64.   $Voice = new-object -com SAPI.SpVoice
  65.  
  66.   if ($collection.count -gt 0) {
  67.     foreach( $item in $collection ) {
  68.       $exit = $Voice.Speak( ($item | out-string), $SPF )
  69.     }
  70.   }
  71. }
  72.  
  73. process
  74. {
  75.   if ($_)
  76.   {
  77.     $exit = $Voice.Speak( ($_ | out-string), $SPF )
  78.     if($passthru) { $_ }
  79.   }
  80. }

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me