1 #!powershell
2 
3 # Copyright: (c) 2016, Jon Hawkesworth (@jhawkesworth) <jhawkesworth@protonmail.com>
4 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
5 
6 #AnsibleRequires -CSharpUtil Ansible.Basic
7 
8 $spec = @{
9    options = @{
10       msg = @{ type = "str"  }
11       msg_file = @{ type = "path"  }
12       start_sound_path = @{ type = "path"  }
13       end_sound_path = @{ type = "path"  }
14       voice = @{ type = "str"  }
15       speech_speed = @{ type = "int"; default = 0  }
16    }
17    mutually_exclusive = @(
18      ,@('msg', 'msg_file')
19    )
20    required_one_of = @(
21      ,@('msg', 'msg_file', 'start_sound_path', 'end_sound_path')
22    )
23    supports_check_mode = $true
24 }
25 
26 $module = [Ansible.Basic.AnsibleModule]::Create($args, $spec)
27 
28 
29 $msg = $module.Params.msg
30 $msg_file = $module.Params.msg_file
31 $start_sound_path = $module.Params.start_sound_path
32 $end_sound_path = $module.Params.end_sound_path
33 $voice = $module.Params.voice
34 $speech_speed = $module.Params.speech_speed
35 
36 if ($speech_speed -lt -10 -or $speech_speed -gt 10) {
37    $module.FailJson("speech_speed needs to be an integer in the range -10 to 10.  The value $speech_speed is outside this range.")
38 }
39 
40 $words = $null
41 
42 if ($msg_file) {
43    if (-not (Test-Path -LiteralPath $msg_file)) {
44       $module.FailJson("Message file $msg_file could not be found or opened.  Ensure you have specified the full path to the file, and the ansible windows user has permission to read the file.")
45    }
46    $words = Get-Content -LiteralPath $msg_file | Out-String
47 }
48 
49 if ($msg) {
50    $words = $msg
51 }
52 
53 if ($start_sound_path) {
54    if (-not (Test-Path -LiteralPath $start_sound_path)) {
55       $module.FailJson("Start sound file $start_sound_path could not be found or opened.  Ensure you have specified the full path to the file, and the ansible windows user has permission to read the file.")
56    }
57    if (-not $module.CheckMode) {
58       (new-object Media.SoundPlayer $start_sound_path).playSync()
59    }
60 }
61 
62 if ($words) {
63    Add-Type -AssemblyName System.speech
64    $tts = New-Object System.Speech.Synthesis.SpeechSynthesizer
65    if ($voice) {
66       try {
67          $tts.SelectVoice($voice)
68       } catch  [System.Management.Automation.MethodInvocationException] {
69          $module.Result.voice_info = "Could not load voice '$voice', using system default voice."
70          $module.Warn("Could not load voice '$voice', using system default voice.")
71       }
72    }
73 
74    $module.Result.voice = $tts.Voice.Name
75    if ($speech_speed -ne 0) {
76       $tts.Rate = $speech_speed
77    }
78    if (-not $module.CheckMode) {
79       $tts.Speak($words)
80    }
81    $tts.Dispose()
82 }
83 
84 if ($end_sound_path) {
85    if (-not (Test-Path -LiteralPath $end_sound_path)) {
86       $module.FailJson("End sound file $start_sound_path could not be found or opened.  Ensure you have specified the full path to the file, and the ansible windows user has permission to read the file.")
87    }
88    if (-not $module.CheckMode) {
89       (new-object Media.SoundPlayer $end_sound_path).playSync()
90    }
91 }
92 
93 $module.Result.message_text = $words.ToString()
94 
95 $module.ExitJson()
96