-
Notifications
You must be signed in to change notification settings - Fork 0
/
menu.py
executable file
·232 lines (196 loc) · 6.86 KB
/
menu.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#!/usr/bin/env python
import pyaudio
import wave
import sys
import time
import audioop
import os
import math
import datetime
from threading import Thread
from pathlib import Path
import dothat.backlight as backlight
import dothat.lcd as lcd
import dothat.touch as nav
from dot3k.menu import Menu, MenuIcon, MenuOption
sys.path.append('.')
from plugins.clock import Clock
from plugins.graph import IPAddress, GraphTemp, GraphCPU, GraphNetSpeed
from plugins.text import Text
from plugins.utils import Backlight, Contrast
from plugins.wlan import Wlan
class Audio(MenuOption):
def __init__(self):
self.inputDevices = []
self.selectedInputDeviceIndex = -1
self._icons_setup = False
self.isRecording = False
self.stopRecording = False
self.startedAt = datetime.datetime.now()
self.p = pyaudio.PyAudio()
self.enumerate_devices()
MenuOption.__init__(self)
def right(self):
if self.selectedInputDeviceIndex < len(self.inputDevices) - 1:
self.selectedInputDeviceIndex += 1
self.update_selectedInputDeviceIndex()
return True
else:
return False
def left(self):
if self.selectedInputDeviceIndex > -1:
self.selectedInputDeviceIndex -= 1
self.update_selectedInputDeviceIndex()
return True
else:
return False
def enumerate_devices(self):
info = self.p.get_host_api_info_by_index(0)
numdevices = info.get('deviceCount')
for i in range (0,numdevices):
if self.p.get_device_info_by_host_api_device_index(0,i).get('maxInputChannels')>0:
self.inputDevices.append(self.p.get_device_info_by_host_api_device_index(0,i).get('name'))
self.selectedInputDeviceIndex = self.p.get_device_info_by_index(1)
def setup_icons(self, menu):
menu.lcd.create_char(0, MenuIcon.arrow_left_right)
self._icons_setup = True
def cleanup(self):
self._icons_setup = False
self.p.terminate()
def setup(self, config):
self.config = config
self.selectedInputDeviceIndex = bool(self.get_option('Audio', 'selectedInputDeviceIndex', -1))
def update_selectedInputDeviceIndex(self):
self.set_option('Audio', 'selectedInputDeviceIndex', self.selectedInputDeviceIndex)
def redraw(self, menu):
if not self._icons_setup:
self.setup_icons(menu)
menu.write_row(0, 'Audio')
menu.write_row(1, chr(0) + 'In: ' + self.inputDevices[self.selectedInputDeviceIndex])
menu.clear_row(2)
def start_recording_async(self, callback = None):
if not self.isRecording:
self.recordingThread = Thread(target=self.start_recording, args=[callback])
self.recordingThread.start()
def start_recording(self, callback = None):
self.startedAt = datetime.datetime.now()
chunk = 1024
sample_format = pyaudio.paInt16
channels = 2
fs = 44100
seconds = 3
print("Starting recording...")
filename = self.getOutputFileName()
print("Output = " + filename)
stream = self.p.open(input_device_index=self.selectedInputDeviceIndex, format=sample_format, channels=channels,rate=fs, frames_per_buffer=chunk,input=True)
wf = wave.open(filename, "wb")
wf.setnchannels(channels)
wf.setsampwidth(self.p.get_sample_size(sample_format))
wf.setframerate(fs)
self.isRecording = True
while True:
if callback:
callback(self.startedAt)
data = stream.read(chunk)
rms = audioop.rms(data, 2) #our stereo rms, we want separate left and right though
decibel = 20 * math.log10(rms)
#print str(decibel)
wf.writeframes(b''.join(data))
if self.stopRecording:
print("Stopping recording...")
stream.close()
wf.close()
self.stopRecording = False
break
def getOutputFileName(self):
offset = 0
while True:
offset = offset + 1
output = "output" + str(offset) + ".wav"
outputFile = Path(output)
if not outputFile.is_file():
return output
class Video(MenuOption):
def __init__(self):
self.enabled = True
self._icons_setup = False
MenuOption.__init__(self)
def right(self):
self.enabled = True
self.update_enabled()
return True
def left(self):
self.enabled = False
self.update_enabled()
return True
def setup_icons(self, menu):
menu.lcd.create_char(0, MenuIcon.arrow_left_right)
self._icons_setup = True
def cleanup(self):
self._icons_setup = False
def setup(self, config):
self.config = config
self.enabled = bool(self.get_option('Video', 'enabled', True))
def update_enabled(self):
self.set_option('Video', 'enabled', str(self.enabled))
def redraw(self, menu):
if not self._icons_setup:
self.setup_icons(menu)
menu.write_row(0, 'Video')
menu.write_row(1, chr(0) + 'Enabled: ' + str(self.enabled))
menu.clear_row(2)
class Performance(MenuOption):
def __init__(self):
self.isRecording = False
self.elapsed = 0
MenuOption.__init__(self)
def right(self):
audio.start_recording_async(self.audioRecordingCallback)
self.isRecording = True
self.update_status()
def setup(self, config):
self.config = config
def audioRecordingCallback(self, startedAt):
self.elapsed = (datetime.datetime.now() - startedAt).total_seconds()
def update_status(self):
self.set_option('Performance', 'recording', str(self.isRecording))
def redraw(self, menu):
menu.write_row(0, "Performance")
if self.isRecording:
menu.write_row(1, "Recording...")
else:
menu.write_row(1, "")
menu.clear_row(2)
elapsed = datetime.datetime.now() - audio.startedAt
menu.write_row(2, str(datetime.timedelta(seconds=self.elapsed)))
audio = Audio()
video = Video()
performance = Performance()
menu = Menu(
structure={
'Performance': performance,
'Status': {
'Wifi': Wlan(),
'IP': IPAddress(),
'CPU': GraphCPU(backlight),
'Temp': GraphTemp(),
'Clock': Clock(backlight)
},
'Settings': {
'Display': {
'Contrast': Contrast(lcd),
'Backlight': Backlight(backlight)
},
'Audio': audio,
'Video': video
}
},
lcd=lcd,
#idle_handler=my_invader,
idle_timeout=30,
input_handler=Text())
nav.bind_defaults(menu)
lcd.set_contrast(50)
while 1:
menu.redraw()
time.sleep(0.05)