Skip to content
This repository was archived by the owner on Sep 27, 2023. It is now read-only.

Commit 6846213

Browse files
committed
Allowing sound effects to replace playing sound effects
1 parent b320b53 commit 6846213

File tree

1 file changed

+28
-22
lines changed

1 file changed

+28
-22
lines changed

main.go

+28-22
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ const soundsDir string = "sounds"
2525

2626
var hkey = hotkey.New()
2727
var ctrl = &beep.Ctrl{}
28+
var quit = make(chan bool)
2829

2930
func main() {
3031
rand.Seed(time.Now().Unix())
@@ -38,10 +39,14 @@ func main() {
3839
}
3940
}()
4041

41-
err := configureShortcuts()
42-
if err != nil {
43-
log.Fatal("Could not configure shortcuts:", err.Error())
44-
}
42+
go func() {
43+
err := configureShortcuts()
44+
if err != nil {
45+
log.Fatal("Could not configure shortcuts:", err.Error())
46+
}
47+
}()
48+
49+
<-quit // Keep the program alive until we kill it with a keyboard shortcut
4550
}
4651

4752
func readConfigFile() {
@@ -102,7 +107,8 @@ func generateTwitchHelp(user string, allSoundDirectories []string) string {
102107
}
103108

104109
func notify() {
105-
playSfx(soundsDir + "/chat-notification.ogg")
110+
// TODO: Make this play over top other sound effects
111+
// playSfx(soundsDir + "/chat-notification.ogg")
106112
}
107113

108114
func executeTwitchMessage(message twitch.PrivateMessage, allSoundDirectories []string) bool {
@@ -122,8 +128,6 @@ func executeTwitchMessage(message twitch.PrivateMessage, allSoundDirectories []s
122128
}
123129

124130
func configureShortcuts() error {
125-
quit := make(chan bool)
126-
127131
fmt.Println("Push Shift+Alt+Q to quit")
128132
hkey.Register(hotkey.Shift+hotkey.Alt, 'Q', func() {
129133
fmt.Println("Quit")
@@ -139,8 +143,6 @@ func configureShortcuts() error {
139143
return err
140144
}
141145

142-
<-quit // Keep the program alive until we kill it with a keyboard shortcut
143-
144146
return nil
145147
}
146148

@@ -174,24 +176,28 @@ func randomSfx(directory string) func() {
174176
}
175177

176178
func playSfx(path string) error {
177-
streamer, format, err := decodeFile(path)
178-
if err != nil {
179-
return err
180-
}
181-
defer streamer.Close()
179+
// TODO: Figure out a cleaner way to stop a sound effect and play another (this Goroutine is dirty)
180+
go func() error {
181+
streamer, format, err := decodeFile(path)
182+
if err != nil {
183+
return err
184+
}
185+
defer streamer.Close()
182186

183-
sr := format.SampleRate * 2
184-
speaker.Init(sr, sr.N(time.Second/10))
187+
sr := format.SampleRate * 2
188+
speaker.Init(sr, sr.N(time.Second/10))
185189

186-
resampled := beep.Resample(4, format.SampleRate, sr, streamer)
190+
resampled := beep.Resample(4, format.SampleRate, sr, streamer)
187191

188-
log.Println("Playing " + path)
192+
log.Println("Playing " + path)
189193

190-
done := make(chan bool)
191-
ctrl = &beep.Ctrl{Streamer: beep.Seq(resampled, beep.Callback(func() { done <- true })), Paused: false}
192-
speaker.Play(ctrl)
194+
done := make(chan bool)
195+
speaker.Play(beep.Seq(resampled, beep.Callback(func() { done <- true })))
193196

194-
<-done // Block until the sound file is done playing
197+
<-done // Block until the sound file is done playing
198+
199+
return nil
200+
}()
195201

196202
return nil
197203
}

0 commit comments

Comments
 (0)