Skip to content

Commit

Permalink
add spectrum
Browse files Browse the repository at this point in the history
  • Loading branch information
aizcutei committed Jun 4, 2024
1 parent de23257 commit 6385f2b
Show file tree
Hide file tree
Showing 12 changed files with 59 additions and 20 deletions.
Binary file modified .DS_Store
Binary file not shown.
29 changes: 27 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<p align=center>
<picture>
<img alt="nanometers" title="Nanometers" src="assets/icon-1024.png" width=200>
<img alt="nanometers" title="Nanometers" src="./assets/icon-1024.png" width=200>
</picture>
</p>

Expand All @@ -19,7 +19,19 @@ Use together with [nanometers-plugin](https://github.com/aizcutei/nanometers_plu
---
## Preview

![nanometers](./static/example.gif)
![demo1](./static/demo1.png)
![demo2](./static/demo2.png)
![demo3](./static/demo3.png)


<details>
<summary>More:</summary>

![nanometers](./static/Example.gif)
![demo4](./static/demo4.png)
![demo5](./static/demo5.png)
![demo6](./static/demo6.png)
</details>

---
## Audio routing support
Expand All @@ -31,6 +43,19 @@ Use together with [nanometers-plugin](https://github.com/aizcutei/nanometers_plu
|Linux|🚧|✅ * No test yet|✅ * No test yet|

---

### Self build

You should have Rust installed. If not, install it from [here](https://www.rust-lang.org/tools/install).

Clone the repository:

`git clone`

Build the project:

`cargo build --release`

### Testing locally

Make sure you are using the latest version of stable rust by running `rustup update`.
Expand Down
22 changes: 13 additions & 9 deletions src/audio/callback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{setting::*, utils::*};
use crossbeam_channel::{Receiver, Sender};
use egui::{Color32, Pos2};
use realfft::RealFftPlanner;
use rustfft::num_complex::ComplexFloat;
use rustfft::num_complex::{Complex, ComplexFloat};
use std::sync::{Arc, Mutex};

const SQRT_2: f32 = 1.4142135;
Expand Down Expand Up @@ -40,6 +40,10 @@ pub fn get_callback(
let r = data[1][i];
let m = (l + r) / 2.0;
let s = (l - r) / 2.0;
buf.raw.l.push(l);
buf.raw.r.push(r);
buf.raw.m.push(m);
buf.raw.s.push(s);

let raw_len = buf.raw.l.len();

Expand Down Expand Up @@ -215,10 +219,6 @@ pub fn get_callback(
let high_r = multiband_high_filter(r, &mut buf.multiband.high_buf);
let high_m = multiband_high_filter(m, &mut buf.multiband.high_buf);
let high_s = multiband_high_filter(s, &mut buf.multiband.high_buf);
buf.raw.l.push(l);
buf.raw.r.push(r);
buf.raw.m.push(m);
buf.raw.s.push(s);
buf.low_raw.l.push(low_l);
buf.low_raw.r.push(low_r);
buf.low_raw.m.push(low_m);
Expand Down Expand Up @@ -400,17 +400,21 @@ fn process_spectrum(buf: &mut AudioSourceBuffer, send_data: &mut SendData, data:
data.l.extend_from_slice(&[0.0; 2048]);
data.r.extend_from_slice(&[0.0; 2048]);
r2c.process(&mut data.l, &mut spectrum).unwrap();
send_data.spectrum.l = spectrum.clone().iter().map(|i| i.norm()).collect();
send_data.spectrum.l = spectrum.clone().iter().map(|i| amp_to_db(i)).collect();
r2c.process(&mut data.r, &mut spectrum).unwrap();
send_data.spectrum.r = spectrum.clone().iter().map(|i| i.norm()).collect();
send_data.spectrum.r = spectrum.clone().iter().map(|i| amp_to_db(i)).collect();
}
SpectrumChannel::MS => {
data.m.extend_from_slice(&[0.0; 2048]);
data.s.extend_from_slice(&[0.0; 2048]);
r2c.process(&mut data.m, &mut spectrum).unwrap();
send_data.spectrum.l = spectrum.clone().iter().map(|i| i.norm()).collect();
send_data.spectrum.l = spectrum.clone().iter().map(|i| amp_to_db(i)).collect();
r2c.process(&mut data.s, &mut spectrum).unwrap();
send_data.spectrum.r = spectrum.clone().iter().map(|i| i.norm()).collect();
send_data.spectrum.r = spectrum.clone().iter().map(|i| amp_to_db(i)).collect();
}
}
}

fn amp_to_db(amp: &Complex<f32>) -> f32 {
((amp.norm() / 2048.0).log10() * 20.0 + 150.0) / 150.0
}
2 changes: 1 addition & 1 deletion src/frame/setting_panel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,7 @@ impl NanometersApp {
ui.horizontal(|ui| {
ui.label("Smoothing");
ui.add(
egui::Slider::new(&mut self.setting.spectrum.smoothing, 0.0..=1.25)
egui::Slider::new(&mut self.setting.spectrum.smoothing, 0.0..=0.99)
.text(""),
);
});
Expand Down
16 changes: 10 additions & 6 deletions src/frame/spectrum_meter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ impl NanometersApp {
let mut wave_0_points = Vec::new();
let mut wave_1_points = Vec::new();
if !data.l.is_empty() {
println!("{:?}", data.l);
// println!("{:?}", data.l[0]);
for i in 0..2049 {
if data.l[i] > self.spectrum.ch0[i] {
self.spectrum.ch0[i] = data.l[i];
Expand Down Expand Up @@ -272,7 +272,7 @@ fn ref_lines(ui: &mut Ui, rect: Rect, width: f32, color: Color32) -> Vec<Shape>
fonts,
pos2(rect.left() + 0.5983756514 * rect.width(), 5.0),
Align2::CENTER_CENTER,
"1000Hz",
"1kHz",
FontId::monospace(9.0),
color,
));
Expand Down Expand Up @@ -345,7 +345,7 @@ fn ref_lines(ui: &mut Ui, rect: Rect, width: f32, color: Color32) -> Vec<Shape>
fonts,
pos2(rect.left() + 0.8975634771 * rect.width(), 5.0),
Align2::CENTER_CENTER,
"10000Hz",
"10kHz",
FontId::monospace(9.0),
color,
));
Expand All @@ -363,9 +363,13 @@ fn ref_lines(ui: &mut Ui, rect: Rect, width: f32, color: Color32) -> Vec<Shape>
fn freq_to_pos(rect: Rect) -> Vec<f32> {
let mut pos = Vec::with_capacity(2049);
for i in 0..2049 {
pos.push(
rect.left() + 0.2991878257 * (((i - 1) as f32 * 23.4375).log10() - 1.0) * rect.width(),
);
if i == 0 {
pos.push(rect.left());
} else {
pos.push(
rect.left() + 0.2991878257 * ((i as f32 * 11.71875).log10() - 1.0) * rect.width(),
);
}
}
pos
}
10 changes: 8 additions & 2 deletions src/utils/data_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ impl SpectrogramOneWindow {
}
}

#[derive(Debug, Clone, Default)]
#[derive(Debug, Clone)]
pub struct SpectrogramCalcBuffer {
pub ab: bool,
pub a: SpectrogramOneWindow,
Expand All @@ -176,11 +176,17 @@ impl SpectrogramCalcBuffer {
ab: false,
a: SpectrogramOneWindow::new(),
b: SpectrogramOneWindow::new(),
image: vec![Color32::TRANSPARENT; 4096 * 4096],
image: vec![Color32::TRANSPARENT; 4000 * 4000],
}
}
}

impl Default for SpectrogramCalcBuffer {
fn default() -> Self {
Self::new()
}
}

#[derive(Debug, Clone, Default)]
pub struct SpectrumCalcBuffer {
pub ab: bool,
Expand Down
Binary file added static/demo1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added static/demo2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added static/demo3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added static/demo4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added static/demo5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added static/demo6.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 6385f2b

Please sign in to comment.