-
Notifications
You must be signed in to change notification settings - Fork 1
/
listener.py
75 lines (58 loc) · 2.15 KB
/
listener.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
import librosa
import pyaudio
import wave
import datetime
import numpy as np
class Listener:
"""docstring for Listener."""
def __init__(self):
self.CHUNK = 1600
self.FORMAT = pyaudio.paInt16
self.CHANNELS = 2
self.RATE = 16000
self.WAVE_OUTPUT_FILENAME = "buffer.wav"
def listen_continuous(self):
self.pa = pyaudio.PyAudio()
def callback(in_data, frame_count, time_info, flag):
audio_data = np.fromstring(in_data, dtype=np.int16)
# Instead of printing, process here the audio chunk 'audio_data' with libROSA
# [...]
print(audio_data)
return None, pyaudio.paContinue
stream = self.pa.open(format=self.FORMAT,
channels=self.CHANNELS,
rate=self.RATE,
input=True,
frames_per_buffer=self.CHUNK,
stream_callback=callback)
stream.start_stream()
while stream.is_active():
time.sleep(0.25)
stream.close()
pa.terminate()
def listen_sec(self, length_seconds):
self.pa = pyaudio.PyAudio()
stream = self.pa.open(format=self.FORMAT,
channels=self.CHANNELS,
rate=self.RATE,
input=True,
frames_per_buffer=self.CHUNK)
frames = []
print('recording...')
for i in range(0, int(self.RATE / self.CHUNK * length_seconds)):
data = stream.read(self.CHUNK)
frames.append(data)
print('done')
stream.stop_stream()
stream.close()
self.pa.terminate()
return frames
def save_wav(self, frames):
timestamp = '{:%Y-%m-%d-%H-%M-%S}'.format(datetime.datetime.now())
waveFile = wave.open('../record-'+timestamp+'.wav', 'wb')
waveFile.setnchannels(self.CHANNELS)
waveFile.setsampwidth(self.pa.get_sample_size(self.FORMAT))
waveFile.setframerate(self.RATE)
waveFile.writeframes(b''.join(frames))
waveFile.close()
return '../record-'+timestamp+'.wav'