How to fix registering audio callback as NaN? #666
-
|
I wrote voice-to-text(real-time and wav-file) for chatroom-perry.
Converting a wav-file to text works fine. However, real-time voice-to-text does not work. Here is the Speech Recognition Architecture Bug: Register audio callback as NaNAdd audio callback mechanism for voice-to-text processing
pub unsafe extern "C" fn register_audio_callback(callback: f64) {
eprintln!("[audio] register_audio_callback called, callback: {:?}", callback);
eprintln!("[audio] AUDIO_CALLBACK mutex is being updated");
*AUDIO_CALLBACK.lock().unwrap() = Some(callback);
eprintln!("[audio] AUDIO_CALLBACK has been set to: {:?}", *AUDIO_CALLBACK.lock().unwrap());
}Check audio.rs for the implementation of register_audio_callback. Register audio callback in TypeScriptconsole.log("[SpeechRecognizer] Session started, registering audio callback");
audioRegisterCallback((samplesPtr: any, numSamples: number) => {
this.sampleCount += numSamples;
if (this.sampleCount % (48000 * 2) === 0) {
console.log("[SpeechRecognizer] Audio callback called, total samples:", this.sampleCount);
}
voskProcessSamples(samplesPtr, numSamples);
});
console.log("[SpeechRecognizer] Starting audio");
audioStart();
console.log("[SpeechRecognizer] Returning true");Check SpeechRecognizer.ts for the call of audioRegisterCallback. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
|
The In Perry, function values are NaN-boxed: the The actual bug is in your invocation in let cb_bits = cb.to_bits();
let closure_ptr = cb_bits as *const u8; // ← keeps the 0x7FFD tag in the high bits
js_closure_call2(closure_ptr, samples_ptr_val, num_samples_val);
extern "C" {
fn js_nanbox_get_pointer(value: f64) -> i64;
fn js_nanbox_pointer(ptr: i64) -> f64;
fn js_closure_call2(closure: *const u8, a0: f64, a1: f64) -> f64;
}
let closure_ptr = unsafe { js_nanbox_get_pointer(cb) } as *const u8;
// Both args to js_closure_call2 are NaN-boxed JS values, not raw C values:
let samples_arg = unsafe { js_nanbox_pointer(samples_ptr as i64) }; // box pointer w/ POINTER_TAG
let count_arg = num_samples as f64; // plain numbers ride f64 directly
unsafe { js_closure_call2(closure_ptr, samples_arg, count_arg); }Three rules of thumb for FFI like this:
One more thing to check on the TS side: in your callback signature |
Beta Was this translation helpful? Give feedback.
The
callback=NaNlog is a red herring — that's expected, not the bug.In Perry, function values are NaN-boxed: the
f64bit pattern is0x7FFD_0000_0000_0000 | (ptr & 0xFFFF_FFFF_FFFF). That's a quiet NaN by IEEE-754 construction, soeprintln!("{:?}", cb_f64)printsNaNfor any function/object/string value. The bits underneath still hold the pointer. (Switch your logs toeprintln!("{:#x}", cb.to_bits())and you'll see7ffd…for closures,7fff…for strings, etc.)The actual bug is in your invocation in
audio.rs: