Skip to content

Commit

Permalink
Merge pull request #954 from jithesh82/master
Browse files Browse the repository at this point in the history
compiled aubio
  • Loading branch information
mhsmith committed Sep 19, 2023
2 parents eefb98d + 8e7f7cb commit 73a1e53
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 4 deletions.
10 changes: 6 additions & 4 deletions server/pypi/build-wheel.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,6 @@ def unpack_and_build(self):
self.apply_patches()
self.create_host_env()

self.update_env()

# ProjectBuilder requires at least one of pyproject.toml or setup.py to exist,
# which may not be the case for packages built using build.sh (e.g.
# tflite-runtime).
Expand All @@ -164,6 +162,10 @@ def unpack_and_build(self):
if self.no_build:
log("Skipping build due to --no-build")
else:
# These environment variables are specific to the host platform, so they
# must not be visible to create_build_env.
self.update_environ()

self.create_dummy_libs()
wheel_filename = self.build_wheel()
self.fix_wheel(wheel_filename)
Expand Down Expand Up @@ -227,6 +229,7 @@ def create_build_env(self, src_is_pyproject):
# about 3.5 seconds on Python 3.8, and 6 seconds on Python 3.11. To avoid this,
# we create one bootstrap environment per Python version, shared between all
# packages, and use that to install the build environments.
os.environ["PIP_DISABLE_PIP_VERSION_CHECK"] = "1"
bootstrap_env = self.get_bootstrap_env()
ensure_empty(self.build_env)
run(f"python{self.python} -m venv --without-pip {self.build_env}")
Expand Down Expand Up @@ -469,7 +472,7 @@ def build_with_pep517(self):
except build.BuildBackendException as e:
raise CommandError(e)

