Browse Source

feat: Adding support for m3u and stream playlists

Matthias Ladkau 4 years ago
parent
commit
47ae12af24

BIN
models/button.blend


+ 3 - 0
musikautomat/config.yml

@@ -10,3 +10,6 @@ playlist: music.yml
 
 # Fontsize for drawing
 fontsize: 12
+
+# Auto start the first item
+autostart: false

+ 20 - 0
musikautomat/mock_rpi_gpio.py

@@ -0,0 +1,20 @@
+#!./bin/python3
+
+# Mock GPIO module for non-Raspberry PI platforms
+
+BOARD=1
+IN=1
+PUD_DOWN=1
+PUD_UP=2
+
+def setmode(mode):
+    pass
+
+def setwarnings(mode):
+    pass
+
+def setup(pin, mode, pull_up_down=None):
+    pass
+
+def input(pin):
+    return 1

+ 4 - 43
musikautomat/music.yml

@@ -1,49 +1,10 @@
 playlist:
   - name: Domemucke
     type: stream
-    url: bla
+    url: http://web:gemasuxx@devt.de:5051/pleasuredome
   - name: Aerosmith
     type: dir
     path: /home/ml/Music/aerosmith
-  - name: ACDC
-    type: dir
-    path: /home/ml/Music/acdc
-  - name: TesT
-    type: dir
-    path: /home/ml/Music/test
-  - name: Pantera1
-    type: dir
-    path: foo
-  - name: Pantera2
-    type: dir
-    path: foo
-  - name: Pantera3
-    type: dir
-    path: foo
-  - name: Pantera4
-    type: dir
-    path: foo
-  - name: Pantera5
-    type: dir
-    path: foo
-  - name: Pantera6
-    type: dir
-    path: foo
-  - name: Pantera7
-    type: dir
-    path: foo
-  - name: Pantera8
-    type: dir
-    path: foo
-  - name: Pantera9
-    type: dir
-    path: foo
-  - name: Pantera10
-    type: dir
-    path: foo
-  - name: Pantera11
-    type: dir
-    path: foo
-  - name: Pantera12
-    type: dir
-    path: foo
+  - name: Good Stuff
+    type: m3u
+    path: /home/ml/Music/goodstuff.m3u

+ 49 - 6
musikautomat/musikautomat.py

@@ -11,7 +11,10 @@ import pygame # type: ignore
 import tornado.ioloop, tornado.web
 import asyncio
 
-import RPi.GPIO as GPIO # Support for Raspberry PI GPIO input
+try:
+    import RPi.GPIO as GPIO # Support for Raspberry PI GPIO input
+except:
+    import mock_rpi_gpio as GPIO
 
 from display import Display
 from player import Player
@@ -38,6 +41,7 @@ class Musikautomat:
 
         # Initialise state objects
 
+        self._autostart = config.get("autostart")
         self._pydsp = pydsp
         self._display = Display(config, self._pydsp)
         self._display.update()
@@ -95,6 +99,10 @@ class Musikautomat:
         clk = pygame.time.Clock()
         gpio_event_timer = 0
 
+        if self._autostart:
+            self.eventRight()
+            self.eventRight()
+
         while True:
             clk.tick(10) # We only need 10 FPS
 
@@ -154,6 +162,9 @@ class Musikautomat:
 
             pygame.display.update()
 
+    def toDisplayName(self, s):
+        s = os.path.splitext(s)[0]
+        return s.replace("_", " ").title()
 
     def play(self, item, start=False):
         '''
@@ -166,6 +177,10 @@ class Musikautomat:
         t = item.get("type")
         if t == "dir":
             self.playDir(item)
+        elif t == "m3u":
+            self.playM3U(item)
+        elif t == "stream":
+            self.playStream(item)
         else:
             raise Exception("Unknown type: %s" % t)
 
@@ -173,6 +188,18 @@ class Musikautomat:
             self._player.togglePlay()
 
 
+    def playStream(self, item):
+        '''
+        Play a stream.
+        '''
+        self._player.setPlaylist([{
+            "name" : item["name"],
+            "path" : item["url"]
+        }])
+
+        self._player.update()
+
+
     def playDir(self, item):
         '''
         Play all files in a directory.
@@ -180,13 +207,29 @@ 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" : toDisplayName(f),
+            "name" : self.toDisplayName(f),
             "path" : os.path.join(path, f)
         } for f in files], key=lambda i: i["name"]))
 
         self._player.update()
+
+
+    def playM3U(self, item):
+        '''
+        Play all files of a M3U playlist.
+        '''
+        path = item.get("path")
+        items = []
+
+        with open(path) as f:
+            for line in f:
+                item_path = str(line).strip()
+                if not item_path.startswith("#"):
+                    items.append({
+                        "name" : self.toDisplayName(os.path.basename(item_path)),
+                        "path" : os.path.join(os.path.dirname(path), item_path)
+                    })
+
+        self._player.setPlaylist(items)
+        self._player.update()

+ 1 - 1
musikautomat/requirements.txt

@@ -5,7 +5,7 @@ pkg-resources==0.0.0
 pygame==2.0.0.dev6
 python-magic==0.4.15
 PyYAML==5.1.2
-RPi.GPIO==0.7.0
+RPi.GPIO==0.7.0; platform_machine == 'armv7l'
 six==1.13.0
 tornado==6.0.3
 typed-ast==1.4.0