Changeset - 90792e984249
[Not reviewed]
default
0 2 1
drewp@bigasterisk.com - 8 months ago 2024-05-25 22:41:27
drewp@bigasterisk.com
isolate import warnings to one file
3 files changed with 7 insertions and 5 deletions:
0 comments (0 inline, 0 general)
src/light9/ascoltami/import_gst.py
Show inline comments
 
new file 100644
 
import gi
 

	
 
gi.require_version('Gst', '1.0')
 
gi.require_version('Gtk', '3.0')
 
from gi.repository import Gst  # type: ignore # noqa: E402, F401
src/light9/ascoltami/main.py
Show inline comments
 
#!bin/python
 
import logging
 
import optparse
 
import sys
 
from typing import cast
 

	
 
import gi
 
from rdflib import URIRef
 
from starlette.applications import Starlette
 
from starlette.routing import Route
 
from starlette_exporter import PrometheusMiddleware, handle_metrics
 
from twisted.internet import reactor
 
from twisted.internet.interfaces import IReactorCore
 

	
 
from light9.run_local import log
 

	
 
gi.require_version('Gst', '1.0')
 
gi.require_version('Gtk', '3.0')
 

	
 
from gi.repository import Gst  # type: ignore
 

	
 
from light9 import networking, showconfig
 
from light9.ascoltami import webapp
 
from light9.ascoltami.player import Player
 
from light9.ascoltami.import_gst import Gst
 
from light9.ascoltami.playlist import NoSuchSong, Playlist
 

	
 
reactor = cast(IReactorCore, reactor)
 

	
 

	
 
class Ascoltami:
 

	
 
    def __init__(self, graph, show):
 
        self.graph = graph
 
        self.player = Player(onEOS=self.onEOS, autoStopOffset=0)
 
        self.show = show
 
        self.playlist = Playlist.fromShow(graph, show)
 

	
 
    def onEOS(self, song):
 
        self.player.pause()
 
        self.player.seek(0)
 

	
 
        thisSongUri = webapp.songUri(self.graph, URIRef(song))
 

	
 
        try:
 
            nextSong = self.playlist.nextSong(thisSongUri)
 
        except NoSuchSong:  # we're at the end of the playlist
 
            return
 

	
 
        self.player.setSong(webapp.songLocation(self.graph, nextSong), play=False)
 

	
 

	
 
def main():
 
    logging.getLogger('sse_starlette.sse').setLevel(logging.INFO)
 
    Gst.init(None)
 

	
 
    graph = showconfig.getGraph()
 
    asco = Ascoltami(graph, showconfig.showUri())
 

	
 
    app = Starlette(
 
        debug=True,
 
        routes=[
 
            Route("/config", webapp.get_config),
 
            Route("/time", webapp.get_time, methods=["GET"]),
 
            Route("/time", webapp.post_time, methods=["POST"]),
 
            Route("/time/stream", webapp.timeStream),
 
            Route("/song", webapp.post_song, methods=["POST"]),
 
            Route("/songs", webapp.get_songs),
 
            Route("/seekPlayOrPause", webapp.post_seekPlayOrPause),
 
            Route("/output", webapp.post_output, methods=["POST"]),
 
            Route("/go", webapp.post_goButton, methods=["POST"]),
 
        ],
 
    )
src/light9/ascoltami/player.py
Show inline comments
 
"""
 
alternate to the mpd music player, for ascoltami
 
"""
 

	
 
import logging
 
import time
 
import traceback
 
from dataclasses import dataclass
 
from pathlib import Path
 

	
 
from gi.repository import Gst
 
from rdflib import URIRef
 
from twisted.internet import task
 
from light9.ascoltami.import_gst import Gst  # type: ignore
 
from light9.metrics import metrics
 
log = logging.getLogger()
 

	
 

	
 

	
 
class Player:
 

	
 
    def __init__(self, autoStopOffset=4, onEOS=None):
 
        """autoStopOffset is the number of seconds before the end of
 
        song before automatically stopping (which is really pausing).
 
        onEOS is an optional function to be called when we reach the
 
        end of a stream (for example, can be used to advance the song).
 
        It is called with one argument which is the URI of the song that
 
        just finished."""
 
        self.autoStopOffset = autoStopOffset
 
        self.playbin = self.pipeline = Gst.ElementFactory.make('playbin', None)
 

	
 
        self._playStartTime = 0
 
        self._lastWatchTime = 0
 
        self._autoStopTime = 0
 
        self._lastSetSongUri = None
 
        self._onEOS = onEOS
 

	
 
        task.LoopingCall(self.watchTime).start(.050)
 

	
 
        #bus = self.pipeline.get_bus()
 
        # not working- see notes in pollForMessages
 
        #self.watchForMessages(bus)
 

	
 
    def watchTime(self):
 
        try:
 
            self.pollForMessages()
 

	
 
            t = self.currentTime()
 
            log.debug("watch %s < %s < %s", self._lastWatchTime, self._autoStopTime, t)
 
            if self._lastWatchTime < self._autoStopTime < t:
 
                log.info("autostop")
 
                self.pause()
 

	
 
            self._lastWatchTime = t
 
        except Exception:
 
            traceback.print_exc()
 

	
 
    def watchForMessages(self, bus):
 
        """this would be nicer than pollForMessages but it's not working for
 
        me. It's like add_signal_watch isn't running."""
 
        bus.add_signal_watch()
 

	
0 comments (0 inline, 0 general)