-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplayer.go
89 lines (76 loc) · 1.78 KB
/
player.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
package main
import (
"log"
"os"
"sync"
"time"
"github.com/faiface/beep"
"github.com/faiface/beep/speaker"
"github.com/faiface/beep/wav"
"github.com/fatih/color"
)
var (
hihatSound = sound("drums/hihat.wav")
kickSound = sound("drums/kick.wav")
snareSound = sound("drums/snare.wav")
)
type Drum struct {
Tempo int
}
func (d Drum) hihat(rhythm string, wg *sync.WaitGroup) {
defer wg.Done()
d.playbeat("prato", rhythm, hihatSound, blue)
}
func (d Drum) kick(rhythm string, wg *sync.WaitGroup) {
defer wg.Done()
d.playbeat("bumbo", rhythm, kickSound, green)
}
func (d Drum) snare(rhythm string, wg *sync.WaitGroup) {
defer wg.Done()
d.playbeat("caixa", rhythm, snareSound, yellow)
}
func (d Drum) playbeat(name string, beats string, sound beep.StreamSeeker, color *color.Color) {
ticker := time.NewTicker(time.Duration(d.Tempo) * time.Millisecond)
defer ticker.Stop()
runes := []rune(beats)
count := 0
loop := 1
for count < len(runes) {
select {
case _ = <-ticker.C:
r := runes[count]
if r == 'x' {
go play(sound)
color.Printf("[T%d]%s\n", loop, name)
}
count++
loop++
}
}
//Give a little time to the last note to echo
time.Sleep(time.Duration(d.Tempo) * time.Millisecond)
}
func play(sound beep.StreamSeeker) {
playbackDone := make(chan bool)
speaker.Play(beep.Seq(sound, beep.Callback(func() {
playbackDone <- true
sound.Seek(0)
})))
<-playbackDone
}
func sound(filename string) beep.StreamSeeker {
f, err := os.Open(filename)
if err != nil {
log.Fatal(err)
}
streamer, format, err := wav.Decode(f)
if err != nil {
log.Fatal(err)
}
speaker.Init(format.SampleRate, format.SampleRate.N(time.Second/10))
buffer := beep.NewBuffer(format)
buffer.Append(streamer)
streamer.Close()
sound := buffer.Streamer(0, buffer.Len())
return sound
}