changeset 262:7063ad521438

Add Python bindings to build system
author Jamie Bullock <jamie@jamiebullock.com>
date Sat, 08 Nov 2014 00:06:06 +0000
parents 19618ffb2613
children 5b13897691e8
files Makefile swig/Makefile swig/python/.gitignore swig/python/__init__.py swig/python/test.py swig/test.py
diffstat 5 files changed, 93 insertions(+), 60 deletions(-) [+]
line wrap: on
line diff
--- a/Makefile	Fri Nov 07 23:58:26 2014 +0000
+++ b/Makefile	Sat Nov 08 00:06:06 2014 +0000
@@ -6,7 +6,7 @@
 
 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 @@
 examples:
 	@$(MAKE) -C $@
 
+swig: src
+	@$(MAKE) -C $@
+
 install:
 	@$(MAKE) -C src install
 	@$(MAKE) -C examples install
@@ -28,4 +31,5 @@
 clean:
 	@$(MAKE) -C src clean
 	@$(MAKE) -C examples clean
+	@$(MAKE) -C swig clean
 	@$(RM) -r dist
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/swig/Makefile	Sat Nov 08 00:06:06 2014 +0000
@@ -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
+
--- a/swig/python/.gitignore	Fri Nov 07 23:58:26 2014 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,2 +0,0 @@
-xtract.py
-xtract_wrap.c
\ No newline at end of file
--- a/swig/python/test.py	Fri Nov 07 23:58:26 2014 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -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'
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/swig/test.py	Sat Nov 08 00:06:06 2014 +0000
@@ -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'