-
Notifications
You must be signed in to change notification settings - Fork 0
/
audio.go
231 lines (192 loc) · 6.24 KB
/
audio.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package audio
import (
"encoding/json"
"github.com/maxence-charriere/go-app/v9/pkg/app"
"github.com/mlctrez/goapp-audioplayer/goapp"
"github.com/mlctrez/goapp-audioplayer/model"
"time"
)
var _ app.Mounter = (*Audio)(nil)
type Actions struct {
app.Context
}
func Action(ctx app.Context) *Actions {
return &Actions{ctx}
}
const audioSrc = "audio.action.src"
const audioPlay = "audio.action.play"
const audioStart = "audio.action.start"
const audioPause = "audio.action.pause"
const audioCurrentTime = "audio.action.currentTime"
const audioVolume = "audio.action.volume"
func (ac *Actions) Src(md *model.Metadata) { ac.NewActionWithValue(audioSrc, md) }
func (ac *Actions) Play() { ac.NewAction(audioPlay) }
func (ac *Actions) Start(md *model.Metadata) { ac.NewActionWithValue(audioStart, md) }
func (ac *Actions) Pause() { ac.NewAction(audioPause) }
func (ac *Actions) CurrentTime(value float64) { ac.NewActionWithValue(audioCurrentTime, value) }
func (ac *Actions) Volume(value float64) { ac.NewActionWithValue(audioVolume, value) }
func (ac *Actions) handle(audio *Audio) {
ac.Handle(audioSrc, audio.src)
ac.Handle(audioPlay, audio.play)
ac.Handle(audioStart, audio.start)
ac.Handle(audioPause, audio.pause)
ac.Handle(audioCurrentTime, audio.currentTime)
ac.Handle(audioVolume, audio.volume)
}
type Audio struct {
app.Compo
goapp.Logging
md *model.Metadata
}
func (a *Audio) Render() app.UI {
return app.Audio().Src("").Preload("auto")
}
const EventCanPlay = "audio.event.canplay"
const EventEnded = "audio.event.ended"
const EventPause = "audio.event.pause"
const EventPlay = "audio.event.play"
const EventSeeked = "audio.event.seeked"
const EventTimeUpdate = "audio.event.timeupdate"
func (a *Audio) eventListener(ctx app.Context) app.Func {
lastTimeUpdate := time.Now()
return app.FuncOf(func(this app.Value, args []app.Value) any {
if len(args) < 1 || args[0].Get("type").IsUndefined() {
return nil
}
eventType := args[0].Get("type").String()
if eventType != "timeupdate" {
a.Logf("audio event %s", eventType)
}
switch eventType {
case "canplay":
ctx.NewAction(EventCanPlay)
case "ended":
ctx.NewAction(EventEnded)
case "pause":
ctx.NewAction(EventPause)
case "play":
lastTimeUpdate = time.Now().Add(-1 * time.Second)
ctx.NewAction(EventPlay)
case "seeked":
lastTimeUpdate = time.Now().Add(-1 * time.Second)
ctx.NewAction(EventSeeked)
case "timeupdate":
now := time.Now()
if now.Sub(lastTimeUpdate) > time.Millisecond*200 {
lastTimeUpdate = now
a.timeUpdate(ctx)
}
}
return nil
})
}
func (a *Audio) timeUpdate(ctx app.Context) {
duration := a.JSValue().Get("duration")
if duration.IsNaN() {
return
}
currentTime := a.JSValue().Get("currentTime")
actionValue := &TimeUpdate{CurrentTime: currentTime.Float(), Duration: duration.Float()}
ctx.NewActionWithValue(EventTimeUpdate, actionValue)
}
type TimeUpdate struct {
CurrentTime float64
Duration float64
}
func (a *Audio) OnMount(ctx app.Context) {
Action(ctx).handle(a)
handleEvents := func(eventNames ...string) {
listener := a.eventListener(ctx)
for _, name := range eventNames {
a.JSValue().Call("addEventListener", name, listener)
}
}
handleEvents("canplay", "ended", "pause", "play", "seeked", "timeupdate")
// load last volume state into audio control volume
// -1 is here to skip when value is not set in state
var volume float64 = -1
ctx.GetState("volume", &volume)
if volume >= 0 {
a.JSValue().Set("volume", app.ValueOf(volume))
}
}
func actionFunc(ctx app.Context, actionName string) app.Func {
return app.FuncOf(func(this app.Value, args []app.Value) any {
ctx.NewAction(actionName)
return nil
})
}
func (a *Audio) src(ctx app.Context, action app.Action) {
if md, ok := action.Value.(*model.Metadata); ok {
if md == nil {
a.pause(ctx, action)
} else {
a.md = md
a.JSValue().Set("src", app.ValueOf(md.FlacUrl()))
}
}
}
func (a *Audio) play(ctx app.Context, _ app.Action) {
playPromise := a.JSValue().Call("play")
playPromise.Call("then", app.FuncOf(func(this app.Value, args []app.Value) any {
a.Log("setting metadata on play")
// https://github.com/w3c/mediasession/blob/main/explainer.md
mediaSession := app.Window().Get("navigator").Get("mediaSession")
if !mediaSession.IsUndefined() {
metadata := app.Window().Get("MediaMetadata").New()
metadata.Set("title", app.ValueOf(a.md.Title))
metadata.Set("artist", app.ValueOf(a.md.Artist))
metadata.Set("album", app.ValueOf(a.md.Album))
metadata.Set("artwork", mediaArtwork(a.md))
mediaSession.Set("metadata", metadata)
mediaSession.Set("playbackState", "playing")
mediaSession.Call("setActionHandler", "previoustrack",
actionFunc(ctx, "mediaSession.previoustrack"))
mediaSession.Call("setActionHandler", "nexttrack",
actionFunc(ctx, "mediaSession.nexttrack"))
mediaSession.Call("setActionHandler", "nexttrack",
actionFunc(ctx, "mediaSession.nexttrack"))
}
return nil
}))
}
func mediaArtwork(md *model.Metadata) app.Value {
type MediaArtwork struct {
Sizes string `json:"sizes"`
Src string `json:"src"`
Type string `json:"type"`
}
// these are the sizes that music.youtube.com uses, don't know on
mediaSizes := map[string]int{"60x60": 60, "120x120": 120, "226x226": 226, "544x544": 544}
var a []MediaArtwork
for sizes, size := range mediaSizes {
a = append(a, MediaArtwork{Sizes: sizes, Src: md.CoverArtUrl(size), Type: "image/png"})
}
marshal, _ := json.Marshal(a)
return app.Window().Get("JSON").Call("parse", string(marshal))
}
func (a *Audio) pause(_ app.Context, _ app.Action) {
a.JSValue().Call("pause")
mediaSession := app.Window().Get("navigator").Get("mediaSession")
if !mediaSession.IsUndefined() {
mediaSession.Set("playbackState", "paused")
}
}
func (a *Audio) currentTime(ctx app.Context, action app.Action) {
if value, ok := action.Value.(float64); ok {
a.JSValue().Set("currentTime", app.ValueOf(value))
a.timeUpdate(ctx)
}
}
func (a *Audio) start(ctx app.Context, action app.Action) {
a.Log("")
a.src(ctx, action)
a.play(ctx, action)
}
func (a *Audio) volume(_ app.Context, action app.Action) {
if volume, ok := action.Value.(float64); ok {
if volume >= 0 || volume <= 1 {
a.JSValue().Set("volume", app.ValueOf(volume))
}
}
}