This repository has been archived by the owner on Apr 18, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.py
61 lines (49 loc) · 1.7 KB
/
main.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
#!/usr/bin/python
import sys
import numpy as np
from PySide import QtCore, QtGui
from visualizer import *
app = QtGui.QApplication(sys.argv)
def record_qt_multimedia():
info = QtMultimedia.QAudioDeviceInfo.defaultInputDevice()
format = info.preferredFormat()
format.setChannels(CHANNEL_COUNT)
format.setChannelCount(CHANNEL_COUNT)
format.setSampleSize(SAMPLE_SIZE)
format.setSampleRate(SAMPLE_RATE)
if not info.isFormatSupported(format):
print 'Format not supported, using nearest available'
format = nearestFormat(format)
if format.sampleSize != SAMPLE_SIZE:
#this is important, since effects assume this sample size.
raise RuntimeError('16-bit sample size not supported!')
audio_input = QtMultimedia.QAudioInput(format, app)
audio_input.setBufferSize(BUFFER_SIZE)
source = audio_input.start()
def read_data():
data = np.fromstring(source.readAll(), 'int16').astype(float)
if len(data):
return data
return read_data
def record_pyaudio():
p = pyaudio.PyAudio()
stream = p.open(format = pyaudio.paInt16,
channels = CHANNEL_COUNT,
rate = SAMPLE_RATE,
input = True,
frames_per_buffer = BUFFER_SIZE)
def read_data():
data = np.fromstring(stream.read(stream.get_read_available()), 'int16').astype(float)
if len(data):
return data
return read_data
try:
from PySide import QtMultimedia
read_data = record_qt_multimedia()
except ImportError:
print 'Using PyAudio'
import pyaudio
read_data = record_pyaudio()
window = LineVisualizer(read_data)
window.show()
app.exec_()