-
Notifications
You must be signed in to change notification settings - Fork 0
/
AudioRecorder.py
78 lines (71 loc) · 2.87 KB
/
AudioRecorder.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
from PyQt5.QtCore import (QObject, pyqtSignal, QTimer, Qt, pyqtSlot, QThread,
QPointF, QRectF, QLineF, QRect)
import pyaudio
import queue
import wave
import numpy as np
RATE = 44100
CHUNK = 4096
class AudioRecorder(QObject):
'''
AudioRecorder(QObject): thread which accepts input from specified
audio input device (default is 0) in chunks, then pushes audio to
queue for processing by Chromatizer thread.
'''
signalToChromatizer = pyqtSignal(object)
def __init__(self, queue, wavfile=None, rate = RATE, chunk = CHUNK,
input_device_index = 0):
QObject.__init__(self)
self.rate = rate
self.i=0
if wavfile != None:
self.file = wave.open(wavfile, 'r')
else:
self.file = None
self.chunk = chunk
self.queue = queue
self.p = pyaudio.PyAudio()
self.input_device_index = input_device_index
if self.file == None:
self.stream = self.p.open(format= pyaudio.paFloat32,
channels = 1,
rate = self.rate,
input = True,
input_device_index = self.input_device_index,
frames_per_buffer = self.chunk,
stream_callback = self._callback)
else:
self.stream = self.p.open(format=self.p.get_format_from_width(self.file.getsampwidth()),
channels = self.file.getnchannels(),
rate = self.rate,
input = True,
output = True,
#output = True,
frames_per_buffer = self.chunk,
stream_callback = self._callback)
self.stop = False
def stopStream(self):
self.stream.stop_stream()
self.stream.close()
self.p.terminate()
self.file.close()
def _callback(self, in_data, frame_count, time_info, status):
"""
grab data from buffer,
put data and rate into queue
continue
"""
data = self.file.readframes(frame_count)
if self.file != None:
if data != '':
data = np.frombuffer(data, "int16")
data_per_channel=[data[chan::self.file.getnchannels()] for chan in range(self.file.getnchannels())]
mono = (data_per_channel[0] + data_per_channel[1])/2
self.signalToChromatizer.emit(data)
self.i += 1
else:
self.stopStream()
else:
data = np.frombuffer(in_data, "float32")
self.signalToChromatizer.emit(data)
return (data, pyaudio.paContinue)