# POWER Maintainer: Alexander Baldeck <alex.bldck@gmail.com>
# Maintainer: David Runge <dvzrv@archlinux.org>
# Contributor: Ray Rashif <schiv@archlinux.org>
# Contributor: Tobias Kieslich tobias [funnychar] archlinux org

pkgbase=aubio
pkgname=(aubio python-aubio)
pkgver=0.4.9
pkgrel=19
pkgdesc="A tool for extracting annotations from audio signals"
arch=(x86_64 powerpc64le powerpc64 powerpc riscv64)
url="https://aubio.org/"
license=(GPL-3.0-or-later)
makedepends=(
  doxygen
  ffmpeg
  fftw
  jack
  libsamplerate
  libsndfile
  python-build
  python-installer
  python-numpy
  python-setuptools
  python-wheel
  txt2man
  waf
)
checkdepends=(python-pytest)
source=(
  https://$pkgbase.org/pub/$pkgbase-$pkgver.tar.bz2
  $pkgname-0.4.9-ffmpeg5.patch::https://github.com/aubio/aubio/commit/8a05420e5dd8c7b8b2447f82dc919765876511b3.patch
  https://github.com/aubio/aubio/commit/cdfe9cef.patch
  https://github.com/aubio/aubio/commit/245deead.patch
  https://github.com/aubio/aubio/commit/0b947f96.patch
  https://github.com/aubio/aubio/commit/53bc55cd.patch
  ffmpeg7.patch
  numpy-2.0.patch
)
sha512sums=('0cb81bb4b15051db3f3f4d160d500af56fdfb237e0a74e3f366f53c2870030aa0a7cee8469a611a9694c36b8866d3d42ffb48241c999de08f3fee43e6d903130'
            '8ce2a1fc35f73586d92e637d76354843c8cd1bdd5cda2b8e56db6b05ddcea4d2297500f9039f53d9482bff68fc1ee9e60c9459799a900e85f373745b3258b7ef'
            '9a9eaa609958f4a5b3994a6da103c3497d3a85239f6c45e95dcdc8fbe304f16d8c07adb6076b4a8832d0b8ca66ec210e0d8ecdcda309e0d12a57d9072e4b2449'
            '31577c9af6f07e0022fd83f15eacc373253f76b0d7982ff19f42ad3308e41e80188ec6a8785139f5fabd23a8efb1097d0558905443672cd8da6eacfa7d262f64'
            '731dfea4b98f09b8107c8e4ba13b951da095f2b29000f8f533506913e45bfebfb2b68a5918d6af045a9e719e0fca0b6ee9a1b76c77831b759f4f2cff93184de5'
            '5a5635224a42713c1196c27bb72154daf3f5b300f704968c960548d3b146f2587039a766985358afbe916a59d459f623c6eeb691ff864060dc82e83a512e3ca2'
            'b99aaf419a6695c6086c556b269b542b9cc67e2d18545cdd393df8842d727ac4e829fc464715aa933efde1180886a90a7f51e105ec139fc3b73116cc7c0c5dca'
            'da3e963dcc52abdf2cbf683203cd9cc3e2d1cfcb3350de711168458f7ccfb34dd6f7229717c980d884752ea2707ce8c3a595667ab0bf0e13d2efb8c1a89aca4c')
b2sums=('b849d92678ea8fb20e17921b1a61fdc85a84d4a528acfefa3278169ab5f7fe935217ec37dd9f931202f0f0a1fd3a9518372afbce34cc2dd3ca30c6b4e5d9a301'
        'aa4c52f5d38d72e00691566fff80400acf0bb95bbdcd9f08da4083fe7d782c04e23bb65f6b547a65d3ec711fc33b9ac46bbe386b263b2d3c16265405f01db450'
        'f37d42d6631c04b1fef9d6d5aca0c049705f6a306743e9229b4e005c577e58945dd01990a4155cbf261bdfdd0a99d68a695e2d97e6111ed36021aa47e5582f70'
        'caa5a972c81f7587ac409bacf24427f13bdcfb0e0daa614405c6b11da32be6f5de5540ea8760023bf96faa37ace3b613a70424c78efe1fb8789e5d1a9cec8b90'
        '385d78ee72fa88ca503a8ae584705177dab04788e94b7bb655deb78d19fe86dd156701f71710a704b57977ae4ec396ff86ad65c98280e8f46266a47c9668e094'
        '8c465895a426c142db4c18a587d88beebd1c05e63d31059998260bdf1623ea094ffe64965830cef3ecd1e49da956277f5d3fa9f44a33ad3eba1ba4fac6c9f4b5'
        '6521c7f52152d83e152646f3d9f8b15fd62794f1217b7ed5947797a891630a65fbb00914774a8317249a345fd987d770224bc425cce9631a50aae591d39b66bd'
        '90d52e814c19727347dd719104bdaabdf790daa7fc9d60dbcd33e4d6d92953ea079ca29798feed63493cf1f68263d7e7ceb64ee4655225e112ded774c29ad1ba')

