Compare commits

...

3 Commits

Author SHA1 Message Date
Sébastien Bourdeauducq a12e42681a add FFT function 2024-10-04 18:59:46 +08:00
Sébastien Bourdeauducq 216e96bb05 define plot function 2024-10-04 18:15:34 +08:00
Sébastien Bourdeauducq 0488f85b9e define slider function 2024-10-04 16:23:52 +08:00
3 changed files with 188 additions and 0 deletions

60
Cargo.lock generated
View File

@ -698,7 +698,9 @@ dependencies = [
"egui_plot",
"libloading 0.8.5",
"nac3core",
"num-complex",
"parking_lot",
"rustfft",
"tempfile",
]
@ -2213,12 +2215,30 @@ version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2bf50223579dc7cdcfb3bfcacf7069ff68243f8c363f62ffa99cf000a6b9c451"
[[package]]
name = "num-complex"
version = "0.4.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "73f88a1307638156682bada9d7604135552957b7818057dcef22705b4d509495"
dependencies = [
"num-traits",
]
[[package]]
name = "num-conv"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "51d515d32fb182ee37cda2ccdcb92950d6a3c2893aa280e540671c2cd0f3b1d9"
[[package]]
name = "num-integer"
version = "0.1.46"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7969661fd2958a5cb096e56c8e1ad0444ac2bbcd0061bd28660485a44879858f"
dependencies = [
"num-traits",
]
[[package]]
name = "num-traits"
version = "0.2.19"
@ -2673,6 +2693,15 @@ version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e8cf8e6a8aa66ce33f63993ffc4ea4271eb5b0530a9002db8455ea6050c77bfa"
[[package]]
name = "primal-check"
version = "0.3.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dc0d895b311e3af9902528fbb8f928688abbd95872819320517cc24ca6b2bd08"
dependencies = [
"num-integer",
]
[[package]]
name = "proc-macro-crate"
version = "1.3.1"
@ -2864,6 +2893,21 @@ version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2"
[[package]]
name = "rustfft"
version = "6.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "43806561bc506d0c5d160643ad742e3161049ac01027b5e6d7524091fd401d86"
dependencies = [
"num-complex",
"num-integer",
"num-traits",
"primal-check",
"strength_reduce",
"transpose",
"version_check",
]
[[package]]
name = "rustix"
version = "0.37.27"
@ -3153,6 +3197,12 @@ version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f"
[[package]]
name = "strength_reduce"
version = "0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fe895eb47f22e2ddd4dabc02bce419d2e643c8e3b585c78158b349195bc24d82"
[[package]]
name = "strict-num"
version = "0.1.1"
@ -3428,6 +3478,16 @@ dependencies = [
"once_cell",
]
[[package]]
name = "transpose"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1ad61aed86bc3faea4300c7aee358b4c6d0c8d6ccc36524c96e4c92ccf26e77e"
dependencies = [
"num-integer",
"strength_reduce",
]
[[package]]
name = "ttf-parser"
version = "0.24.1"

View File

@ -10,6 +10,8 @@ egui_plot = "0.28"
parking_lot = "0.12"
tempfile = "3.12"
libloading = "0.8"
num-complex = "0.4"
rustfft = "6.2"
[dependencies.nac3core]
git = "https://git.m-labs.hk/M-Labs/nac3"

View File

