2020-06-22 00:05:56 +08:00
|
|
|
// Copyright (C) 2020 Raziman
|
|
|
|
|
2020-06-20 23:00:19 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-06-26 17:09:15 +08:00
|
|
|
"fmt"
|
2020-06-20 23:00:19 +08:00
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/faiface/beep"
|
2020-06-21 23:47:02 +08:00
|
|
|
"github.com/faiface/beep/effects"
|
2020-06-20 23:00:19 +08:00
|
|
|
"github.com/faiface/beep/mp3"
|
|
|
|
"github.com/faiface/beep/speaker"
|
2020-07-11 12:47:28 +08:00
|
|
|
"github.com/spf13/viper"
|
2020-06-20 23:00:19 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type Player struct {
|
2020-06-21 23:47:02 +08:00
|
|
|
IsRunning bool
|
|
|
|
hasInit bool
|
|
|
|
format *beep.Format
|
2020-06-22 21:18:42 +08:00
|
|
|
|
2020-06-24 12:05:30 +08:00
|
|
|
isSkipped chan bool
|
2020-06-23 18:42:18 +08:00
|
|
|
done chan bool
|
|
|
|
|
2020-06-21 23:47:02 +08:00
|
|
|
// to control the _volume internally
|
|
|
|
_volume *effects.Volume
|
2020-06-23 22:26:28 +08:00
|
|
|
ctrl *beep.Ctrl
|
2020-06-21 23:47:02 +08:00
|
|
|
volume float64
|
2020-06-23 18:42:18 +08:00
|
|
|
resampler *beep.Resampler
|
2020-06-21 23:47:02 +08:00
|
|
|
position time.Duration
|
|
|
|
length time.Duration
|
2020-07-10 23:30:28 +08:00
|
|
|
currentSong *AudioFile
|
2020-06-22 13:18:25 +08:00
|
|
|
}
|
|
|
|
|
2020-07-11 12:47:28 +08:00
|
|
|
func NewPlayer() *Player {
|
|
|
|
|
|
|
|
// Read initial volume from config
|
|
|
|
var initVol float64 = (viper.GetFloat64("volume") - 50.0) / 10.0
|
2020-06-20 23:00:19 +08:00
|
|
|
|
2020-07-11 12:47:28 +08:00
|
|
|
return &Player{volume: initVol}
|
|
|
|
}
|
2020-06-21 23:47:02 +08:00
|
|
|
|
2020-07-11 12:44:43 +08:00
|
|
|
func (p *Player) Run(currSong *AudioFile) {
|
2020-07-10 23:30:28 +08:00
|
|
|
|
2020-07-11 12:44:43 +08:00
|
|
|
p.isSkipped = make(chan bool, 1)
|
2020-07-10 23:30:28 +08:00
|
|
|
|
2020-07-11 12:44:43 +08:00
|
|
|
f, err := os.Open(currSong.Path)
|
2020-06-20 23:00:19 +08:00
|
|
|
|
2020-07-06 17:02:59 +08:00
|
|
|
if err != nil {
|
|
|
|
appLog(err)
|
|
|
|
}
|
|
|
|
|
2020-06-20 23:00:19 +08:00
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
streamer, format, err := mp3.Decode(f)
|
2020-06-22 21:18:42 +08:00
|
|
|
|
2020-06-21 23:47:02 +08:00
|
|
|
// song duration
|
|
|
|
p.length = format.SampleRate.D(streamer.Len())
|
|
|
|
|
|
|
|
if !p.hasInit {
|
2020-07-03 00:50:32 +08:00
|
|
|
err := speaker.Init(format.SampleRate, format.SampleRate.N(time.Second/10))
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
appLog(err)
|
|
|
|
}
|
2020-07-11 12:44:43 +08:00
|
|
|
|
2020-06-21 23:47:02 +08:00
|
|
|
p.hasInit = true
|
|
|
|
}
|
2020-06-20 23:00:19 +08:00
|
|
|
|
|
|
|
p.format = &format
|
|
|
|
|
|
|
|
if err != nil {
|
2020-07-02 16:11:10 +08:00
|
|
|
appLog(err)
|
2020-06-20 23:00:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
defer streamer.Close()
|
|
|
|
|
2020-06-21 23:47:02 +08:00
|
|
|
if err != nil {
|
2020-07-02 16:11:10 +08:00
|
|
|
appLog(err)
|
2020-06-20 23:00:19 +08:00
|
|
|
}
|
|
|
|
|
2020-07-11 12:44:43 +08:00
|
|
|
p.currentSong = currSong
|
2020-06-21 23:47:02 +08:00
|
|
|
|
2020-07-11 12:44:43 +08:00
|
|
|
popupMessage := fmt.Sprintf("%s\n\n[ %s ]", currSong.Name, fmtDuration(p.length))
|
2020-06-26 17:09:15 +08:00
|
|
|
|
2020-07-04 09:47:41 +08:00
|
|
|
timedPopup(" Current Song ", popupMessage, getPopupTimeout())
|
2020-06-26 17:09:15 +08:00
|
|
|
|
2020-06-24 12:05:30 +08:00
|
|
|
done := make(chan bool, 1)
|
2020-06-20 23:00:19 +08:00
|
|
|
|
2020-06-23 18:42:18 +08:00
|
|
|
p.done = done
|
|
|
|
|
2020-06-20 23:00:19 +08:00
|
|
|
sstreamer := beep.Seq(streamer, beep.Callback(func() {
|
2020-06-24 12:05:30 +08:00
|
|
|
done <- true
|
2020-06-20 23:00:19 +08:00
|
|
|
}))
|
|
|
|
|
2020-06-21 23:47:02 +08:00
|
|
|
ctrl := &beep.Ctrl{Streamer: sstreamer, Paused: false}
|
2020-06-23 22:26:28 +08:00
|
|
|
p.ctrl = ctrl
|
2020-06-20 23:00:19 +08:00
|
|
|
|
2020-06-23 18:42:18 +08:00
|
|
|
resampler := beep.ResampleRatio(4, 1, ctrl)
|
|
|
|
p.resampler = resampler
|
|
|
|
|
2020-06-21 23:47:02 +08:00
|
|
|
volume := &effects.Volume{
|
2020-06-23 18:42:18 +08:00
|
|
|
Streamer: resampler,
|
2020-06-21 23:47:02 +08:00
|
|
|
Base: 2,
|
|
|
|
Volume: 0,
|
|
|
|
Silent: false,
|
2020-06-20 23:00:19 +08:00
|
|
|
}
|
|
|
|
|
2020-06-21 23:47:02 +08:00
|
|
|
// sets the volume of previous player
|
|
|
|
volume.Volume += p.volume
|
|
|
|
|
|
|
|
p._volume = volume
|
2020-06-20 23:00:19 +08:00
|
|
|
|
2020-06-21 23:47:02 +08:00
|
|
|
speaker.Play(p._volume)
|
|
|
|
|
|
|
|
position := func() time.Duration {
|
|
|
|
return format.SampleRate.D(streamer.Position())
|
|
|
|
}
|
|
|
|
|
|
|
|
p.position = position()
|
2020-06-20 23:00:19 +08:00
|
|
|
p.IsRunning = true
|
|
|
|
|
2020-07-11 12:44:43 +08:00
|
|
|
gomu.PlayingBar.NewProgress(currSong.Name, int(p.length.Seconds()), 100)
|
2020-07-04 16:16:57 +08:00
|
|
|
gomu.PlayingBar.Run()
|
2020-06-21 23:47:02 +08:00
|
|
|
|
2020-06-25 14:12:02 +08:00
|
|
|
// is used to send progress
|
|
|
|
i := 0
|
2020-06-21 23:47:02 +08:00
|
|
|
|
2020-06-23 18:42:18 +08:00
|
|
|
next:
|
2020-06-20 23:00:19 +08:00
|
|
|
for {
|
2020-06-25 14:12:02 +08:00
|
|
|
|
2020-06-20 23:00:19 +08:00
|
|
|
select {
|
|
|
|
case <-done:
|
2020-06-21 23:47:02 +08:00
|
|
|
close(done)
|
2020-06-23 20:15:29 +08:00
|
|
|
p.position = 0
|
2020-06-22 21:18:42 +08:00
|
|
|
p.IsRunning = false
|
2020-06-23 20:15:29 +08:00
|
|
|
p.format = nil
|
2020-07-04 16:16:57 +08:00
|
|
|
gomu.PlayingBar.Stop()
|
2020-06-22 21:18:42 +08:00
|
|
|
|
2020-07-11 12:44:43 +08:00
|
|
|
nextSong, err := gomu.Queue.Dequeue()
|
|
|
|
gomu.App.Draw()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
break next
|
2020-06-23 18:42:18 +08:00
|
|
|
}
|
2020-07-11 12:44:43 +08:00
|
|
|
|
|
|
|
go p.Run(nextSong)
|
|
|
|
|
2020-06-23 18:42:18 +08:00
|
|
|
break next
|
2020-06-25 14:12:02 +08:00
|
|
|
|
2020-06-20 23:00:19 +08:00
|
|
|
case <-time.After(time.Second):
|
2020-06-25 14:12:02 +08:00
|
|
|
// stop progress bar from progressing when paused
|
|
|
|
if !p.IsRunning {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
i++
|
2020-07-04 16:16:57 +08:00
|
|
|
gomu.PlayingBar.progress <- 1
|
2020-06-25 14:12:02 +08:00
|
|
|
|
2020-06-20 23:00:19 +08:00
|
|
|
speaker.Lock()
|
|
|
|
p.position = position()
|
|
|
|
speaker.Unlock()
|
2020-06-25 14:12:02 +08:00
|
|
|
|
2020-07-04 16:16:57 +08:00
|
|
|
if i > gomu.PlayingBar.full {
|
2020-06-25 14:12:02 +08:00
|
|
|
break next
|
|
|
|
}
|
|
|
|
|
2020-06-20 23:00:19 +08:00
|
|
|
}
|
2020-06-25 14:12:02 +08:00
|
|
|
|
2020-06-20 23:00:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Player) Pause() {
|
|
|
|
speaker.Lock()
|
2020-06-23 22:26:28 +08:00
|
|
|
p.ctrl.Paused = true
|
2020-06-20 23:00:19 +08:00
|
|
|
p.IsRunning = false
|
|
|
|
speaker.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Player) Play() {
|
|
|
|
speaker.Lock()
|
2020-06-23 22:26:28 +08:00
|
|
|
p.ctrl.Paused = false
|
2020-06-20 23:00:19 +08:00
|
|
|
p.IsRunning = true
|
|
|
|
speaker.Unlock()
|
|
|
|
}
|
|
|
|
|
2020-06-21 23:47:02 +08:00
|
|
|
// volume up and volume down using -0.5 or +0.5
|
2020-06-26 17:09:15 +08:00
|
|
|
func (p *Player) Volume(v float64) float64 {
|
|
|
|
|
2020-07-12 09:48:48 +08:00
|
|
|
// check if no songs playing currently
|
2020-06-26 17:09:15 +08:00
|
|
|
if p._volume == nil {
|
|
|
|
p.volume += v
|
2020-07-04 10:17:52 +08:00
|
|
|
return p.volume
|
2020-06-26 17:09:15 +08:00
|
|
|
}
|
|
|
|
|
2020-07-12 09:48:48 +08:00
|
|
|
defer func() {
|
|
|
|
volume := int(p.volume*10) + 50
|
|
|
|
viper.Set("volume", volume)
|
|
|
|
}()
|
|
|
|
|
2020-06-21 23:47:02 +08:00
|
|
|
speaker.Lock()
|
|
|
|
p._volume.Volume += v
|
|
|
|
p.volume = p._volume.Volume
|
|
|
|
speaker.Unlock()
|
2020-06-26 17:09:15 +08:00
|
|
|
return p.volume
|
2020-06-21 23:47:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Player) TogglePause() {
|
2020-06-23 22:26:28 +08:00
|
|
|
if p.ctrl.Paused {
|
2020-06-21 23:47:02 +08:00
|
|
|
p.Play()
|
|
|
|
} else {
|
|
|
|
p.Pause()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-23 18:42:18 +08:00
|
|
|
// skips current song
|
|
|
|
func (p *Player) Skip() {
|
2020-07-01 17:48:33 +08:00
|
|
|
|
2020-07-04 16:16:57 +08:00
|
|
|
if gomu.Queue.GetItemCount() > 0 {
|
2020-06-24 12:05:30 +08:00
|
|
|
p.ctrl.Streamer = nil
|
2020-06-23 18:42:18 +08:00
|
|
|
p.done <- true
|
|
|
|
}
|
|
|
|
}
|
2020-07-02 20:47:20 +08:00
|
|
|
|
|
|
|
// gets the length of the song in the queue
|
|
|
|
func GetLength(audioPath string) (time.Duration, error) {
|
|
|
|
|
|
|
|
f, err := os.Open(audioPath)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
2020-07-06 17:02:59 +08:00
|
|
|
defer f.Close()
|
|
|
|
|
2020-07-02 20:47:20 +08:00
|
|
|
streamer, format, err := mp3.Decode(f)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
2020-07-06 17:02:59 +08:00
|
|
|
defer streamer.Close()
|
|
|
|
|
2020-07-02 20:47:20 +08:00
|
|
|
return format.SampleRate.D(streamer.Len()), nil
|
|
|
|
}
|