PoshCode Logo PowerShell Code Repository

Out-Voice 2.0 (modification of post by Joel Bennett view diff)
diff | embed code: <script type="text/javascript" src="http://PoshCode.org/embed/111"></script>download | new post

Speaks text using Speech.Net (requires .Net 3.0 or higher) text-to-speech api (if you don’t have .Net 3.x, please just use the old one that uses the COM objects.

Improvement over previous script version:
+ No more COM interop.
+ Can write output to wave files.

  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. ## <Version>2.0</Version>
  13. ## </Script>
  14. # ---------------------------------------------------------------------------
  15.  
  16. param([string]$wavFile, [switch]$wait, [switch]$purge, [switch]$passthru, [string[]]$inputObject)
  17.  
  18. begin
  19. {  
  20.    if ($args -eq '-?') {
  21.       ''
  22.       'Usage: Out-Speech [[-Collection] <array>]'
  23.       ''
  24.       'Parameters:'
  25.       '    -Collection : A collection of items to speak.'
  26.       '    -?          : Display this usage information'
  27.       '  Switches:'
  28.       '    -wavFile    : Path to a wav file to write output'
  29.       '    -wait       : Wait for the machine to read each item (NOT asynchronous).'
  30.       '    -purge      : Purge all other speech requests before making this call.'
  31.       '    -passthru   : Pass on the input as output.'
  32.       ''
  33.       'Examples:'
  34.       '    PS> Out-Speech "Hello World"'
  35.       '    PS> Select-RandomLine quotes.txt | Out-Speech -wait'
  36.       '    PS> Get-Content "Hitchhiker''s Guide To The Galaxy.txt" | Out-Speech -wav "Hitchiker.wav"'
  37.       ''
  38.       exit
  39.    }
  40.    if ($inputObject) {
  41.       Write-Output $inputObject | &($MyInvocation.InvocationName) $wavFile -wait:$wait -purge:$purge -passthru:$passthru
  42.       break;
  43.    } else {
  44.       [Reflection.Assembly]::LoadWithPartialName("System.Speech") | out-null
  45.       $speaker = new System.Speech.Synthesis.SpeechSynthesizer
  46.       if( $purge ) {
  47.          $speaker.SpeakAsyncCancelAll()
  48.       }
  49.       if( ![string]::IsNullOrEmpty( $wavFile ) ){
  50.          # this is just for your convenience, because you should wait before trying to use the file.
  51.          if(!$wait.IsPresent){$wait =$true}
  52.          $speaker.SetOutputToWaveFile( $(if( Split-Path $wavFile ) { $wavFile } else { "$pwd\$wavFile" }) )
  53.       }
  54.    }
  55. }
  56.  
  57. process
  58. {
  59.    if ($_)
  60.    {
  61.       if($wait) {
  62.          $speaker.Speak( ($_ | out-string) )
  63.       } else {
  64.          $speaker.SpeakAsync( ($_ | out-string) )
  65.       }
  66.       if($passthru) { $_ }
  67.    }
  68. }
  69.  
  70. end {
  71.    if( ![string]::IsNullOrEmpty( $wavFile ) ){
  72.       $speaker.SetOutputToDefaultAudioDevice()
  73.    }
  74.    $speaker.Dispose()
  75. }

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