Skip to content

Commit 4dae122

Browse files
committed
Various viz improvements
1 parent 07b8fe4 commit 4dae122

File tree

5 files changed

+50
-10
lines changed

5 files changed

+50
-10
lines changed

live/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ documentation = "https://docs.rs/caw_live"
1212
[dependencies]
1313
caw_core = { version = "0.5", path = "../core" }
1414
caw_player = { version = "0.8", path = "../player" }
15+
caw_viz_udp_app_lib = { version = "0.1", path = "../viz-udp-app-lib" }
1516
lazy_static = "1.5"

live/src/lib.rs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ use caw_core::{Sig, SigBoxedVar, Stereo, StereoPair, svf32};
22
use caw_player::{
33
PlayerConfig, PlayerVisualizationData, VisualizationDataPolicy, play_stereo,
44
};
5+
use caw_viz_udp_app_lib::{VizAppConfig, VizUdpServer};
56
use lazy_static::lazy_static;
6-
use std::sync::Mutex;
7+
use std::{sync::Mutex, thread, time::Duration};
78

89
lazy_static! {
910
static ref OUT: StereoPair<Sig<SigBoxedVar<f32>>> =
@@ -13,9 +14,11 @@ lazy_static! {
1314
static ref INITIALIZED: Mutex<bool> = Mutex::new(false);
1415
}
1516

16-
pub fn live_stereo_visualized(
17+
pub type LiveStereoOut = StereoPair<Sig<SigBoxedVar<f32>>>;
18+
19+
pub fn live_stereo_viz(
1720
visualization_data_policy: VisualizationDataPolicy,
18-
) -> (StereoPair<Sig<SigBoxedVar<f32>>>, PlayerVisualizationData) {
21+
) -> (LiveStereoOut, PlayerVisualizationData) {
1922
let mut initialized = INITIALIZED.lock().unwrap();
2023
if *initialized {
2124
return (OUT.clone(), VISUALIZATION_DATA.lock().unwrap().clone());
@@ -35,7 +38,20 @@ pub fn live_stereo_visualized(
3538
(OUT.clone(), visualization_data_ref.clone())
3639
}
3740

38-
pub fn live_stereo() -> StereoPair<Sig<SigBoxedVar<f32>>> {
41+
pub fn live_stereo_viz_udp(config: VizAppConfig) -> LiveStereoOut {
42+
let (out, viz_data) = live_stereo_viz(VisualizationDataPolicy::All);
43+
let mut viz = VizUdpServer::new(config).unwrap();
44+
thread::spawn(move || {
45+
loop {
46+
// TODO: Is it ok to ignore errors here?
47+
let _ = viz_data.with_and_clear(|buf| viz.send_samples(buf));
48+
thread::sleep(Duration::from_millis(16));
49+
}
50+
});
51+
out
52+
}
53+
54+
pub fn live_stereo() -> LiveStereoOut {
3955
let mut initialized = INITIALIZED.lock().unwrap();
4056
if *initialized {
4157
return OUT.clone();

viz-udp-app-lib/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ impl Default for VizAppConfig {
3131
program_name: PROGRAM_NAME.to_string(),
3232
width: 640,
3333
height: 480,
34-
scale: 100.0,
34+
scale: 1000.0,
3535
max_num_samples: 5000,
3636
line_width: 1,
3737
alpha_scale: 255,

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.1.0"
3+
version = "0.2.0"
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: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ struct Args {
1313
width: u32,
1414
#[arg(long, default_value_t = 480)]
1515
height: u32,
16-
#[arg(long, default_value_t = 100.0)]
16+
#[arg(long, default_value_t = 1000.0)]
1717
scale: f32,
1818
#[arg(long, default_value_t = 5000)]
1919
max_num_samples: usize,
@@ -35,7 +35,7 @@ struct ScopeState {
3535
}
3636

3737
fn main() -> anyhow::Result<()> {
38-
let args = Args::parse();
38+
let mut args = Args::parse();
3939
let sdl_context = sdl2::init().map_err(|e| anyhow!(e))?;
4040
let video_subsystem = sdl_context.video().map_err(|e| anyhow!(e))?;
4141
let window = video_subsystem
@@ -53,8 +53,31 @@ fn main() -> anyhow::Result<()> {
5353
let mut scope_state = ScopeState::default();
5454
loop {
5555
for event in event_pump.poll_iter() {
56-
if let Event::Quit { .. } = event {
57-
std::process::exit(0);
56+
match event {
57+
Event::Quit { .. } => std::process::exit(0),
58+
Event::MouseWheel { y, .. } => {
59+
let ratio = 1.1;
60+
if y > 0 {
61+
args.scale *= ratio;
62+
} else {
63+
args.scale /= ratio;
64+
}
65+
}
66+
Event::MouseMotion {
67+
mousestate, xrel, ..
68+
} => {
69+
if mousestate.left() {
70+
args.line_width = ((args.line_width as i32) + xrel)
71+
.clamp(1, 20)
72+
as u32;
73+
}
74+
if mousestate.right() {
75+
args.alpha_scale = (args.alpha_scale as i32 + xrel)
76+
.clamp(1, 255)
77+
as u8;
78+
}
79+
}
80+
_ => (),
5881
}
5982
}
6083
while let Ok(true) = viz_udp_client.recv_sample() {

0 commit comments

Comments
 (0)