summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Glover <j@johnglover.net>2012-07-20 21:35:41 +0100
committerJohn Glover <j@johnglover.net>2012-07-20 21:35:41 +0100
commit212b366c2c15b9cbe7439a87a0e4189a824a9e2c (patch)
treed7f30618490d8b88d6995567e21b124e15277344
parente3d5489b10e8f27970b2f59ae6e1f2ec398a032d (diff)
downloadsimpl-212b366c2c15b9cbe7439a87a0e4189a824a9e2c.tar.gz
simpl-212b366c2c15b9cbe7439a87a0e4189a824a9e2c.tar.bz2
simpl-212b366c2c15b9cbe7439a87a0e4189a824a9e2c.zip
[peak_detection] Add a Cython implementation of PeakDetection.find_peaks so that find_peaks_in_frame will be
called from derived Python classes (as well as C++).
-rw-r--r--simpl/base.pyx1
-rw-r--r--simpl/peak_detection.pyx31
2 files changed, 16 insertions, 16 deletions
diff --git a/simpl/base.pyx b/simpl/base.pyx
index 0fa6e84..dda9240 100644
--- a/simpl/base.pyx
+++ b/simpl/base.pyx
@@ -48,6 +48,7 @@ cdef class Frame:
def __dealloc__(self):
if self.created and self.thisptr:
del self.thisptr
+ self.thisptr = <c_Frame*>0
cdef set_frame(self, c_Frame* f):
self.thisptr = f
diff --git a/simpl/peak_detection.pyx b/simpl/peak_detection.pyx
index 6afc80a..0b598b8 100644
--- a/simpl/peak_detection.pyx
+++ b/simpl/peak_detection.pyx
@@ -12,9 +12,11 @@ from base cimport c_Frame
cdef class PeakDetection:
cdef c_PeakDetection* thisptr
+ cdef public list frames
def __cinit__(self):
self.thisptr = new c_PeakDetection()
+ self.frames = []
def __dealloc__(self):
if self.thisptr:
@@ -61,15 +63,6 @@ cdef class PeakDetection:
f.set_frame(c_f)
return f
- property frames:
- def __get__(self):
- return [self.frame(i) for i in range(self.thisptr.num_frames())]
- def __set__(self, new_frames):
- cdef vector[c_Frame*] c_frames
- for f in new_frames:
- c_frames.push_back((<Frame>f).thisptr)
- self.thisptr.frames(c_frames)
-
def find_peaks_in_frame(self, Frame frame not None):
peaks = []
cdef vector[c_Peak*] c_peaks = self.thisptr.find_peaks_in_frame(frame.thisptr)
@@ -80,13 +73,19 @@ cdef class PeakDetection:
return peaks
def find_peaks(self, np.ndarray[dtype_t, ndim=1] audio):
- frames = []
- cdef vector[c_Frame*] output_frames = self.thisptr.find_peaks(len(audio), <double*> audio.data)
- for i in range(output_frames.size()):
- f = Frame(output_frames[i].size(), False)
- f.set_frame(output_frames[i])
- frames.append(f)
- return frames
+ self.frames = []
+ cdef int pos = 0
+ while pos < len(audio):
+ if not self.static_frame_size:
+ self.frame_size = self.next_frame_size()
+ frame = Frame(self.frame_size)
+ frame.audio = audio[pos:pos + self.frame_size]
+ frame.max_peaks = self.max_peaks
+ peaks = self.find_peaks_in_frame(frame)
+ self.frames.append(frame)
+ pos += self.hop_size
+
+ return self.frames
cdef class SMSPeakDetection(PeakDetection):