Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
aizcutei committed May 31, 2024
1 parent 460c918 commit 8acee83
Show file tree
Hide file tree
Showing 7 changed files with 146 additions and 340 deletions.
14 changes: 8 additions & 6 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,28 @@ use std::{thread, vec};
#[derive(serde::Deserialize, serde::Serialize)]
#[serde(default)] // if we add new fields, give them default values when deserializing old state
pub struct NanometersApp {
/// Thread to capture audio data and process it.
#[serde(skip)]
pub(crate) audio_source: Option<Box<dyn AudioSource>>,
/// Audio source data buffer.
#[serde(skip)]
pub(crate) audio_source_buffer: Arc<Mutex<AudioSourceBuffer>>,
/// Calculate the frame time and FPS.
#[serde(skip)]
pub(crate) frame_history: FrameHistory,

/// Data channel between audio source and GUI.
#[serde(skip)]
pub(crate) tx_data: Option<Sender<SendData>>,
#[serde(skip)]
pub(crate) rx_data: Option<Receiver<SendData>>,

/// Setting channel between GUI and audio source.
#[serde(skip)]
pub(crate) tx_setting: Option<Sender<Setting>>,
#[serde(skip)]
pub(crate) rx_setting: Option<Receiver<Setting>>,

#[serde(skip)]
pub(crate) audio_source_buffer: Arc<Mutex<AudioSourceBuffer>>,

pub(crate) setting: Setting,
pub(crate) sample_rate: AtomicU32,

Expand Down Expand Up @@ -69,12 +73,12 @@ impl Default for NanometersApp {

Self {
audio_source,
audio_source_buffer,
frame_history: Default::default(),
tx_data: Some(tx_data),
rx_data: Some(rx_data),
tx_setting: Some(tx_setting),
rx_setting: Some(rx_setting),
audio_source_buffer,
setting,
sample_rate: AtomicU32::new(48000),
setting_switch: false,
Expand All @@ -93,8 +97,6 @@ impl Default for NanometersApp {
impl NanometersApp {
/// Called once before the first frame.
pub fn new(cc: &eframe::CreationContext<'_>) -> Self {
// This is also where you can customize the look and feel of egui using
// `cc.egui_ctx.set_visuals` and `cc.egui_ctx.set_fonts`.
let version = env!("CARGO_PKG_VERSION").to_string();

if let Some(storage) = cc.storage {
Expand Down
199 changes: 61 additions & 138 deletions src/audio/callback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ pub fn get_callback(

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

if spectrogram_on {
if spectrogram_on || spectrum_on {
if buf.spectrogram.ab {
let spectrogram_index = buf.spectrogram.a.index.clone();
if spectrogram_index >= 1024 {
Expand All @@ -95,150 +95,27 @@ pub fn get_callback(
if buf.spectrogram.ab {
if buf.spectrogram.a.index >= 2048 {
let mut spectrum_buffer = buf.spectrogram.clone();
buf.spectrogram.image.drain(0..2048 * 7);
buf.spectrogram
.image
.extend(vec![Color32::TRANSPARENT; 2048 * 7]);
let mut real_planner = RealFftPlanner::<f32>::new();
let r2c = real_planner.plan_fft_forward(2048);
let mut spectrum = r2c.make_output_vec();
r2c.process(&mut spectrum_buffer.a.raw_hann, &mut spectrum)
.unwrap();
let x = spectrum.clone();
let magsqrd: Vec<f32> = x.iter().map(|i| i.norm_sqr()).collect();
r2c.process(&mut spectrum_buffer.a.raw_hann_dt, &mut spectrum)
.unwrap();
let xdt = spectrum.clone();
r2c.process(&mut spectrum_buffer.a.raw_hann_t, &mut spectrum)
.unwrap();
let xt = spectrum.clone();

let mut forigin = vec![];
let mut fcorrect = vec![];
let mut tcorrect = vec![];
let mut ccorrect = vec![];
for i in 0..1025 {
if magsqrd[i] > 0.0 {
let fc_temp =
(-(xdt[i] * x[i].conj()).im() / magsqrd[i]) + FREQFRAME_2048[i];
let tc_temp = (xt[i] * x[i].conj()).re() / magsqrd[i];

if fc_temp > 10.0 && fc_temp < 24000.0 {
fcorrect.push(fc_temp);
let image_x = fc_temp / 24000.0 * 2048.0;
let image_y = 3840.0 + tc_temp * 7.0;
buf.spectrogram.image
[image_x as usize + (image_y as usize) * 2048] =
Color32::from_rgba_unmultiplied(
255,
255,
255,
(255.0 * x[i].norm()) as u8,
);
} else {
fcorrect.push(0.0);
}

tcorrect.push(tc_temp);
let c_temp = x[i].norm().log10() * 20.0;
ccorrect.push(
if fc_temp > 10.0 && fc_temp < 24000.0 && c_temp > -80.0 {
// c_temp + 60.0
x[i].norm()
} else {
0.0
},
);
} else {
fcorrect.push(0.0);
tcorrect.push(0.0);
ccorrect.push(0.021322916666666667);
}
forigin.push(magsqrd[i].sqrt());
}
send_data.spectrogram.push(SpectrogramFrame {
f: forigin,
fc: fcorrect,
tc: tcorrect,
cc: ccorrect,
});
send_data.spectrogram_image = buf.spectrogram.image.clone();
process_spectrogram(
&mut buf,
&mut send_data,
&mut spectrum_buffer.a.raw_hann,
&mut spectrum_buffer.a.raw_hann_dt,
&mut spectrum_buffer.a.raw_hann_t,
);
buf.spectrogram.a.reset();
buf.spectrogram.ab = !buf.spectrogram.ab;
}
} else {
if buf.spectrogram.b.index >= 2048 {
let mut spectrum_buffer = buf.spectrogram.clone();
buf.spectrogram.image.drain(0..2048 * 3);
buf.spectrogram
.image
.extend(vec![Color32::TRANSPARENT; 2048 * 3]);
let mut real_planner = RealFftPlanner::<f32>::new();
let r2c = real_planner.plan_fft_forward(2048);
let mut spectrum = r2c.make_output_vec();
r2c.process(&mut spectrum_buffer.b.raw_hann, &mut spectrum)
.unwrap();
let x = spectrum.clone();
let magsqrd: Vec<f32> = x.iter().map(|i| i.norm_sqr()).collect();
r2c.process(&mut spectrum_buffer.b.raw_hann_dt, &mut spectrum)
.unwrap();
let xdt = spectrum.clone();
r2c.process(&mut spectrum_buffer.b.raw_hann_t, &mut spectrum)
.unwrap();
let xt = spectrum.clone();

let mut forigin = vec![];
let mut fcorrect = vec![];
let mut tcorrect = vec![];
let mut ccorrect = vec![];
for i in 0..1025 {
if magsqrd[i] > 0.0 {
let fc_temp =
(-(xdt[i] * x[i].conj()).im() / magsqrd[i]) + FREQFRAME_2048[i];
let tc_temp = (xt[i] * x[i].conj()).re() / magsqrd[i];

if fc_temp > 10.0 && fc_temp < 24000.0 {
fcorrect.push(fc_temp);
let image_x = fc_temp / 24000.0 * 2048.0;
let image_y = 3840.0 + tc_temp * 15.0;
buf.spectrogram.image
[image_x as usize + (image_y as usize) * 2048] =
Color32::from_rgba_unmultiplied(
255,
255,
255,
(255.0 * x[i].norm()) as u8,
);
} else {
fcorrect.push(0.0);
}

tcorrect.push(tc_temp);
let c_temp = x[i].norm().log10() * 20.0;
ccorrect.push(
if fc_temp > 10.0 && fc_temp < 24000.0 && c_temp > -80.0 {
// c_temp + 60.0
x[i].norm()
} else {
0.0
},
);
} else {
fcorrect.push(0.0);
tcorrect.push(0.0);
ccorrect.push(0.021322916666666667);
}
forigin.push(magsqrd[i].sqrt());
}
send_data.spectrogram.push(SpectrogramFrame {
f: forigin,
fc: fcorrect,
tc: tcorrect,
cc: ccorrect,
});
send_data.spectrogram_image = buf.spectrogram.image.clone();
process_spectrogram(
&mut buf,
&mut send_data,
&mut spectrum_buffer.b.raw_hann,
&mut spectrum_buffer.b.raw_hann_dt,
&mut spectrum_buffer.b.raw_hann_t,
);
buf.spectrogram.b.reset();
buf.spectrogram.ab = !buf.spectrogram.ab;
}
}
} else {
Expand Down Expand Up @@ -385,3 +262,49 @@ pub fn get_callback(
tx_data.send(send_data).unwrap();
})
}

fn process_spectrogram(
buf: &mut AudioSourceBuffer,
send_data: &mut SendData,
raw_hann: &mut [f32],
raw_hann_dt: &mut [f32],
raw_hann_t: &mut [f32],
) {
buf.spectrogram.image.drain(0..2048 * 2);
buf.spectrogram
.image
.extend(vec![Color32::TRANSPARENT; 2048 * 2]);
let mut real_planner = RealFftPlanner::<f32>::new();
let r2c = real_planner.plan_fft_forward(2048);
let mut spectrum = r2c.make_output_vec();
r2c.process(raw_hann, &mut spectrum).unwrap();
let fft_x = spectrum.clone();
let magsqrd: Vec<_> = fft_x.iter().map(|i| i.norm_sqr()).collect();
r2c.process(raw_hann_dt, &mut spectrum).unwrap();
let fft_xdt = spectrum.clone();
r2c.process(raw_hann_t, &mut spectrum).unwrap();
let fft_xt = spectrum.clone();
for i in 0..1025 {
let fc_temp = (-(fft_xdt[i] * fft_x[i].conj()).im() / magsqrd[i]) + FREQFRAME_2048[i];
let tc_temp = (fft_xt[i] * fft_x[i].conj()).re() / magsqrd[i];

if fc_temp > 10.0 && fc_temp < 24000.0 {
let image_x = fc_temp / 24000.0 * 2048.0;
let image_y = 3840.0 + tc_temp * 12.0;
let origin_color =
buf.spectrogram.image[image_x as usize + (image_y as usize) * 2048].a();
buf.spectrogram.image[image_x as usize + (image_y as usize) * 2048] =
Color32::from_rgba_unmultiplied(
buf.setting.theme.main.r(),
buf.setting.theme.main.g(),
buf.setting.theme.main.b(),
origin_color.wrapping_add(
(255.0 * fft_x[i].norm() * buf.setting.spectrogram.brightness_boost as f32)
as u8,
),
);
}
}
send_data.spectrogram_image = buf.spectrogram.image.clone();
buf.spectrogram.ab = !buf.spectrogram.ab;
}
11 changes: 3 additions & 8 deletions src/frame/main_canvas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ impl NanometersApp {
let mut update_iir_data = Vec::new();
let mut update_db_data = DBData::new();
let mut update_osc_data = OscilloscopeSendData::new();
let mut update_spectrogram_data = Vec::new();

let mut update_spectrogram_image = Vec::new();

self.rx_data.as_mut().unwrap().try_iter().for_each(|data| {
Expand All @@ -91,7 +91,7 @@ impl NanometersApp {
.extend_from_slice(&data.vectorscope.log);
update_waveform_data.concat(&data.waveform);
update_osc_data = data.oscilloscope;
update_spectrogram_data.extend_from_slice(&data.spectrogram);

update_spectrogram_image = data.spectrogram_image;
});

Expand All @@ -104,12 +104,7 @@ impl NanometersApp {
self.waveform_meter(&update_waveform_data, meter_rect, ui);
}
ModuleList::Spectrogram => {
self.spectrogram_meter(
&update_spectrogram_data,
update_spectrogram_image.clone(),
meter_rect,
ui,
);
self.spectrogram_meter(update_spectrogram_image.clone(), meter_rect, ui);
}
ModuleList::Peak => {
self.peak_meter(&update_iir_data, &update_db_data, meter_rect, ui);
Expand Down
Loading

0 comments on commit 8acee83

Please sign in to comment.