Skip to content

Commit

Permalink
remove callback sync trait
Browse files Browse the repository at this point in the history
  • Loading branch information
aizcutei committed Jul 5, 2024
1 parent 30fdafe commit 132bffd
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 107 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ realfft = "3.3.0"
ruhear = "0.1.0"
log = "0.4"
rayon = "1.10"
crossbeam-channel = "0.5.12"
# crossbeam-channel = "0.5.12"
puffin = { version = "0.19", optional = true }
puffin_http = { version = "0.16", optional = true }
piet-common = "0.6.2"
Expand Down
30 changes: 12 additions & 18 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ use crate::frame::*;
use crate::setting::*;
use crate::utils::*;

use crossbeam_channel::unbounded;
use crossbeam_channel::{Receiver, Sender};
// use crossbeam_channel::unbounded;
// use crossbeam_channel::{Receiver, Sender};
use eframe::egui::{self, ViewportCommand};
use eframe::wgpu::core::storage;
use eframe::wgpu::rwh::HasWindowHandle;
use egui::*;
use rayon::prelude::*;
use std::sync::atomic::AtomicU32;
use std::sync::mpsc::{channel, Receiver, Sender};
use std::sync::{Arc, Mutex};
use std::{thread, vec};

Expand All @@ -25,6 +26,9 @@ pub struct NanometersApp {
/// Audio source data buffer.
#[serde(skip)]
pub(crate) audio_source_buffer: Arc<Mutex<AudioSourceBuffer>>,
#[serde(skip)]
pub(crate) audio_source_setting: Arc<Mutex<Setting>>,

/// Calculate the frame time and FPS.
#[serde(skip)]
pub(crate) frame_history: FrameHistory,
Expand All @@ -35,12 +39,6 @@ pub struct NanometersApp {
#[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>>,

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

Expand All @@ -59,14 +57,13 @@ pub struct NanometersApp {

impl Default for NanometersApp {
fn default() -> Self {
let (tx_data, rx_data) = unbounded();
let (tx_setting, rx_setting) = unbounded();
let (tx_data, rx_data) = channel();
let audio_source_buffer = Arc::new(Mutex::new(AudioSourceBuffer::new()));
let setting = Setting::default();
let audio_source_setting = Arc::new(Mutex::new(setting.clone()));
let mut system_capture = SystemCapture::new(get_callback(
tx_data.clone(),
rx_setting.clone(),
audio_source_setting.clone(),
audio_source_buffer.clone(),
));
system_capture.start();
Expand All @@ -75,11 +72,10 @@ impl Default for NanometersApp {
Self {
audio_source,
audio_source_buffer,
audio_source_setting,
frame_history: Default::default(),
tx_data: Some(tx_data),
rx_data: Some(rx_data),
tx_setting: Some(tx_setting),
rx_setting: Some(rx_setting),
setting,
sample_rate: AtomicU32::new(48000),
setting_switch: false,
Expand All @@ -105,14 +101,12 @@ impl NanometersApp {
let mut app: NanometersApp =
eframe::get_value(storage, eframe::APP_KEY).unwrap_or_default();
cc.egui_ctx.set_visuals(set_theme(&mut app));
app.audio_source_buffer = Arc::new(Mutex::new(AudioSourceBuffer::new_with_setting(
app.setting.clone(),
)));
app.audio_source_setting = Arc::new(Mutex::new(app.setting.clone()));
match app.setting.audio_device.device {
AudioDevice::OutputCapture => {
let mut system_capture = SystemCapture::new(get_callback(
app.tx_data.clone().unwrap(),
app.rx_setting.clone().unwrap(),
app.audio_source_setting.clone(),
app.audio_source_buffer.clone(),
));
system_capture.start();
Expand All @@ -121,7 +115,7 @@ impl NanometersApp {
AudioDevice::PluginCapture => {
let mut plugin_client = PluginClient::new(get_callback(
app.tx_data.clone().unwrap(),
app.rx_setting.clone().unwrap(),
app.audio_source_setting.clone(),
app.audio_source_buffer.clone(),
));
plugin_client.start();
Expand Down
13 changes: 9 additions & 4 deletions src/audio/callback.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
use crate::{setting::*, utils::*};
use crossbeam_channel::{Receiver, Sender};
// use crossbeam_channel::{Receiver, Sender};
use egui::*;
use realfft::RealFftPlanner;
use rustfft::num_complex::{Complex, ComplexFloat};
use std::sync::mpsc::Sender;
use std::sync::{Arc, Mutex};
const SQRT_2: f32 = 1.4142135;

pub fn get_callback(
tx_data: Sender<SendData>,
rx_setting: Receiver<Setting>,
setting: Arc<Mutex<Setting>>,
buffer: Arc<Mutex<AudioSourceBuffer>>,
) -> Box<dyn FnMut(Vec<Vec<f32>>) + Send + Sync> {
) -> Box<dyn FnMut(Vec<Vec<f32>>) + Send> {
Box::new(move |data: Vec<Vec<f32>>| {
#[cfg(feature = "puffin")]
puffin::profile_scope!("callback");

let mut buf = buffer.lock().unwrap();
rx_setting.try_iter().for_each(|s| buf.setting = s);
{
let setting = setting.lock().unwrap();
buf.setting = setting.clone();
}

let mut send_data = SendData::new();
let len = data[0].len();

Expand Down
Loading

0 comments on commit 132bffd

Please sign in to comment.