forked from ggerganov/whisper.cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Works but it is very slow because no SIMD is used. For example, jfk.wav is processed in ~23 seconds using "tiny.en" model
- Loading branch information
Showing
14 changed files
with
441 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
if (EMSCRIPTEN) | ||
add_subdirectory(javascript) | ||
endif() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
publish.log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
set(TARGET libwhisper) | ||
|
||
add_executable(${TARGET} | ||
emscripten.cpp | ||
) | ||
|
||
target_link_libraries(${TARGET} PRIVATE | ||
whisper | ||
) | ||
|
||
unset(EXTRA_FLAGS) | ||
if (WHISPER_WASM_SINGLE_FILE) | ||
set(EXTRA_FLAGS "-s SINGLE_FILE=1") | ||
message(STATUS "Embedding WASM inside whisper.js") | ||
|
||
add_custom_command( | ||
TARGET libwhisper POST_BUILD | ||
COMMAND ${CMAKE_COMMAND} -E copy | ||
${CMAKE_BINARY_DIR}/bin/libwhisper.js | ||
${CMAKE_CURRENT_SOURCE_DIR}/whisper.js | ||
) | ||
endif() | ||
|
||
set_target_properties(${TARGET} PROPERTIES LINK_FLAGS " \ | ||
--bind \ | ||
-s MODULARIZE=1 \ | ||
-s ASSERTIONS=1 \ | ||
-s USE_PTHREADS=1 \ | ||
-s PTHREAD_POOL_SIZE=8 \ | ||
-s TOTAL_MEMORY=536870912 \ | ||
-s FORCE_FILESYSTEM=1 \ | ||
-s EXPORT_NAME=\"'whisper_factory'\" \ | ||
${EXTRA_FLAGS} \ | ||
") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
#include "whisper.h" | ||
|
||
#include <emscripten.h> | ||
#include <emscripten/bind.h> | ||
|
||
#include <vector> | ||
|
||
std::vector<struct whisper_context *> g_contexts(4, nullptr); | ||
|
||
EMSCRIPTEN_BINDINGS(whisper) { | ||
emscripten::function("init", emscripten::optional_override([](const std::string & path_model) { | ||
for (size_t i = 0; i < g_contexts.size(); ++i) { | ||
if (g_contexts[i] == nullptr) { | ||
g_contexts[i] = whisper_init(path_model.c_str()); | ||
return i + 1; | ||
} | ||
} | ||
|
||
return (size_t) 0; | ||
})); | ||
|
||
emscripten::function("free", emscripten::optional_override([](size_t index) { | ||
--index; | ||
|
||
if (index < g_contexts.size()) { | ||
whisper_free(g_contexts[index]); | ||
g_contexts[index] = nullptr; | ||
} | ||
})); | ||
|
||
emscripten::function("full_default", emscripten::optional_override([](size_t index, const emscripten::val & audio) { | ||
--index; | ||
|
||
if (index >= g_contexts.size()) { | ||
return -1; | ||
} | ||
|
||
if (g_contexts[index] == nullptr) { | ||
return -2; | ||
} | ||
|
||
struct whisper_full_params params = whisper_full_default_params(whisper_sampling_strategy::WHISPER_SAMPLING_GREEDY); | ||
|
||
params.print_realtime = true; | ||
params.print_progress = false; | ||
params.print_timestamps = true; | ||
params.print_special_tokens = false; | ||
params.translate = false; | ||
params.language = "en"; | ||
params.n_threads = 4; | ||
params.offset_ms = 0; | ||
|
||
std::vector<float> pcmf32; | ||
const int n = audio["length"].as<int>(); | ||
|
||
emscripten::val heap = emscripten::val::module_property("HEAPU8"); | ||
emscripten::val memory = heap["buffer"]; | ||
|
||
pcmf32.resize(n); | ||
|
||
emscripten::val memoryView = audio["constructor"].new_(memory, reinterpret_cast<uintptr_t>(pcmf32.data()), n); | ||
memoryView.call<void>("set", audio); | ||
|
||
int ret = whisper_full(g_contexts[index], params, pcmf32.data(), pcmf32.size()); | ||
|
||
whisper_print_timings(g_contexts[index]); | ||
|
||
return ret; | ||
})); | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# Add new build types | ||
|
||
# ReleaseGG - Release with enabled asserts | ||
|
||
SET(CMAKE_CXX_FLAGS_RELEASEGG | ||
"-O3" | ||
CACHE STRING "Flags used by the c++ compiler during release builds with enabled asserts." | ||
FORCE ) | ||
SET(CMAKE_C_FLAGS_RELEASEGG | ||
"-O3" | ||
CACHE STRING "Flags used by the compiler during release builds with enabled asserts." | ||
FORCE ) | ||
SET(CMAKE_EXE_LINKER_FLAGS_RELEASEGG | ||
"" | ||
CACHE STRING "Flags used for linking binaries during release builds with enabled asserts." | ||
FORCE ) | ||
SET(CMAKE_SHARED_LINKER_FLAGS_RELEASEGG | ||
"" | ||
CACHE STRING "Flags used by the shared libraries linker during release builds with enabled asserts." | ||
FORCE ) | ||
MARK_AS_ADVANCED( | ||
CMAKE_CXX_FLAGS_RELEASEGG | ||
CMAKE_C_FLAGS_RELEASEGG | ||
CMAKE_EXE_LINKER_FLAGS_RELEASEGG | ||
CMAKE_SHARED_LINKER_FLAGS_RELEASEGG ) | ||
|
||
# RelWithDebInfoGG - RelWithDebInfo with enabled asserts | ||
|
||
SET(CMAKE_CXX_FLAGS_RELWITHDEBINFOGG | ||
"-O2 -g" | ||
CACHE STRING "Flags used by the c++ compiler during release builds with debug symbols and enabled asserts." | ||
FORCE ) | ||
SET(CMAKE_C_FLAGS_RELWITHDEBINFOGG | ||
"-O2 -g" | ||
CACHE STRING "Flags used by the compiler during release builds with debug symbols and enabled asserts." | ||
FORCE ) | ||
SET(CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFOGG | ||
"" | ||
CACHE STRING "Flags used for linking binaries during release builds with debug symbols and enabled asserts." | ||
FORCE ) | ||
SET(CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFOGG | ||
"" | ||
CACHE STRING "Flags used by the shared libraries linker during release builds with debug symbols and enabled asserts." | ||
FORCE ) | ||
MARK_AS_ADVANCED( | ||
CMAKE_CXX_FLAGS_RELWITHDEBINFOGG | ||
CMAKE_C_FLAGS_RELWITHDEBINFOGG | ||
CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFOGG | ||
CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFOGG ) | ||
|
||
if (NOT XCODE AND NOT MSVC AND NOT CMAKE_BUILD_TYPE) | ||
set(CMAKE_BUILD_TYPE Release CACHE STRING "Build type" FORCE) | ||
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release" "MinSizeRel" "RelWithDebInfo" "ReleaseGG" "RelWithDebInfoGG") | ||
endif() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
find_package(Git) | ||
|
||
# the commit's SHA1 | ||
execute_process(COMMAND | ||
"${GIT_EXECUTABLE}" describe --match=NeVeRmAtCh --always --abbrev=8 | ||
WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}" | ||
OUTPUT_VARIABLE GIT_SHA1 | ||
ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE) | ||
|
||
# the date of the commit | ||
execute_process(COMMAND | ||
"${GIT_EXECUTABLE}" log -1 --format=%ad --date=local | ||
WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}" | ||
OUTPUT_VARIABLE GIT_DATE | ||
ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE) | ||
|
||
# the subject of the commit | ||
execute_process(COMMAND | ||
"${GIT_EXECUTABLE}" log -1 --format=%s | ||
WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}" | ||
OUTPUT_VARIABLE GIT_COMMIT_SUBJECT | ||
ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# dependencies | ||
|
||
find_package(Threads REQUIRED) | ||
|
||
# third-party | ||
|
||
#add_subdirectory(third-party) | ||
|
||
# examples | ||
|
||
if (EMSCRIPTEN) | ||
add_subdirectory(whisper.wasm) | ||
else() | ||
endif() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
set(TARGET whisper.wasm) | ||
|
||
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/index-tmpl.html ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${TARGET}/index.html @ONLY) | ||
configure_file(${CMAKE_SOURCE_DIR}/bindings/javascript/whisper.js ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${TARGET}/whisper.js COPYONLY) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# whisper.wasm | ||
|
||
Live demo: https://whisper.ggerganov.com |
Oops, something went wrong.