prepare() {
  # make compatible with ffmpeg >= 5: https://github.com/aubio/aubio/issues/353
  patch -Np1 -d $pkgbase-$pkgver -i ../$pkgname-0.4.9-ffmpeg5.patch
  # Fix build with FFmpeg 7
  patch -p1 -d $pkgbase-$pkgver -i ../cdfe9cef.patch
  patch -p1 -d $pkgbase-$pkgver -i ../245deead.patch
  patch -p1 -d $pkgbase-$pkgver -i ../0b947f96.patch
  patch -p1 -d $pkgbase-$pkgver -i ../53bc55cd.patch
  patch -p1 -d $pkgbase-$pkgver -i ../ffmpeg7.patch
  # Fix test with numpy 2.0
  patch -p1 -d $pkgbase-$pkgver -i ../numpy-2.0.patch
  cd $pkgbase-$pkgver
  rm -rv waflib
}

build() {
  local waf_configure_options=(
    --alltests
    --enable-fftw3
    --libdir=/usr/lib
    --prefix=/usr
  )

  cd $pkgbase-$pkgver
  export LINKFLAGS="$LDFLAGS"
  waf configure "${waf_configure_options[@]}"
  waf build

  CFLAGS+=" -Wno-incompatible-pointer-types" \
  python -m build --wheel --no-isolation
}

check() {
  local _site_packages=$(python -c "import site; print(site.getsitepackages()[0])")

  cd $pkgbase-$pkgver
  # install to temporary location
  python -m installer --destdir=test_dir dist/*.whl
  export PYTHONPATH="test_dir/$_site_packages:$PYTHONPATH"
  export LD_LIBRARY_PATH="$PWD/build/src"
  export PYTHONDONTWRITEBYTECODE=1
  pytest -vv python/tests
}

package_aubio() {
  pkgdesc="A tool for extracting annotations from audio signals"
  depends=(
    ffmpeg libavcodec.so libavformat.so libavutil.so libswresample.so
    fftw
    glibc
    jack libjack.so
    libsamplerate libsamplerate.so
    libsndfile libsndfile.so
  )
  optdepends=('python-aubio: Python bindings')
  provides=(libaubio.so)
  groups=(pro-audio)

  cd $pkgbase-$pkgver
  waf --destdir="$pkgdir" install

  # move doc, as waf configure doesn't honor the htmldir and docdir settings
  mv -v "$pkgdir/usr/share/doc/lib$pkgname-doc" "$pkgdir/usr/share/doc/$pkgname"
  install -vDm 644 {AUTHORS,ChangeLog,README.md} -t "$pkgdir/usr/share/doc/$pkgname/"

  if [ -d ${pkgdir}/usr/lib64 ]; then
    mkdir -p ${pkgdir}/usr/lib
    mv ${pkgdir}/usr/lib64/* ${pkgdir}/usr/lib/
    rmdir ${pkgdir}/usr/lib64
  fi
}

package_python-aubio() {
  pkgdesc="Python bindings for aubio"
  depends=(
    aubio libaubio.so
    glibc
    python
    python-numpy
  )

  cd $pkgbase-$pkgver
  python -m installer --destdir="$pkgdir" dist/*.whl
  install -vDm 644 {AUTHORS,ChangeLog,README.md} -t "$pkgdir/usr/share/doc/$pkgname/"
}
# vim:set ts=2 sw=2 et:
