From af0755de97b8cbe80467061826fa5ccba28a182e Mon Sep 17 00:00:00 2001 From: John Glover Date: Sat, 27 Aug 2011 20:06:59 +0100 Subject: Add frame peaks iterators and tidy up PeakDetection --- src/simpl/base.cpp | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++++- src/simpl/base.h | 19 +++++++++++++---- 2 files changed, 76 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/simpl/base.cpp b/src/simpl/base.cpp index bcfea65..91bf50e 100644 --- a/src/simpl/base.cpp +++ b/src/simpl/base.cpp @@ -134,16 +134,34 @@ void Frame::add_peak(Peak peak) _peaks.push_back(peak); } +void Frame::add_peaks(Peaks* peaks) +{ + for(Peaks::iterator i = peaks->begin(); i != peaks->end(); i++) + { + add_peak(Peak(*i)); + } +} + Peak Frame::peak(int peak_number) { return _peaks[peak_number]; } -Peaks::iterator Frame::peaks() +void Frame::clear_peaks() +{ + _peaks.clear(); +} + +Peaks::iterator Frame::peaks_begin() { return _peaks.begin(); } +Peaks::iterator Frame::peaks_end() +{ + return _peaks.end(); +} + // Frame - partials // ---------------- @@ -249,6 +267,12 @@ PeakDetection::PeakDetection() PeakDetection::~PeakDetection() { + while(!_frames.empty()) + { + Frame* f = &_frames.back(); + _frames.pop_back(); + delete f; + } } int PeakDetection::sampling_rate() @@ -340,4 +364,40 @@ Frames* PeakDetection::frames() return &_frames; } +// Find and return all spectral peaks in a given frame of audio +Peaks* PeakDetection::find_peaks_in_frame(const Frame& frame) +{ + Peaks* peaks = new Peaks(); + return peaks; +} + +// Find and return all spectral peaks in a given audio signal. +// If the signal contains more than 1 frame worth of audio, it will be broken +// up into separate frames, each containing a std::vector of peaks. +Frames* PeakDetection::find_peaks(const samples& audio) +{ + _frames.clear(); + unsigned int pos = 0; + while(pos < audio.size()) + { + // get the next frame size + if(!_static_frame_size) + { + _frame_size = next_frame_size(); + } + + // get the next frame + Frame* f = new Frame(); + f->size(_frame_size); + f->audio(&(audio[pos])); + + // find peaks + f->add_peaks(find_peaks_in_frame(*f)); + _frames.push_back(*f); + pos += _hop_size; + } + + return &_frames; +} + } // end of namespace Simpl diff --git a/src/simpl/base.h b/src/simpl/base.h index ad201b3..15a66a8 100644 --- a/src/simpl/base.h +++ b/src/simpl/base.h @@ -103,8 +103,11 @@ public: int max_peaks(); void max_peaks(int new_max_peaks); void add_peak(Peak peak); + void add_peaks(Peaks* peaks); Peak peak(int peak_number); - Peaks::iterator peaks(); + void clear_peaks(); + Peaks::iterator peaks_begin(); + Peaks::iterator peaks_end(); // partials int num_partials(); @@ -136,7 +139,7 @@ typedef std::vector Frames; class PeakDetection { -protected: +private: int _sampling_rate; int _frame_size; bool _static_frame_size; @@ -149,7 +152,7 @@ protected: public: PeakDetection(); - ~PeakDetection(); + virtual ~PeakDetection(); int sampling_rate(); void sampling_rate(int new_sampling_rate); @@ -157,7 +160,7 @@ public: void frame_size(int new_frame_size); bool static_frame_size(); void static_frame_size(bool new_static_frame_size); - int next_frame_size(); + virtual int next_frame_size(); int hop_size(); void hop_size(int new_hop_size); int max_peaks(); @@ -169,6 +172,14 @@ public: number min_peak_separation(); void min_peak_separation(number new_min_peak_separation); Frames* frames(); + + // Find and return all spectral peaks in a given frame of audio + virtual Peaks* find_peaks_in_frame(const Frame& frame); + + // Find and return all spectral peaks in a given audio signal. + // If the signal contains more than 1 frame worth of audio, it will be broken + // up into separate frames, with a std::vector of peaks returned for each frame. + virtual Frames* find_peaks(const samples& audio); }; } // end of namespace Simpl -- cgit v1.2.3