piano-appp/backend/app.py

48 lines
855 B
Python
Raw Normal View History

2022-10-03 03:32:41 +00:00
from flask import Flask
from threading import Thread
import os
2022-10-03 04:08:58 +00:00
import subprocess
2022-10-03 03:32:41 +00:00
app = Flask(__name__)
threadFlag = True
def playFunction(freq):
global threadFlag
2022-10-03 04:08:58 +00:00
p = subprocess.Popen(["beep", "-f", freq, "-l", "100000"])
2022-10-03 03:32:41 +00:00
while not threadFlag:
2022-10-03 04:03:36 +00:00
pass
2022-10-03 03:32:41 +00:00
#print("beep -f %f" % (freq))
2022-10-03 04:08:58 +00:00
os.kill(p.pid, 15)
2022-10-03 03:32:41 +00:00
2022-10-03 04:01:29 +00:00
thread2 = 0
2022-10-03 03:32:41 +00:00
tc = 0
@app.before_first_request
def activate_job():
global tc
tc = 0
@app.route('/startfreq/<freq>')
def freq(freq):
2022-10-03 03:56:47 +00:00
global threadFlag
2022-10-03 04:01:29 +00:00
global thread2
2022-10-03 03:56:47 +00:00
threadFlag = False
2022-10-03 03:32:41 +00:00
print(freq)
2022-10-03 03:54:22 +00:00
thread2 = Thread(target=playFunction, args=(freq,))
2022-10-03 03:56:47 +00:00
thread2.start()
2022-10-03 03:32:41 +00:00
return("200")
@app.route('/stopfreq/<freq2>')
def freq2(freq2):
global threadFlag
2022-10-03 04:01:29 +00:00
global thread2
2022-10-03 03:32:41 +00:00
threadFlag = True
return("200")
if __name__ == "__main__":
app.run(debug=True, host='0.0.0.0')
2022-10-03 04:22:00 +00:00