-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplayer.go
157 lines (136 loc) · 3.7 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
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
package main
import (
"github.com/ktye/fft"
"github.com/murlokswarm/app"
//"log"
"math"
"math/cmplx"
"mistlur/play"
"time"
)
// Player is the component disPlaying Player.
type Player struct {
Bar [10]float64
PlayBtn string
Tag play.Tag
}
const (
// Let us make it less computationally invasive
FFTSamples = 1024
// This is fast enough for the eye, no? Maybe a little choppy
// but that is a trade-off.
RefreshEveryMillisec = 10
BtnPlay = "play"
BtnPause = "pause"
)
var (
guiIsDone chan struct{}
fftc fft.FFT
csamples []complex128
)
func Init() {
fftc, _ = fft.New(FFTSamples)
csamples = make([]complex128, FFTSamples)
}
func (p *Player) OnMount() {
p.PlayBtn = BtnPause
// Make a channel to control UI.
guiIsDone = make(chan struct{})
// Rendering loop.
go func() {
c := time.Tick(RefreshEveryMillisec * time.Millisecond)
for _ = range c {
select {
default:
// Render pl0x.
app.Render(p)
case <-guiIsDone:
return
}
}
}()
// FFT loop.
go func() {
c := time.Tick(RefreshEveryMillisec * time.Millisecond)
for _ = range c {
select {
default:
if !playlist.IsPlaying() {
p.ClearBars()
continue
}
s := playlist.GetSamples()
samples := *s
// Convert channel slices to complex128 (mono).
for i := 0; i < FFTSamples; i++ {
csamples[i] = complex((samples[i][0] + samples[i][1]), 0)
}
// An FFT walks into...
fftc.Transform(csamples)
// ...a bar...
for j := 0; j < len(p.Bar); j++ {
// Consider only half of the frequencies.
for i := 0; i < FFTSamples/len(p.Bar)/2; i++ {
p.Bar[j] = 20 * (math.Log(1 + cmplx.Abs(csamples[i+j])))
}
}
// ...and the whole scene unfolds with tedious inevitability.
// #complexjoke
case <-guiIsDone:
return
}
}
}()
//done <- struct{}{}
}
func (p *Player) BackBtn() {
playlist.Back()
}
func (p *Player) NextBtn() {
playlist.Next()
}
func (p *Player) TogglePlayBtn() {
playlist.TogglePause()
}
func (p *Player) ClearBars() {
for j := 0; j < len(p.Bar); j++ {
p.Bar[j] = 0
}
}
func (p *Player) OnDismount() {
// Tell UI it is done here.
guiIsDone <- struct{}{}
playlist.Done()
}
func (p *Player) Render() string {
// Tell UI to toggle the button.
if playlist.IsPlaying() {
p.PlayBtn = BtnPause
} else {
p.PlayBtn = BtnPlay
}
p.Tag = playlist.GetTags()
// UI component
return `
<div class="center">
<div class="graph">
<div style="height: 120px; background-color: rgba(0,0,0,0)" class="bar"/>
{{ range $key, $data := .Bar }}
<div style="height: {{$data}}px;" class="bar"/>
{{ end }}
<div style="height: 120px; background-color: rgba(0,0,0,0)" class="bar"/>
</div>
</div>
<h1>{{ .Tag.Artist }} </h1>
<h2>{{ .Tag.Title }} </h2>
<div>
<button class="button back" onclick="BackBtn"/>
<button class="button {{.PlayBtn}}" onclick="TogglePlayBtn"/>
<button class="button next" onclick="NextBtn"/>
</div>
`
}
// /!\ Register the component. Required to use the component into a context.
func init() {
app.RegisterComponent(&Player{})
}