Browse Source

refactor out window code

canon
Hazel Levine 9 months ago
parent
commit
cf892a3150
5 changed files with 38 additions and 39 deletions
  1. +2
    -2
      README.md
  2. +0
    -8
      src/fourier.rs
  3. +1
    -0
      src/lib.rs
  4. +4
    -29
      src/main.rs
  5. +31
    -0
      src/window.rs

+ 2
- 2
README.md View File

@ -3,6 +3,6 @@
A music visualizer for my AP Calculus BC final project.
## Running
`ffmpeg -f pulse -i <your input device/monitor> -f s16le - | wizja`
`wizja`
change accordingly if you're on big endian or not running PulseAudio
if you aren't using PulseAudio this won't work. i don't really care though

+ 0
- 8
src/fourier.rs View File

@ -2,14 +2,6 @@ use rustfft::num_complex::Complex;
use rustfft::num_traits::Zero;
use rustfft::FFTplanner;
fn mean(list: &[f32]) -> u64 {
if list.len() == 0 {
0
} else {
(list.iter().sum::<f32>() / list.len() as f32) as u64
}
}
#[derive(Debug)]
pub struct Fourier {
pub buf: Vec<Complex<f32>>,


+ 1
- 0
src/lib.rs View File

@ -1,3 +1,4 @@
pub mod event;
pub mod fourier;
pub mod pulse;
pub mod window;

+ 4
- 29
src/main.rs View File

@ -12,35 +12,10 @@ use tui::{
use wizja::{
event::{Event, Events},
fourier::Fourier,
pulse::PARecorder,
window::Window,
};
#[derive(Debug)]
struct Window<'a> {
spectrum: Vec<(&'a str, u64)>,
}
impl<'a> Window<'a> {
fn new() -> Window<'a> {
Window {
spectrum: vec![("", 0); 12],
}
}
fn update(&mut self, rec: &mut PARecorder) -> Result<()> {
rec.update()?;
self.spectrum = Fourier::new(&rec.buf)
.normalize()
.phase_shift()
.cutoff_filter()
.analyze(12);
Ok(())
}
}
fn main() -> Result<()> {
let stdout = io::stdout().into_raw_mode()?;
let stdout = MouseTerminal::from(stdout);
@ -50,9 +25,9 @@ fn main() -> Result<()> {
terminal.hide_cursor()?;
let events = Events::new();
let mut win = Window::new();
let mut win = Window::new(24);
let mut rec = PARecorder::new(256)?;
let mut rec = PARecorder::new(1024)?;
loop {
terminal.draw(|mut f| {
@ -65,7 +40,7 @@ fn main() -> Result<()> {
.block(Block::default().title("wizja 0.1.0").borders(Borders::ALL))
.data(&win.spectrum)
.bar_width(4)
.max(100)
.max(500)
.style(Style::default().fg(Color::Blue))
.value_style(Style::default().fg(Color::Black).bg(Color::Blue));
f.render_widget(barchart, chunks[0]);


+ 31
- 0
src/window.rs View File

@ -0,0 +1,31 @@
use anyhow::Result;
use crate::{
fourier::Fourier,
pulse::PARecorder,
};
#[derive(Debug)]
pub struct Window<'a> {
pub spectrum: Vec<(&'a str, u64)>,
}
impl<'a> Window<'a> {
pub fn new(n_bars: usize) -> Window<'a> {
Window {
spectrum: vec![("", 0); n_bars],
}
}
pub fn update(&mut self, rec: &mut PARecorder) -> Result<()> {
rec.update()?;
self.spectrum = Fourier::new(&rec.buf)
.normalize()
.phase_shift()
.cutoff_filter()
.analyze(self.spectrum.len());
Ok(())
}
}

Loading…
Cancel
Save