diff options
author | John Glover <j@johnglover.net> | 2012-03-22 22:46:30 +0000 |
---|---|---|
committer | John Glover <j@johnglover.net> | 2012-03-22 22:46:30 +0000 |
commit | 2d6b45e5e5b080d9b4f527734e7f3ae5fb1bf6f4 (patch) | |
tree | e6874ace59d1f00ff45c2bc7ca2799ca29608c1d | |
parent | d9a7ad89d8a7554898eedbd72113eb7e461906ae (diff) | |
download | simpl-2d6b45e5e5b080d9b4f527734e7f3ae5fb1bf6f4.tar.gz simpl-2d6b45e5e5b080d9b4f527734e7f3ae5fb1bf6f4.tar.bz2 simpl-2d6b45e5e5b080d9b4f527734e7f3ae5fb1bf6f4.zip |
[base] updating Cython Frame and PeakDetection classes
-rw-r--r-- | simpl/base.pyx | 138 | ||||
-rw-r--r-- | src/simpl/base.cpp | 6 | ||||
-rw-r--r-- | src/simpl/base.h | 6 | ||||
-rw-r--r-- | tests/test_base.py | 20 |
4 files changed, 152 insertions, 18 deletions
diff --git a/simpl/base.pyx b/simpl/base.pyx index 6cb4fa8..cb43d2f 100644 --- a/simpl/base.pyx +++ b/simpl/base.pyx @@ -1,10 +1,20 @@ import numpy as np cimport numpy as np +from libcpp.vector cimport vector + np.import_array() dtype = np.float64 ctypedef np.double_t dtype_t + +cdef extern from "<string>" namespace "std": + cdef cppclass string: + string() + string(char *) + char * c_str() + + cdef extern from "../src/simpl/base.h" namespace "simpl": cdef cppclass c_Peak "simpl::Peak": c_Peak() @@ -17,13 +27,13 @@ cdef extern from "../src/simpl/base.h" namespace "simpl": c_Frame(int frame_size) # peaks - # int num_peaks() - # int max_peaks() - # void max_peaks(int new_max_peaks) - # void add_peak(Peak peak) + int num_peaks() + int max_peaks() + void max_peaks(int new_max_peaks) + void add_peak(c_Peak* peak) # void add_peaks(Peaks* peaks) - # Peak peak(int peak_number) - # void clear_peaks() + c_Peak* peak(int peak_number) + void clear_peaks() # Peaks::iterator peaks_begin() # Peaks::iterator peaks_end() @@ -46,10 +56,48 @@ cdef extern from "../src/simpl/base.h" namespace "simpl": void synth_residual(double* new_synth_residual) double* synth_residual() + cdef cppclass c_PeakDetection "simpl::PeakDetection": + c_PeakDetection() + + int sampling_rate() + void sampling_rate(int new_sampling_rate) + int frame_size() + void frame_size(int new_frame_size) + int static_frame_size() + void static_frame_size(int new_static_frame_size) + int next_frame_size() + int hop_size() + void hop_size(int new_hop_size) + int max_peaks() + void max_peaks(int new_max_peaks) + string window_type() + void window_type(string new_window_type) + int window_size() + void window_size(int new_window_size) + double min_peak_separation() + void min_peak_separation(double new_min_peak_separation) + # Frames* frames() + # virtual Peaks* find_peaks_in_frame(const Frame& frame) + # virtual Frames* find_peaks(number* audio) + + cdef class Peak: - cdef c_Peak *thisptr - def __cinit__(self): self.thisptr = new c_Peak() - def __dealloc__(self): del self.thisptr + cdef c_Peak* thisptr + cdef int peak_created + + def __cinit__(self, create_new=True): + if create_new: + self.thisptr = new c_Peak() + self.peak_created = True + else: + self.peak_created = False + + def __dealloc__(self): + if self.peak_created: + del self.thisptr + + cdef set_peak(self, c_Peak* p): + self.thisptr = p property amplitude: def __get__(self): return self.thisptr.amplitude @@ -63,8 +111,9 @@ cdef class Peak: def __get__(self): return self.thisptr.phase def __set__(self, double x): self.thisptr.phase = x + cdef class Frame: - cdef c_Frame *thisptr + cdef c_Frame* thisptr def __cinit__(self, size=None): if size: @@ -74,9 +123,34 @@ cdef class Frame: def __dealloc__(self): del self.thisptr + # peaks + property num_peaks: + def __get__(self): return self.thisptr.num_peaks() + + property max_peaks: + def __get__(self): return self.thisptr.max_peaks() + def __set__(self, int i): self.thisptr.max_peaks(i) + + def add_peak(self, Peak p not None): + self.thisptr.add_peak(p.thisptr) + + def peak(self, int i): + cdef c_Peak* c_p = self.thisptr.peak(i) + p = Peak(False) + p.set_peak(c_p) + return p + + property peaks: + def __get__(self): + return [self.peak(i) for i in range(self.thisptr.num_peaks())] + + def clear_peaks(self): + self.thisptr.clear_peaks() + + # audio buffers property size: def __get__(self): return self.thisptr.size() - def __set__(self, int n): self.thisptr.size(n) + def __set__(self, int i): self.thisptr.size(i) property audio: def __get__(self): @@ -109,3 +183,45 @@ cdef class Frame: return np.PyArray_SimpleNewFromData(1, shape, np.NPY_DOUBLE, self.thisptr.synth_residual()) def __set__(self, np.ndarray[dtype_t, ndim=1] a): self.thisptr.synth_residual(<double*> a.data) + + +cdef class PeakDetection: + cdef c_PeakDetection* thisptr + + def __cinit__(self): self.thisptr = new c_PeakDetection() + def __dealloc__(self): del self.thisptr + + property sampling_rate: + def __get__(self): return self.thisptr.sampling_rate() + def __set__(self, int i): self.thisptr.sampling_rate(i) + + property frame_size: + def __get__(self): return self.thisptr.frame_size() + def __set__(self, int i): self.thisptr.frame_size(i) + + property static_frame_size: + def __get__(self): return self.thisptr.static_frame_size() + def __set__(self, int i): self.thisptr.static_frame_size(i) + + def next_frame_size(self): + return self.thisptr.next_frame_size() + + property hop_size: + def __get__(self): return self.thisptr.hop_size() + def __set__(self, int i): self.thisptr.hop_size(i) + + property max_peaks: + def __get__(self): return self.thisptr.max_peaks() + def __set__(self, int i): self.thisptr.max_peaks(i) + + property window_type: + def __get__(self): return self.thisptr.window_type().c_str() + def __set__(self, char* s): self.thisptr.window_type(string(s)) + + property window_size: + def __get__(self): return self.thisptr.window_size() + def __set__(self, int i): self.thisptr.window_size(i) + + property min_peak_separation: + def __get__(self): return self.thisptr.min_peak_separation() + def __set__(self, double d): self.thisptr.min_peak_separation(d) diff --git a/src/simpl/base.cpp b/src/simpl/base.cpp index f5a42b0..e07f6fe 100644 --- a/src/simpl/base.cpp +++ b/src/simpl/base.cpp @@ -110,7 +110,7 @@ void Frame::max_peaks(int new_max_peaks) } } -void Frame::add_peak(Peak peak) +void Frame::add_peak(Peak* peak) { _peaks.push_back(peak); } @@ -119,11 +119,11 @@ void Frame::add_peaks(Peaks* peaks) { for(Peaks::iterator i = peaks->begin(); i != peaks->end(); i++) { - add_peak(Peak(*i)); + add_peak(*i); } } -Peak Frame::peak(int peak_number) +Peak* Frame::peak(int peak_number) { return _peaks[peak_number]; } diff --git a/src/simpl/base.h b/src/simpl/base.h index 12df565..f70c952 100644 --- a/src/simpl/base.h +++ b/src/simpl/base.h @@ -39,7 +39,7 @@ public: bool is_free(const string direction = string("forwards")); }; -typedef std::vector<Peak> Peaks; +typedef std::vector<Peak*> Peaks; // --------------------------------------------------------------------------- // Partial @@ -82,9 +82,9 @@ public: int num_peaks(); int max_peaks(); void max_peaks(int new_max_peaks); - void add_peak(Peak peak); + void add_peak(Peak* peak); void add_peaks(Peaks* peaks); - Peak peak(int peak_number); + Peak* peak(int peak_number); void clear_peaks(); Peaks::iterator peaks_begin(); Peaks::iterator peaks_end(); diff --git a/tests/test_base.py b/tests/test_base.py index 4dac897..f9807ef 100644 --- a/tests/test_base.py +++ b/tests/test_base.py @@ -24,8 +24,26 @@ class TestFrame(object): f.synth_residual = a assert np.all(f.synth_residual == a) - def test_peak(self): + def test_peaks(self): p = base.Peak() p.amplitude = 0.5 p.frequency = 220.0 p.phase = 0.0 + + f = base.Frame() + assert f.num_peaks == 0 + assert f.max_peaks > 0 + f.add_peak(p) + + assert f.num_peaks == 1 + assert f.peak(0).amplitude == p.amplitude + assert f.peaks[0].amplitude == p.amplitude + + f.clear_peaks() + assert f.num_peaks == 0 + + +class TestPeakDetection(object): + def test_peak_detection(self): + pd = base.PeakDetection() + print pd |