generated from emilk/eframe_template
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
32 changed files
with
1,001 additions
and
415 deletions.
There are no files selected for viewing
Empty file.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,11 @@ | ||
pub(crate) mod audio_source; | ||
pub(crate) mod callback; | ||
pub(crate) mod plugin_client; | ||
pub(crate) mod system_capture; | ||
pub(crate) mod system_input; | ||
|
||
pub use audio_source::*; | ||
pub use callback::*; | ||
pub use plugin_client::*; | ||
pub use system_capture::*; | ||
pub use system_input::*; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
pub trait AudioSource { | ||
fn get_name(&self) -> String; | ||
fn start(&mut self); | ||
fn stop(&mut self); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
use realfft::RealFftPlanner; | ||
|
||
use std::sync::{Arc, Mutex}; | ||
|
||
use crate::utils::*; | ||
use crossbeam_channel::Sender; | ||
// use std::collections::VecDeque; | ||
// use std::sync::{Arc, Mutex}; | ||
|
||
pub fn get_callback( | ||
tx_lrms: Sender<SendData>, | ||
buffer: Arc<Mutex<AudioSourceBuffer>>, | ||
) -> Box<dyn FnMut(Vec<Vec<f32>>) + Send + Sync> { | ||
Box::new(move |data: Vec<Vec<f32>>| { | ||
#[cfg(feature = "puffin")] | ||
puffin::profile_scope!("callback"); | ||
|
||
let mut buffer = buffer.lock().unwrap(); | ||
let block_length = 256; | ||
|
||
let mut send_data = SendData::new(); | ||
let len = data[0].len(); | ||
let mut amp_l = 0.0; | ||
let mut amp_r = 0.0; | ||
for i in 0..len { | ||
// Waveform | ||
buffer.waveform.update_l(data[0][i]); | ||
buffer.waveform.update_r(data[1][i]); | ||
buffer.waveform.update_m((data[0][i] + data[1][i]) / 2.0); | ||
buffer.waveform.update_s((data[0][i] - data[1][i]) / 2.0); | ||
buffer.waveform.index += 1; | ||
if buffer.waveform.index >= block_length { | ||
let mut waveform_buffer = buffer.waveform.clone(); | ||
buffer.waveform.reset(); | ||
send_data.waveform_data.l.push(waveform_buffer.l); | ||
send_data.waveform_data.r.push(waveform_buffer.r); | ||
send_data.waveform_data.m.push(waveform_buffer.m); | ||
send_data.waveform_data.s.push(waveform_buffer.s); | ||
|
||
let mut real_planner = RealFftPlanner::<f32>::new(); | ||
let r2c = real_planner.plan_fft_forward(block_length); | ||
let mut spectrum = r2c.make_output_vec(); | ||
r2c.process(&mut waveform_buffer.raw.l, &mut spectrum) | ||
.unwrap(); | ||
send_data | ||
.waveform_data | ||
.l_freq | ||
.push(max_index(spectrum.iter().map(|x| x.norm()).collect())); | ||
r2c.process(&mut waveform_buffer.raw.r, &mut spectrum) | ||
.unwrap(); | ||
send_data | ||
.waveform_data | ||
.r_freq | ||
.push(max_index(spectrum.iter().map(|x| x.norm()).collect())); | ||
r2c.process(&mut waveform_buffer.raw.m, &mut spectrum) | ||
.unwrap(); | ||
send_data | ||
.waveform_data | ||
.m_freq | ||
.push(max_index(spectrum.iter().map(|x| x.norm()).collect())); | ||
r2c.process(&mut waveform_buffer.raw.s, &mut spectrum) | ||
.unwrap(); | ||
send_data | ||
.waveform_data | ||
.s_freq | ||
.push(max_index(spectrum.iter().map(|x| x.norm()).collect())); | ||
} | ||
|
||
// Peak | ||
// DB | ||
amp_l += data[0][i].abs(); | ||
amp_r += data[1][i].abs(); | ||
//IIR | ||
let iir_l = combined_filter(data[0][i], &mut buffer.peak.iir_l); | ||
let iir_r = combined_filter(data[1][i], &mut buffer.peak.iir_r); | ||
buffer.peak.sum_l += iir_l * iir_l; | ||
buffer.peak.sum_r += iir_r * iir_r; | ||
buffer.peak.index += 1; | ||
if buffer.peak.index >= 4800 { | ||
let peak_buffer = buffer.peak.clone(); | ||
buffer.peak.reset_sum(); | ||
send_data.iir_data.l.push(peak_buffer.sum_l); | ||
send_data.iir_data.r.push(peak_buffer.sum_r); | ||
} | ||
} | ||
|
||
//DB | ||
amp_l /= len as f32; | ||
amp_r /= len as f32; | ||
send_data.db_data.l = gain_to_db(amp_l); | ||
send_data.db_data.r = gain_to_db(amp_r); | ||
|
||
tx_lrms.send(send_data).unwrap(); | ||
}) | ||
} | ||
|
||
fn max_index(data: Vec<f32>) -> usize { | ||
data.iter() | ||
.enumerate() | ||
.max_by(|&(_, a), &(_, b)| a.partial_cmp(b).unwrap()) | ||
.map(|(index, _)| index) | ||
.unwrap_or(0) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,79 @@ | ||
#![allow(unused)] | ||
// use crate::AudioSource; | ||
use crate::audio::AudioSource; | ||
// use cpal::traits::{DeviceTrait, HostTrait, StreamTrait}; | ||
use cpal::traits::{DeviceTrait, HostTrait, StreamTrait}; | ||
use std::sync::{Arc, Mutex}; | ||
|
||
pub struct SystemInput { | ||
name: String, | ||
callback: Arc<Mutex<dyn FnMut(Vec<Vec<f32>>) + Send>>, | ||
stream: cpal::Stream, | ||
host: cpal::Host, | ||
device: cpal::Device, | ||
format: cpal::SupportedStreamConfig, | ||
stream: Option<cpal::Stream>, | ||
} | ||
|
||
// impl SystemInput { | ||
// pub fn new(callback: Box<dyn FnMut(Vec<Vec<f32>>) + Send>) -> Self { | ||
// let name = "System Microphone".to_string(); | ||
impl SystemInput { | ||
pub fn new(callback: Box<dyn FnMut(Vec<Vec<f32>>) + Send>) -> Self { | ||
let name = "System Default Microphone".to_string(); | ||
let host = cpal::default_host(); | ||
let device = host.default_input_device().unwrap(); | ||
let format = device.default_input_config().unwrap(); | ||
Self { | ||
name, | ||
callback: Arc::new(Mutex::new(callback)), | ||
host, | ||
device, | ||
format, | ||
stream: None, | ||
} | ||
} | ||
} | ||
|
||
impl AudioSource for SystemInput { | ||
fn get_name(&self) -> String { | ||
self.name.clone() | ||
} | ||
|
||
// } | ||
// } | ||
fn start(&mut self) { | ||
if self.stream.is_none() { | ||
let callback = self.callback.clone(); | ||
let channels = &self.format.channels().clone(); | ||
let channels = *channels as usize; | ||
let stream = match self.format.sample_format() { | ||
cpal::SampleFormat::F32 => self.device.build_input_stream( | ||
&self.format.config(), | ||
move |data: &[f32], &_| { | ||
let mut bufs = vec![vec![]; channels]; | ||
for (i, sample) in data.chunks(channels).enumerate() { | ||
for (j, &channel) in sample.iter().enumerate() { | ||
bufs[j].push(channel as f32); | ||
} | ||
} | ||
if let Ok(mut callback) = callback.lock() { | ||
(*callback)(bufs); | ||
} | ||
}, | ||
|err| { | ||
eprintln!("an error occurred on stream: {}", err); | ||
}, | ||
None, | ||
), | ||
sample_format => { | ||
panic!("unsupported format {:?}", sample_format); | ||
} | ||
} | ||
.unwrap(); | ||
self.stream = Some(stream); | ||
} | ||
if let Some(stream) = &self.stream { | ||
stream.play(); | ||
} | ||
} | ||
|
||
fn stop(&mut self) { | ||
if let Some(stream) = &self.stream { | ||
stream.pause().unwrap(); | ||
} | ||
} | ||
} |
Oops, something went wrong.