Browse Source

fix: First art display

Matthias Ladkau 4 years ago
parent
commit
b0480867c7

+ 42 - 5
musikautomat/display.py

@@ -1,13 +1,17 @@
 #!./bin/python3
 
+import os
+
 import yaml
 import pygame # type: ignore
 from typing import Dict
+import random
 
 # Color constants
 
 BLACK = (0, 0, 0)
 GREEN = (0, 255, 0)
+WHITE = (255, 255, 255)
 
 class Display:
     '''
@@ -29,6 +33,7 @@ class Display:
         self._selection_pointer = 0 # Current selected value
         self._line_offset = 0 # Drawing offset in playlist
         self._current_item = None
+        self._bgImg = None
 
 
     def getPlaylist(self) -> Dict:
@@ -72,12 +77,12 @@ class Display:
 
             if drawline == self._selection_pointer:
                 text = font.render(item["name"], True, BLACK, GREEN)
-                img = item.get("img")
 
-                if img is not None:
-                    pass
-                else:
-                    pygame.draw.rect(self._pydsp, GREEN, (0,0,self._dx,50))
+                pygame.draw.rect(self._pydsp, BLACK, (0,0,self._dx,50))
+                try:
+                    self.drawArt(item)
+                except Exception as e:
+                    print("Error while drawing art:", e)
 
                 self._current_item = item
             else:
@@ -91,6 +96,38 @@ class Display:
             if drawline > self._drawlines:
                 break
 
+    def drawArt(self, item):
+        '''
+        Beautify the display.
+        '''
+        font = pygame.font.Font('freesansbold.ttf', self._fontsize)
+
+        # Draw a background image
+
+        if self._bgImg is None:
+            bg = os.listdir("web/background")
+            bg = bg[random.randint(0, len(bg)-1)]
+            self._bgImg = pygame.image.load(os.path.join("web/background", bg))
+            pygame.transform.scale(self._bgImg, (self._dx, 50))
+
+        self._pydsp.blit(self._bgImg, (0,0), (0, 0, self._dx, 50))
+
+        path = item.get("path")
+        title = item.get("name", "")
+        artist = item.get("artist", "")
+
+        # Try to extract meta data
+
+        if path is not None:
+            pass
+
+        s = title
+        if artist != "":
+            s = "%s (%s)" % (s, artist)
+
+        text = font.render(s, True, WHITE, BLACK)
+        self._pydsp.blit(text, (0, 38))
+
 
     def currentItem(self) -> Dict:
         '''

+ 1 - 1
musikautomat/main.py

@@ -27,7 +27,7 @@ pygame.init()
 
 # Create the display
 
-pydsp = pygame.display.set_mode((dx, dy), 0, 32)
+pydsp = pygame.display.set_mode((dx, dy))
 pygame.display.set_caption("Musikautomat")
 pygame.mouse.set_visible(False)
 pygame.key.set_repeat(400, 100)

+ 7 - 3
musikautomat/musikautomat.py

@@ -71,7 +71,6 @@ class Musikautomat:
             clk.tick(10) # We only need 10 FPS
 
             try:
-
                 for event in pygame.event.get():
 
                     # Handle exit event
@@ -114,6 +113,7 @@ class Musikautomat:
 
                 s = str(e)
                 n = int(self._dx / 6)
+
                 for i, start in enumerate(range(0, len(s), n)):
                     text = font.render(s[start:start+n], True, (255, 0, 0) , (0, 0, 0))
                     self._pydsp.blit(text, (0, i * 12))
@@ -127,7 +127,7 @@ class Musikautomat:
         '''
         Play an item.
         '''
-        print("Playing:", item)
+        print("Player data:", item)
 
         self._eventSink = self._player
 
@@ -148,8 +148,12 @@ class Musikautomat:
         path = item.get("path")
         files = os.listdir(path)
 
+        def toDisplayName(s):
+            s = os.path.splitext(s)[0]
+            return s.replace("_", " ").title()
+
         self._player.setPlaylist(sorted([{
-            "name" : f,
+            "name" : toDisplayName(f),
             "path" : os.path.join(path, f)
         } for f in files], key=lambda i: i["name"]))
 

+ 13 - 2
musikautomat/player.py

@@ -3,6 +3,7 @@
 import time
 import threading
 
+import pygame # type: ignore
 from display import Display
 from typing import Dict
 from pymplb import MPlayer
@@ -20,6 +21,7 @@ class Player(Display):
         }, pydsp)
 
         self.playing = False # Flag if the player is playing
+        self._playingItem = None
         self._playingPlaylist = None # Current playing order
 
         self._data = [] # Current playlist
@@ -32,6 +34,8 @@ class Player(Display):
         '''
         Thread to update the player display from the state of mplayer.
         '''
+        font = pygame.font.Font('freesansbold.ttf', int(self._fontsize))
+
         while True:
 
             try:
@@ -43,7 +47,7 @@ class Player(Display):
                     # Update playlist display if the current song has changed
                     # and the item can be found in the current playlist
 
-                    if player_playing != self._current_item.get("path"):
+                    if player_playing != self._playingItem.get("path"):
 
                         for i, item in enumerate(self._data):
                             if item.get("path") == player_playing:
@@ -65,6 +69,7 @@ class Player(Display):
         '''
         Set the playlist for this player.
         '''
+        self._bgImg = None
         self._selection_pointer = 0
         self._data = data
 
@@ -82,19 +87,25 @@ class Player(Display):
             return
 
         if self._player.p_path == self._current_item["path"]:
-            print("stop")
+
+            # Stop what is currently playing
+
             self.playing = False
             self._player.stop()
+
         else:
             ci = self._current_item["path"]
 
             self._playingPlaylist = []
 
+            # Build up the playlist in mplayer in the correct order
+
             add = 0
             for item in self._data:
 
                 if item == self._current_item:
                     self.playing = True
+                    self._playingItem = item
                     self._player.loadfile(item["path"])
                     self._playingPlaylist.append(item)
                     add = 1

BIN
musikautomat/web/background/purple.jpg