# HG changeset patch # User drewp@bigasterisk.com # Date 2024-05-30 08:08:07 # Node ID 06da5db2fafe6ea81a7f99a41eab4d7ac0ae1d5d # Parent f2b3cfcc23d3d9fdb29ed634b37ffa95790b982e rewrite ascoltami to use the graph for more playback data diff --git a/src/light9/ascoltami/main.py b/src/light9/ascoltami/main.py --- a/src/light9/ascoltami/main.py +++ b/src/light9/ascoltami/main.py @@ -1,8 +1,6 @@ -#!bin/python import logging from typing import cast -import louie from rdfdb.syncedgraph.syncedgraph import SyncedGraph from rdflib import ConjunctiveGraph, Literal, URIRef from rdflib.graph import _ContextType @@ -19,6 +17,7 @@ from light9.namespaces import L9 from light9.newtypes import decimalLiteral from light9.run_local import log + class Ascoltami: def __init__(self, graph: SyncedGraph, show: URIRef): @@ -31,10 +30,7 @@ class Ascoltami: self.playlist = Playlist(graph, show) def onStateChange(self, s: PlayerState): - log.info('louie send') - louie.send(webapp.OnStateChange, s=s) g = self.stateAsGraph(s) - self.graph.patchSubgraph(newGraph=g, context=self.ctx) self.playerState = s @@ -58,19 +54,6 @@ class Ascoltami: def getPlayerState(self) -> PlayerState: return self.playerState - 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) @@ -84,9 +67,7 @@ def main(): debug=True, routes=[ Route("/config", h.get_config), - Route("/time", h.get_time, methods=["GET"]), Route("/time", h.post_time, methods=["POST"]), - Route("/time/stream", h.timeStream), Route("/song", h.post_song, methods=["POST"]), Route("/songs", h.get_songs), Route("/seekPlayOrPause", h.post_seekPlayOrPause), diff --git a/src/light9/ascoltami/player.py b/src/light9/ascoltami/player.py --- a/src/light9/ascoltami/player.py +++ b/src/light9/ascoltami/player.py @@ -30,6 +30,8 @@ class PlayerState: pausedSongTime: float | None = None # if we're paused, this has the song time endOfSong: bool = False # True if we're in the stopped state due to EOS +def roundTime(secs: float) -> float: + return round(secs, 2) class Player: @@ -69,10 +71,10 @@ class Player: playing = self.isPlaying() and not eos ps = PlayerState( song=self._getSongFileUri(), - duration=round(self.duration(), 2), - wallStartTime=round(now - t, 2) if playing else None, + duration=roundTime(self.duration()), + wallStartTime=roundTime(now - t) if playing else None, playing=playing, - pausedSongTime=None if playing else t, + pausedSongTime=None if playing else roundTime(t), endOfSong=eos, ) diff --git a/src/light9/ascoltami/webapp.py b/src/light9/ascoltami/webapp.py --- a/src/light9/ascoltami/webapp.py +++ b/src/light9/ascoltami/webapp.py @@ -1,20 +1,15 @@ """ this module shouldn't be necessary for playback to work """ -import asyncio -import json import logging import socket import subprocess -from dataclasses import dataclass, field -import time +from dataclasses import dataclass from typing import Callable from typing import Literal as Lit -import louie from rdfdb.syncedgraph.syncedgraph import SyncedGraph from rdflib import URIRef -from sse_starlette.sse import EventSourceResponse from starlette.requests import Request from starlette.responses import JSONResponse, PlainTextResponse @@ -25,10 +20,6 @@ from light9.showconfig import showUri log = logging.getLogger("web") -class OnStateChange: - pass - - @dataclass class PlayerState2(PlayerState): song2: URIRef | None = None @@ -55,26 +46,6 @@ class WebHandlers: 'post': 0 })) - def currentState(self, player: Player, playlist: Playlist) -> PlayerState2: - if player.isAutostopped(): - nextAction = 'finish' - elif player.isPlaying(): - nextAction = 'disabled' - else: - nextAction = 'play' - - ps = self.getPlayerState() - return PlayerState2( - song2=playlist.songUri((ps.song)) if ps.song else None, - duration=ps.duration, - playing=ps.playing, - # state= player.states(), - nextAction=nextAction, - ) - - async def get_time(self, request: Request) -> JSONResponse: - return JSONResponse({'t': self.player.currentTime()}) - async def post_time(self, request: Request) -> PlainTextResponse: """ post a json object with {pause: true} or {resume: true} if you @@ -90,40 +61,6 @@ class WebHandlers: self.player.seek(params['t']) return PlainTextResponse("ok") - async def timeStream(self, request: Request): - - async def event_generator(): - last_sent = None - last_sent_time = 0.0 - - def onStateChange(s: PlayerState2): - log.info('ws heanndlerr gets state') - - louie.connect(onStateChange, OnStateChange, weak=False) - - try: - while True: - now = time.time() - msg = self.currentState(self.player, self.playlist) - if msg != last_sent or now > last_sent_time + 2: - event_data = json.dumps({ - # obsolete- watch the graph for these - 'duration': msg.duration, - 'playing': msg.playing, - 'song': self.playlist.songUri(msg.song) if msg.song else None, - 'state': {}, - }) - yield event_data - last_sent = msg - last_sent_time = now - - await asyncio.sleep(0.1) - finally: - log.info(f'bye listnner {event_generator}') - louie.disconnect(onStateChange, OnStateChange, weak=False) - - return EventSourceResponse(event_generator()) - async def get_songs(self, request: Request) -> JSONResponse: songs_data = [ @@ -137,7 +74,7 @@ class WebHandlers: return JSONResponse({"songs": songs_data}) async def post_song(self, request: Request) -> PlainTextResponse: - """post a uri of song to switch to (and start playing)""" + """post a uri of song to switch to (and seek to 0)""" song_uri = URIRef((await request.body()).decode('utf8')) self.player.setSong(self.playlist.fileUri(song_uri)) diff --git a/web/Light9CursorCanvas.ts b/web/Light9CursorCanvas.ts --- a/web/Light9CursorCanvas.ts +++ b/web/Light9CursorCanvas.ts @@ -3,6 +3,7 @@ import { css, html, LitElement, Property import { customElement, property } from "lit/decorators.js"; import Sylvester from "sylvester"; import { line } from "./drawing"; +import { Vector } from "./lib/sylvester"; const $V = Sylvester.Vector.create; diff --git a/web/ascoltami/Light9AscoltamiTimeline.ts b/web/ascoltami/Light9AscoltamiTimeline.ts new file mode 100644 --- /dev/null +++ b/web/ascoltami/Light9AscoltamiTimeline.ts @@ -0,0 +1,114 @@ +import { css, html, LitElement, PropertyValueMap } from "lit"; +import { customElement, property, state } from "lit/decorators.js"; +import Sylvester from "sylvester"; +import { Zoom } from "../light9-timeline-audio"; +import { PlainViewState } from "../Light9CursorCanvas"; +import { getTopGraph } from "../RdfdbSyncedGraph"; +import { show } from "../show_specific"; +import { SyncedGraph } from "../SyncedGraph"; +import { PlayerState } from "./PlayerState"; +export { Light9TimelineAudio } from "../light9-timeline-audio"; +export { Light9CursorCanvas } from "../Light9CursorCanvas"; +export { RdfdbSyncedGraph } from "../RdfdbSyncedGraph"; +export { ResourceDisplay } from "../ResourceDisplay"; + +const $V = Sylvester.Vector.create; + +async function postJson(url: string, jsBody: Object) { + return fetch(url, { + method: "POST", + headers: { "Content-Type": "applcation/json" }, + body: JSON.stringify(jsBody), + }); +} + +@customElement("light9-ascoltami-timeline") +export class Light9AscoltamiTimeline extends LitElement { + static styles = [ + css` + .timeRow { + margin: 14px; + position: relative; + } + #overview { + height: 60px; + } + #zoomed { + margin-top: 40px; + height: 80px; + } + #cursor { + position: absolute; + left: 0; + top: 0; + width: 100%; + height: 100%; + } + `, + ]; + graph!: SyncedGraph; + @property() playerState: PlayerState = { + duration: null, + endOfSong: null, + pausedSongTime: null, + playing: null, + song: null, + wallStartTime: null, + }; + @property() playerTime: number = 0; + @state() zoom: Zoom; + @state() overviewZoom: Zoom; + @state() viewState: PlainViewState | null = null; + constructor() { + super(); + getTopGraph().then((g) => { + this.graph = g; + }); + this.zoom = this.overviewZoom = { duration: null, t1: 0, t2: 1 }; + } + protected willUpdate(_changedProperties: PropertyValueMap): void { + super.willUpdate(_changedProperties); + if ((_changedProperties.has("playerState") || _changedProperties.has("playerTime")) && this.playerState !== null) { + const duration = this.playerState.duration; + const t = this.playerTime; + if (duration !== null) { + const timeRow = this.shadowRoot!.querySelector(".timeRow") as HTMLDivElement; + if (timeRow != null) { + this.updateZooms(duration, t, timeRow); + } + } + } + } + + updateZooms(duration: number, t: number, timeRow: HTMLDivElement) { + this.overviewZoom = { duration: duration, t1: 0, t2: duration }; + const t1 = t - 2; + const t2 = t + 20; + this.zoom = { duration: duration, t1, t2 }; + const w = timeRow.offsetWidth; + this.viewState = { + zoomSpec: { t1: () => t1, t2: () => t2 }, + cursor: { t: () => t }, + audioY: () => 0, + audioH: () => 60, + zoomedTimeY: () => 60, + zoomedTimeH: () => 40, + fullZoomX: (sec: number) => (sec / duration) * w, + zoomInX: (sec: number) => ((sec - t1) / (t2 - t1)) * w, + mouse: { pos: () => $V([0, 0]) }, + }; + } + + render() { + const song = this.playerState?.song; + if (!song) return html`(spectrogram)`; + return html` +
+
+ + + +
+ `; + } +} diff --git a/web/ascoltami/Light9AscoltamiUi.ts b/web/ascoltami/Light9AscoltamiUi.ts --- a/web/ascoltami/Light9AscoltamiUi.ts +++ b/web/ascoltami/Light9AscoltamiUi.ts @@ -1,27 +1,19 @@ import debug from "debug"; -import { css, html, LitElement } from "lit"; +import { css, html, LitElement, PropertyValues } from "lit"; import { customElement, property } from "lit/decorators.js"; -import { classMap } from "lit/directives/class-map.js"; import { NamedNode } from "n3"; -import Sylvester from "sylvester"; -import { Zoom } from "../light9-timeline-audio"; import { PlainViewState } from "../Light9CursorCanvas"; import { getTopGraph } from "../RdfdbSyncedGraph"; import { SyncedGraph } from "../SyncedGraph"; -import { TimingUpdate } from "./main"; -import { showRoot } from "../show_specific"; -export { Light9TimelineAudio } from "../light9-timeline-audio"; -export { Light9CursorCanvas } from "../Light9CursorCanvas"; +import { PlayerState } from "./PlayerState"; export { RdfdbSyncedGraph } from "../RdfdbSyncedGraph"; export { ResourceDisplay } from "../ResourceDisplay"; -const $V = Sylvester.Vector.create; +export { Light9AscoltamiTimeline } from "./Light9AscoltamiTimeline"; +export { Light9SongListing } from "./Light9SongListing"; debug.enable("*"); const log = debug("asco"); -function byId(id: string): HTMLElement { - return document.getElementById(id)!; -} async function postJson(url: string, jsBody: Object) { return fetch(url, { method: "POST", @@ -29,157 +21,154 @@ async function postJson(url: string, jsB body: JSON.stringify(jsBody), }); } + @customElement("light9-ascoltami-ui") export class Light9AscoltamiUi extends LitElement { graph!: SyncedGraph; - times!: { intro: number; post: number }; - @property() nextText: string = ""; - @property() isPlaying: boolean = false; @property() show: NamedNode | null = null; @property() song: NamedNode | null = null; @property() selectedSong: NamedNode | null = null; - @property() currentDuration: number = 0; - @property() zoom: Zoom; - @property() overviewZoom: Zoom; @property() viewState: PlainViewState | null = null; + @property() host: any; + @property() playerState: PlayerState = { duration: null, endOfSong: null, pausedSongTime: null, playing: null, song: null, wallStartTime: null }; + @property() playerTime: number = 0; static styles = [ css` :host { display: flex; flex-direction: column; } - .timeRow { - margin: 14px; - position: relative; - } - #overview { - height: 60px; - } - #zoomed { - margin-top: 40px; - height: 80px; - } - #cursor { - position: absolute; - left: 0; - top: 0; - width: 100%; - height: 100%; - } - #grow { - flex: 1 1 auto; - display: flex; - } - #grow > span { - display: flex; - position: relative; - width: 50%; + + .keyCap { + color: #ccc; + background: #525252; + display: inline-block; + border: 1px outset #b3b3b3; + padding: 2px 3px; + margin: 3px 0; + margin-left: 0.4em; + font-size: 16px; + box-shadow: 0.9px 0.9px 0px 2px #565656; + border-radius: 2px; } - #playSelected { - height: 100px; - } - #songList { - overflow-y: scroll; - position: absolute; - left: 0; - top: 0; - right: 0; - bottom: 0; + + button { + min-height: 48pt; + min-width: 65pt; } - #songList .row { - width: 60%; - min-height: 40px; - text-align: left; - position: relative; - } - #songList .row:nth-child(even) { - background: #333; + + #mainRow { + display: flex; + flex-direction: row; } - #songList .row:nth-child(odd) { - background: #444; + + light9-song-listing { + flex-grow: 1; } - #songList button { - min-height: 40px; - margin-bottom: 10px; - } - #songList .row.playing { - box-shadow: 0 0 30px red; - background-color: #de5050; + + th { + text-align: right; } `, ]; - render() { - return html` + + constructor() { + super(); + getTopGraph().then((g) => { + this.graph = g; + this.graph.runHandler(this.updatePlayState.bind(this), "playstate-ui"); + }); + setInterval(this.updateT.bind(this), 100); + } + + protected async firstUpdated(_changedProperties: PropertyValues) { + this.bindKeys(); + const config = await (await fetch("/service/ascoltami/config")).json(); + document.title = document.title.replace("{{host}}", config.host); + this.host = config.host; + } - + updatePlayState() { + const U = this.graph.U(); + const asco = U(":ascoltami"); + this.playerState = { + duration: this.graph.optionalFloatValue(asco, U(":duration")), + endOfSong: this.graph.optionalBooleanValue(asco, U(":endOfSong")), + pausedSongTime: this.graph.optionalFloatValue(asco, U(":pausedSongTime")), + wallStartTime: this.graph.optionalFloatValue(asco, U(":wallStartTime")), + playing: this.graph.optionalBooleanValue(asco, U(":playing")), + song: this.graph.optionalUriValue(asco, U(":song")), + }; + this.updateT(); + } - + updateT() { + if (this.playerState.wallStartTime !== null) { + this.playerTime = Date.now() / 1000 - this.playerState.wallStartTime; + } else if (this.playerState.pausedSongTime !== null) { + this.playerTime = this.playerState.pausedSongTime; + } else { + this.playerTime = 0; + } + } + + render() { + return html` +

ascoltami on ${this.host}

-
- -
- - ${this.songList.map( - (song) => html` - - - - - ` - )} -
- -
-
-