gomu/player.go

214 lines
3.4 KiB
Go
Raw Normal View History

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-06-26 17:09:15 +08:00
"github.com/spf13/viper"
2020-06-20 23:00:19 +08:00
)
type Song struct {
name string
path string
position string
}
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-06-20 23:00:19 +08:00
currentSong Song
2020-06-22 13:18:25 +08:00
}
2020-06-21 23:47:02 +08:00
func (p *Player) Run() {
2020-06-20 23:00:19 +08:00
2020-06-24 12:05:30 +08:00
p.isSkipped = make(chan bool, 1)
2020-06-26 12:54:48 +08:00
first, err := queue.Pop()
2020-06-21 23:47:02 +08:00
2020-06-20 23:00:19 +08:00
if err != nil {
p.IsRunning = false
2020-07-02 16:11:10 +08:00
appLog(err)
2020-06-23 20:15:29 +08:00
}
2020-06-20 23:00:19 +08:00
f, err := os.Open(first)
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 {
speaker.Init(format.SampleRate, format.SampleRate.N(time.Second/10))
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-06-21 23:47:02 +08:00
song := &Song{name: GetName(f.Name()), path: first}
p.currentSong = *song
2020-06-26 17:09:15 +08:00
popupMessage := fmt.Sprintf("%s\n\n[ %s ]", song.name, fmtDuration(p.length))
timeout := viper.GetInt("popup_timeout")
2020-06-28 14:03:30 +08:00
timedPopup(" Current Song ", popupMessage, time.Second*time.Duration(timeout))
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-06-26 12:54:48 +08:00
playingBar.NewProgress(song.name, int(p.length.Seconds()), 100)
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-06-26 12:54:48 +08:00
playingBar.Stop()
2020-06-22 21:18:42 +08:00
2020-06-26 12:54:48 +08:00
if queue.GetItemCount() != 0 {
2020-06-20 23:00:19 +08:00
go p.Run()
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-06-26 12:54:48 +08:00
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-06-26 12:54:48 +08:00
if i > 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()
}
func (p *Player) CurrentSong() Song {
return p.currentSong
}
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 {
if p._volume == nil {
p.volume += v
return v
}
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-06-26 12:54:48 +08:00
if 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
}
}