Skip to content

Commit 384f034

Browse files
committed
Horizontal zoom in oscilloscope
1 parent e57e5b4 commit 384f034

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

viz-udp-app/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "caw_viz_udp_app"
3-
version = "0.3.0"
3+
version = "0.3.1"
44
edition = "2024"
55
description = "App for visualizing audio data from the caw synthesizer framework"
66
authors = ["Stephen Sherratt <[email protected]>"]

viz-udp-app/src/main.rs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ struct Cli {
110110
struct OscilloscopeUiState {
111111
style: OscilloscopeStyle,
112112
scale: f32,
113+
time_scale: f32,
113114
line_width: u32,
114115
alpha_scale: u8,
115116
}
@@ -151,7 +152,8 @@ impl App {
151152
ui_state: &OscilloscopeUiState,
152153
rgb: Rgb24,
153154
) {
154-
let num_samples_to_draw = screen_size.x as usize;
155+
let num_samples_to_draw =
156+
(screen_size.x as f32 / ui_state.time_scale).ceil() as usize;
155157
// Take the mean so it can be used to identify the relative 0 crossing in case the waveform
156158
// has drifted above 0 mean.
157159
let mean = {
@@ -189,7 +191,7 @@ impl App {
189191
canvas.set_draw_color(Color::RGBA(rgb.r, rgb.g, rgb.b, 255));
190192
let mut prev = None;
191193
for (x, sample) in sample_mean_iter.enumerate() {
192-
let x = x as i32;
194+
let x = (x as f32 * ui_state.time_scale) as i32;
193195
let y = screen_size.y
194196
- ((sample * ui_state.scale) as i32 + (screen_size.y / 2));
195197
let coord = Coord { x, y };
@@ -217,7 +219,8 @@ impl App {
217219
) {
218220
// A zero crossing of the left channel will be centered in an attempt to stabalize the
219221
// visualization over time.
220-
let num_samples_to_draw = screen_size.x as usize;
222+
let num_samples_to_draw =
223+
(screen_size.x as f32 / ui_state.time_scale).ceil() as usize;
221224
// Take the mean so it can be used to identify the relative 0 crossing in case the waveform
222225
// has drifted above 0 mean.
223226
let left_mean = {
@@ -253,7 +256,7 @@ impl App {
253256
canvas.set_draw_color(Color::RGBA(rgb.r, rgb.g, rgb.b, 255));
254257
let mut prev = None;
255258
for (x, sample) in sample_left_iter.enumerate() {
256-
let x = x as i32;
259+
let x = (x as f32 * ui_state.time_scale) as i32;
257260
let y = screen_size.y
258261
- ((sample * ui_state.scale) as i32
259262
+ ((2 * screen_size.y) / 3));
@@ -273,7 +276,7 @@ impl App {
273276
}
274277
let mut prev = None;
275278
for (x, sample) in sample_right_iter.enumerate() {
276-
let x = x as i32;
279+
let x = (x as f32 * ui_state.time_scale) as i32;
277280
let y = screen_size.y
278281
- ((sample * ui_state.scale) as i32
279282
+ ((1 * screen_size.y) / 3));
@@ -314,6 +317,7 @@ impl App {
314317
OscilloscopeUiState {
315318
style: args.style,
316319
scale: args.scale,
320+
time_scale: 1.,
317321
line_width: args.line_width,
318322
alpha_scale: args.alpha_scale,
319323
}
@@ -323,13 +327,21 @@ impl App {
323327
for event in self.event_pump.poll_iter() {
324328
Self::handle_event_common(event.clone(), self.title.as_str());
325329
match event {
326-
Event::MouseWheel { y, .. } => {
330+
Event::MouseWheel { x, y, .. } => {
327331
let ratio = 1.1;
328332
if y > 0 {
329333
ui_state.scale *= ratio;
330334
} else if y < 0 {
331335
ui_state.scale /= ratio;
332336
}
337+
let time_ratio = 1.1;
338+
if x > 0 {
339+
ui_state.time_scale *= time_ratio;
340+
} else if x < 0 {
341+
ui_state.time_scale /= time_ratio;
342+
}
343+
ui_state.time_scale =
344+
ui_state.time_scale.clamp(0.1, 10.);
333345
ui_state.save_(&self.title);
334346
}
335347
Event::KeyDown {

0 commit comments

Comments
 (0)