diff --git a/Cargo.lock b/Cargo.lock index be1b99a..8ed0c93 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -101,6 +101,7 @@ dependencies = [ name = "audio-reactive-source" version = "0.1.0" dependencies = [ + "bytemuck", "color-eyre", "cpal", "env_logger", @@ -171,6 +172,20 @@ name = "bytemuck" version = "1.21.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ef657dfab802224e671f5818e9a4935f9b1957ed18e58292690cc39e7a4092a3" +dependencies = [ + "bytemuck_derive", +] + +[[package]] +name = "bytemuck_derive" +version = "1.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bcfcc3cd946cb52f0bbfdbbcfa2f4e24f75ebb6c0e1002f7c25904fada18b9ec" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] [[package]] name = "bytes" diff --git a/Cargo.toml b/Cargo.toml index ebaf86a..7045454 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,4 +15,6 @@ color-eyre = "0.6" cpal = { version = "0.15", features = ["jack"] } realfft = "3.4" +bytemuck = { version = "1.21", features = ["derive"] } + textplots = "0.8" diff --git a/src/main.rs b/src/main.rs index b8879b0..a6494f4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,6 @@ -use std::{cmp::Ordering, sync::mpsc, thread::sleep, time::Duration}; +use std::{cmp::Ordering, net::UdpSocket, sync::mpsc, thread::sleep, time::Duration}; +use bytemuck::{Pod, Zeroable}; use color_eyre::eyre::{bail, Result}; use cpal::{ traits::{DeviceTrait, HostTrait, StreamTrait}, @@ -72,6 +73,8 @@ fn main() -> color_eyre::Result<()> { None, )?; + let socket = UdpSocket::bind("239.0.0.1:11988")?; + stream.play()?; let mut previous_sample_smth = 0.0; @@ -80,19 +83,24 @@ fn main() -> color_eyre::Result<()> { data.sample_smth = 0.8f32.mul_add(previous_sample_smth, 0.2 * data.sample_raw); previous_sample_smth = data.sample_smth; - let plot_data: Vec<_> = data - .fft_result - .iter() - .enumerate() - .map(|(i, datum)| (i as f32, f32::from(*datum))) - .collect(); - Chart::new_with_y_range(250, 50, 0.0, 16.0, 0.0, 255.0) - .lineplot(&Shape::Bars(&plot_data[..])) - .display(); + if let Err(e) = socket.send_to(bytemuck::bytes_of(&data), "239.0.0.1:11988") { + error!("Unable to send packet over multicast: {e}"); + } + + // let plot_data: Vec<_> = data + // .fft_result + // .iter() + // .enumerate() + // .map(|(i, datum)| (i as f32, f32::from(*datum))) + // .collect(); + // Chart::new_with_y_range(250, 50, 0.0, 16.0, 0.0, 255.0) + // .lineplot(&Shape::Bars(&plot_data[..])) + // .display(); } } /// new "V2" audiosync struct - 44 Bytes +#[derive(Debug, PartialEq, Clone, Copy, Pod, Zeroable)] #[repr(C, packed(1))] struct AudioSyncPacket { /// "00002" for protocol version 2 (includes '\0' for c-style string termination)