mirror of
https://github.com/ggerganov/whisper.cpp.git
synced 2023-11-04 02:52:44 +03:00
Node.js package (#260)
* npm : preparing infra for node package * npm : package infra ready * npm : initial version ready * npm : change name to whisper.cpp whisper.js is taken
This commit is contained in:
@@ -1,4 +1,14 @@
|
||||
if (EMSCRIPTEN)
|
||||
#
|
||||
# test-whisper-js
|
||||
|
||||
set(TEST_TARGET test-whisper-js)
|
||||
|
||||
add_test(NAME ${TEST_TARGET}
|
||||
COMMAND node test-whisper.js --experimental-wasm-threads
|
||||
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
|
||||
)
|
||||
|
||||
return()
|
||||
endif()
|
||||
|
||||
|
||||
58
tests/test-whisper.js
Normal file
58
tests/test-whisper.js
Normal file
@@ -0,0 +1,58 @@
|
||||
var factory = require('../bindings/javascript/whisper.js')
|
||||
|
||||
factory().then(function(whisper) {
|
||||
var fs = require('fs');
|
||||
|
||||
// to avoid reading WAV files and depending on some 3rd-party package, we read
|
||||
// 32-bit float PCM directly. to genereate it:
|
||||
//
|
||||
// $ ffmpeg -i samples/jfk.wav -f f32le -acodec pcm_f32le samples/jfk.pcmf32
|
||||
//
|
||||
let fname_wav = "../samples/jfk.pcmf32";
|
||||
let fname_model = "../models/ggml-base.en.bin";
|
||||
|
||||
// init whisper
|
||||
{
|
||||
// read binary data from file
|
||||
var model_data = fs.readFileSync(fname_model);
|
||||
if (model_data == null) {
|
||||
console.log("whisper: failed to read model file");
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
// write binary data to WASM memory
|
||||
whisper.FS_createDataFile("/", "whisper.bin", model_data, true, true);
|
||||
|
||||
// init the model
|
||||
var ret = whisper.init("whisper.bin");
|
||||
if (ret == false) {
|
||||
console.log('whisper: failed to init');
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
// transcribe wav file
|
||||
{
|
||||
// read raw binary data
|
||||
var pcm_data = fs.readFileSync(fname_wav);
|
||||
if (pcm_data == null) {
|
||||
console.log("whisper: failed to read wav file");
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
// convert to 32-bit float array
|
||||
var pcm = new Float32Array(pcm_data.buffer);
|
||||
|
||||
// transcribe
|
||||
var ret = whisper.full_default(pcm, "en", false);
|
||||
if (ret != 0) {
|
||||
console.log("whisper: failed to transcribe");
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
// free memory
|
||||
{
|
||||
whisper.free();
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user