@ -229,6 +229,41 @@ fn handle_global_var(
}
}
fn register_cells_function(
composer: &mut composer::TopLevelComposer,
resolver: Arc<dyn nac3core::symbol_resolver::SymbolResolver + Send + Sync>,
internal_resolver: &ResolverInternal,
name: &str,
signature: typedef::FunSignature,
) {
let ty = composer.unifier.add_ty(typedef::TypeEnum::TFunc(signature));
let name_ref = name.into();
let mut function_def = composer::TopLevelComposer::make_top_level_function_def(
name.to_string(),
name_ref,
ty,
Some(resolver.clone()),
None,
);
if let toplevel::TopLevelDef::Function {
ref mut instance_to_symbol,
..
} = function_def
{
instance_to_symbol.insert("".to_string(), "__nac3_cells_".to_string() + name);
} else {
unreachable!();
};
composer
.definition_ast_list
.push((RwLock::new(function_def).into(), None));
internal_resolver.add_id_def(
name_ref,
toplevel::DefinitionId(composer.definition_ast_list.len() - 1),
);
internal_resolver.add_id_type(name_ref, ty);
}
fn compile(code: &String, run_symbol: &String, output_filename: &Path) -> Result<(), String> {
let mut target_machine_options = codegen::CodeGenTargetMachineOptions::from_host();
target_machine_options.reloc_mode = inkwell::targets::RelocMode::PIC;
@ -263,6 +298,80 @@ fn compile(code: &String, run_symbol: &String, output_filename: &Path) -> Result
as Arc<dyn nac3core::symbol_resolver::SymbolResolver + Send + Sync>;
let irrt = codegen::irrt::load_irrt(&context, resolver.as_ref());
let list_tvar = if let typedef::TypeEnum::TObj { params, .. } =
&*composer.unifier.get_ty_immutable(primitive.list)
{
typedef::iter_type_vars(params).nth(0).unwrap()
} else {
unreachable!()
};
let list_float = composer
.unifier
.subst(
primitive.list,
&typedef::into_var_map([typedef::TypeVar {
id: list_tvar.id,
ty: primitive.float,
}]),
)
.unwrap();
register_cells_function(
&mut composer,
resolver.clone(),
internal_resolver.as_ref(),
"slider",
typedef::FunSignature {
args: vec![typedef::FuncArg {
name: "prev".into(),
ty: primitive.float,
default_value: None,
is_vararg: false,
}],
ret: primitive.float,
vars: typedef::VarMap::new(),
},
);
register_cells_function(
&mut composer,
resolver.clone(),
internal_resolver.as_ref(),
"plot",
typedef::FunSignature {
args: vec![typedef::FuncArg {
name: "data".into(),
ty: list_float,
default_value: None,
is_vararg: false,
}],
ret: primitive.none,
vars: typedef::VarMap::new(),
},
);
register_cells_function(
&mut composer,
resolver.clone(),
internal_resolver.as_ref(),
"fft",
typedef::FunSignature {
args: vec![
typedef::FuncArg {
name: "reals".into(),
ty: list_float,
default_value: None,
is_vararg: false,
},
typedef::FuncArg {
name: "imags".into(),
ty: list_float,
default_value: None,
is_vararg: false,
},
],
ret: primitive.none,
vars: typedef::VarMap::new(),
},
);
let parser_result =
match nac3parser::parser::parse_program(code.as_str(), String::from("cell1").into()) {
Ok(parser_result) => parser_result,
@ -573,6 +682,23 @@ pub extern "C" fn __nac3_cells_plot(data: *const List) {
.show(ui, |plot_ui| plot_ui.line(line));
}
#[no_mangle]
pub extern "C" fn __nac3_cells_fft(reals: *mut List, imags: *mut List) {
let reals_slice = unsafe { std::slice::from_raw_parts_mut((*reals).data, (*reals).length) };
let imags_slice = unsafe { std::slice::from_raw_parts_mut((*imags).data, (*imags).length) };
let mut planner = rustfft::FftPlanner::<f64>::new();
let fft = planner.plan_fft_forward(reals_slice.len());
let mut buffer = vec![num_complex::Complex { re: 0.0, im: 0.0 }; reals_slice.len()];
for (buf, (re, im)) in buffer.iter_mut().zip(reals_slice.iter().zip(imags_slice.iter())) {
*buf = num_complex::Complex { re: *re, im: *im };
}
fft.process(&mut buffer);
for (buf, (re, im)) in buffer.iter().zip(reals_slice.iter_mut().zip(imags_slice.iter_mut())) {
*re = buf.re;
*im = buf.im;
}
}
impl Cells {
fn new() -> Self {
Self {