-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.go
160 lines (145 loc) · 3.34 KB
/
main.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
package main
import (
"github.com/tejashwikalptaru/gotune/bass"
"github.com/tejashwikalptaru/gotune/ui"
"log"
)
var plView *ui.PlayListView
func handleMainWindowButtonClicks(app *ui.Main, player *bass.Player) {
app.OnPlay(func() {
status, _ := player.Status()
if status == bass.ChannelStatusPlaying {
_, _ = player.Pause()
return
}
_ ,_ = player.Play()
})
app.OnMute(func() {
mute := !player.IsMute()
player.Mute(mute)
})
app.OnStop(func() {
player.Stop()
})
app.OnNext(func() {
player.PlayNext()
})
app.OnPrev(func() {
player.PlayPrevious()
})
app.OnLoop(func() {
loop := !player.IsLoop()
player.Loop(loop)
app.SetLoopState(loop)
})
}
func handleMainWindowCallBacks(app *ui.Main, player *bass.Player) {
app.VolumeUpdateCallBack(func(vol float64) {
player.SetVolume(vol)
})
app.SetPlayListUpdater(func(path string) {
player.AddToQueue(path, false)
})
app.ProgressChanged(func(val float64) {
player.SetChannelPosition(val)
})
app.SetOpenPlayListCallBack(func() {
if plView != nil {
plView.Show()
return
}
plView = ui.NewPlayListView(app.GetApp(), player.GetPlayList(), player.GetPlaylistIndex(), func() {
plView = nil
}, func(path string) {
index := player.PlayFromQueue(path)
if index > -1 {
plView.SetSelected(path)
}
})
plView.Show()
})
app.SetFileOpenCallBack(func(filePath string) {
err := player.AddToQueue(filePath, false)
if err != nil {
log.Fatal(err)
return
}
})
app.OnAppClose(func() {
app.QuitApp()
})
}
func handlePlayerCallbacks(app *ui.Main, player *bass.Player) {
player.ChannelLoadedCallBack(func(status bass.ChannelStatus, totalTime float64, channel int64, meta bass.MusicMetaInfo, currentQueueIndex int) {
app.SetTotalTime(totalTime)
app.SetSongName(meta.Info.Name)
if plView != nil {
plView.SetSelected(meta.Path)
}
if meta.IsMOD {
return
}
if meta.AdditionalMeta != nil && meta.AdditionalMeta.Picture() != nil {
app.SetAlbumArt(meta.AdditionalMeta.Picture().Data)
} else {
app.ClearAlbumArt()
}
})
player.StatusCallBack(func(status bass.ChannelStatus, elapsed float64, mute bool) {
app.SetMuteState(mute)
if status == bass.ChannelStatusPlaying {
app.SetPlayState(true)
app.SetCurrentTime(elapsed)
return
}
app.SetPlayState(false)
})
player.FileAddedCallBack(func(info bass.MusicMetaInfo) {
if plView != nil {
plView.FileAdded(info)
}
// save history
app.SaveHistory(player.GetHistory())
})
}
func createPlayer() *bass.Player {
player, err := bass.New(-1, 44100, bass.InitFlag3D | bass.InitFlagSTEREO)
if err != nil {
log.Fatal(err)
}
player.SetVolume(100)
return player
}
func createMainWindow() *ui.Main {
app := ui.NewMainWindow()
app.SetVolume(100)
return app
}
func main() {
// audio player instance
player := createPlayer()
defer func(player *bass.Player) {
//player.SaveHistory()
err := player.Free()
if err != nil {
log.Fatal(err)
}
}(player)
// main window instance
app := createMainWindow()
defer func(app *ui.Main) {
app.Free()
}(app)
// load history
player.LoadHistory(app.GetHistory())
// main window button clicks callback
handleMainWindowButtonClicks(app, player)
// main window callbacks
handleMainWindowCallBacks(app, player)
// player callbacks
handlePlayerCallbacks(app, player)
// add app shortcuts
app.AddShortCuts()
// finally run
app.ShowAndRun()
}