forked from torvalds/AudioNoise
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvisualize.py
More file actions
374 lines (296 loc) · 13.3 KB
/
visualize.py
File metadata and controls
374 lines (296 loc) · 13.3 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
import sys
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
from matplotlib.widgets import Slider, RectangleSelector
import os
import argparse
# --- Constants ---
INITIAL_WINDOW_SEC = 2.0
BYTES_PER_SAMPLE = 4
MAX_WIDTH_SEC = 2.0 # Max zoom out
class WaveformVisualizer:
def __init__(self, filenames, rate, min_zoom_samples=100):
self.rate = rate
self.navigating = False
self.filenames = filenames
self.min_zoom_samples = min_zoom_samples
self.min_width_sec = min_zoom_samples / rate
self.mapped_files = []
self.lines = []
self.max_samples = 0
# Load files
for f in filenames:
try:
fsize = os.path.getsize(f)
samples = fsize // BYTES_PER_SAMPLE
mm = np.memmap(f, dtype=np.int32, mode='r', shape=(samples,))
self.mapped_files.append((mm, os.path.basename(f)))
self.max_samples = max(self.max_samples, samples)
except Exception as e:
print(f"Error opening {f}: {e}")
if not self.mapped_files:
return
self.duration_sec = self.max_samples / self.rate
self.setup_ui()
def setup_ui(self):
self.fig, self.ax = plt.subplots(figsize=(12, 6))
# Manual "tight layout" to maximize space but keep room for slider
plt.subplots_adjust(left=0.05, right=0.95, top=0.95, bottom=0.15)
# Create line objects
for _, name in self.mapped_files:
line, = self.ax.plot([], [], linewidth=0.8, label=name)
self.lines.append(line)
self.ax.grid(True, which='both', linestyle=':', alpha=0.5)
self.ax.set_xlabel("Time (s)")
self.ax.set_ylabel("Amplitude")
self.ax.legend(loc='upper right', fontsize='x-small')
# Slider setup
ax_slider = plt.axes([0.15, 0.05, 0.7, 0.03])
self.slider = Slider(ax_slider, 'Time', 0, self.duration_sec, valinit=0)
self.slider.on_changed(self.update_slider)
# Scroll Zoom setup
self.fig.canvas.mpl_connect('scroll_event', self.on_scroll)
self.fig.canvas.mpl_connect('key_press_event', self.on_key)
self.ax.callbacks.connect('xlim_changed', self.on_xlim_changed)
# Custom Rectangle Selector
self.rs = RectangleSelector(
self.ax, self.on_select,
useblit=True,
button=[1], # Left mouse button
minspanx=5, minspany=5,
spancoords='pixels',
interactive=False
)
# Initial View
self.update_view(0, INITIAL_WINDOW_SEC)
plt.show()
def get_chunk(self, start_time, window_duration):
start_sample = int(start_time * self.rate)
# Ensure we don't request negative start
start_sample = max(0, start_sample)
end_sample = int((start_time + window_duration) * self.rate)
global_min_y, global_max_y = 1e9, -1e9
has_data = False
for line, (mm, _) in zip(self.lines, self.mapped_files):
if start_sample >= mm.size:
line.set_data([], [])
continue
safe_end = min(end_sample, mm.size)
if safe_end <= start_sample:
line.set_data([], [])
continue
chunk = mm[start_sample:safe_end]
if chunk.size > 0:
normalized = chunk.astype(np.float32) / 2147483648.0
# Precise time axis using arange
t_origin = start_sample / self.rate
t_axis = np.arange(chunk.size) / self.rate + t_origin
line.set_data(t_axis, normalized)
# Show markers if zoomed in enough (few samples visible)
if chunk.size < 300:
line.set_marker('.')
line.set_markersize(3)
else:
line.set_marker("")
global_min_y = min(global_min_y, np.min(normalized))
global_max_y = max(global_max_y, np.max(normalized))
has_data = True
else:
line.set_data([], [])
return has_data, global_min_y, global_max_y
def update_view(self, start_time, width):
"""Core update logic: loads data and sets limits (Constrained Mode)."""
if self.navigating: return
self.navigating = True
try:
# Clamp width
width = max(self.min_width_sec, min(width, MAX_WIDTH_SEC))
# Clamp start time
start_time = max(0, min(start_time, self.duration_sec))
has_data, min_y, max_y = self.get_chunk(start_time, width)
# IMPORTANT: Set limits on the MAIN axes, explicitly using self.ax
self.ax.set_xlim(start_time, start_time + width)
# Tight Y-axis scaling logic
if has_data and max_y > min_y:
# Symmetric zoom centered at 0
max_val = max(abs(min_y), abs(max_y))
if max_val < 1e-6:
max_val = 0.01
else:
max_val *= 1.05
self.ax.set_ylim(-max_val, max_val)
else:
# Default fallback if no data
self.ax.set_ylim(-1.0, 1.0)
self.fig.canvas.draw_idle()
finally:
self.navigating = False
def update_slider(self, val):
"""Callback for slider change."""
if self.navigating: return
# Get current width from the main property
current_xlim = self.ax.get_xlim()
width = current_xlim[1] - current_xlim[0]
# Fallback for init
if width <= 0: width = INITIAL_WINDOW_SEC
self.update_view(val, width)
def on_scroll(self, event):
"""Handle zoom."""
if event.inaxes != self.ax: return
xlim = self.ax.get_xlim()
cur_width = xlim[1] - xlim[0]
scale_factor = 0.8 if event.button == 'up' else 1.2
new_width = cur_width * scale_factor
# Clamp zoom
new_width = max(self.min_width_sec, min(new_width, MAX_WIDTH_SEC))
# Center zoom
rel_x = (event.xdata - xlim[0]) / cur_width
new_start = event.xdata - (new_width * rel_x)
# Clamp bounds
new_start = max(0, min(new_start, self.duration_sec - new_width))
# CRITICAL FIX: Set the plot limits FIRST
self.ax.set_xlim(new_start, new_start + new_width)
# Then update slider
self.slider.set_val(new_start)
def on_xlim_changed(self, ax):
"""Handle external xlim changes (e.g. from Toolbar). Unconstrained load."""
if self.navigating: return
self.navigating = True
try:
xlim = ax.get_xlim()
start_time = xlim[0]
width = xlim[1] - xlim[0]
# Reload data (No constraints)
self.get_chunk(start_time, width)
# Sync slider silently
if hasattr(self, 'slider'):
old_eventson = self.slider.eventson
self.slider.eventson = False
self.slider.set_val(start_time)
self.slider.eventson = old_eventson
finally:
self.navigating = False
def on_key(self, event):
"""Handle keyboard shortcuts (Independent Navigation)."""
if self.navigating: return
self.navigating = True
try:
# Get properties
xlim = self.ax.get_xlim()
ylim = self.ax.get_ylim()
width = xlim[1] - xlim[0]
height = ylim[1] - ylim[0]
changed = False
if event.key == 'right':
shift = width * 0.25
self.ax.set_xlim(xlim[0] + shift, xlim[1] + shift)
changed = True
elif event.key == 'left':
shift = width * 0.25
self.ax.set_xlim(xlim[0] - shift, xlim[1] - shift)
changed = True
elif event.key == 'up':
shift = height * 0.25
self.ax.set_ylim(ylim[0] + shift, ylim[1] + shift)
changed = True
elif event.key == 'down':
shift = height * 0.25
self.ax.set_ylim(ylim[0] - shift, ylim[1] - shift)
changed = True
elif event.key == 'pagedown': # Zoom In (0.5x width)
center = (xlim[0] + xlim[1]) / 2
new_width = width * 0.5
new_width = max(new_width, self.min_width_sec)
new_start = center - (new_width / 2)
# Clamp start
new_start = max(0, min(new_start, self.duration_sec - new_width))
self.ax.set_xlim(new_start, new_start + new_width)
changed = True
elif event.key == 'pageup': # Zoom Out (2x width)
center = (xlim[0] + xlim[1]) / 2
new_width = width * 2.0
new_width = min(new_width, MAX_WIDTH_SEC)
new_start = center - (new_width / 2)
# Clamp start
new_start = max(0, min(new_start, self.duration_sec - new_width))
self.ax.set_xlim(new_start, new_start + new_width)
changed = True
if changed:
# Reload data for new X view
# We need new xlim
new_xlim = self.ax.get_xlim()
self.get_chunk(new_xlim[0], new_xlim[1] - new_xlim[0])
# Sync slider silently
if hasattr(self, 'slider'):
old_eventson = self.slider.eventson
self.slider.eventson = False
self.slider.set_val(new_xlim[0])
self.slider.eventson = old_eventson
self.fig.canvas.draw_idle()
finally:
self.navigating = False
# Space Bar (Reset Zoom) - Only reset Y axis to fit visible data (Auto-scale)
if event.key == ' ':
xlim = self.ax.get_xlim()
width = xlim[1] - xlim[0]
self.update_view(xlim[0], width)
def on_select(self, eclick, erelease):
"""Handle rectangle selection."""
if self.navigating: return
# Calculate new ranges from the selection
x1, y1 = eclick.xdata, eclick.ydata
x2, y2 = erelease.xdata, erelease.ydata
start_time = min(x1, x2)
end_time = max(x1, x2)
width = end_time - start_time
# Enforce minimum width
if width < self.min_width_sec:
center = (start_time + end_time) / 2
width = self.min_width_sec
start_time = center - width / 2
# Clamp start time
start_time = max(0, min(start_time, self.duration_sec - width))
# Update view (which reloads data)
# Note: update_view handles height scaling automatically based on data,
# but if we want to respect the user's Y selection we might need to override.
# However, for audio, usually we want to see the full amplitude or auto-scaled.
# The user asked for "arbitrary zooming be implemented", which implies Y might be important.
# But our `update_view` forces Y symmetry currently.
# Let's trust `update_view` for now as it handles the complexity of data loading.
# If we really want to zoom to the Y rectangle, we would set ylim manually.
# Given the previous context was about "Zoom to Rectangle" interfering with "movement and zoom control",
# let's try to match the "Zoom to Rectangle" behavior which DOES set specific X/Y limits.
# However, our update_view logic re-calculates Y based on data in the new X range...
# Let's modify the flow slightly: use update_view for X, then apply Y if it makes sense?
# Actually, standard audio visualization often keeps Y symmetric or 0-1.
# If the user draws a small box on a peak, they expect to zoom into that peak (Y-wise too).
# So let's set limits directly, similar to on_scroll/on_key, but for both axes.
if self.navigating: return
self.navigating = True
try:
# X Axis Logic
new_width = max(self.min_width_sec, min(width, MAX_WIDTH_SEC))
# If user selected < min width, we centered it above.
self.get_chunk(start_time, new_width)
self.ax.set_xlim(start_time, start_time + new_width)
# Y Axis Logic
y_min = min(y1, y2)
y_max = max(y1, y2)
self.ax.set_ylim(y_min, y_max)
self.fig.canvas.draw_idle()
# Sync Slider
if hasattr(self, 'slider'):
old_eventson = self.slider.eventson
self.slider.eventson = False
self.slider.set_val(start_time)
self.slider.eventson = old_eventson
finally:
self.navigating = False
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Linux Audio Waveform Visualizer 2026 (mmap)")
parser.add_argument('files', nargs='+', help="Input .bin files (int32)")
parser.add_argument('--rate', type=int, default=48000, help="Sample rate (Hz)")
parser.add_argument('--min-zoom-samples', type=int, default=100, help="Minimum samples to show when zoomed in")
args = parser.parse_args()
app = WaveformVisualizer(args.files, args.rate, args.min_zoom_samples)