91 lines
2.3 KiB
Python
91 lines
2.3 KiB
Python
from pygame import mixer
|
|
from pygame import error as pygame_error
|
|
from flask import Flask, render_template, request
|
|
from config import Config
|
|
|
|
import lookup_sounds
|
|
|
|
app = Flask(__name__)
|
|
config = Config.load("~/.config/soundboard.ini")
|
|
|
|
try:
|
|
mixer.init(devicename="soundboard Audio/Sink sink")
|
|
except pygame_error:
|
|
print("Failed to bind to sink")
|
|
exit(1)
|
|
|
|
sounds = lookup_sounds.Sounds()
|
|
sounds.load_sounds()
|
|
|
|
|
|
@app.route("/")
|
|
def index():
|
|
return render_template("sounds.html", data=sounds.sounds)
|
|
|
|
|
|
@app.route("/<string:sound>")
|
|
def play(sound):
|
|
l_sound = sounds.lookup(sound)
|
|
channel = mixer.find_channel()
|
|
if channel is None:
|
|
return "Out of usable audio channels", 418
|
|
if l_sound is None:
|
|
return "False", 404
|
|
m_sound = mixer.Sound(f"{config.application_basedir}{l_sound.filename}")
|
|
channel.set_volume(config.current_volume)
|
|
|
|
channel.play(m_sound)
|
|
return "True"
|
|
|
|
|
|
@app.route("/vol", methods=["GET", "POST"])
|
|
def volume():
|
|
match (request.method):
|
|
case "GET":
|
|
return str(config.current_volume)
|
|
case "POST":
|
|
try:
|
|
float(request.data)
|
|
config.current_volume = float(request.data)
|
|
return request.data
|
|
except Exception as e:
|
|
print(e)
|
|
return str(e)
|
|
case _:
|
|
return "Method not implemented", 418
|
|
|
|
|
|
@app.route("/settings")
|
|
def settings():
|
|
return render_template("settings.html", config=config)
|
|
|
|
|
|
@app.route("/new", methods=["POST"])
|
|
def new_sound():
|
|
match (request.method):
|
|
case "POST":
|
|
data = request.json
|
|
# Validate we have the required data to add a new sound
|
|
for field in ["name", "filename", "route", "file"]:
|
|
if data.get(field, None) is None:
|
|
return f"Bad data missing field {field}", 400
|
|
|
|
ret, message = sounds.add(**data)
|
|
print(ret)
|
|
if ret is False:
|
|
return f"Failed to add sound: {message}"
|
|
|
|
return "Successfully Added the sound", 200
|
|
case _:
|
|
return "Method not implemented", 418
|
|
|
|
|
|
@app.route("/reload")
|
|
def reload():
|
|
sounds.load_sounds()
|
|
return "True"
|
|
|
|
|
|
if __name__ == "__main__":
|
|
app.run(debug=True, host="0.0.0.0")
|