-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinversor.py
More file actions
33 lines (23 loc) · 734 Bytes
/
inversor.py
File metadata and controls
33 lines (23 loc) · 734 Bytes
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
import pyaudio
import struct
import numpy as np
CHUNK = 1024
#CHUNK = 512
CHUNK = 10
WIDTH = 2
CHANNELS = 2
RATE = 44100
RECORD_SECONDS = 5
p = pyaudio.PyAudio()
stream = p.open(format=p.get_format_from_width(WIDTH), channels=CHANNELS, rate=RATE, input=True, output=True, frames_per_buffer=CHUNK)
print("* running...")
for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
buff = stream.read(CHUNK, exception_on_overflow=False)
freq_as_list = np.frombuffer(buff, dtype='B')
cancellation_freq = map(lambda x: x * -1, freq_as_list)
cancellation_buff = np.fromiter(cancellation_freq, dtype=np.int).tobytes()
stream.write(cancellation_buff)
print("* done")
stream.stop_stream()
stream.close()
p.terminate()