-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Renaming of from Plugin -> EffectPlugin before creating GenericPlugin
Also add ctrl-c handler
- Loading branch information
Showing
16 changed files
with
406 additions
and
209 deletions.
There are no files selected for viewing
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
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
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 |
---|---|---|
@@ -0,0 +1,61 @@ | ||
use turbo_plugin::audio_api::AudioApi; | ||
|
||
use crate::audio::audio_processing::FftResult; | ||
use std::{ | ||
boxed::Box, | ||
sync::{Arc, RwLock}, | ||
}; | ||
|
||
pub fn create_audio_api(fft_result: Arc<RwLock<FftResult>>) -> AudioApi { | ||
extern "C" fn get_average_amplitude( | ||
instance: *const std::ffi::c_void, | ||
lower_frequency: std::ffi::c_float, | ||
upper_frequency: std::ffi::c_float, | ||
) -> std::ffi::c_float { | ||
let fft_result = unsafe { &*(instance as *const Arc<RwLock<FftResult>>) }; | ||
fft_result | ||
.read() | ||
.unwrap() | ||
.get_average_amplitude(lower_frequency, upper_frequency) | ||
.unwrap_or_else(|| { | ||
log::error!("Invalid frequencies: {lower_frequency} & {upper_frequency}"); | ||
0.0f32 | ||
}) | ||
} | ||
|
||
extern "C" fn get_frequency_amplitude( | ||
instance: *const std::ffi::c_void, | ||
frequency: std::ffi::c_float, | ||
) -> std::ffi::c_float { | ||
let fft_result = unsafe { &*(instance as *const Arc<RwLock<FftResult>>) }; | ||
fft_result | ||
.read() | ||
.unwrap() | ||
.get_frequency_amplitude(frequency) | ||
.unwrap_or_else(|| { | ||
log::error!("Invalid frequency: {frequency}"); | ||
0.0f32 | ||
}) | ||
} | ||
|
||
extern "C" fn get_max_frequency(instance: *const std::ffi::c_void) -> std::ffi::c_float { | ||
let fft_result = unsafe { &*(instance as *const Arc<RwLock<FftResult>>) }; | ||
fft_result.read().unwrap().get_max_frequency() | ||
} | ||
|
||
extern "C" fn free(instance: *const std::ffi::c_void) { | ||
unsafe { | ||
drop(Box::from_raw(instance as *mut Arc<RwLock<FftResult>>)); | ||
} | ||
} | ||
|
||
let fft_result = Box::new(fft_result); | ||
|
||
AudioApi::new( | ||
Box::into_raw(fft_result) as *const _, | ||
get_average_amplitude, | ||
get_frequency_amplitude, | ||
get_max_frequency, | ||
free, | ||
) | ||
} |
File renamed without changes.
File renamed without changes.
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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
pub mod audio_api; | ||
pub mod effects; |
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,97 @@ | ||
use std::{ | ||
process::abort, | ||
sync::{Mutex, OnceLock}, | ||
}; | ||
|
||
#[derive(Copy, Clone)] | ||
#[repr(C)] | ||
pub struct AudioApi { | ||
instance: *const std::ffi::c_void, | ||
get_average_amplitude: extern "C" fn( | ||
*const std::ffi::c_void, | ||
std::ffi::c_float, | ||
std::ffi::c_float, | ||
) -> std::ffi::c_float, | ||
get_frequency_amplitude: | ||
extern "C" fn(*const std::ffi::c_void, std::ffi::c_float) -> std::ffi::c_float, | ||
get_max_frequency: extern "C" fn(*const std::ffi::c_void) -> std::ffi::c_float, | ||
free: extern "C" fn(*const std::ffi::c_void), | ||
} | ||
|
||
unsafe impl Send for AudioApi {} | ||
unsafe impl Sync for AudioApi {} | ||
|
||
impl AudioApi { | ||
pub fn new( | ||
instance: *const std::ffi::c_void, | ||
|
||
get_average_amplitude: extern "C" fn( | ||
*const std::ffi::c_void, | ||
std::ffi::c_float, | ||
std::ffi::c_float, | ||
) -> std::ffi::c_float, | ||
get_frequency_amplitude: extern "C" fn( | ||
*const std::ffi::c_void, | ||
std::ffi::c_float, | ||
) -> std::ffi::c_float, | ||
get_max_frequency: extern "C" fn(*const std::ffi::c_void) -> std::ffi::c_float, | ||
free: extern "C" fn(*const std::ffi::c_void), | ||
) -> Self { | ||
Self { | ||
instance, | ||
get_average_amplitude, | ||
get_frequency_amplitude, | ||
get_max_frequency, | ||
free, | ||
} | ||
} | ||
} | ||
|
||
static AUDIO_API_INSTANCE: OnceLock<Mutex<AudioApi>> = OnceLock::new(); | ||
|
||
pub fn on_load(audio_api: AudioApi) { | ||
let mut api = AUDIO_API_INSTANCE | ||
.get_or_init(|| Mutex::new(audio_api)) | ||
.lock() | ||
.unwrap(); | ||
*api = audio_api; | ||
} | ||
|
||
pub fn get_average_amplitude(lower_freq: f32, upper_freq: f32) -> f32 { | ||
let Some(api) = AUDIO_API_INSTANCE.get() else { | ||
eprintln!("PLUGIN ERROR: Couldn't get the audio api pointer"); | ||
abort(); | ||
}; | ||
let api = api.lock().unwrap(); | ||
(api.get_average_amplitude)(api.instance, lower_freq, upper_freq) | ||
} | ||
|
||
pub fn get_frequency_amplitude(frequency: f32) -> f32 { | ||
let Some(api) = AUDIO_API_INSTANCE.get() else { | ||
eprintln!("PLUGIN ERROR: Couldn't get the audio api pointer"); | ||
abort(); | ||
}; | ||
let api = api.lock().unwrap(); | ||
|
||
(api.get_frequency_amplitude)(api.instance, frequency) | ||
} | ||
|
||
pub fn get_max_frequency() -> std::ffi::c_float { | ||
let Some(api) = AUDIO_API_INSTANCE.get() else { | ||
eprintln!("PLUGIN ERROR: Couldn't get the audio api pointer"); | ||
abort(); | ||
}; | ||
let api = api.lock().unwrap(); | ||
|
||
(api.get_max_frequency)(api.instance) | ||
} | ||
|
||
pub fn free() { | ||
let Some(api) = AUDIO_API_INSTANCE.get() else { | ||
eprintln!("PLUGIN ERROR: Couldn't get the audio api pointer"); | ||
abort(); | ||
}; | ||
let api = api.lock().unwrap(); | ||
|
||
(api.free)(api.instance) | ||
} |
Oops, something went wrong.