2020-11-01 02:35:14 +00:00
|
|
|
"""
|
|
|
|
BAPSicle Server
|
|
|
|
Next-gen audio playout server for University Radio York playout,
|
|
|
|
based on WebStudio interface.
|
|
|
|
|
|
|
|
Audio Player
|
|
|
|
|
|
|
|
Authors:
|
|
|
|
Matthew Stratford
|
|
|
|
Michael Grace
|
|
|
|
|
|
|
|
Date:
|
|
|
|
October, November 2020
|
|
|
|
"""
|
|
|
|
|
2020-10-30 19:31:18 +00:00
|
|
|
# This is the player. Reliability is critical here, so we're catching
|
|
|
|
# literally every exception possible and handling it.
|
|
|
|
|
|
|
|
# It is key that whenever the parent server tells us to do something
|
|
|
|
# that we respond with something, FAIL or OKAY. The server doesn't like to be kept waiting.
|
|
|
|
|
2020-12-19 14:57:37 +00:00
|
|
|
from helpers.types import PlayerState, RepeatMode
|
2020-10-29 21:23:37 +00:00
|
|
|
from queue import Empty
|
|
|
|
import multiprocessing
|
|
|
|
import setproctitle
|
|
|
|
import copy
|
|
|
|
import json
|
|
|
|
import time
|
2020-11-01 00:31:58 +00:00
|
|
|
import sys
|
2020-11-03 22:52:58 +00:00
|
|
|
|
2020-12-19 14:57:37 +00:00
|
|
|
from typing import Any, Callable, Dict, List, Optional
|
2020-11-03 22:52:58 +00:00
|
|
|
|
2020-11-05 23:53:27 +00:00
|
|
|
from plan import PlanItem
|
2020-11-03 22:52:58 +00:00
|
|
|
|
2020-10-30 22:15:51 +00:00
|
|
|
# Stop the Pygame Hello message.
|
|
|
|
import os
|
|
|
|
os.environ['PYGAME_HIDE_SUPPORT_PROMPT'] = "hide"
|
2020-10-29 21:23:37 +00:00
|
|
|
from pygame import mixer
|
2020-10-25 01:23:24 +00:00
|
|
|
from mutagen.mp3 import MP3
|
2020-10-30 22:15:51 +00:00
|
|
|
|
2020-11-05 23:53:27 +00:00
|
|
|
from helpers.myradio_api import MyRadioAPI
|
2020-11-05 19:06:16 +00:00
|
|
|
from helpers.os_environment import isMacOS
|
2020-10-30 22:06:03 +00:00
|
|
|
from helpers.state_manager import StateManager
|
|
|
|
from helpers.logging_manager import LoggingManager
|
2020-10-23 20:10:32 +00:00
|
|
|
|
2020-11-01 02:44:35 +00:00
|
|
|
|
2020-10-24 20:31:52 +00:00
|
|
|
class Player():
|
|
|
|
state = None
|
|
|
|
running = False
|
2020-10-30 00:32:34 +00:00
|
|
|
out_q = None
|
|
|
|
last_msg = None
|
2020-10-30 22:06:03 +00:00
|
|
|
logger = None
|
2020-10-24 20:31:52 +00:00
|
|
|
|
|
|
|
__default_state = {
|
2020-10-30 00:32:34 +00:00
|
|
|
"initialised": False,
|
2020-11-01 02:35:14 +00:00
|
|
|
"loaded_item": None,
|
2020-10-24 20:31:52 +00:00
|
|
|
"channel": -1,
|
|
|
|
"playing": False,
|
2020-10-30 19:31:18 +00:00
|
|
|
"paused": False,
|
2020-10-29 22:25:17 +00:00
|
|
|
"loaded": False,
|
2020-10-24 20:31:52 +00:00
|
|
|
"pos": 0,
|
2020-10-30 19:31:18 +00:00
|
|
|
"pos_offset": 0,
|
|
|
|
"pos_true": 0,
|
2020-10-24 20:31:52 +00:00
|
|
|
"remaining": 0,
|
|
|
|
"length": 0,
|
2020-11-03 01:07:25 +00:00
|
|
|
"auto_advance": True,
|
2020-11-15 19:34:13 +00:00
|
|
|
"repeat": "NONE", # NONE, ONE or ALL
|
2020-11-03 01:07:25 +00:00
|
|
|
"play_on_load": False,
|
2020-11-01 02:35:14 +00:00
|
|
|
"output": None,
|
|
|
|
"show_plan": []
|
2020-10-24 20:31:52 +00:00
|
|
|
}
|
|
|
|
|
2020-11-04 01:19:56 +00:00
|
|
|
__rate_limited_params = [
|
|
|
|
"pos",
|
|
|
|
"pos_offset",
|
|
|
|
"pos_true",
|
|
|
|
"remaining"
|
|
|
|
]
|
|
|
|
|
2020-10-29 22:25:17 +00:00
|
|
|
@property
|
2020-10-24 20:31:52 +00:00
|
|
|
def isInit(self):
|
|
|
|
try:
|
2020-10-25 01:23:24 +00:00
|
|
|
mixer.music.get_busy()
|
2020-10-24 20:31:52 +00:00
|
|
|
except:
|
|
|
|
return False
|
2020-10-29 22:25:17 +00:00
|
|
|
|
|
|
|
return True
|
2020-10-24 20:31:52 +00:00
|
|
|
|
2020-10-29 21:23:37 +00:00
|
|
|
@property
|
2020-10-24 20:31:52 +00:00
|
|
|
def isPlaying(self):
|
2020-10-29 22:25:17 +00:00
|
|
|
if self.isInit:
|
2020-10-30 19:31:18 +00:00
|
|
|
return (not self.isPaused) and bool(mixer.music.get_busy())
|
2020-10-29 22:25:17 +00:00
|
|
|
return False
|
2020-10-24 20:31:52 +00:00
|
|
|
|
2020-10-30 19:31:18 +00:00
|
|
|
@property
|
2020-12-19 14:57:37 +00:00
|
|
|
def isPaused(self) -> bool:
|
2020-10-30 19:31:18 +00:00
|
|
|
return self.state.state["paused"]
|
|
|
|
|
2020-10-29 21:23:37 +00:00
|
|
|
@property
|
|
|
|
def isLoaded(self):
|
2020-11-01 02:35:14 +00:00
|
|
|
if not self.state.state["loaded_item"]:
|
2020-10-29 22:25:17 +00:00
|
|
|
return False
|
|
|
|
if self.isPlaying:
|
|
|
|
return True
|
|
|
|
|
2020-10-30 00:32:34 +00:00
|
|
|
# Because Pygame/SDL is annoying
|
|
|
|
# We're not playing now, so we can quickly test run
|
|
|
|
# If that works, we're loaded.
|
2020-10-29 22:25:17 +00:00
|
|
|
try:
|
2020-12-19 14:57:37 +00:00
|
|
|
position: float = self.state.state["pos"]
|
2020-10-30 00:32:34 +00:00
|
|
|
mixer.music.set_volume(0)
|
|
|
|
mixer.music.play(0)
|
2020-10-29 22:25:17 +00:00
|
|
|
except:
|
2020-10-30 00:32:34 +00:00
|
|
|
try:
|
|
|
|
mixer.music.set_volume(1)
|
|
|
|
except:
|
2020-11-03 22:48:11 +00:00
|
|
|
self.logger.log.exception("Failed to reset volume after attempting loaded test.")
|
2020-10-30 00:32:34 +00:00
|
|
|
pass
|
2020-10-29 22:25:17 +00:00
|
|
|
return False
|
2020-10-30 00:32:34 +00:00
|
|
|
if position > 0:
|
2020-10-30 19:31:18 +00:00
|
|
|
self.pause()
|
2020-10-30 00:32:34 +00:00
|
|
|
else:
|
2020-10-30 19:31:18 +00:00
|
|
|
self.stop()
|
2020-10-30 00:32:34 +00:00
|
|
|
mixer.music.set_volume(1)
|
|
|
|
return True
|
2020-10-29 22:25:17 +00:00
|
|
|
|
2020-10-30 00:32:34 +00:00
|
|
|
@property
|
|
|
|
def status(self):
|
2020-11-01 02:35:14 +00:00
|
|
|
state = copy.copy(self.state.state)
|
|
|
|
|
|
|
|
# Not the biggest fan of this, but maybe I'll get a better solution for this later
|
2020-11-03 00:32:43 +00:00
|
|
|
state["loaded_item"] = state["loaded_item"].__dict__ if state["loaded_item"] else None
|
|
|
|
state["show_plan"] = [repr.__dict__ for repr in state["show_plan"]]
|
2020-11-01 02:35:14 +00:00
|
|
|
|
|
|
|
res = json.dumps(state)
|
2020-10-30 00:32:34 +00:00
|
|
|
return res
|
2020-10-29 21:23:37 +00:00
|
|
|
|
2020-11-15 19:34:13 +00:00
|
|
|
# Audio Playout Related Methods
|
2020-11-03 01:07:25 +00:00
|
|
|
|
2020-12-19 14:57:37 +00:00
|
|
|
def play(self, pos: float = 0):
|
2020-10-30 19:31:18 +00:00
|
|
|
try:
|
|
|
|
mixer.music.play(0, pos)
|
|
|
|
self.state.update("pos_offset", pos)
|
|
|
|
except:
|
2020-11-03 22:48:11 +00:00
|
|
|
self.logger.log.exception("Failed to play at pos: " + str(pos))
|
2020-10-30 19:31:18 +00:00
|
|
|
return False
|
|
|
|
self.state.update("paused", False)
|
|
|
|
return True
|
2020-10-24 20:31:52 +00:00
|
|
|
|
|
|
|
def pause(self):
|
2020-10-30 19:31:18 +00:00
|
|
|
try:
|
|
|
|
mixer.music.pause()
|
|
|
|
except:
|
2020-11-03 22:48:11 +00:00
|
|
|
self.logger.log.exception("Failed to pause.")
|
2020-10-30 19:31:18 +00:00
|
|
|
return False
|
|
|
|
self.state.update("paused", True)
|
|
|
|
return True
|
2020-10-24 20:31:52 +00:00
|
|
|
|
|
|
|
def unpause(self):
|
2020-10-29 22:25:17 +00:00
|
|
|
if not self.isPlaying:
|
2020-12-19 14:57:37 +00:00
|
|
|
position: float = self.state.state["pos_true"]
|
2020-10-29 22:25:17 +00:00
|
|
|
try:
|
2020-11-03 22:48:11 +00:00
|
|
|
self.play(position)
|
2020-10-29 22:25:17 +00:00
|
|
|
except:
|
2020-11-03 22:48:11 +00:00
|
|
|
self.logger.log.exception("Failed to unpause from pos: " + str(position))
|
2020-10-29 22:25:17 +00:00
|
|
|
return False
|
2020-10-30 19:31:18 +00:00
|
|
|
self.state.update("paused", False)
|
2020-10-29 22:25:17 +00:00
|
|
|
return True
|
|
|
|
return False
|
2020-10-24 20:31:52 +00:00
|
|
|
|
|
|
|
def stop(self):
|
2020-10-30 19:31:18 +00:00
|
|
|
# if self.isPlaying or self.isPaused:
|
|
|
|
try:
|
|
|
|
mixer.music.stop()
|
|
|
|
except:
|
2020-11-03 22:48:11 +00:00
|
|
|
self.logger.log.exception("Failed to stop playing.")
|
2020-10-30 19:31:18 +00:00
|
|
|
return False
|
|
|
|
self.state.update("pos", 0)
|
|
|
|
self.state.update("pos_offset", 0)
|
|
|
|
self.state.update("pos_true", 0)
|
|
|
|
self.state.update("paused", False)
|
2020-12-20 01:10:19 +00:00
|
|
|
|
2020-10-30 19:31:18 +00:00
|
|
|
return True
|
|
|
|
# return False
|
2020-10-24 20:31:52 +00:00
|
|
|
|
2020-12-19 14:57:37 +00:00
|
|
|
def seek(self, pos: float) -> bool:
|
2020-10-29 22:25:17 +00:00
|
|
|
if self.isPlaying:
|
|
|
|
try:
|
2020-10-30 19:31:18 +00:00
|
|
|
self.play(pos)
|
2020-10-29 22:25:17 +00:00
|
|
|
except:
|
2020-11-03 22:48:11 +00:00
|
|
|
self.logger.log.exception("Failed to seek to pos: " + str(pos))
|
2020-10-29 22:25:17 +00:00
|
|
|
return False
|
|
|
|
return True
|
2020-10-30 19:31:18 +00:00
|
|
|
else:
|
|
|
|
self.state.update("paused", True)
|
|
|
|
self._updateState(pos=pos)
|
|
|
|
return True
|
2020-10-24 20:31:52 +00:00
|
|
|
|
2020-11-03 01:07:25 +00:00
|
|
|
def set_auto_advance(self, message: int) -> bool:
|
|
|
|
if message == 0:
|
|
|
|
self.state.update("auto_advance", False)
|
2020-11-15 19:34:13 +00:00
|
|
|
return True # It did it
|
2020-11-03 01:07:25 +00:00
|
|
|
elif message == 1:
|
|
|
|
self.state.update("auto_advance", True)
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
def set_repeat(self, message: str) -> bool:
|
|
|
|
if message in ["ALL", "ONE", "NONE"]:
|
|
|
|
self.state.update("repeat", message)
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
def set_play_on_load(self, message: int) -> bool:
|
|
|
|
if message == 0:
|
|
|
|
self.state.update("play_on_load", False)
|
2020-11-15 19:34:13 +00:00
|
|
|
return True # It did it
|
2020-11-03 01:07:25 +00:00
|
|
|
elif message == 1:
|
|
|
|
self.state.update("play_on_load", True)
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
2020-11-15 19:34:13 +00:00
|
|
|
# Show Plan Related Methods
|
2020-11-03 01:07:25 +00:00
|
|
|
|
2020-12-19 14:57:37 +00:00
|
|
|
def add_to_plan(self, new_item: Dict[str, Any]) -> bool:
|
2020-11-05 23:53:27 +00:00
|
|
|
self.state.update("show_plan", self.state.state["show_plan"] + [PlanItem(new_item)])
|
2020-11-01 02:35:14 +00:00
|
|
|
return True
|
|
|
|
|
2020-12-20 01:10:19 +00:00
|
|
|
def remove_from_plan(self, channel_weight: int) -> bool:
|
2020-12-19 14:57:37 +00:00
|
|
|
plan_copy: List[PlanItem] = copy.copy(self.state.state["show_plan"])
|
|
|
|
for i in plan_copy:
|
2020-12-20 01:10:19 +00:00
|
|
|
if i.channelWeight == channel_weight:
|
2020-11-02 23:06:45 +00:00
|
|
|
plan_copy.remove(i)
|
|
|
|
self.state.update("show_plan", plan_copy)
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
def clear_channel_plan(self) -> bool:
|
|
|
|
self.state.update("show_plan", [])
|
|
|
|
return True
|
|
|
|
|
2020-12-20 01:10:19 +00:00
|
|
|
def load(self, channelWeight: int):
|
2020-10-29 22:25:17 +00:00
|
|
|
if not self.isPlaying:
|
2020-10-30 19:31:18 +00:00
|
|
|
self.unload()
|
2020-10-29 22:25:17 +00:00
|
|
|
|
2020-12-08 19:41:11 +00:00
|
|
|
showplan = self.state.state["show_plan"]
|
|
|
|
|
2020-12-19 14:57:37 +00:00
|
|
|
loaded_item: Optional[PlanItem] = None
|
2020-12-08 19:41:11 +00:00
|
|
|
|
|
|
|
for i in range(len(showplan)):
|
2020-12-20 01:10:19 +00:00
|
|
|
if showplan[i].channelWeight == channelWeight:
|
2020-12-08 19:41:11 +00:00
|
|
|
loaded_item = showplan[i]
|
2020-11-01 02:35:14 +00:00
|
|
|
break
|
|
|
|
|
2020-12-19 14:57:37 +00:00
|
|
|
if loaded_item == None:
|
2020-12-20 01:10:19 +00:00
|
|
|
self.logger.log.error("Failed to find channelWeight: {}".format(channelWeight))
|
2020-11-01 02:35:14 +00:00
|
|
|
return False
|
|
|
|
|
2020-12-08 19:41:11 +00:00
|
|
|
if (loaded_item.filename == "" or loaded_item.filename == None):
|
|
|
|
loaded_item.filename = MyRadioAPI.get_filename(item = loaded_item)
|
|
|
|
|
2020-12-19 14:57:37 +00:00
|
|
|
if not loaded_item.filename:
|
|
|
|
return False
|
|
|
|
|
2020-12-08 19:41:11 +00:00
|
|
|
self.state.update("loaded_item", loaded_item)
|
2020-10-29 22:25:17 +00:00
|
|
|
|
2020-12-08 19:41:11 +00:00
|
|
|
for i in range(len(showplan)):
|
2020-12-20 01:10:19 +00:00
|
|
|
if showplan[i].channelWeight == channelWeight:
|
2020-12-08 19:41:11 +00:00
|
|
|
self.state.update("show_plan", index=i, value=loaded_item)
|
|
|
|
break
|
2020-11-05 23:53:27 +00:00
|
|
|
# TODO: Update the show plan filenames
|
2020-10-29 22:25:17 +00:00
|
|
|
|
|
|
|
try:
|
2020-12-08 19:41:11 +00:00
|
|
|
self.logger.log.info("Loading file: " + str(loaded_item.filename))
|
|
|
|
mixer.music.load(loaded_item.filename)
|
2020-10-29 22:25:17 +00:00
|
|
|
except:
|
|
|
|
# We couldn't load that file.
|
2020-12-08 19:41:11 +00:00
|
|
|
self.logger.log.exception("Couldn't load file: " + str(loaded_item.filename))
|
2020-10-29 22:25:17 +00:00
|
|
|
return False
|
|
|
|
|
|
|
|
try:
|
2020-12-19 14:57:37 +00:00
|
|
|
if ".mp3" in loaded_item.filename:
|
|
|
|
song = MP3(loaded_item.filename)
|
2020-10-29 22:25:17 +00:00
|
|
|
self.state.update("length", song.info.length)
|
|
|
|
else:
|
2020-12-19 14:57:37 +00:00
|
|
|
self.state.update("length", mixer.Sound(loaded_item.filename).get_length()/1000)
|
2020-10-29 22:25:17 +00:00
|
|
|
except:
|
2020-11-03 22:48:11 +00:00
|
|
|
self.logger.log.exception("Failed to update the length of item.")
|
2020-10-29 22:25:17 +00:00
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
|
|
|
def unload(self):
|
|
|
|
if not self.isPlaying:
|
|
|
|
try:
|
|
|
|
mixer.music.unload()
|
2020-10-30 19:31:18 +00:00
|
|
|
self.state.update("paused", False)
|
2020-11-01 02:35:14 +00:00
|
|
|
self.state.update("loaded_item", None)
|
2020-10-29 22:25:17 +00:00
|
|
|
except:
|
2020-11-03 22:48:11 +00:00
|
|
|
self.logger.log.exception("Failed to unload channel.")
|
2020-10-29 22:25:17 +00:00
|
|
|
return False
|
|
|
|
return not self.isLoaded
|
2020-10-24 20:31:52 +00:00
|
|
|
|
|
|
|
def quit(self):
|
2020-11-03 22:48:11 +00:00
|
|
|
try:
|
|
|
|
mixer.quit()
|
|
|
|
self.state.update("paused", False)
|
|
|
|
except:
|
|
|
|
self.logger.log.exception("Failed to quit mixer.")
|
2020-10-24 20:31:52 +00:00
|
|
|
|
2020-12-19 14:57:37 +00:00
|
|
|
def output(self, name: Optional[str] = None):
|
2020-11-01 03:19:21 +00:00
|
|
|
wasPlaying = self.state.state["playing"]
|
2020-11-05 18:59:28 +00:00
|
|
|
name = None if name == "none" else name
|
|
|
|
|
2020-10-24 20:31:52 +00:00
|
|
|
self.quit()
|
2020-10-30 00:32:34 +00:00
|
|
|
self.state.update("output", name)
|
2020-10-24 20:31:52 +00:00
|
|
|
try:
|
|
|
|
if name:
|
2020-11-01 03:27:26 +00:00
|
|
|
mixer.init(44100, -16, 2, 1024, devicename=name)
|
2020-10-24 20:31:52 +00:00
|
|
|
else:
|
2020-11-01 03:27:26 +00:00
|
|
|
mixer.init(44100, -16, 2, 1024)
|
2020-10-24 20:31:52 +00:00
|
|
|
except:
|
2020-11-03 22:48:11 +00:00
|
|
|
self.logger.log.exception("Failed to init mixer with device name: " + str(name))
|
2020-10-30 00:32:34 +00:00
|
|
|
return False
|
2020-10-24 20:31:52 +00:00
|
|
|
|
2020-11-05 18:59:28 +00:00
|
|
|
loadedItem = self.state.state["loaded_item"]
|
|
|
|
if (loadedItem):
|
2020-12-20 01:10:19 +00:00
|
|
|
self.load(loadedItem.channelWeight)
|
2020-11-01 03:19:21 +00:00
|
|
|
if wasPlaying:
|
|
|
|
self.unpause()
|
|
|
|
|
2020-10-30 00:32:34 +00:00
|
|
|
return True
|
2020-10-24 20:31:52 +00:00
|
|
|
|
2020-12-19 14:57:37 +00:00
|
|
|
def _updateState(self, pos: Optional[float] = None):
|
2020-11-04 01:19:56 +00:00
|
|
|
|
2020-10-30 00:32:34 +00:00
|
|
|
self.state.update("initialised", self.isInit)
|
|
|
|
if self.isInit:
|
2020-11-04 01:19:56 +00:00
|
|
|
if (pos):
|
|
|
|
self.state.update("pos", max(0, pos))
|
|
|
|
elif self.isPlaying:
|
2020-10-30 19:31:18 +00:00
|
|
|
# Get one last update in, incase we're about to pause/stop it.
|
|
|
|
self.state.update("pos", max(0, mixer.music.get_pos()/1000))
|
2020-10-30 00:32:34 +00:00
|
|
|
self.state.update("playing", self.isPlaying)
|
|
|
|
self.state.update("loaded", self.isLoaded)
|
2020-10-30 19:31:18 +00:00
|
|
|
|
|
|
|
self.state.update("pos_true", self.state.state["pos"] + self.state.state["pos_offset"])
|
|
|
|
|
|
|
|
self.state.update("remaining", self.state.state["length"] - self.state.state["pos_true"])
|
2020-10-30 00:32:34 +00:00
|
|
|
|
2020-12-19 14:57:37 +00:00
|
|
|
loaded_item = self.state.state["loaded_item"]
|
|
|
|
if loaded_item == None or self.state.state["remaining"] != 0:
|
|
|
|
return
|
|
|
|
|
|
|
|
# Track has ended
|
|
|
|
print("Finished", loaded_item.name)
|
2020-11-05 18:58:18 +00:00
|
|
|
|
2020-12-19 14:57:37 +00:00
|
|
|
# Repeat 1
|
|
|
|
if self.state.state["repeat"] == "ONE":
|
|
|
|
self.play()
|
2020-11-03 01:07:25 +00:00
|
|
|
|
2020-12-19 14:57:37 +00:00
|
|
|
# Auto Advance
|
|
|
|
elif self.state.state["auto_advance"]:
|
|
|
|
for i in range(len(self.state.state["show_plan"])):
|
2020-12-20 01:10:19 +00:00
|
|
|
if self.state.state["show_plan"][i].channelWeight == loaded_item.channelWeight:
|
2020-12-19 14:57:37 +00:00
|
|
|
if len(self.state.state["show_plan"]) > i+1:
|
2020-12-20 01:10:19 +00:00
|
|
|
self.load(self.state.state["show_plan"][i+1].channelWeight)
|
2020-12-19 14:57:37 +00:00
|
|
|
break
|
2020-11-03 01:07:25 +00:00
|
|
|
|
2020-12-19 14:57:37 +00:00
|
|
|
# Repeat All
|
|
|
|
elif self.state.state["repeat"] == "ALL":
|
2020-12-20 01:10:19 +00:00
|
|
|
self.load(self.state.state["show_plan"][0].channelWeight)
|
2020-11-05 18:58:18 +00:00
|
|
|
|
2020-12-19 14:57:37 +00:00
|
|
|
# Play on Load
|
|
|
|
if self.state.state["play_on_load"]:
|
|
|
|
self.play()
|
2020-11-05 18:58:18 +00:00
|
|
|
|
2020-11-03 01:07:25 +00:00
|
|
|
|
2020-12-19 14:57:37 +00:00
|
|
|
def _retMsg(self, msg: Any, okay_str: Any = False):
|
2020-10-30 00:32:34 +00:00
|
|
|
response = self.last_msg + ":"
|
|
|
|
if msg == True:
|
|
|
|
response += "OKAY"
|
|
|
|
elif isinstance(msg, str):
|
|
|
|
if okay_str:
|
|
|
|
response += "OKAY:" + msg
|
|
|
|
else:
|
|
|
|
response += "FAIL:" + msg
|
2020-10-24 20:31:52 +00:00
|
|
|
else:
|
2020-10-30 00:32:34 +00:00
|
|
|
response += "FAIL"
|
|
|
|
if self.out_q:
|
|
|
|
self.out_q.put(response)
|
2020-10-24 20:31:52 +00:00
|
|
|
|
2020-12-19 14:57:37 +00:00
|
|
|
def __init__(self, channel: int, in_q: multiprocessing.Queue, out_q: multiprocessing.Queue):
|
2020-10-30 00:32:34 +00:00
|
|
|
|
2020-10-30 23:14:29 +00:00
|
|
|
process_title = "Player: Channel " + str(channel)
|
|
|
|
setproctitle.setproctitle(process_title)
|
|
|
|
multiprocessing.current_process().name = process_title
|
2020-10-24 20:31:52 +00:00
|
|
|
|
2020-10-30 22:06:03 +00:00
|
|
|
self.running = True
|
|
|
|
self.out_q = out_q
|
2020-10-24 20:31:52 +00:00
|
|
|
|
2020-10-30 22:06:03 +00:00
|
|
|
self.logger = LoggingManager("channel" + str(channel))
|
|
|
|
|
2020-11-15 19:34:13 +00:00
|
|
|
self.state = StateManager("channel" + str(channel), self.logger,
|
|
|
|
self.__default_state, self.__rate_limited_params)
|
2020-10-30 23:59:58 +00:00
|
|
|
self.state.update("channel", channel)
|
|
|
|
|
2020-10-24 20:31:52 +00:00
|
|
|
loaded_state = copy.copy(self.state.state)
|
|
|
|
|
|
|
|
if loaded_state["output"]:
|
2020-12-19 14:57:37 +00:00
|
|
|
self.logger.log.info("Setting output to: " + str(loaded_state["output"]))
|
2020-10-24 20:31:52 +00:00
|
|
|
self.output(loaded_state["output"])
|
|
|
|
else:
|
2020-10-30 22:06:03 +00:00
|
|
|
self.logger.log.info("Using default output device.")
|
2020-10-24 20:31:52 +00:00
|
|
|
self.output()
|
|
|
|
|
2020-12-19 14:57:37 +00:00
|
|
|
loaded_item = loaded_state["loaded_item"]
|
|
|
|
if loaded_item:
|
|
|
|
self.logger.log.info("Loading filename: " + str(loaded_item.filename))
|
2020-12-20 01:10:19 +00:00
|
|
|
self.load(loaded_item.channelWeight)
|
2020-10-24 20:31:52 +00:00
|
|
|
|
2020-10-30 19:31:18 +00:00
|
|
|
if loaded_state["pos_true"] != 0:
|
2020-10-30 22:06:03 +00:00
|
|
|
self.logger.log.info("Seeking to pos_true: " + str(loaded_state["pos_true"]))
|
2020-10-30 19:31:18 +00:00
|
|
|
self.seek(loaded_state["pos_true"])
|
2020-10-24 20:31:52 +00:00
|
|
|
|
2020-10-29 21:23:37 +00:00
|
|
|
if loaded_state["playing"] == True:
|
2020-10-30 22:06:03 +00:00
|
|
|
self.logger.log.info("Resuming.")
|
2020-10-29 21:23:37 +00:00
|
|
|
self.unpause()
|
|
|
|
else:
|
2020-10-30 22:06:03 +00:00
|
|
|
self.logger.log.info("No file was previously loaded.")
|
2020-10-24 20:31:52 +00:00
|
|
|
|
|
|
|
while self.running:
|
2020-10-30 00:32:34 +00:00
|
|
|
time.sleep(0.1)
|
|
|
|
self._updateState()
|
2020-10-29 21:23:37 +00:00
|
|
|
try:
|
|
|
|
try:
|
2020-10-30 00:32:34 +00:00
|
|
|
self.last_msg = in_q.get_nowait()
|
2020-10-29 21:23:37 +00:00
|
|
|
except Empty:
|
|
|
|
# The incomming message queue was empty,
|
|
|
|
# skip message processing
|
|
|
|
pass
|
|
|
|
else:
|
2020-10-30 00:32:34 +00:00
|
|
|
|
2020-10-29 21:23:37 +00:00
|
|
|
# We got a message.
|
2020-10-29 22:25:17 +00:00
|
|
|
|
2020-10-30 00:32:34 +00:00
|
|
|
# Output re-inits the mixer, so we can do this any time.
|
|
|
|
if (self.last_msg.startswith("OUTPUT")):
|
|
|
|
split = self.last_msg.split(":")
|
|
|
|
self._retMsg(self.output(split[1]))
|
|
|
|
|
|
|
|
elif self.isInit:
|
2020-10-29 21:23:37 +00:00
|
|
|
|
2020-12-19 14:57:37 +00:00
|
|
|
message_types: Dict[str, Callable[..., Any]] = { # TODO Check Types
|
2020-11-03 01:07:25 +00:00
|
|
|
"STATUS": lambda: self._retMsg(self.status, True),
|
2020-11-05 18:58:18 +00:00
|
|
|
|
2020-11-03 01:07:25 +00:00
|
|
|
# Audio Playout
|
2020-11-15 19:34:13 +00:00
|
|
|
"PLAY": lambda: self._retMsg(self.play()),
|
|
|
|
"PAUSE": lambda: self._retMsg(self.pause()),
|
|
|
|
"UNPAUSE": lambda: self._retMsg(self.unpause()),
|
|
|
|
"STOP": lambda: self._retMsg(self.stop()),
|
|
|
|
"SEEK": lambda: self._retMsg(self.seek(float(self.last_msg.split(":")[1]))),
|
|
|
|
"AUTOADVANCE": lambda: self._retMsg(self.set_auto_advance(int(self.last_msg.split(":")[1]))),
|
|
|
|
"REPEAT": lambda: self._retMsg(self.set_repeat(self.last_msg.split(":")[1])),
|
|
|
|
"PLAYONLOAD": lambda: self._retMsg(self.set_play_on_load(int(self.last_msg.split(":")[1]))),
|
2020-11-03 01:07:25 +00:00
|
|
|
|
|
|
|
# Show Plan Items
|
2020-11-15 19:34:13 +00:00
|
|
|
"LOAD": lambda: self._retMsg(self.load(int(self.last_msg.split(":")[1]))),
|
|
|
|
"LOADED?": lambda: self._retMsg(self.isLoaded),
|
|
|
|
"UNLOAD": lambda: self._retMsg(self.unload()),
|
|
|
|
"ADD": lambda: self._retMsg(self.add_to_plan(json.loads(":".join(self.last_msg.split(":")[1:])))),
|
|
|
|
"REMOVE": lambda: self._retMsg(self.remove_from_plan(int(self.last_msg.split(":")[1]))),
|
|
|
|
"CLEAR": lambda: self._retMsg(self.clear_channel_plan())
|
2020-11-01 02:37:56 +00:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:32:43 +00:00
|
|
|
message_type: str = self.last_msg.split(":")[0]
|
|
|
|
|
|
|
|
if message_type in message_types.keys():
|
|
|
|
message_types[message_type]()
|
2020-10-30 00:32:34 +00:00
|
|
|
|
|
|
|
elif (self.last_msg == 'QUIT'):
|
2020-10-29 21:23:37 +00:00
|
|
|
self.running = False
|
2020-10-30 00:32:34 +00:00
|
|
|
continue
|
|
|
|
|
|
|
|
else:
|
|
|
|
self._retMsg("Unknown Command")
|
|
|
|
else:
|
|
|
|
if (self.last_msg == 'STATUS'):
|
|
|
|
self._retMsg(self.status)
|
|
|
|
else:
|
|
|
|
self._retMsg(False)
|
2020-10-29 21:23:37 +00:00
|
|
|
|
|
|
|
# Catch the player being killed externally.
|
|
|
|
except KeyboardInterrupt:
|
2020-11-03 22:48:11 +00:00
|
|
|
self.logger.log.info("Received KeyboardInterupt")
|
2020-10-29 21:23:37 +00:00
|
|
|
break
|
|
|
|
except SystemExit:
|
2020-11-03 22:48:11 +00:00
|
|
|
self.logger.log.info("Received SystemExit")
|
2020-10-29 21:23:37 +00:00
|
|
|
break
|
|
|
|
except:
|
2020-11-03 22:48:11 +00:00
|
|
|
self.logger.log.exception("Received unexpected exception.")
|
|
|
|
break
|
2020-10-24 20:31:52 +00:00
|
|
|
|
2020-10-30 22:06:03 +00:00
|
|
|
self.logger.log.info("Quiting player ", channel)
|
2020-10-29 21:23:37 +00:00
|
|
|
self.quit()
|
2020-10-30 00:32:34 +00:00
|
|
|
self._retMsg("EXIT")
|
2020-11-01 00:31:58 +00:00
|
|
|
sys.exit(0)
|
2020-10-29 21:23:37 +00:00
|
|
|
|
|
|
|
|
2020-12-19 14:57:37 +00:00
|
|
|
def showOutput(in_q: multiprocessing.Queue, out_q: multiprocessing.Queue):
|
2020-10-29 21:23:37 +00:00
|
|
|
print("Starting showOutput().")
|
|
|
|
while True:
|
|
|
|
time.sleep(0.01)
|
2020-10-30 19:31:18 +00:00
|
|
|
last_msg = out_q.get()
|
|
|
|
print(last_msg)
|
2020-10-29 21:23:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2020-11-05 19:06:16 +00:00
|
|
|
if isMacOS():
|
2020-11-01 02:44:35 +00:00
|
|
|
multiprocessing.set_start_method("spawn", True)
|
2020-10-29 21:23:37 +00:00
|
|
|
|
2020-12-19 14:57:37 +00:00
|
|
|
in_q: multiprocessing.Queue[Any] = multiprocessing.Queue()
|
|
|
|
out_q: multiprocessing.Queue[Any] = multiprocessing.Queue()
|
2020-10-29 21:23:37 +00:00
|
|
|
|
|
|
|
outputProcess = multiprocessing.Process(
|
|
|
|
target=showOutput,
|
|
|
|
args=(in_q, out_q),
|
|
|
|
).start()
|
|
|
|
|
|
|
|
playerProcess = multiprocessing.Process(
|
|
|
|
target=Player,
|
|
|
|
args=(-1, in_q, out_q),
|
|
|
|
).start()
|
|
|
|
|
|
|
|
# Do some testing
|
2020-10-29 22:25:17 +00:00
|
|
|
in_q.put("LOADED?")
|
|
|
|
in_q.put("PLAY")
|
2020-11-03 22:52:58 +00:00
|
|
|
in_q.put("LOAD:dev/test.mp3")
|
2020-10-29 22:25:17 +00:00
|
|
|
in_q.put("LOADED?")
|
|
|
|
in_q.put("PLAY")
|
2020-10-29 21:23:37 +00:00
|
|
|
print("Entering infinite loop.")
|
|
|
|
while True:
|
|
|
|
pass
|