2024-07-20 17:09:22 +01:00
|
|
|
import threading
|
2024-07-20 21:18:41 +01:00
|
|
|
|
|
|
|
# from generate import generate
|
2024-07-20 21:47:57 +01:00
|
|
|
from contextlib import asynccontextmanager
|
2024-07-20 17:09:22 +01:00
|
|
|
from fastapi import FastAPI
|
|
|
|
from fastapi.responses import FileResponse
|
|
|
|
from fastapi.staticfiles import StaticFiles
|
|
|
|
|
2024-07-20 21:47:57 +01:00
|
|
|
# the index of the current audio track from 0 to 9
|
2024-07-20 21:18:41 +01:00
|
|
|
current_index = -1
|
2024-07-20 21:47:57 +01:00
|
|
|
# the timer that periodically advances the current audio track
|
|
|
|
t = None
|
|
|
|
|
|
|
|
|
|
|
|
@asynccontextmanager
|
|
|
|
async def lifespan(app: FastAPI):
|
|
|
|
advance()
|
|
|
|
yield
|
|
|
|
if t:
|
|
|
|
t.cancel()
|
2024-07-20 17:09:22 +01:00
|
|
|
|
|
|
|
|
|
|
|
def advance():
|
2024-07-20 21:47:57 +01:00
|
|
|
global current_index, t
|
2024-07-20 17:09:22 +01:00
|
|
|
|
|
|
|
# if current_index == 0:
|
|
|
|
# generate(offset=5)
|
|
|
|
# elif current_index == 5:
|
|
|
|
# generate(offset=0)
|
|
|
|
|
|
|
|
if current_index == 9:
|
|
|
|
current_index = 0
|
|
|
|
else:
|
|
|
|
current_index = current_index + 1
|
|
|
|
|
2024-07-20 21:18:41 +01:00
|
|
|
print(f"advancing, current index {current_index}")
|
|
|
|
|
2024-07-20 17:09:22 +01:00
|
|
|
t = threading.Timer(60, advance)
|
|
|
|
t.start()
|
|
|
|
|
|
|
|
|
2024-07-20 21:47:57 +01:00
|
|
|
app = FastAPI(lifespan=lifespan)
|
2024-07-20 21:18:41 +01:00
|
|
|
|
|
|
|
|
|
|
|
@app.get("/current.mp3")
|
|
|
|
def get_current_audio():
|
|
|
|
print("hello")
|
|
|
|
return FileResponse(f"{current_index}.mp3")
|
|
|
|
|
|
|
|
|
|
|
|
app.mount("/", StaticFiles(directory="web", html=True), name="web")
|