3
This plot displays the audio spectrum from the microphone.
5
Based on updating_plot.py
8
# Major library imports
10
from numpy import zeros, linspace, short, fromstring, hstack, transpose
13
# Enthought library imports
14
from chaco.default_colormaps import jet
15
from traits.api import HasTraits, Instance, Any
18
from chaco.api import Plot, ArrayPlotData, HPlotContainer, \
20
from chaco.tools.api import MoveTool, PanTool, ZoomTool
24
SPECTROGRAM_LENGTH = 100
26
def create_plot_component(obj):
27
# Setup the spectrum plot
28
frequencies = linspace(0., float(SAMPLING_RATE)/2, num=NUM_SAMPLES/2)
29
obj.spectrum_data = ArrayPlotData(frequency=frequencies)
30
empty_amplitude = zeros(NUM_SAMPLES/2)
31
obj.spectrum_data.set_data('amplitude', empty_amplitude)
33
obj.spectrum_plot = Plot(obj.spectrum_data)
34
obj.spectrum_plot.plot(("frequency", "amplitude"), name="Spectrum",
36
obj.spectrum_plot.padding = 50
37
obj.spectrum_plot.title = "Spectrum"
38
spec_range = obj.spectrum_plot.plots.values()[0][0].value_mapper.range
41
obj.spectrum_plot.index_axis.title = 'Frequency (hz)'
42
obj.spectrum_plot.value_axis.title = 'Amplitude'
45
times = linspace(0., float(NUM_SAMPLES)/SAMPLING_RATE, num=NUM_SAMPLES)
46
obj.time_data = ArrayPlotData(time=times)
47
empty_amplitude = zeros(NUM_SAMPLES)
48
obj.time_data.set_data('amplitude', empty_amplitude)
50
obj.time_plot = Plot(obj.time_data)
51
obj.time_plot.plot(("time", "amplitude"), name="Time", color="blue")
52
obj.time_plot.padding = 50
53
obj.time_plot.title = "Time"
54
obj.time_plot.index_axis.title = 'Time (seconds)'
55
obj.time_plot.value_axis.title = 'Amplitude'
56
time_range = obj.time_plot.plots.values()[0][0].value_mapper.range
61
spectrogram_data = zeros(( NUM_SAMPLES/2, SPECTROGRAM_LENGTH))
62
obj.spectrogram_plotdata = ArrayPlotData()
63
obj.spectrogram_plotdata.set_data('imagedata', spectrogram_data)
64
spectrogram_plot = Plot(obj.spectrogram_plotdata)
65
spectrogram_time = linspace(
66
0.0, float(SPECTROGRAM_LENGTH*NUM_SAMPLES)/float(SAMPLING_RATE),
67
num=SPECTROGRAM_LENGTH)
68
spectrogram_freq = linspace(0.0, float(SAMPLING_RATE/2), num=NUM_SAMPLES/2)
69
spectrogram_plot.img_plot('imagedata',
71
xbounds=spectrogram_time,
72
ybounds=spectrogram_freq,
75
range_obj = spectrogram_plot.plots['Spectrogram'][0].value_mapper.range
78
spectrogram_plot.title = 'Spectrogram'
79
obj.spectrogram_plot = spectrogram_plot
81
return obj.spectrum_plot, obj.time_plot, obj.spectrogram_plot
87
# The audio stream is opened the first time this function gets called.
88
# The stream is always closed (if it was opened) in a try finally
89
# block at the end of this file,
90
pa = pyaudio.PyAudio()
91
_stream = pa.open(format=pyaudio.paInt16, channels=1,
93
input=True, frames_per_buffer=NUM_SAMPLES)
95
audio_data = fromstring(_stream.read(NUM_SAMPLES), dtype=short)
96
normalized_data = audio_data / 32768.0
97
return (abs(fft(normalized_data))[:NUM_SAMPLES/2], normalized_data)
99
class TimerController(HasTraits):
103
def on_timer(self, vtk_obj=None, eventname=""):
105
spectrum, time = get_audio_data()
108
self.spectrum_data.set_data('amplitude', spectrum)
109
self.time_data.set_data('amplitude', time)
110
spectrogram_data = self.spectrogram_plotdata.get_data('imagedata')
111
spectrogram_data = hstack((spectrogram_data[:,1:],
112
transpose([spectrum])))
114
self.spectrogram_plotdata.set_data('imagedata', spectrogram_data)
115
self.spectrum_plot.request_redraw()
119
from tvtk.api import tvtk
120
from mayavi import mlab
121
from enable.vtk_backend.vtk_window import EnableVTKWindow
122
f = mlab.figure(size=(900,850))
124
scene = mlab.gcf().scene
125
render_window = scene.render_window
126
renderer = scene.renderer
127
rwi = scene.interactor
130
timer_controller = TimerController()
131
plots = create_plot_component(timer_controller)
132
specplot, timeplot, spectrogram = plots
134
for i, p in enumerate(plots):
135
p.set(resizable = "", bounds = [200,200], outer_x = 0,
136
bgcolor = "transparent",
139
p.tools.append(MoveTool(p, drag_button="right"))
140
p.tools.append(PanTool(p))
141
p.tools.append(ZoomTool(p))
143
spectrogram.tools[-1].set(tool_mode="range", axis="value")
144
spectrogram.tools[-2].set(constrain=True, constrain_direction="y")
146
container = OverlayPlotContainer(bgcolor = "transparent",
148
container.add(*plots)
149
container.timer_callback = timer_controller.on_timer
151
window = EnableVTKWindow(rwi, renderer,
152
component = container,
153
istyle_class = tvtk.InteractorStyleTrackballCamera,
154
bgcolor = "transparent",
155
event_passthrough = True,
160
if __name__ == "__main__":