From 89a9ec7a352dc35141b8f9a87c202fdd448e9a6f Mon Sep 17 00:00:00 2001 From: Jamie Bullock Date: Sat, 8 Nov 2014 00:06:06 +0000 Subject: Add Python bindings to build system --- Makefile | 6 +++++- swig/Makefile | 31 +++++++++++++++++++++++++++ swig/python/.gitignore | 2 -- swig/python/__init__.py | 0 swig/python/test.py | 57 ------------------------------------------------- swig/test.py | 57 +++++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 93 insertions(+), 60 deletions(-) create mode 100644 swig/Makefile delete mode 100644 swig/python/.gitignore delete mode 100644 swig/python/__init__.py delete mode 100644 swig/python/test.py create mode 100644 swig/test.py diff --git a/Makefile b/Makefile index e2f1cbb..e880ca9 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ HPATH = include/xtract export XTRACT_VERSION PREFIX LIBRARY -.PHONY: examples clean install doc src +.PHONY: examples clean install doc src swig all: src examples @@ -19,6 +19,9 @@ doc: examples: @$(MAKE) -C $@ +swig: src + @$(MAKE) -C $@ + install: @$(MAKE) -C src install @$(MAKE) -C examples install @@ -28,4 +31,5 @@ install: clean: @$(MAKE) -C src clean @$(MAKE) -C examples clean + @$(MAKE) -C swig clean @$(RM) -r dist diff --git a/swig/Makefile b/swig/Makefile new file mode 100644 index 0000000..2d47e12 --- /dev/null +++ b/swig/Makefile @@ -0,0 +1,31 @@ +NAME = xtract + +OS := $(shell uname) + +# Assume that since we're building the python bindings, we have a python installed! +INCLUDEPY = $(shell python -m sysconfig | grep -w INCLUDEPY | awk '{print $$3}') + +ifeq ($(OS), Darwin) + CFLAGS=-g -c + LD=gcc + LDFLAGS=-bundle -flat_namespace -undefined suppress +else + CFLAGS=-g -c -fPIC + LD=ld + LDFLAGS=-shared +endif + +.PHONY: python + +python: + @swig -I../include -python $(NAME).i + @$(CC) $(CFLAGS) $(NAME)_wrap.c -o $(NAME)_wrap.o -I$(INCLUDEPY) -I../include + @$(CC) $(LDFLAGS) ../src/lib$(NAME).a $(NAME)_wrap.o -o _$(NAME).so -framework Accelerate + +clean: + @$(RM) *.o + @$(RM) *.pyc + @$(RM) *.so + @$(RM) $(NAME)_wrap.c + @$(RM) $(NAME).py + diff --git a/swig/python/.gitignore b/swig/python/.gitignore deleted file mode 100644 index 6dfa0ae..0000000 --- a/swig/python/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -xtract.py -xtract_wrap.c \ No newline at end of file diff --git a/swig/python/__init__.py b/swig/python/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/swig/python/test.py b/swig/python/test.py deleted file mode 100644 index a89a6f9..0000000 --- a/swig/python/test.py +++ /dev/null @@ -1,57 +0,0 @@ -#!/usr/bin/python - -try: - import libxtract.xtract as xtract -except ImportError: - print 'Failed to load the library "xtract"' - -print '\nRunning libxtract Python bindings test...\n' - -len = 8 - -a = xtract.doubleArray(len) -temp = [] - -for i in range(0, len): - a[i] = 2 * i - temp.append(str(a[i])) - -mean = xtract.xtract_mean(a,len,None)[1] - -print 'The mean of ' + ', '.join(temp) + ' is: %.2f' % mean - -argv = xtract.doubleArray(1) -argv[0] = mean - -variance = xtract.xtract_variance(a, len, argv)[1] - -print 'The variance is %.2f' % variance - -print 'Computing spectrum...' - -argv = xtract.doubleArray(1) -argv[0] = 44100.0 / len # Fake sample rate - -xtract.xtract_init_fft(len, xtract.XTRACT_SPECTRUM); - -result = xtract.doubleArray(len) - -xtract.xtract_spectrum(a,len,argv, result) - - -for i in range(len): - print result[i] - - -print 'Computing windowed subframes...' - -for i in range(0, len): - a[i] = 1.0 - -window = xtract.xtract_init_window(len / 2, xtract.XTRACT_HANN) -xtract.xtract_features_from_subframes(a, len, xtract.XTRACT_WINDOWED, window, result) - -for i in range(len): - print result[i] - -print '\nFinished!\n' diff --git a/swig/test.py b/swig/test.py new file mode 100644 index 0000000..4d14171 --- /dev/null +++ b/swig/test.py @@ -0,0 +1,57 @@ +#!/usr/bin/python + +try: + import xtract +except ImportError: + print 'Failed to load the library "xtract"' + +print '\nRunning libxtract Python bindings test...\n' + +len = 8 + +a = xtract.doubleArray(len) +temp = [] + +for i in range(0, len): + a[i] = 2 * i + temp.append(str(a[i])) + +mean = xtract.xtract_mean(a,len,None)[1] + +print 'The mean of ' + ', '.join(temp) + ' is: %.2f' % mean + +argv = xtract.doubleArray(1) +argv[0] = mean + +variance = xtract.xtract_variance(a, len, argv)[1] + +print 'The variance is %.2f' % variance + +print 'Computing spectrum...' + +argv = xtract.doubleArray(1) +argv[0] = 44100.0 / len # Fake sample rate + +xtract.xtract_init_fft(len, xtract.XTRACT_SPECTRUM); + +result = xtract.doubleArray(len) + +xtract.xtract_spectrum(a,len,argv, result) + + +for i in range(len): + print result[i] + + +print 'Computing windowed subframes...' + +for i in range(0, len): + a[i] = 1.0 + +window = xtract.xtract_init_window(len / 2, xtract.XTRACT_HANN) +xtract.xtract_features_from_subframes(a, len, xtract.XTRACT_WINDOWED, window, result) + +for i in range(len): + print result[i] + +print '\nFinished!\n' -- cgit v1.2.3