Embedded Linux (aarch64)

This page describes how to build sherpa-ncnn for embedded Linux (aarch64, 64-bit) with cross-compiling on an x86 machine with Ubuntu OS.

Caution

If you want to build sherpa-ncnn directly on your board, please don’t use this document. Refer to Linux instead.

Caution

If you want to build sherpa-ncnn directly on your board, please don’t use this document. Refer to Linux instead.

Caution

If you want to build sherpa-ncnn directly on your board, please don’t use this document. Refer to Linux instead.

Hint

This page is for cross-compiling.

Install toolchain

The first step is to install a toolchain for cross-compiling.

Warning

You can use any toolchain that is suitable for your platform. The toolchain we use below is just an example.

Visit https://releases.linaro.org/components/toolchain/binaries/latest-7/aarch64-linux-gnu/ to download the toolchain.

We are going to download gcc-linaro-7.5.0-2019.12-x86_64_aarch64-linux-gnu.tar.xz, which has been uploaded to https://huggingface.co/csukuangfj/sherpa-ncnn-toolchains.

Assume you want to install it in the folder $HOME/software:

mkdir -p $HOME/software
cd $HOME/software
wget https://huggingface.co/csukuangfj/sherpa-ncnn-toolchains/resolve/main/gcc-linaro-7.5.0-2019.12-x86_64_aarch64-linux-gnu.tar.xz

# For users from China
# 中国国内用户,如果访问不了 huggingface, 请使用
# wget https://hf-mirror.com/csukuangfj/sherpa-ncnn-toolchains/resolve/main/gcc-linaro-7.5.0-2019.12-x86_64_aarch64-linux-gnu.tar.xz
tar xvf gcc-linaro-7.5.0-2019.12-x86_64_aarch64-linux-gnu.tar.xz

Next, we need to set the following environment variable:

export PATH=$HOME/software/gcc-linaro-7.5.0-2019.12-x86_64_aarch64-linux-gnu/bin:$PATH

To check that we have installed the cross-compiling toolchain successfully, please run:

aarch64-linux-gnu-gcc --version

which should print the following log:

aarch64-linux-gnu-gcc (Linaro GCC 7.5-2019.12) 7.5.0
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Congratulations! You have successfully installed a toolchain for cross-compiling sherpa-ncnn.

Build sherpa-ncnn

Finally, let us build sherpa-ncnn.

git clone https://github.com/k2-fsa/sherpa-ncnn
cd sherpa-ncnn
./build-aarch64-linux-gnu.sh

After building, you will get two binaries:

$ ls -lh build-aarch64-linux-gnu/install/bin/
total 10M
-rwxr-xr-x 1 kuangfangjun root 3.4M Jan 13 21:16 sherpa-ncnn
-rwxr-xr-x 1 kuangfangjun root 3.4M Jan 13 21:16 sherpa-ncnn-alsa

That’s it!

Hint

  • sherpa-ncnn is for decoding a single file

  • sherpa-ncnn-alsa is for real-time speech recongition by reading the microphone with ALSA

sherpa-ncnn-alsa

Caution

We recommend that you use sherpa-ncnn-alsa on embedded systems such as Raspberry pi.

You need to provide a device_name when invoking sherpa-ncnn-alsa. We describe below how to find the device name for your microphone.

Run the following command:

arecord -l

to list all avaliable microphones for recording. If it complains that arecord: command not found, please use sudo apt-get install alsa-utils to install it.

If the above command gives the following output:

**** List of CAPTURE Hardware Devices ****
card 3: UACDemoV10 [UACDemoV1.0], device 0: USB Audio [USB Audio]
  Subdevices: 1/1
  Subdevice #0: subdevice #0

In this case, I only have 1 microphone. It is card 3 and that card has only device 0. To select card 3 and device 0 on that card, we need to pass plughw:3,0 to sherpa-ncnn-alsa. (Note: It has the format plughw:card_number,device_index.)

For instance, you have to use

./bin/sherpa-ncnn-alsa \
  ./sherpa-ncnn-conv-emformer-transducer-small-2023-01-09/tokens.txt \
  ./sherpa-ncnn-conv-emformer-transducer-small-2023-01-09/encoder_jit_trace-pnnx.ncnn.param \
  ./sherpa-ncnn-conv-emformer-transducer-small-2023-01-09/encoder_jit_trace-pnnx.ncnn.bin \
  ./sherpa-ncnn-conv-emformer-transducer-small-2023-01-09/decoder_jit_trace-pnnx.ncnn.param \
  ./sherpa-ncnn-conv-emformer-transducer-small-2023-01-09/decoder_jit_trace-pnnx.ncnn.bin \
  ./sherpa-ncnn-conv-emformer-transducer-small-2023-01-09/joiner_jit_trace-pnnx.ncnn.param \
  ./sherpa-ncnn-conv-emformer-transducer-small-2023-01-09/joiner_jit_trace-pnnx.ncnn.bin \
  "plughw:3,0"

Please change the card number and also the device index on the selected card accordingly in your own situation. Otherwise, you won’t be able to record with your microphone.

Please read Pre-trained models for usages about the generated binaries.

Hint

If you want to select a pre-trained model for Raspberry that can be run on real-time, we recommend you to use marcoyang/sherpa-ncnn-conv-emformer-transducer-small-2023-01-09 (English).

Read below if you want to learn more.

Hint

By default, all external dependencies are statically linked. That means, the generated binaries are self-contained.

You can use the following commands to check that and you will find they depend only on system libraries.

$ readelf -d build-aarch64-linux-gnu/install/bin/sherpa-ncnn

Dynamic section at offset 0x302a80 contains 30 entries:
  Tag        Type                         Name/Value
 0x0000000000000001 (NEEDED)             Shared library: [libgomp.so.1]
 0x0000000000000001 (NEEDED)             Shared library: [libpthread.so.0]
 0x0000000000000001 (NEEDED)             Shared library: [libstdc++.so.6]
 0x0000000000000001 (NEEDED)             Shared library: [libm.so.6]
 0x0000000000000001 (NEEDED)             Shared library: [libgcc_s.so.1]
 0x0000000000000001 (NEEDED)             Shared library: [libc.so.6]
 0x000000000000000f (RPATH)              Library rpath: [$ORIGIN]

$ readelf -d build-aarch64-linux-gnu/install/bin/sherpa-ncnn-alsa

Dynamic section at offset 0x34ea48 contains 31 entries:
  Tag        Type                         Name/Value
 0x0000000000000001 (NEEDED)             Shared library: [libasound.so.2]
 0x0000000000000001 (NEEDED)             Shared library: [libgomp.so.1]
 0x0000000000000001 (NEEDED)             Shared library: [libpthread.so.0]
 0x0000000000000001 (NEEDED)             Shared library: [libstdc++.so.6]
 0x0000000000000001 (NEEDED)             Shared library: [libm.so.6]
 0x0000000000000001 (NEEDED)             Shared library: [libgcc_s.so.1]
 0x0000000000000001 (NEEDED)             Shared library: [libc.so.6]
 0x000000000000000f (RPATH)              Library rpath: [$ORIGIN]

Please create an issue at https://github.com/k2-fsa/sherpa-ncnn/issues if you have any problems.