-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
690 lines (593 loc) · 17.4 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
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
package main
import (
"fmt"
"log"
"math"
"math/rand"
"os"
"path/filepath"
"strings"
"sync"
"time"
"github.com/faiface/beep"
"github.com/faiface/beep/effects"
"github.com/faiface/beep/mp3"
"github.com/faiface/beep/speaker"
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
)
func main() {
MusicPlayerMain()
}
type MusicPlayer struct {
streamer beep.StreamSeekCloser
ctrl *beep.Ctrl
volume *effects.Volume
format beep.Format
done chan bool
isPlaying bool
currentTrack string
tracks []string
visualizer *FibonacciVisualizer
stopVisualizer chan bool
visualizerTicker *time.Ticker
playbackLock sync.Mutex
updateInfoFunc func()
}
func NewMusicPlayer() *MusicPlayer {
tracks := loadTracks()
return &MusicPlayer{
done: make(chan bool),
tracks: tracks,
visualizer: NewFibonacciVisualizer(),
stopVisualizer: make(chan bool),
}
}
func (mp *MusicPlayer) SetUpdateInfoFunc(updateFunc func()) {
mp.updateInfoFunc = updateFunc
}
func loadTracks() []string {
var tracks []string
files, err := os.ReadDir("media")
if err != nil {
log.Printf("Error reading media directory: %v", err)
return tracks
}
for _, file := range files {
if !file.IsDir() && strings.HasSuffix(strings.ToLower(file.Name()), ".mp3") {
tracks = append(tracks, filepath.Join("media", file.Name()))
}
}
if len(tracks) == 0 {
log.Printf("No tracks found in the media directory.")
}
return tracks
}
func (mp *MusicPlayer) Play() error {
mp.playbackLock.Lock()
defer mp.playbackLock.Unlock()
if mp.isPlaying {
if err := mp.Stop(); err != nil {
log.Printf("Error stopping current track: %v", err)
}
}
if len(mp.tracks) == 0 {
return fmt.Errorf("no tracks available to play")
}
rng := rand.New(rand.NewSource(time.Now().UnixNano()))
audioFile := mp.tracks[rng.Intn(len(mp.tracks))]
mp.currentTrack = audioFile
f, err := os.Open(audioFile)
if err != nil {
return fmt.Errorf("error opening audio file: %v", err)
}
if mp.updateInfoFunc != nil {
mp.updateInfoFunc()
}
mp.streamer, mp.format, err = mp3.Decode(f)
if err != nil {
return fmt.Errorf("error decoding audio file: %v", err)
}
mp.ctrl = &beep.Ctrl{Streamer: mp.streamer, Paused: false}
mp.volume = &effects.Volume{
Streamer: mp.ctrl,
Base: 2,
Volume: 0,
Silent: false,
}
analyzer := NewPeakAnalyzer(mp.volume)
speaker.Init(mp.format.SampleRate, mp.format.SampleRate.N(time.Second/10))
speaker.Play(beep.Seq(analyzer, beep.Callback(func() {
mp.done <- true
})))
mp.SetVolume(0)
mp.isPlaying = true
mp.visualizerTicker = time.NewTicker(time.Second / 60)
select {
case mp.stopVisualizer <- true:
default:
}
go func() {
for {
select {
case <-mp.stopVisualizer:
mp.visualizerTicker.Stop()
return
case <-mp.visualizerTicker.C:
peak := analyzer.Peak
mp.visualizer.UpdateWithPeak(peak)
}
}
}()
go func() {
<-mp.done
mp.Play()
}()
return nil
}
func (mp *MusicPlayer) Stop() error {
if !mp.isPlaying {
return fmt.Errorf("not playing")
}
mp.stopVisualizer <- true
if mp.visualizerTicker != nil {
mp.visualizerTicker.Stop()
}
speaker.Clear()
err := mp.streamer.Close()
if err != nil {
return fmt.Errorf("error stopping playback: %v", err)
}
mp.isPlaying = false
mp.currentTrack = ""
return nil
}
func (mp *MusicPlayer) Shuffle() error {
return mp.Play()
}
func (mp *MusicPlayer) SetVolume(percentage float64) {
if mp.volume != nil {
percentage = math.Max(-100, math.Min(100, percentage))
volume := math.Log2(float64(percentage)/100 + 1)
speaker.Lock()
mp.volume.Volume = volume
speaker.Unlock()
}
}
func (mp *MusicPlayer) GetVolumePercentage() float64 {
if mp.volume == nil {
return 0
}
return (math.Pow(2, mp.volume.Volume) - 1) * 100
}
func MusicPlayerMain() {
player := NewMusicPlayer()
app := tview.NewApplication()
app.SetAfterDrawFunc(func(screen tcell.Screen) {
width, height := screen.Size()
player.visualizer.SetRect(0, 0, width, height)
})
infoTextNowPlaying := tview.NewTextView().
SetDynamicColors(true).
SetTextAlign(tview.AlignCenter)
infoTextVolume := tview.NewTextView().
SetDynamicColors(true).
SetTextAlign(tview.AlignCenter)
updateInfo := func() {
infoTextNowPlaying.SetText(filepath.Base(player.currentTrack))
infoTextVolume.SetText(fmt.Sprintf("Volume: %.0f%%", player.GetVolumePercentage()))
}
visualizer := player.visualizer
player.SetUpdateInfoFunc(updateInfo)
fullScreenVisualizer := tview.NewBox().SetDrawFunc(func(screen tcell.Screen, x, y, width, height int) (int, int, int, int) {
visualizer.SetRect(x, y, width, height)
visualizer.Draw(screen)
animateLogo(screen, x, y, width, height)
tview.Print(screen, infoTextNowPlaying.GetText(true), x, y, width, tview.AlignCenter, tcell.ColorWhite)
tview.Print(screen, infoTextVolume.GetText(true), x, y+1, width, tview.AlignCenter, tcell.ColorWhite)
tview.Print(screen, "MILKSHAKER PLAYER", x, height-2, width, tview.AlignCenter, tcell.ColorGreen)
tview.Print(screen, "S (Shuffle), U/D (Volume), Q (Quit)", x, height-1, width, tview.AlignCenter, tcell.ColorGreenYellow)
return x, y, width, height
})
updateInfo()
go func() {
for {
time.Sleep(time.Second / 60) // 60 FPS
app.Draw()
}
}()
app.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
switch event.Rune() {
case 's', 'S':
player.Play()
case 'u', 'U':
player.SetVolume(player.GetVolumePercentage() + 10)
case 'd', 'D':
player.SetVolume(player.GetVolumePercentage() - 10)
case 'q', 'Q':
if err := player.Stop(); err != nil {
log.Printf("Error stopping playback: %v", err)
}
app.Stop()
}
updateInfo()
return event
})
if err := app.SetRoot(fullScreenVisualizer, true).SetFocus(fullScreenVisualizer).Run(); err != nil {
log.Fatalf("Error running application: %v", err)
}
}
type PeakAnalyzer struct {
Streamer beep.Streamer
Peak float64
decay float64
}
func NewPeakAnalyzer(streamer beep.Streamer) *PeakAnalyzer {
return &PeakAnalyzer{Streamer: streamer, decay: 0.99}
}
func (a *PeakAnalyzer) Stream(samples [][2]float64) (n int, ok bool) {
n, ok = a.Streamer.Stream(samples)
a.Peak *= a.decay
for i := 0; i < n; i++ {
a.Peak = math.Max(a.Peak, math.Abs(samples[i][0]))
a.Peak = math.Max(a.Peak, math.Abs(samples[i][1]))
}
return n, ok
}
func (a *PeakAnalyzer) Err() error {
return nil
}
type FibonacciVisualizer struct {
*tview.Box
points []float64
fibonacci []int
angle float64
scale float64
depth int
colorCache map[int]tcell.Color
sinCache []float64
cosCache []float64
lastUpdate time.Time
}
func NewFibonacciVisualizer() *FibonacciVisualizer {
v := &FibonacciVisualizer{
Box: tview.NewBox(),
points: make([]float64, 18),
fibonacci: generateFibonacci(20),
angle: 0,
scale: 1,
depth: 3,
colorCache: make(map[int]tcell.Color),
sinCache: make([]float64, 360),
cosCache: make([]float64, 360),
lastUpdate: time.Now(),
}
for i := 0; i < 360; i++ {
angle := float64(i) * math.Pi / 180
v.sinCache[i] = math.Sin(angle)
v.cosCache[i] = math.Cos(angle)
}
return v
}
func (v *FibonacciVisualizer) Draw(screen tcell.Screen) {
now := time.Now()
elapsed := now.Sub(v.lastUpdate).Seconds()
v.lastUpdate = now
x, y, width, height := v.GetInnerRect()
centerX, centerY := x+width/2, y+height/2
baseScale := math.Min(float64(width), float64(height)) / 200
goldenAngle := math.Pi * (3 - math.Sqrt(5))
chars := []rune{'•', '◦', '○', '◎', '◉', '⚬', '⚭', '⚮', '.', '·', '˙', '⋅', '∙', '⁘', '⁛', '⁝', '·', '˙', '∙', '°', '⋅', '∘', '⁖'}
for d := 0; d < v.depth; d++ {
for i := 0; i < len(v.fibonacci)-1; i++ {
amplitude := v.points[i%len(v.points)]
radius := float64(v.fibonacci[i]) * baseScale * v.scale * (1 - float64(d)*0.2) * (1 + amplitude*0.5)
rotationDirection := float64(1 - 2*(d%2))
angleVariation := v.sinCache[i%360] * 0.2
angle := math.Mod(v.angle*rotationDirection+float64(i)*goldenAngle+float64(d)*0.2+angleVariation, 2*math.Pi)
angleIndex := int(angle*180/math.Pi) % 360
if angleIndex < 0 {
angleIndex += 360
}
startX := float64(centerX) + radius*v.cosCache[angleIndex]
startY := float64(centerY) + radius*v.sinCache[angleIndex]
curvature := v.sinCache[(i*2)%360] * 10
endAngle := math.Mod(angle+goldenAngle, 2*math.Pi)
endAngleIndex := int(endAngle*180/math.Pi) % 360
if endAngleIndex < 0 {
endAngleIndex += 360
}
endX := float64(centerX) + float64(v.fibonacci[i+1])*baseScale*v.scale*(1-float64(d)*0.2)*v.cosCache[endAngleIndex]
endY := float64(centerY) + float64(v.fibonacci[i+1])*baseScale*v.scale*(1-float64(d)*0.2)*v.sinCache[endAngleIndex]
colorKey := i*1000 + d
color, exists := v.colorCache[colorKey]
if !exists {
color = v.getColor(i, amplitude, float64(d), curvature, angleVariation)
v.colorCache[colorKey] = color
}
rng := rand.New(rand.NewSource(time.Now().UnixNano()))
charIndex := (d + i + int(amplitude*10)) % len(chars)
drawFunkyLine(screen, int(startX), int(startY), int(endX), int(endY), color, chars[charIndex], amplitude)
drawRandomPattern(screen, rng, color, amplitude)
}
}
v.angle += 0.2 * elapsed
v.angle = math.Mod(v.angle, 2*math.Pi)
if v.angle < 0.01 {
v.colorCache = make(map[int]tcell.Color)
}
}
func (v *FibonacciVisualizer) UpdateWithPeak(peak float64) {
for i := range v.points {
v.points[i] = peak * math.Sin(float64(i)*math.Pi/50)
}
v.scale = 1 + peak*0.2
v.depth = 3 + int(peak*3)
}
func (v *FibonacciVisualizer) getColor(i int, amplitude, depth, curvature, angleVariation float64) tcell.Color {
hue := math.Mod((float64(i)/float64(len(v.fibonacci)) + v.angle/(2*math.Pi) + curvature*0.01 + angleVariation*0.1), 1)
saturation := 0.8 + amplitude*0.2
value := 0.7 + amplitude*0.3 - depth*0.1
return hsvToRGB(hue, saturation, value)
}
func drawFunkyLine(screen tcell.Screen, x1, y1, x2, y2 int, color tcell.Color, char rune, amplitude float64) {
dx := abs(x2 - x1)
dy := abs(y2 - y1)
sx, sy := 1, 1
if x1 >= x2 {
sx = -1
}
if y1 >= y2 {
sy = -1
}
err := dx - dy
frequency := 0.2
basePhase := float64(time.Now().UnixNano()) / 1e9
for {
t := float64(x1+x2+y1+y2) * frequency
waveOffset := amplitude * math.Sin(t+basePhase)
wx := float64(x1)
wy := float64(y1)
if dx > dy {
wy += waveOffset
} else {
wx += waveOffset
}
screen.SetContent(int(wx), int(wy), char, nil, tcell.StyleDefault.Foreground(color))
if x1 == x2 && y1 == y2 {
break
}
e2 := 2 * err
if e2 > -dy {
err -= dy
x1 += sx
}
if e2 < dx {
err += dx
y1 += sy
}
}
}
func drawRandomPattern(screen tcell.Screen, rng *rand.Rand, color tcell.Color, amplitude float64) {
width, height := screen.Size()
char := randomRune(rng)
patterns := []func(tcell.Screen, int, int, tcell.Color, rune, *rand.Rand, float64){
drawZigZag,
drawSpiral,
drawStarburst,
drawRandomWalk,
}
patternIndex := int(amplitude * float64(len(patterns)))
if patternIndex >= len(patterns) {
patternIndex = len(patterns) - 1
}
pattern := patterns[patternIndex]
pattern(screen, width, height, color, char, rng, amplitude)
}
func drawZigZag(screen tcell.Screen, width, height int, color tcell.Color, char rune, rng *rand.Rand, peak float64) {
step := 1
for x, y := 0, 0; x < width; x++ {
screen.SetContent(x, y, char, nil, tcell.StyleDefault.Foreground(color))
if y >= height-1 || y <= 0 {
step = -step
}
y += step
}
}
func drawSpiral(screen tcell.Screen, width, height int, color tcell.Color, char rune, rng *rand.Rand, peak float64) {
centerX, centerY := width/2, height/2
radius := 1.0 * peak
angle := 0.0
angleStep := 0.1
amplitude := 5.0 * peak
frequency := 0.2 + 0.1*peak
basePhase := float64(time.Now().UnixNano()) / 1e9
for radius < float64(min(width, height))/2 {
waveOffset := amplitude * math.Sin(frequency*angle+basePhase)
x := centerX + int((radius+waveOffset)*math.Cos(angle))
y := centerY + int((radius+waveOffset)*math.Sin(angle))
screen.SetContent(x, y, char, nil, tcell.StyleDefault.Foreground(color))
radius += 0.1
angle += angleStep
}
}
func drawStarburst(screen tcell.Screen, width, height int, color tcell.Color, char rune, rng *rand.Rand, peak float64) {
centerX, centerY := width/2, height/2
basePhase := float64(time.Now().UnixNano()) / 1e9
amplitude := 5.0 * peak
for angle := 0.0; angle < 2*math.Pi; angle += math.Pi / 8 {
for radius := 0.0; radius < float64(min(width, height))/2; radius += peak {
waveOffset := amplitude * math.Sin(angle+basePhase)
x := centerX + int((radius+waveOffset)*math.Cos(angle))
y := centerY + int((radius+waveOffset)*math.Sin(angle))
screen.SetContent(x, y, char, nil, tcell.StyleDefault.Foreground(color))
}
}
}
func drawRandomWalk(screen tcell.Screen, width, height int, color tcell.Color, char rune, rng *rand.Rand, peak float64) {
x, y := width/2, height/2
for i := 0; i < 100; i++ {
screen.SetContent(x, y, char, nil, tcell.StyleDefault.Foreground(color))
switch rng.Intn(4) {
case 0:
x++
case 1:
x--
case 2:
y++
case 3:
y--
}
if x < 0 {
x = 0
} else if x >= width {
x = width - 1
}
if y < 0 {
y = 0
} else if y >= height {
y = height - 1
}
}
}
func randomRune(rng *rand.Rand) rune {
runes := []rune{'*', '+', 'x', 'o', '~', '@', '#', '$', '%', '&'}
return runes[rng.Intn(len(runes))]
}
func generateFibonacci(n int) []int {
fib := make([]int, n)
fib[0], fib[1] = 1, 1
for i := 2; i < n; i++ {
fib[i] = fib[i-1] + fib[i-2]
}
return fib
}
func hsvToRGB(h, s, v float64) tcell.Color {
i := int(h * 6)
f := h*6 - float64(i)
p := v * (1 - s)
q := v * (1 - f*s)
t := v * (1 - (1-f)*s)
var r, g, b float64
switch i % 6 {
case 0:
r, g, b = v, t, p
case 1:
r, g, b = q, v, p
case 2:
r, g, b = p, v, t
case 3:
r, g, b = p, q, v
case 4:
r, g, b = t, p, v
case 5:
r, g, b = v, p, q
}
return tcell.NewRGBColor(int32(r*255), int32(g*255), int32(b*255))
}
func abs(x int) int {
if x < 0 {
return -x
}
return x
}
const (
logoRevealInterval = 20 * time.Millisecond
cycleWaitDuration = 20 * time.Second
stayVisibleDuration = 10 * time.Second
)
var (
lastLogoTime time.Time
logoMask [][]bool
revealedCount int
fadeOutCount int
isFadingOut bool
cycleEndTime time.Time
)
func animateLogo(screen tcell.Screen, x, y, width, height int) {
now := time.Now()
if now.Sub(lastLogoTime) < logoRevealInterval {
return
}
lastLogoTime = now
logoFrames := []string{
" __ __ __ __ __ __ ______ __ __ ______ __ __ ______ ______ ",
"/\\ \"-./ \\ /\\ \\ /\\ \\ /\\ \\/ / /\\ ___\\ /\\ \\_\\ \\ /\\ __ \\ /\\ \\/ / /\\ ___\\ /\\ == \\ ",
"\\ \\ \\-./\\ \\ \\ \\ \\ \\ \\ \\____ \\ \\ _\"-. \\ \\___ \\ \\ \\ __ \\ \\ \\ __ \\ \\ \\ _\"-. \\ \\ __\\ \\ \\ __< ",
" \\ \\_\\ \\ \\_\\ \\ \\_\\ \\ \\_____\\ \\ \\_\\ \\_\\ \\/\\_____\\ \\ \\_\\ \\_\\ \\ \\_\\ \\_\\ \\ \\_\\ \\_\\ \\ \\_____\\ \\ \\_\\ \\_\\ ",
" \\/_/ \\/_/ \\/_/ \\/_____/ \\/_/\\/_/ \\/_____/ \\/_/\\/_/ \\/_/\\/_/ \\/_/\\/_/ \\/_____/ \\/_/ /_/ ",
}
middleY := y + (height / 2) - (len(logoFrames) / 2)
middleX := x + (width / 2) - (len(logoFrames[0]) / 2)
// Initialize logoMask if it's empty
if len(logoMask) == 0 {
logoMask = make([][]bool, len(logoFrames))
for i := range logoMask {
logoMask[i] = make([]bool, len(logoFrames[0]))
}
}
totalNonSpaceChars := countNonSpaceChars(logoFrames)
if cycleEndTime.IsZero() {
cycleEndTime = now.Add(stayVisibleDuration)
}
if !isFadingOut {
if revealedCount < totalNonSpaceChars {
for {
i := rand.Intn(len(logoMask))
j := rand.Intn(len(logoMask[0]))
if !logoMask[i][j] && logoFrames[i][j] != ' ' {
logoMask[i][j] = true
revealedCount++
break
}
}
} else if now.After(cycleEndTime) {
isFadingOut = true
}
} else {
if fadeOutCount < totalNonSpaceChars {
for {
i := rand.Intn(len(logoMask))
j := rand.Intn(len(logoMask[0]))
if logoMask[i][j] && logoFrames[i][j] != ' ' {
logoMask[i][j] = false
fadeOutCount++
break
}
}
} else {
cycleEndTime = now.Add(cycleWaitDuration)
resetCycle()
}
}
for i, line := range logoFrames {
for j, char := range line {
if logoMask[i][j] {
style := tcell.StyleDefault.Foreground(tcell.ColorFloralWhite)
screen.SetContent(middleX+j, middleY+i, rune(char), nil, style)
}
}
}
}
func countNonSpaceChars(logoFrames []string) int {
count := 0
for _, line := range logoFrames {
for _, char := range line {
if char != ' ' {
count++
}
}
}
return count
}
func resetCycle() {
for i := range logoMask {
for j := range logoMask[i] {
logoMask[i][j] = false
}
}
revealedCount = 0
fadeOutCount = 0
isFadingOut = false
lastLogoTime = time.Time{}
}