Add an async version of the analyze function

This commit is contained in:
Anton Liaposhchenko 2023-12-26 23:04:13 +02:00
parent 5fd2ef3902
commit 2a89ddb2ca
2 changed files with 37 additions and 4 deletions

View File

@ -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,

View File

@ -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<JsPromise> {
let path = cx.argument::<JsString>(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<JsUint8Array> {
let path = cx.argument::<JsString>(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<JsUint8Array> {
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))
}