-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
148 lines (136 loc) · 6.17 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# This app creates a simple gui for audio parameters to pass to the create_app function.
from server import create_app
import pyaudio
import PySimpleGUI as sg
from threading import Thread
import socket
import qrcode
import os
import webbrowser
def get_ip():
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.settimeout(0)
try:
# doesn't even have to be reachable
s.connect(('10.254.254.254', 1))
IP = s.getsockname()[0]
except Exception:
IP = '127.0.0.1'
finally:
s.close()
return IP
def generate_qr_code(link):
#Creating an instance of qrcode
qr = qrcode.QRCode(
version=1,
box_size=10,
border=5)
qr.add_data(link)
qr.make(fit=True)
img = qr.make_image(fill='black', back_color='white')
file_name = "qr_code"+ ".png"
path = os.path.join(os.getcwd(), file_name)
img.save(path)
return path
def checkSampleRates(deviceIndex):
normalRates=[44100, 48000, 88200, 96000, 176400, 19200]
supportedRates=[]
audio = pyaudio.PyAudio()
for i in range(len(normalRates)):
try:
if audio.is_format_supported(normalRates[i], input_device=deviceIndex, input_channels=1, input_format=pyaudio.paInt16):
supportedRates.append(normalRates[i])
except:
pass
return supportedRates
def getAudioSettings():
devicesList=[]
audio = pyaudio.PyAudio()
# Write get
for i in range(audio.get_device_count()):
if audio.get_device_info_by_index(i)['maxInputChannels'] > 0 and audio.get_device_info_by_index(i)['hostApi'] == 0:
device={}
info=audio.get_device_info_by_index(i)
device['supportedSampleRates']=checkSampleRates(i)
device['name']=info['name']
device['index']=info['index']
device['channels']=info['maxInputChannels']
# If device isn't already in the list, add it
if device not in devicesList:
devicesList.append(device)
return devicesList
def runServer(deviceIndex, channels, sampleRate, bitDepth, chunkSize, port):
app = create_app(deviceIndex, channels, sampleRate, bitDepth, chunkSize)
app.run(host='0.0.0.0', debug=False, threaded=True,port=port)
def serverRunningUI(deviceIndex, channels, sampleRate, bitDepth, chunkSize):
port=5000
ip=get_ip()
url="http://"+ip+":"+str(port)
path=generate_qr_code(url)
layout = [[sg.Text('Server Running')],[sg.Image(path)],[sg.Text('Scan QR code to connect')],[sg.Text('or go to: ')],[sg.Text(url,enable_events=True, key="link", font=(None, 12, "underline"),text_color='blue')],[sg.Button('Stop Server')]]
window = sg.Window('Server Running', layout)
server = Thread(target=runServer, args=(deviceIndex, channels, sampleRate, bitDepth, chunkSize, port))
server.setDaemon(True)
server.start()
while True:
event, values = window.read()
if event == 'link':
webbrowser.open(url)
if event == sg.WIN_CLOSED or event == 'Stop Server':
break
window.close()
def main():
# Get the list of devices
devicesList = getAudioSettings()
bitDepths = [16, 24, 32]
# Create a list of devices to display in the GUI (id: name)
devicesDisplay = []
for i in range(len(devicesList)):
devicesDisplay.append(str(devicesList[i]['index']) + ': ' + devicesList[i]['name'])
# Draw the GUI
layout = [[sg.Text('Audio Settings')],
[sg.Text('Device', size=(15, 1)), sg.Combo(devicesDisplay, size=(30, 1), key="device",enable_events=True)],
[sg.Text('Sample Rate', size=(15, 1)), sg.Combo([], size=(30, 1), key="sampleRate",enable_events=True)],
[sg.Text('Bit Depth', size=(15, 1)), sg.Combo([], size=(30, 1), key="bitDepth",enable_events=True)],
[sg.Text('Channels', size=(15, 1)), sg.Combo([], size=(30, 1), key="channels",enable_events=True)],
[sg.Text('Chunk Size', size=(15, 1)), sg.InputText('1024',size=(10, 1), key="chunkSize")],
[sg.Submit(), sg.Cancel()]]
window = sg.Window('Audio Settings', layout)
while True:
event, values = window.read()
if event == 'Submit':
window.close()
serverRunningUI(int(values['device'].split(':')[0]), int(values['channels']), int(values['sampleRate']), int(values['bitDepth']), int(values['chunkSize']))
if event == 'device':
newDeviceIndex = int(values['device'].split(':')[0])
newDeviceSampleRates = devicesList[newDeviceIndex]['supportedSampleRates']
newDeviceMaxChannels = devicesList[newDeviceIndex]['channels']
previousSampleRate = values['sampleRate']
previousBitDepth = values['bitDepth']
previousChannels = values['channels']
# Update the sample rate list and select the previous sample rate if it is still supported
window['sampleRate'].update(values=newDeviceSampleRates)
if previousSampleRate in newDeviceSampleRates:
window['sampleRate'].update(value=previousSampleRate)
else:
window['sampleRate'].update(value=newDeviceSampleRates[0])
# Update the bit depth list and select the previous bit depth if it is still supported
window['bitDepth'].update(values=bitDepths)
if previousBitDepth in bitDepths:
window['bitDepth'].update(value=previousBitDepth)
else:
window['bitDepth'].update(value=bitDepths[0])
# Update the channels list and select the previous channels if it is still supported
window['channels'].update(values=list(range(1,newDeviceMaxChannels+1)))
if previousChannels in list(range(1,newDeviceMaxChannels+1)):
window['channels'].update(value=previousChannels)
else:
window['channels'].update(value=1)
if event == 'Cancel' or event == sg.WIN_CLOSED:
# if QR code file exists, delete it
if os.path.exists("qr_code.png"):
os.remove("qr_code.png")
window.close()
return None
if __name__ == '__main__':
main()