-
Notifications
You must be signed in to change notification settings - Fork 0
/
3_waveform.py
55 lines (39 loc) · 1.24 KB
/
3_waveform.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
# Learning to visualize audio
# Mr. Poznanski == Poz - nan - ski == Pause - NaN - Ski
# Now, we are going to draw the waveform as a graph
import numpy as np
import PySimpleGUI as sg
import pyaudio
sg.theme("BluePurple")
layout = [
[sg.Graph(canvas_size=(500,300),
graph_bottom_left=(0, 0),
graph_top_right=(1,1), key="graph")],
]
window = sg.Window("Visualization", layout, finalize=True)
graph = window["graph"]
# Setup the microphone
RATE = 44100 # (sampling rate) number of frames per second
CHANNELS = 1
CHUNK = 1000 # signal is split into CHUNK number of frames
FORMAT = pyaudio.paInt16
p = pyaudio.PyAudio()
stream = p.open(format=FORMAT,
channels=CHANNELS,
rate=RATE,
input=True,
frames_per_buffer=CHUNK)
while True:
event, values = window.read(timeout=10)
if event == sg.WIN_CLOSED:
break
raw = stream.read(CHUNK, exception_on_overflow=False)
data = np.frombuffer(raw, dtype=np.int16)
data = data.astype(np.float32) / 65535.0
graph.erase()
points = []
for x, y in enumerate(data):
points.append((x / CHUNK, y * 2 + 0.5))
graph.draw_lines(points, color="red", width=1)
stream.close()
window.close()