# Maintainer: aur.chaotic.cx

_pkgname="piper-voices"
pkgname="$_pkgname-common"
pkgver=1.0.0
pkgrel=7
pkgdesc="Voices for Piper text to speech system (common files)"
url="https://huggingface.co/rhasspy/piper-voices"
license=("MIT")
arch=("any")

depends=(
  "piper-tts"
  "alsa-utils"
)
optdepends=(
  'speech-dispatcher: tts support for third party apps'
)

install="$pkgname.install"

source=("voices-$pkgver.json"::"https://huggingface.co/rhasspy/piper-voices/resolve/v$pkgver/voices.json")
sha256sums=('66f6b221551769c5237f46c6f104cd1ff099a6f1e7a91c6e6eea4dc4bd4f1f03')

package() {
  install -Dm664 "voices-$pkgver.json" "$pkgdir/usr/share/$_pkgname/voices.json"

  # https://aur.archlinux.org/packages/piper-tts-bin#comment-939482
  install -Dm775 /dev/stdin "$pkgdir/usr/bin/piper-dispatcher" << END
#!/usr/bin/env bash

VOICES_PATH="/usr/share/piper-voices"

if [[ \${VOICE: -3} = low ]]; then
  RATE=16000
else
  RATE=22050
fi

printf %s "\$DATA" | piper-tts --model "\$VOICES_PATH/\$VOICE.onnx" --output-raw | aplay -r "\$RATE" -f S16_LE -t raw -

wait
END

  install -Dm664 /dev/stdin "$pkgdir/etc/speech-dispatcher/modules/piper-generic.conf" << END
Debug "1"

GenericPunctNone ""
GenericPunctSome "--punct=\"()[]{};:\""
GenericPunctMost "--punct=\"()[]{};:\""
GenericPunctAll "--punct"

GenericExecuteSynth "env DATA="\"\$DATA\"" VOICE="\"\$VOICE\"" /usr/bin/piper-dispatcher"

GenericCmdDependency "piper-tts"

AddVoice "en" "MALE1"    "en/en_US/ryan/high/en_US-ryan-high"

DefaultVoice             "en/en_US/ryan/high/en_US-ryan-high"
END
}
