This repository has been archived by the owner on May 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecorder.py
67 lines (48 loc) · 1.75 KB
/
recorder.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
import matplotlib
matplotlib.use('TkAgg') # <-- THIS MAKES IT FAST!
import numpy
import scipy
import struct
import pyaudio
import threading
import pylab
import struct
class VocolRecorder:
### grab stuff from the microphone ###
def __init__(self):
# tiny setup stuff
self.RATE = 48100
self.BUFFERSIZE = 2**12 # 2^12 = 4096
self.secToRecord = .1
self.cancelNow = False
def setup(self):
### setup soundcard ###
self.buffersToRecord=int(self.RATE * self.secToRecord / self.BUFFERSIZE)
if self.buffersToRecord == 0:
self.buffersToRecord = 1
self.samplesToRecord = int(self.BUFFERSIZE * self.buffersToRecord)
self.chunksToRecord = int(self.samplesToRecord / self.BUFFERSIZE)
self.secPerPoint = 1.0 / self.RATE
# setup pyaudio
self.p = pyaudio.PyAudio()
# set the stream
self.inStream = self.p.open(format = pyaudio.paInt16, channels=1, rate=self.RATE, input=True, frames_per_buffer = self.BUFFERSIZE)
# prep stuff for math
self.xsBuffer = numpy.arange(self.BUFFERSIZE) * self.secPerPoint
self.xs = numpy.arange(self.chunksToRecord * self.BUFFERSIZE) * self.secPerPoint
self.audio = numpy.empty((self.chunksToRecord * self.BUFFERSIZE), dtype=numpy.int16)
def record(self):
i = 0
while not self.cancelNow:
if i == 30: self.stopRecording()
i += 1
print(i)
print('recording')
def startRecording(self):
# setup threading and start recording
self.t = threading.Thread(target = self.record)
self.t.start()
def stopRecording(self):
self.cancelNow = True
Vocol = VocolRecorder()
Vocol.startRecording()