forked from talkkonnect/talkkonnect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmedia.go
190 lines (165 loc) · 5.82 KB
/
media.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
* talkkonnect headless mumble client/gateway with lcd screen and channel control
* Copyright (C) 2018-2019, Suvir Kumar <[email protected]>
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* talkkonnect is the based on talkiepi and barnard by Daniel Chote and Tim Cooper
*
* The Initial Developer of the Original Code is
* Suvir Kumar <[email protected]>
* Portions created by the Initial Developer are Copyright (C) Suvir Kumar. All Rights Reserved.
*
* Code Copied from https://www.socketloop.com/tutorials/golang-convert-seconds-to-human-readable-time-format-example
*
* Contributor(s):
*
* Suvir Kumar <[email protected]>
*
* My Blog is at www.talkkonnect.com
* The source code is hosted at github.com/talkkonnect
*
*
*/
package talkkonnect
import (
"bytes"
"fmt"
"log"
"os/exec"
"strconv"
)
func aplayLocal(fileNameWithPath string) {
var player string
var CmdArguments = []string{}
if path, err := exec.LookPath("aplay"); err == nil {
CmdArguments = []string{fileNameWithPath, "-q", "-N"}
player = path
} else if path, err := exec.LookPath("paplay"); err == nil {
CmdArguments = []string{fileNameWithPath}
player = path
} else {
return
}
log.Printf("debug: player %v CmdArguments %v", player, CmdArguments)
cmd := exec.Command(player, CmdArguments...)
_, err := cmd.CombinedOutput()
if err != nil {
return
}
}
func localMediaPlayer(fileNameWithPath string, playbackvolume int, blocking bool, duration float32, loop int) {
if loop == 0 || loop > 3 {
log.Println("warn: Infinite Loop or more than 3 loops not allowed")
return
}
CmdArguments := []string{fileNameWithPath, "-volume", strconv.Itoa(playbackvolume), "-autoexit", "-loop", strconv.Itoa(loop), "-autoexit", "-nodisp"}
if duration > 0 {
CmdArguments = []string{fileNameWithPath, "-volume", strconv.Itoa(playbackvolume), "-autoexit", "-t", fmt.Sprintf("%.1f", duration), "-loop", strconv.Itoa(loop), "-autoexit", "-nodisp"}
}
cmd := exec.Command("/usr/bin/ffplay", CmdArguments...)
WaitForFFPlay := make(chan struct{})
go func() {
cmd.Run()
if blocking {
WaitForFFPlay <- struct{}{} // signal that the routine has completed
}
}()
if blocking {
<-WaitForFFPlay
}
}
func (b *Talkkonnect) PlayTone(toneFreq int, toneDuration float32, destination string, withRXLED bool) {
if destination == "local" {
cmdArguments := []string{"-f", "lavfi", "-i", "sine=frequency=" + strconv.Itoa(toneFreq) + ":duration=" + fmt.Sprintf("%f", toneDuration), "-autoexit", "-nodisp"}
cmd := exec.Command("/usr/bin/ffplay", cmdArguments...)
var out bytes.Buffer
cmd.Stdout = &out
if withRXLED {
GPIOOutPin("voiceactivity", "on")
}
err := cmd.Run()
if err != nil {
log.Println("error: ffplay error ", err)
if withRXLED {
GPIOOutPin("voiceactivity", "off")
}
return
}
if withRXLED {
GPIOOutPin("voiceactivity", "off")
}
log.Printf("info: Played Tone at Frequency %v Hz With Duration of %v Seconds\n", toneFreq, toneDuration)
}
}
/*
func playAnnouncementMedia(id int) {
for _, multimedia := range Config.Global.Multimedia.ID {
apiid, err := strconv.Atoi(multimedia.Value)
if apiid == id && err == nil {
if multimedia.Params.Localplay {
if multimedia.Params.GPIO.Enabled {
GPIOOutPin(multimedia.Params.GPIO.Name, "on")
}
if multimedia.Params.Predelay.Enabled && multimedia.Params.Predelay.Value > 0 {
time.Sleep(multimedia.Params.Predelay.Value * time.Second)
}
if multimedia.Params.Announcementtone.Enabled && FileExists(multimedia.Params.Announcementtone.File) {
localMediaPlayer(multimedia.Params.Announcementtone.File, multimedia.Params.Announcementtone.Volume, multimedia.Params.Announcementtone.Blocking, 0, 1) //todo replace 1 with volume from xmlconfig
}
for _, source := range multimedia.Media.Source {
if source.Enabled {
log.Printf("debug: Playing %v filename %v\n", source.Name, source.File)
localMediaPlayer(source.File, source.Volume, multimedia.Params.Announcementtone.Blocking, source.Duration, source.Loop)
}
}
if multimedia.Params.Postdelay.Enabled && multimedia.Params.Postdelay.Value > 0 {
time.Sleep(multimedia.Params.Postdelay.Value * time.Second)
}
if multimedia.Params.GPIO.Enabled {
GPIOOutPin(multimedia.Params.GPIO.Name, "off")
}
}
if multimedia.Params.Playintostream {
log.Println("alert: todo play into stream not implemented yet")
}
if multimedia.Params.Voicetarget {
log.Println("alert: todo play to voice targets not implemented yet")
}
}
}
}
*/
func findEventSound(findEventSound string) EventSoundStruct {
for _, sound := range Config.Global.Software.Sounds.Sound {
if sound.Enabled && sound.Event == findEventSound {
return EventSoundStruct{sound.Enabled, sound.File, sound.Volume, sound.Blocking}
}
}
return EventSoundStruct{false, "", "0", false}
}
func findInputEventSoundFile(findInputEventSound string) InputEventSoundFileStruct {
for _, sound := range Config.Global.Software.Sounds.Input.Sound {
if sound.Event == findInputEventSound {
if sound.Enabled {
return InputEventSoundFileStruct{sound.Event, sound.File, sound.Enabled}
}
}
}
return InputEventSoundFileStruct{findInputEventSound, "", false}
}
func playIOMedia(inputEvent string) {
if Config.Global.Software.Sounds.Input.Enabled {
var inputEventSoundFile InputEventSoundFileStruct = findInputEventSoundFile(inputEvent)
if inputEventSoundFile.Enabled {
go aplayLocal(inputEventSoundFile.File)
}
}
}