def update_env(self):
def update_environ(self):
env = {}
build_common_output = run(
f"abi={self.abi}; api_level={self.api_level}; prefix={self.host_env}/chaquopy; "
Expand Down Expand Up @@ -551,7 +554,6 @@ def update_env(self):
assert_exists(self.python_lib)
self.standard_libs.append(libpython)

env["PIP_DISABLE_PIP_VERSION_CHECK"] = "1"
env["CHAQUOPY_PYTHON"] = self.python
# Use -idirafter so that package-specified -I directories take priority (e.g.
# in grpcio and typed-ast).
Expand Down
16 changes: 16 additions & 0 deletions server/pypi/packages/aubio/meta.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{% if PY_VER == "3.8" %}
{% set numpy_version = "1.19.5" %}
{% else %}
{% set numpy_version = "1.23.3" %}
{% endif %}

package:
name: aubio
version: "0.4.9"

build:
number: 1

requirements:
host:
- numpy {{ numpy_version }}
14 changes: 14 additions & 0 deletions server/pypi/packages/aubio/patches/chaquopy.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
--- src-original/setup.py 2018-12-24 11:49:22.000000000 +0000
+++ src/setup.py 2023-09-19 17:31:43.825715582 +0000
@@ -15,6 +15,11 @@
__version__ = get_aubio_pyversion()
__aubio_version__ = get_aubio_version()

+# Chaquopy
+if "egg_info" not in sys.argv:
+ import builtins
+ builtins.__NUMPY_SETUP__ = True # Prevent the compiled parts from being imported.
+
include_dirs = []
library_dirs = []
define_macros = [('AUBIO_VERSION', '%s' % __aubio_version__)]
121 changes: 121 additions & 0 deletions server/pypi/packages/aubio/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
from numpy.testing import TestCase, assert_equal
from numpy import sin, arange, mean, median, isnan, pi
from aubio import fvec, pitch, freqtomidi, float_type


class aubio_pitch_Good_Values(TestCase):

def skip_test_new_default(self):
" creating a pitch object without parameters "
p = pitch()
assert_equal ( [p.method, p.buf_size, p.hop_size, p.samplerate],
['default', 1024, 512, 44100])

def test_run_on_silence(self):
" creating a pitch object with parameters "
p = pitch('default', 2048, 512, 32000)
assert_equal ( [p.method, p.buf_size, p.hop_size, p.samplerate],
['default', 2048, 512, 32000])

def test_run_on_zeros(self):
" running on silence gives 0 "
p = pitch('default', 2048, 512, 32000)
f = fvec (512)
for _ in range(10): assert_equal (p(f), 0.)

def test_run_on_ones(self):
" running on ones gives 0 "
p = pitch('default', 2048, 512, 32000)
f = fvec (512)
f[:] = 1
for _ in range(10): assert_equal (p(f), 0.)


class aubio_pitch_Sinusoid(TestCase):

def run_pitch_on_sinusoid(self, method, buf_size, hop_size, samplerate, freq):
# create pitch object
p = pitch(method, buf_size, hop_size, samplerate)
# duration in seconds
seconds = .3
# duration in samples
duration = seconds * samplerate
# increase to the next multiple of hop_size
duration = duration - duration % hop_size + hop_size;
# build sinusoid
sinvec = self.build_sinusoid(duration, freq, samplerate)

self.run_pitch(p, sinvec, freq)

def build_sinusoid(self, length, freq, samplerate):
return sin( 2. * pi * arange(length).astype(float_type) * freq / samplerate)

def run_pitch(self, p, input_vec, freq):
pitches, errors = [], []
input_blocks = input_vec.reshape((-1, p.hop_size))
for new_block in input_blocks:
pitch = p(new_block)[0]
pitches.append(pitch)
errors.append(1. - freqtomidi(pitch) / freqtomidi(freq))
assert_equal ( len(input_blocks), len(pitches) )
assert_equal ( isnan(pitches), False )
# cut the first candidates
#cut = ( p.buf_size - p.hop_size ) / p.hop_size
pitches = pitches[2:]
errors = errors[2:]
# check that the mean of all relative errors is less than 10%
#assert abs (mean(errors) ) < 0.1, pitches
assert abs (median(errors) ) < 0.06, "median of relative errors is bigger than 0.06 (%f)\n found %s\n errors %s" % (mean(errors), pitches, errors)
#print 'len(pitches), cut:', len(pitches), cut
#print 'median errors: ', median(errors), 'median pitches: ', median(pitches)

pitch_algorithms = [ "default", "yinfft", "yin", "yinfast", "schmitt", "mcomb", "fcomb" , "specacf" ]
pitch_algorithms = [ "default", "yinfft", "yin", "yinfast", "schmitt", "mcomb", "fcomb" ]

#freqs = [ 27.5, 55., 110., 220., 440., 880., 1760., 3520. ]
freqs = [ 110., 220., 440., 880., 1760., 3520. ]
signal_modes = []
for freq in freqs:
signal_modes += [
( 4096, 2048, 44100, freq ),
( 2048, 512, 44100, freq ),
( 2048, 1024, 44100, freq ),
( 2048, 1024, 32000, freq ),
]

freqs = [ ] #55., 110., 220., 440., 880., 1760., 3520. ]
for freq in freqs:
signal_modes += [
( 2048, 1024, 22050, freq ),
( 1024, 256, 16000, freq ),
( 1024, 256, 8000, freq ),
( 1024, 512+256, 8000, freq ),
]

"""
signal_modes = [
( 4096, 512, 44100, 2.*882. ),
( 4096, 512, 44100, 882. ),
( 4096, 512, 44100, 440. ),
( 2048, 512, 44100, 440. ),
( 2048, 1024, 44100, 440. ),
( 2048, 1024, 44100, 440. ),
( 2048, 1024, 32000, 440. ),
( 2048, 1024, 22050, 440. ),
( 1024, 256, 16000, 440. ),
( 1024, 256, 8000, 440. ),
( 1024, 512+256, 8000, 440. ),
]
"""

def create_test (algo, mode):
def do_test_pitch(self):
self.run_pitch_on_sinusoid(algo, mode[0], mode[1], mode[2], mode[3])
return do_test_pitch

for algo in pitch_algorithms:
for mode in signal_modes:
_test_method = create_test (algo, mode)
_test_method.__name__ = 'test_pitch_%s_%d_%d_%dHz_sin_%.0f' % ( algo,
mode[0], mode[1], mode[2], mode[3] )
setattr (aubio_pitch_Sinusoid, _test_method.__name__, _test_method)

0 comments on commit 73a1e53

Please sign in to comment.