From 2a89ddb2ca8627ce0bdba98898a92600e8c23903 Mon Sep 17 00:00:00 2001 From: Anton Date: Tue, 26 Dec 2023 23:04:13 +0200 Subject: [PATCH] Add an async version of the analyze function --- src/bliss_lib.rs | 3 +++ src/lib.rs | 38 ++++++++++++++++++++++++++++++++++---- 2 files changed, 37 insertions(+), 4 deletions(-) diff --git a/src/bliss_lib.rs b/src/bliss_lib.rs index 5a5b5d9..a8f638d 100644 --- a/src/bliss_lib.rs +++ b/src/bliss_lib.rs @@ -80,7 +80,10 @@ use thiserror::Error; pub use crate::song::{Analysis, AnalysisIndex, Song, NUMBER_FEATURES}; +/// Target channels for ffmpeg pub const CHANNELS: u16 = 1; + +/// Target sample rate for ffmpeg pub const SAMPLE_RATE: u32 = 22050; /// Stores the current version of bliss-rs' features. /// It is bumped every time one or more feature is added, updated or removed, diff --git a/src/lib.rs b/src/lib.rs index eafad6a..7377069 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -12,21 +12,44 @@ mod utils; use neon::{prelude::*, types::buffer::TypedArray}; use song::Song; +use bliss_lib::BlissResult; #[neon::main] fn main(mut cx: ModuleContext) -> NeonResult<()> { - cx.export_function("analyze", analyze)?; + cx.export_function("analyzeSync", analyze)?; + cx.export_function("analyze", analyze_async)?; Ok(()) } +#[allow(deprecated)] +fn analyze_async(mut cx: FunctionContext) -> JsResult { + let path = cx.argument::(0)?.value(&mut cx); + let promise = cx.task(move || { + analyze_raw(&path) + }).promise(|mut cx, result| { + result + .map(|(version_bytes, analysis_bytes)| { + let mut buffer_handle = JsUint8Array::new( + &mut cx, + analysis_bytes.len() + version_bytes.len(), + ).unwrap(); + let buffer = buffer_handle.as_mut_slice(&mut cx); + + buffer[0..version_bytes.len()].copy_from_slice(&version_bytes); + buffer[version_bytes.len()..].copy_from_slice(&analysis_bytes); + buffer_handle + }) + .or_else(|e| cx.throw_error(e.to_string())) + }); + Ok(promise) +} + /// Returns a Uint8Array, with the first 2 bytes being the version in platform endianness /// and the rest (currently 80 bytes) being the analysis data in little endian fn analyze(mut cx: FunctionContext) -> JsResult { let path = cx.argument::(0)?.value(&mut cx); - let song = Song::from_path(path) + let (version_bytes, analysis_bytes) = analyze_raw(&path) .or_else(|e| cx.throw_error(e.to_string()))?; - let version_bytes = song.features_version.to_ne_bytes(); - let analysis_bytes = song.analysis.as_bytes(); let mut buffer_handle = JsUint8Array::new( &mut cx, @@ -39,3 +62,10 @@ fn analyze(mut cx: FunctionContext) -> JsResult { Ok(buffer_handle) } + +fn analyze_raw(path: &str) -> BlissResult<([u8; 2], [u8; 80])> { + let song = Song::from_path(path)?; + let version_bytes = song.features_version.to_ne_bytes(); + let analysis_bytes = song.analysis.as_bytes(); + Ok((version_bytes, analysis_bytes)) +}