-
Notifications
You must be signed in to change notification settings - Fork 0
/
performancerecorder.py.save.1
123 lines (106 loc) · 2.59 KB
/
performancerecorder.py.save.1
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
import pyaudio
import wave
import time
import tty
import termios
import select
import sys
sys.path.append('../')
import audioop
import os
import math
import signal
import dothat.backlight as backlight
import dothat.lcd as lcd
import dothat.touch as nav
from UsbDetector import UsbDetector
from pathlib import Path
button = 0
@nav.on(nav.UP)
def handle_up(ch, evt):
button = nav.UP
lcd.clear();
backlight.rgb(255, 0, 0)
lcd.write("Up!")
@nav.on(nav.DOWN)
def handle_down(ch, evt):
button = nav.DOWN
lcd.clear();
backlight.rgb(0, 255, 0)
lcd.write("Down!")
@nav.on(nav.LEFT)
def handle_down(ch, evt):
button = nav.LEFT
lcd.clear();
backlight.rgb(0, 0, 255)
lcd.write("Left!")
@nav.on(nav.RIGHT)
def handle_down(ch, evt):
button = nav.RIGHT
@nav.on(nav.BUTTON) def handle_down(ch, evt):
button = nav.BUTTON
@nav.on(nav.CANCEL)
def handle_down(ch, evt):
button = nav.CANCEL
def getOutputFileName():
offset = 0
while True:
offset = offset + 1
output = "output" + str(offset) + ".wav"
outputFile = Path(output)
if not outputFile.is_file():
return output
def isData():
return select.select([sys.stdin], [], [], 0) == ([sys.stdin], [], [])
if __name__ == '__main__':
usbDetector = UsbDetector()
chunk = 1024
sample_format = pyaudio.paInt16
channels = 2
fs = 44100
seconds = 3
p = pyaudio.PyAudio()
old_settings = termios.tcgetattr(sys.stdin)
try:
tty.setcbreak(sys.stdin.fileno())
record = False
stopRecord = False
backlight.rgb(0, 0, 0)
while True:
if button == nav.BUTTON:
record = True
button = 0
#if isData():
# c = sys.stdin.read(1)
# if c == "r":
# record = True
if record:
backlight.rgb(255, 0, 0)
print("Starting recording...")
filename = getOutputFileName()
print("Output = " + filename)
stream = p.open(format=sample_format, channels=channels,rate=fs, frames_per_buffer=chunk,input=True)
wf = wave.open(filename, "wb")
wf.setnchannels(channels)
wf.setsampwidth(p.get_sample_size(sample_format))
wf.setframerate(fs)
while True:
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 isData():
c = sys.stdin.read(1)
if c == "r":
stopRecord = True
if stopRecord == True:
print("Stopping recording...")
stream.close()
wf.close()
record = False
stopRecord = False
backlight.rgb(0, 0, 0)
break
finally:
termios.tcsetattr(sys.stdin, termios.TCSADRAIN, old_settings)