2021-12-22 08:49:37 -07:00
|
|
|
package render
|
2021-10-24 01:45:45 -06:00
|
|
|
|
|
|
|
import (
|
2021-12-12 13:13:20 -07:00
|
|
|
"image"
|
|
|
|
"os"
|
|
|
|
|
2021-12-24 03:07:21 -07:00
|
|
|
"github.com/aditya-K2/gomp/ui"
|
|
|
|
"github.com/fhs/gompd/mpd"
|
|
|
|
|
2021-12-16 12:36:18 -07:00
|
|
|
"github.com/aditya-K2/gomp/cache"
|
|
|
|
"github.com/aditya-K2/gomp/utils"
|
2021-12-12 13:13:20 -07:00
|
|
|
"github.com/nfnt/resize"
|
2021-11-05 01:10:46 -06:00
|
|
|
"github.com/spf13/viper"
|
2021-10-24 01:45:45 -06:00
|
|
|
"gitlab.com/diamondburned/ueberzug-go"
|
|
|
|
)
|
|
|
|
|
2021-12-22 08:49:37 -07:00
|
|
|
var (
|
2021-12-24 03:07:21 -07:00
|
|
|
CONN *mpd.Client
|
|
|
|
Notify interface{ Send(string) }
|
2021-12-22 08:49:37 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
func SetConnection(c *mpd.Client) {
|
|
|
|
CONN = c
|
|
|
|
}
|
|
|
|
|
|
|
|
func SetNotificationServer(n interface{ Send(string) }) {
|
|
|
|
Notify = n
|
|
|
|
}
|
|
|
|
|
2022-03-15 15:18:53 -06:00
|
|
|
// Renderer is just a channel on which we will send the Path to the song whose
|
|
|
|
// Image is to be Rendered. This channel is passed to the OpenImage which in turn is called
|
|
|
|
// by the Start() function as a go routine.
|
2021-10-24 01:45:45 -06:00
|
|
|
type Renderer struct {
|
|
|
|
c chan string
|
|
|
|
}
|
|
|
|
|
2022-03-15 15:18:53 -06:00
|
|
|
// NewRenderer Returns a new Renderer with a string channel
|
2021-12-22 08:49:37 -07:00
|
|
|
func NewRenderer() *Renderer {
|
2021-10-24 01:45:45 -06:00
|
|
|
c := make(chan string)
|
|
|
|
return &Renderer{
|
|
|
|
c: c,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-24 03:07:21 -07:00
|
|
|
// Send Image Path to Renderer
|
2022-03-15 15:18:53 -06:00
|
|
|
func (r *Renderer) Send(path string) {
|
|
|
|
r.c <- path
|
2021-10-24 01:45:45 -06:00
|
|
|
}
|
|
|
|
|
2022-03-15 15:18:53 -06:00
|
|
|
// OpenImage Go Routine that will Be Called and will listen on the channel c
|
2021-12-24 03:07:21 -07:00
|
|
|
// for changes and on getting a string over the channel will open the Image and
|
2022-03-15 15:18:53 -06:00
|
|
|
// keep listening again. This will keep the image blocked ( i.e. no need to use time.Sleep() etc. )
|
2021-12-24 03:07:21 -07:00
|
|
|
// and saves resources too.
|
2021-12-22 08:49:37 -07:00
|
|
|
func OpenImage(path string, c chan string) {
|
2021-12-12 13:05:40 -07:00
|
|
|
fw, fh := utils.GetFontWidth()
|
2021-10-24 01:45:45 -06:00
|
|
|
var im *ueberzug.Image
|
|
|
|
if path != "stop" {
|
2021-12-22 08:49:37 -07:00
|
|
|
extractedImage := GetImagePath(path)
|
2021-12-12 13:13:20 -07:00
|
|
|
img2, _ := GetImg(extractedImage)
|
2022-03-15 15:18:53 -06:00
|
|
|
im, _ = ueberzug.NewImage(img2,
|
|
|
|
int(float32(ui.ImgX)*fw)+viper.GetInt("ADDITIONAL_PADDING_X"),
|
|
|
|
int(float32(ui.ImgY)*fh)+viper.GetInt("ADDITIONAL_PADDING_Y"))
|
2021-10-24 01:45:45 -06:00
|
|
|
}
|
|
|
|
d := <-c
|
|
|
|
if im != nil {
|
|
|
|
im.Clear()
|
|
|
|
}
|
|
|
|
if d != "stop" {
|
2021-12-22 08:49:37 -07:00
|
|
|
OpenImage(d, c)
|
2021-10-24 01:45:45 -06:00
|
|
|
} else {
|
2021-12-22 08:49:37 -07:00
|
|
|
OpenImage("stop", c)
|
2021-10-24 01:45:45 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-15 15:18:53 -06:00
|
|
|
// Start Initialises the Renderer and calls the go routine OpenImage and passes the channel
|
2021-12-24 03:07:21 -07:00
|
|
|
// as argument.
|
2022-03-15 15:18:53 -06:00
|
|
|
func (r *Renderer) Start(path string) {
|
|
|
|
go OpenImage(path, r.c)
|
2021-10-24 01:45:45 -06:00
|
|
|
}
|
2021-11-20 09:45:10 -07:00
|
|
|
|
2022-03-15 15:18:53 -06:00
|
|
|
// GetImagePath This Function returns the path to the image that is to be
|
|
|
|
// rendered it checks first for the image in the cache
|
2021-12-24 03:07:21 -07:00
|
|
|
// else it adds the image to the cache and then extracts it and renders it.
|
2021-12-22 08:49:37 -07:00
|
|
|
func GetImagePath(path string) string {
|
2021-11-20 09:45:10 -07:00
|
|
|
a, err := CONN.ListInfo(path)
|
|
|
|
var extractedImage string
|
|
|
|
if err == nil && len(a) != 0 {
|
2021-12-08 14:02:02 -07:00
|
|
|
if cache.Exists(a[0]["artist"], a[0]["album"]) {
|
|
|
|
extractedImage = cache.GenerateName(a[0]["artist"], a[0]["album"])
|
2021-11-20 09:45:10 -07:00
|
|
|
} else {
|
2021-12-08 14:02:02 -07:00
|
|
|
imagePath := cache.GenerateName(a[0]["artist"], a[0]["album"])
|
2021-12-12 13:05:40 -07:00
|
|
|
absPath := utils.CheckDirectoryFmt(viper.GetString("MUSIC_DIRECTORY")) + path
|
2021-12-22 08:49:37 -07:00
|
|
|
extractedImage = ExtractImageFromFile(absPath, imagePath)
|
2022-03-15 15:18:53 -06:00
|
|
|
if extractedImage == viper.GetString("DEFAULT_IMAGE_PATH") &&
|
|
|
|
viper.GetString("GET_COVER_ART_FROM_LAST_FM") == "TRUE" {
|
2021-11-20 09:45:10 -07:00
|
|
|
downloadedImage, err := getImageFromLastFM(a[0]["artist"], a[0]["album"], imagePath)
|
|
|
|
if err == nil {
|
2021-12-22 08:11:48 -07:00
|
|
|
Notify.Send("Image From LastFM")
|
2021-11-20 09:45:10 -07:00
|
|
|
extractedImage = downloadedImage
|
2021-11-26 09:32:20 -07:00
|
|
|
} else {
|
2021-12-22 08:11:48 -07:00
|
|
|
Notify.Send("Falling Back to Default Image.")
|
2021-11-20 09:45:10 -07:00
|
|
|
}
|
2021-11-28 11:08:52 -07:00
|
|
|
} else {
|
2021-12-22 08:11:48 -07:00
|
|
|
Notify.Send("Extracted Image Successfully")
|
2021-11-20 09:45:10 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return extractedImage
|
|
|
|
}
|
2021-12-12 13:13:20 -07:00
|
|
|
|
|
|
|
func GetImg(uri string) (image.Image, error) {
|
|
|
|
f, err := os.Open(uri)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
img, _, err := image.Decode(f)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
fw, fh := utils.GetFontWidth()
|
|
|
|
img = resize.Resize(
|
2022-03-15 15:18:53 -06:00
|
|
|
uint(float32(ui.ImgW)*(fw+float32(viper.GetFloat64("IMAGE_WIDTH_EXTRA_X")))),
|
|
|
|
uint(float32(ui.ImgH)*(fh+float32(viper.GetFloat64("IMAGE_WIDTH_EXTRA_Y")))),
|
2021-12-12 13:13:20 -07:00
|
|
|
img,
|
|
|
|
resize.Bilinear,
|
|
|
|
)
|
|
|
|
return img, nil
|
|
|
|
}
|