forked from bhar92/Thingamajig
-
Notifications
You must be signed in to change notification settings - Fork 0
/
inputdevice.py
71 lines (55 loc) · 1.73 KB
/
inputdevice.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
import pyaudio
import serial
import time
import wave
from array import array
from struct import pack
from sys import byteorder
class InputDevice:
def __init__(self, serial):
self.serial = serial
"""Read from RFID reader and return rfid if found"""
def read(self):
self.serial.flushInput()
rfid = self.serial.readline()
rfid = rfid.rstrip()
return rfid
"""Record audio from mic, store to file and return file name"""
def record(self):
print "Recording"
RATE = 44100
FORMAT = pyaudio.paInt16
THRESHOLD = 500
CHUNK_SIZE = 1024
p = pyaudio.PyAudio()
stream = p.open(format=FORMAT, channels=1, rate=RATE,
input=True, output=True,
frames_per_buffer=CHUNK_SIZE)
snd_started = False
data = array('h')
while True:
snd_data = array('h', stream.read(CHUNK_SIZE))
if byteorder == 'big': snd_data.byteswap()
data.extend(snd_data)
keypress = self.serial.read()
self.serial.flushInput()
print keypress
if keypress == '0':
break;
self.serial.write('c')
self.serial.flushInput()
sample_width = p.get_sample_size(FORMAT)
stream.stop_stream()
stream.close()
p.terminate()
sound_file = str(int(time.time()))+".wav"
path = "sounds/"+sound_file
data = pack('<' + ('h'*len(data)), *data)
wf = wave.open(path, 'wb')
wf.setnchannels(1)
wf.setsampwidth(sample_width)
wf.setframerate(RATE)
wf.writeframes(data)
wf.close()
print "Done recording"
return sound_file