# Contributor: John Gebbie <me@johngebbie.com>
# Maintainer: John Gebbie <me@johngebbie.com>
pkgname=vosk-api
pkgver=0.3.50
pkgrel=1
_kaldi=2b69aed630e26fb2c700bba8c45f3bd012371c5c
pkgdesc="Offline speech recognition toolkit"
url="https://alphacephei.com/vosk"
arch="x86_64 aarch64 armv7"
license="Apache-2.0"
depends_dev="$pkgname=$pkgver-r$pkgrel"
makedepends="
	bash
	libgfortran
	openblas-dev
	openfst-dev
	py3-cffi
	py3-gpep517
	py3-setuptools
	py3-wheel
	"
checkdepends="vosk-model-small-en-us"
subpackages="$pkgname-dev py3-$pkgname-pyc py3-$pkgname:_py3:noarch"
source="https://github.com/alphacep/vosk-api/archive/v$pkgver/vosk-api-$pkgver.tar.gz
	kaldi-$_kaldi.tar.gz::https://github.com/alphacep/kaldi/archive/$_kaldi.tar.gz
	kaldi-no-armv7-neon.patch.noauto
	kaldi-to-openblas-0.3.21.patch.noauto
	kaldi-to-openfst-1.8.2.patch.noauto
	vosk-openblas-one-thread.patch
	vosk-openblas.patch
	vosk-shared-openfst.patch
	"

prepare() {
	cd "$srcdir"
	patch -Np1 -d "kaldi-$_kaldi" < kaldi-to-openblas-0.3.21.patch.noauto
	patch -Np1 -d "kaldi-$_kaldi" < kaldi-to-openfst-1.8.2.patch.noauto
	patch -Np1 -d "kaldi-$_kaldi" < kaldi-no-armv7-neon.patch.noauto

	# link to system dirs
	cd "$srcdir"/kaldi-$_kaldi/tools
	mkdir -p OpenBLAS/install/lib
	ln -sfv /usr/include/openblas OpenBLAS/install/include
	ln -sfv /usr/lib/* OpenBLAS/install/lib
	mkdir -p openfst/include
	ln -sfv /usr/include/fst openfst/include
	ln -sfv /usr/lib openfst/lib

	default_prepare
}

build() {
	cd "$srcdir/kaldi-$_kaldi/src"
	# not autotools
	./configure \
		--mathlib=OPENBLAS \
		--shared \
		--use-cuda=no
	# Avoid the backtrace function which is not in musl.
	sed -i 's/-DHAVE_EXECINFO_H=1//g' kaldi.mk
	make online2 lm rnnlm

	cd "$builddir/src"
	make \
		KALDI_ROOT="../../kaldi-$_kaldi" \
		USE_SHARED_MATH=1 \
		HAVE_OPENBLAS=1 \
		HAVE_OPENBLAS_CLAPACK=0

	cd "$builddir/python"
	gpep517 build-wheel \
		--wheel-dir .dist \
		--output-fd 3 3>&1 >&2
}

package() {
	install -Dm644 src/vosk_api.h -t "$pkgdir"/usr/include
	install -Dm644 src/libvosk.so -t "$pkgdir"/usr/lib

	cd "$builddir/python"
	python3 -m installer -d "$pkgdir" \
		.dist/*.whl
}

check() {
	cd c
	ln -sfv /usr/share/vosk-models/small-en-us model
	ln -sfv ../python/example/test.wav .
	make LDFLAGS="-L../src -lvosk -pthread -lopenblas -lgfortran -Wl,-rpath,../src $LDFLAGS"
	./test_vosk | grep -q '"text" : "zero one eight zero three"'
}

_py3() {
	depends="
		py3-requests
		py3-srt
		py3-tqdm
		py3-websockets
		"
	amove usr/lib/python*
	ln -sfv ../../../libvosk.so "$subpkgdir"/usr/lib/python*/site-packages/vosk/libvosk.so
}

sha512sums="
cc627fefd87336a7507444ffa201d1ef0b325fdc79659e6dc0f616671d82226977d567decda414b4a1b17986a9755e400c135d9637069dc96a067240a69484fa  vosk-api-0.3.50.tar.gz
e70f06e03445cd07aa4c20f7e1af1e9e922a708e24b68e35f1cd68249bf2ed0b4c91260b253ec3cb1665835db4bb18bdf8edf2dc94ad78401a115b61771c9448  kaldi-2b69aed630e26fb2c700bba8c45f3bd012371c5c.tar.gz
2abc35c26790aa8e512330a76c258444801f5389c6b5dda15ce185cd901eaac1626fe6d27c6b5698a1376c3a91f498f870f3b1f7a14dea6b5f3c94225b2cb191  kaldi-no-armv7-neon.patch.noauto
64aa075252aabed4f5f1664e86afda53105148de649b9d7bef690673d3d17cbebfee94f1b8d289104f45fa6cb2d7d9c8c3f6f44f28878e00efed5d2520210240  kaldi-to-openblas-0.3.21.patch.noauto
c932ff420094fd9f5a2f66e90da0a9d5b8513e39bb990d4b68faad609bf0c8e223fd933b4536afffd5d28eea233b1aa1918df53ad5ec761289e29f35412c848f  kaldi-to-openfst-1.8.2.patch.noauto
ba4fe230ba1f8e1cec6ee93a1ce9a7586a2358c5b4844f89da5c59f94e03499e6b872a5bba6d0e1cab0b58aba8812d0fc5147c320b5bbf8d9199ff406d72c15d  vosk-openblas-one-thread.patch
9fa662cc1f6fb475164eb2d7ce76baa3e483ccb7459f0615141cb3df52fdc9774bc995df8d1b36db683d85a5b74578fbf5eeaac6f1de8a814ce276d8ee99dcc7  vosk-openblas.patch
ef5b6eb20384569cbe67af66a8425249fe80a733fe69e6afa0e8d5484cb216635828d33aa673a1ba3e9dff95869a1320a7fd5da6e77ec029f1eb830d248408f7  vosk-shared-openfst.patch
"
