-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrealtime_woclasses.py
79 lines (66 loc) · 1.9 KB
/
realtime_woclasses.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
import pyaudio
#import matplotlib.pyplot as plt
import pyqtgraph as pg
from pyqtgraph.Qt import QtGui, QtCore
import numpy
import threading
import time
import wave
import sys
CHUNK = 128 #1024
FORMAT = pyaudio.paInt16 # Try paFloat32 too
CHANNELS = 2
RATE = 48000
RECORD_SECONDS = 20
frames = ''
final = numpy.fromstring(frames, dtype=numpy.int16)
wf = wave.open(sys.argv[1], 'wb')
wf.setnchannels(CHANNELS)
wf.setsampwidth(2)
wf.setframerate(RATE/4)
app = QtGui.QApplication([])
mw = QtGui.QMainWindow()
mw.resize(800, 800)
view = pg.GraphicsLayoutWidget()
mw.setCentralWidget(view)
mw.show()
w1 = view.addPlot(title = "Amplitude vs Time")
w1.setRange(xRange=[0.0, 20000.0], yRange=[0.0, 50000.0])
curve = w1.plot(pen='g')
def callback(in_data, frame_count, time_info, status):
global frames, final, curve
frames += in_data
final = numpy.fromstring(frames, dtype=numpy.int16)
final = abs(final)
final = final
masked = numpy.ma.masked_where(final < 0, final)
masked.fill_value = 0
final = numpy.ma.filled(masked)
wf.writeframes(in_data)
curve.setData(final[-20000:])
print final
return (frames, pyaudio.paContinue)
def getaudio():
global final
frames = ''
p = pyaudio.PyAudio()
stream = p.open(format=FORMAT,
channels=CHANNELS,
rate=RATE, #sampling rate
input=True,
frames_per_buffer=CHUNK,
stream_callback=callback)
stream.start_stream()
stream.stop_stream()
stream.close()
p.terminate()
def main():
frames = ''
final = numpy.fromstring(frames, dtype=numpy.int16)
if __name__ == "__main__":
import sys
if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
timer = QtCore.QTimer()
timer.timeout.connect(getaudio)
timer.start(1)
pg.QtGui.QApplication.instance().exec_()