forked from TUIlmenauAMS/Videocoding
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpyrecplotanimation.py
87 lines (65 loc) · 2.02 KB
/
pyrecplotanimation.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
79
80
81
82
83
84
85
"""
Using Pyaudio, record sound from the audio device and plot, for 8 seconds, and display it live in a Window.
Usage example: python pyrecplotanimation.py test.wav
Gerald Schuller, October 2014
"""
import pyaudio
import struct
#import math
#import array
import numpy as np
import sys
import wave
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import pylab
import cv2
CHUNK = 1024 #Blocksize
WIDTH = 2 #2 bytes per sample
CHANNELS = 1 #2
RATE = 32000 #Sampling Rate in Hz
RECORD_SECONDS = 70
fig, ax = plt.subplots()
x = np.arange(0, CHUNK) # x-array
#Scale axis as this sine function:
line, = ax.plot(x, 20000.0*np.sin(x))
def animate(i):
# update the data
#Reading from audio input stream into data with block length "CHUNK":
data = stream.read(CHUNK)
#Convert from stream of bytes to a list of short integers (2 bytes here) in "samples":
#shorts = (struct.unpack( "128h", data ))
shorts = (struct.unpack( 'h' * CHUNK, data ));
samples=np.array(list(shorts),dtype=float);
#plt.plot(samples) #<-- here goes the signal processing.
#line.set_ydata(np.log((np.abs(pylab.fft(samples))+0.1))/np.log(10.0))
line.set_ydata(samples)
return line,
def init():
line.set_ydata(np.ma.array(x, mask=True))
return line,
p = pyaudio.PyAudio()
a = p.get_device_count()
print("device count=",a)
for i in range(0, a):
print("i = ",i)
b = p.get_device_info_by_index(i)['maxInputChannels']
print(b)
b = p.get_device_info_by_index(i)['defaultSampleRate']
print(b)
stream = p.open(format=p.get_format_from_width(WIDTH),
channels=CHANNELS,
rate=RATE,
input=True,
output=True,
#input_device_index=3,
frames_per_buffer=CHUNK)
print("* recording")
ani = animation.FuncAnimation(fig, animate, np.arange(1, 200), init_func=init,
interval=25, blit=True)
plt.show()
# When everything done, release the capture
print("* done")
f.close()
stream.stop_stream()
stream.close()