k2-fsa / sherpa-ncnn

Real-time speech recognition using next-gen Kaldi with ncnn without Internet connection. Support iOS, Android, Raspberry Pi, VisionFive2, LicheePi4A etc.
https://k2-fsa.github.io/sherpa/ncnn/index.html
Apache License 2.0
902 stars 138 forks source link

[Help needed] Support wasm #289

Closed csukuangfj closed 5 months ago

csukuangfj commented 7 months ago

Currently, sherpa-ncnn is able to build for wasm with the following commands:

#!/usr/bin/env bash
# Copyright (c)  2023  Xiaomi Corporation
#
# This script is to build sherpa-ncnn for WebAssembly
#
# See also
# https://github.com/Tencent/ncnn/wiki/how-to-build#build-for-webassembly

set -ex

if [ x"$EMSCRIPTEN" == x"" ]; then
  if ! command -v emcc &> /dev/null; then
    echo "Please install emsripten first"
    echo ""
    echo "You can use the following commands to install it:"
    echo ""
    echo "git clone https://github.com/emscripten-core/emsdk.git"
    echo "cd emsdk"
    echo "git pull"
    echo "./emsdk install latest"
    echo "./emsdk activate latest"
    echo "source ./emsdk_env.sh"
    exit 1
  else
    EMSCRIPTEN=$(dirname $(realpath $(which emcc)))
  fi
fi

export EMSCRIPTEN=$EMSCRIPTEN
echo "EMSCRIPTEN: $EMSCRIPTEN"
if [ ! -f $EMSCRIPTEN/cmake/Modules/Platform/Emscripten.cmake ]; then
  echo "Cannot find $EMSCRIPTEN/cmake/Modules/Platform/Emscripten.cmake"
  echo "Please make sure you have installed emsdk correctly"
  exit 1
fi

# basic build

mkdir -p build-wasm-basic
pushd build-wasm-basic

cmake \
  -DCMAKE_INSTALL_PREFIX=./install \
  -DCMAKE_BUILD_TYPE=Release \
  -DCMAKE_TOOLCHAIN_FILE=$EMSCRIPTEN/cmake/Modules/Platform/Emscripten.cmake \
  -DNCNN_THREADS=OFF \
  -DNCNN_OPENMP=OFF \
  -DNCNN_SIMPLEOMP=OFF \
  -DNCNN_RUNTIME_CPU=OFF \
  -DNCNN_SSE2=OFF \
  -DNCNN_AVX2=OFF \
  -DNCNN_AVX=OFF \
  -DNCNN_BUILD_TOOLS=OFF \
  -DNCNN_BUILD_EXAMPLES=OFF \
  -DNCNN_BUILD_BENCHMARK=OFF \
  \
  -DBUILD_SHARED_LIBS=OFF \
  -DSHERPA_NCNN_ENABLE_PYTHON=OFF \
  -DSHERPA_NCNN_ENABLE_PORTAUDIO=OFF \
  -DSHERPA_NCNN_ENABLE_JNI=OFF \
  -DSHERPA_NCNN_ENABLE_BINARY=OFF \
  -DSHERPA_NCNN_ENABLE_TEST=OFF \
  -DSHERPA_NCNN_ENABLE_C_API=ON \
  -DSHERPA_NCNN_ENABLE_GENERATE_INT8_SCALE_TABLE=OFF \
  -DSHERPA_NCNN_ENABLE_FFMPEG_EXAMPLES=OFF \
  ..
make -j2
make install

It would be nice if someone can provide the frontend part.

deepsadness commented 7 months ago

你好. 我试了这个命令行. 结果好像不对. 输出的结果还是

-- Target arch: x86 32bit
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/joshzxzhong/StudioProjects/sherpa-ncnn/build-wasm-basic
+ make -j2
Consolidate compiler generated dependencies of target kaldi-native-fbank-core
[  0%] Built target ncnn-generate-spirv
[  7%] Built target kaldi-native-fbank-core
Consolidate compiler generated dependencies of target ncnn
[ 79%] Built target ncnn
Consolidate compiler generated dependencies of target sherpa-ncnn-core
[ 98%] Built target sherpa-ncnn-core
Consolidate compiler generated dependencies of target sherpa-ncnn-c-api
[100%] Built target sherpa-ncnn-c-api
+ make install
[  0%] Built target ncnn-generate-spirv
[ 72%] Built target ncnn
[ 79%] Built target kaldi-native-fbank-core
[ 98%] Built target sherpa-ncnn-core
[100%] Built target sherpa-ncnn-c-api
Install the project...
-- Install configuration: "Release"
-- Up-to-date: /Users/joshzxzhong/StudioProjects/sherpa-ncnn/build-wasm-basic/install/lib/libkaldi-native-fbank-core.a
-- Up-to-date: /Users/joshzxzhong/StudioProjects/sherpa-ncnn/build-wasm-basic/install/lib/libncnn.a
-- Up-to-date: /Users/joshzxzhong/StudioProjects/sherpa-ncnn/build-wasm-basic/install/./sherpa-ncnn.pc
-- Up-to-date: /Users/joshzxzhong/StudioProjects/sherpa-ncnn/build-wasm-basic/install/lib/libsherpa-ncnn-core.a
-- Up-to-date: /Users/joshzxzhong/StudioProjects/sherpa-ncnn/build-wasm-basic/install/lib/libsherpa-ncnn-c-api.a
-- Up-to-date: /Users/joshzxzhong/StudioProjects/sherpa-ncnn/build-wasm-basic/install/include/sherpa-ncnn/c-api/c-api.h

没有输出对应的js文件

csukuangfj commented 7 months ago

没有输出对应的js文件

还需要再改一下 sherpa-ncnn/c-api/c-api.h

你可以参考 https://github.com/nihui/ncnn-webassembly-nanodet


如果你以前没有做过 wasm 相关的东西,这个对你还是有一定难度的。

csukuangfj commented 5 months ago

Should be fixed in #300

csukuangfj commented 5 months ago

Closed by #300 and #302