summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJohn Glover <j@johnglover.net>2013-06-07 13:00:55 +0200
committerJohn Glover <j@johnglover.net>2013-06-07 13:00:55 +0200
commitd60bbc935a1c7137c766792bf569ea7d6800fba9 (patch)
tree2247eb781e2b2f28a1f13253517b2c5eed11a3ae /src
parentd91fc3a5b7b9d7a4ca85882b029fefe2c8daa156 (diff)
downloadsimpl-d60bbc935a1c7137c766792bf569ea7d6800fba9.tar.gz
simpl-d60bbc935a1c7137c766792bf569ea7d6800fba9.tar.bz2
simpl-d60bbc935a1c7137c766792bf569ea7d6800fba9.zip
Fix memory leaks in peak detection processes
Diffstat (limited to 'src')
-rw-r--r--src/mq/mq.cpp32
-rw-r--r--src/simpl/base.cpp66
-rw-r--r--src/simpl/base.h6
-rw-r--r--src/simpl/partial_tracking.cpp6
-rw-r--r--src/simpl/peak_detection.cpp75
-rw-r--r--src/simpl/peak_detection.h10
6 files changed, 115 insertions, 80 deletions
diff --git a/src/mq/mq.cpp b/src/mq/mq.cpp
index 694d670..6c25a88 100644
--- a/src/mq/mq.cpp
+++ b/src/mq/mq.cpp
@@ -62,7 +62,7 @@ int simpl::destroy_mq(MQParameters* params) {
// Add new_peak to the doubly linked list of peaks, keeping peaks sorted
// with the largest amplitude peaks at the start of the list
void simpl::mq_add_peak(MQPeak* new_peak, MQPeakList* peak_list) {
- do {
+ while(true) {
if(peak_list->peak) {
if(peak_list->peak->amplitude > new_peak->amplitude) {
if(peak_list->next) {
@@ -73,7 +73,7 @@ void simpl::mq_add_peak(MQPeak* new_peak, MQPeakList* peak_list) {
new_node->peak = new_peak;
new_node->prev = peak_list;
new_node->next = NULL;
- new_node->prev->next = new_node;
+ peak_list->next = new_node;
return;
}
}
@@ -93,7 +93,6 @@ void simpl::mq_add_peak(MQPeak* new_peak, MQPeakList* peak_list) {
return;
}
}
- while(1);
}
void simpl::delete_peak_list(MQPeakList* peak_list) {
@@ -107,11 +106,15 @@ void simpl::delete_peak_list(MQPeakList* peak_list) {
peak_list = temp;
}
if(peak_list) {
+ if(peak_list->peak) {
+ delete peak_list->peak;
+ peak_list->peak = NULL;
+ }
peak_list->next = NULL;
peak_list->prev = NULL;
delete peak_list;
+ peak_list = NULL;
}
- peak_list = NULL;
}
sample get_magnitude(sample x, sample y) {
@@ -127,9 +130,6 @@ MQPeakList* simpl::mq_find_peaks(int signal_size, sample* signal,
int num_peaks = 0;
sample prev_amp, current_amp, next_amp;
MQPeakList* peak_list = new MQPeakList();
- peak_list->next = NULL;
- peak_list->prev = NULL;
- peak_list->peak = NULL;
// take fft of the signal
memcpy(params->fft_in, signal, sizeof(sample)*params->frame_size);
@@ -178,7 +178,8 @@ MQPeakList* simpl::mq_find_peaks(int signal_size, sample* signal,
num_peaks = params->max_peaks;
}
- return simpl::mq_sort_peaks_by_frequency(peak_list, num_peaks);
+ // return simpl::mq_sort_peaks_by_frequency(peak_list, num_peaks);
+ return peak_list;
}
// ----------------------------------------------------------------------------
@@ -258,16 +259,16 @@ MQPeakList* merge_sort(MQPeakList* peak_list, int num_peaks) {
// give the extra peak to the left
int middle;
if(num_peaks % 2 == 0) {
- middle = num_peaks/2;
+ middle = num_peaks / 2;
}
else {
- middle = (num_peaks/2) + 1;
+ middle = (num_peaks / 2) + 1;
}
// split the peak list into left and right at the middle value
left = peak_list;
while(current) {
- if(n == middle-1) {
+ if(n == middle - 1) {
right = current->next;
current->next = NULL;
break;
@@ -279,16 +280,19 @@ MQPeakList* merge_sort(MQPeakList* peak_list, int num_peaks) {
// recursively sort and merge
left = merge_sort(left, middle);
- right = merge_sort(right, num_peaks-middle);
+ right = merge_sort(right, num_peaks - middle);
return merge(left, right);
}
// Sort peak_list into a list order from smaller to largest frequency.
MQPeakList* simpl::mq_sort_peaks_by_frequency(MQPeakList* peak_list,
int num_peaks) {
- if(!peak_list || num_peaks == 0) {
+ if(!peak_list) {
return NULL;
}
+ else if(num_peaks == 0) {
+ return peak_list;
+ }
else {
return merge_sort(peak_list, num_peaks);
}
@@ -363,7 +367,7 @@ MQPeakList* simpl::mq_track_peaks(MQPeakList* peak_list,
MQParameters* params) {
MQPeakList* current = peak_list;
- // MQ algorithm needs 2 frames of data, so return if this is the
+ // MQ algorithm needs 2 frames of data, so do nothing if this is the
// first frame
if(params->prev_peaks) {
// find all matches for previous peaks in the current frame
diff --git a/src/simpl/base.cpp b/src/simpl/base.cpp
index 3504d95..248bd06 100644
--- a/src/simpl/base.cpp
+++ b/src/simpl/base.cpp
@@ -8,15 +8,26 @@ using namespace simpl;
// Peak
// ---------------------------------------------------------------------------
Peak::Peak() {
- amplitude = 0.0;
- frequency = 0.0;
- phase = 0.0;
- bandwidth = 0.0;
+ reset();
+}
+
+Peak::Peak(sample new_amplitude, sample new_frequency,
+ sample new_phase, sample new_bandwidth) {
+ amplitude = new_amplitude;
+ frequency = new_frequency;
+ phase = new_phase;
+ bandwidth = new_bandwidth;
}
Peak::~Peak() {
}
+void Peak::reset() {
+ amplitude = 0.0;
+ frequency = 0.0;
+ phase = 0.0;
+ bandwidth = 0.0;
+}
// ---------------------------------------------------------------------------
// Frame
@@ -40,9 +51,16 @@ Frame::Frame(int frame_size, bool alloc_memory) {
}
Frame::~Frame() {
- _peaks.clear();
+ clear_peaks();
_partials.clear();
+ for(int i = 0; i < _peaks.size(); i++) {
+ if(_peaks[i]) {
+ delete _peaks[i];
+ _peaks[i] = NULL;
+ }
+ }
+
if(_alloc_memory) {
destroy_arrays();
}
@@ -53,12 +71,12 @@ void Frame::init() {
_max_peaks = 100;
_num_partials = 0;
_max_partials = 100;
- _peaks.resize(_max_peaks);
_partials.resize(_max_partials);
_audio = NULL;
_synth = NULL;
_residual = NULL;
_synth_residual = NULL;
+ resize_peaks(_max_peaks);
}
void Frame::create_arrays() {
@@ -100,6 +118,23 @@ void Frame::destroy_synth_arrays() {
}
}
+void Frame::resize_peaks(int new_num_peaks) {
+ clear_peaks();
+
+ for(int i = 0; i < _peaks.size(); i++) {
+ if(_peaks[i]) {
+ delete _peaks[i];
+ _peaks[i] = NULL;
+ }
+ }
+
+ _peaks.resize(new_num_peaks);
+
+ for(int i = 0; i < _peaks.size(); i++) {
+ _peaks[i] = new Peak();
+ }
+}
+
void Frame::clear() {
clear_peaks();
clear_partials();
@@ -112,6 +147,11 @@ void Frame::clear() {
void Frame::clear_peaks() {
_num_peaks = 0;
+ for(int i = 0; i < _peaks.size(); i++) {
+ if(_peaks[i]) {
+ _peaks[i]->reset();
+ }
+ }
}
void Frame::clear_partials() {
@@ -151,7 +191,7 @@ void Frame::max_peaks(int new_max_peaks) {
"of peaks, some existing data was lost.\n");
}
- _peaks.resize(_max_peaks);
+ resize_peaks(_max_peaks);
}
void Frame::add_peak(Peak* peak) {
@@ -162,10 +202,22 @@ void Frame::add_peak(Peak* peak) {
return;
}
+ if(_peaks[_num_peaks]) {
+ delete _peaks[_num_peaks];
+ }
_peaks[_num_peaks] = peak;
_num_peaks++;
}
+void Frame::add_peak(sample amplitude, sample frequency,
+ sample phase, sample bandwidth) {
+ _peaks[_num_peaks]->amplitude = amplitude;
+ _peaks[_num_peaks]->frequency = frequency;
+ _peaks[_num_peaks]->phase = phase;
+ _peaks[_num_peaks]->bandwidth = bandwidth;
+ _num_peaks++;
+}
+
Peak* Frame::peak(int peak_number) {
return _peaks[peak_number];
}
diff --git a/src/simpl/base.h b/src/simpl/base.h
index 106cbb4..97f75e7 100644
--- a/src/simpl/base.h
+++ b/src/simpl/base.h
@@ -28,7 +28,10 @@ class Peak {
sample bandwidth;
Peak();
+ Peak(sample new_amplitude, sample new_frequency,
+ sample new_phase, sample new_bandwidth);
~Peak();
+ void reset();
};
typedef std::vector<Peak*> Peaks;
@@ -65,6 +68,7 @@ class Frame {
void destroy_arrays();
void create_synth_arrays();
void destroy_synth_arrays();
+ void resize_peaks(int new_num_peaks);
public:
Frame();
@@ -81,6 +85,8 @@ class Frame {
int max_peaks();
void max_peaks(int new_max_peaks);
void add_peak(Peak* peak);
+ void add_peak(sample amplitude, sample frequency,
+ sample phase, sample bandwidth);
Peak* peak(int peak_number);
void peak(int peak_number, Peak* peak);
diff --git a/src/simpl/partial_tracking.cpp b/src/simpl/partial_tracking.cpp
index 0f28f4d..81644b0 100644
--- a/src/simpl/partial_tracking.cpp
+++ b/src/simpl/partial_tracking.cpp
@@ -96,13 +96,15 @@ MQPartialTracking::MQPartialTracking() {
MQPartialTracking::~MQPartialTracking() {
destroy_mq(&_mq_params);
- delete_peak_list(_peak_list);
+ delete_peak_list(_prev_peak_list);
+ _peak_list = NULL;
_prev_peak_list = NULL;
}
void MQPartialTracking::reset() {
reset_mq(&_mq_params);
- delete_peak_list(_peak_list);
+ delete_peak_list(_prev_peak_list);
+ _peak_list = NULL;
_prev_peak_list = NULL;
}
diff --git a/src/simpl/peak_detection.cpp b/src/simpl/peak_detection.cpp
index ba73fd8..f142757 100644
--- a/src/simpl/peak_detection.cpp
+++ b/src/simpl/peak_detection.cpp
@@ -118,14 +118,7 @@ void PeakDetection::frames(Frames new_frames) {
}
// Find and return all spectral peaks in a given frame of audio
-Peaks PeakDetection::find_peaks_in_frame(Frame* frame) {
- Peaks peaks;
-
- for(int i = 0; i < peaks.size(); i++) {
- frame->add_peak(peaks[i]);
- }
-
- return peaks;
+void PeakDetection::find_peaks_in_frame(Frame* frame) {
}
// Find and return all spectral peaks in a given audio signal.
@@ -202,25 +195,21 @@ void MQPeakDetection::max_peaks(int new_max_peaks) {
reset();
}
-Peaks MQPeakDetection::find_peaks_in_frame(Frame* frame) {
- Peaks peaks;
-
+void MQPeakDetection::find_peaks_in_frame(Frame* frame) {
MQPeakList* pl = mq_find_peaks(_frame_size, frame->audio(), &_mq_params);
+ MQPeakList* pl_start = pl;
int num_peaks = 0;
while(pl && pl->peak && num_peaks < _max_peaks) {
- Peak* p = new Peak();
- p->amplitude = pl->peak->amplitude;
- p->frequency = pl->peak->frequency;
- p->phase = pl->peak->phase;
- peaks.push_back(p);
- frame->add_peak(p);
-
+ frame->add_peak(pl->peak->amplitude,
+ pl->peak->frequency,
+ pl->peak->phase,
+ 0.0);
pl = pl->next;
num_peaks++;
}
- return peaks;
+ delete_peak_list(pl_start);
}
// ---------------------------------------------------------------------------
@@ -305,22 +294,16 @@ void SMSPeakDetection::realtime(int new_realtime) {
}
// Find and return all spectral peaks in a given frame of audio
-Peaks SMSPeakDetection::find_peaks_in_frame(Frame* frame) {
- Peaks peaks;
-
+void SMSPeakDetection::find_peaks_in_frame(Frame* frame) {
int num_peaks = sms_findPeaks(frame->size(), frame->audio(),
&_analysis_params, &_peaks);
for(int i = 0; i < num_peaks; i++) {
- Peak* p = new Peak();
- p->amplitude = _peaks.pSpectralPeaks[i].fMag;
- p->frequency = _peaks.pSpectralPeaks[i].fFreq;
- p->phase = _peaks.pSpectralPeaks[i].fPhase;
- peaks.push_back(p);
-
- frame->add_peak(p);
+ frame->add_peak(_peaks.pSpectralPeaks[i].fMag,
+ _peaks.pSpectralPeaks[i].fFreq,
+ _peaks.pSpectralPeaks[i].fPhase,
+ 0.0);
}
- return peaks;
}
// Find and return all spectral peaks in a given audio signal.
@@ -427,23 +410,17 @@ void SndObjPeakDetection::max_peaks(int new_max_peaks) {
reset();
}
-Peaks SndObjPeakDetection::find_peaks_in_frame(Frame* frame) {
- Peaks peaks;
-
+void SndObjPeakDetection::find_peaks_in_frame(Frame* frame) {
_input->PushIn(frame->audio(), frame->size());
_ifgram->DoProcess();
int num_peaks = _analysis->FindPeaks();
for(int i = 0; i < num_peaks; i++) {
- Peak* p = new Peak();
- p->amplitude = _analysis->Output(i * 3);
- p->frequency = _analysis->Output((i * 3) + 1);
- p->phase = _analysis->Output((i * 3) + 2);
- peaks.push_back(p);
- frame->add_peak(p);
+ frame->add_peak(_analysis->Output(i * 3),
+ _analysis->Output((i * 3) + 1),
+ _analysis->Output((i * 3) + 2),
+ 0.0);
}
-
- return peaks;
}
@@ -530,9 +507,7 @@ void LorisPeakDetection::max_peaks(int new_max_peaks) {
reset();
}
-Peaks LorisPeakDetection::find_peaks_in_frame(Frame* frame) {
- Peaks peaks;
-
+void LorisPeakDetection::find_peaks_in_frame(Frame* frame) {
_analyzer->analyze(frame->size(), frame->audio());
int num_peaks = _analyzer->peaks.size();
@@ -541,13 +516,9 @@ Peaks LorisPeakDetection::find_peaks_in_frame(Frame* frame) {
}
for(int i = 0; i < num_peaks; i++) {
- Peak* p = new Peak();
- p->amplitude = _analyzer->peaks[i].amplitude();
- p->frequency = _analyzer->peaks[i].frequency();
- p->bandwidth = _analyzer->peaks[i].bandwidth();
- peaks.push_back(p);
- frame->add_peak(p);
+ frame->add_peak(_analyzer->peaks[i].amplitude(),
+ _analyzer->peaks[i].frequency(),
+ 0.0,
+ _analyzer->peaks[i].bandwidth());
}
-
- return peaks;
}
diff --git a/src/simpl/peak_detection.h b/src/simpl/peak_detection.h
index 02d8988..3835b07 100644
--- a/src/simpl/peak_detection.h
+++ b/src/simpl/peak_detection.h
@@ -76,7 +76,7 @@ class PeakDetection {
void frames(Frames new_frames);
// Find and return all spectral peaks in a given frame of audio
- virtual Peaks find_peaks_in_frame(Frame* frame);
+ virtual void find_peaks_in_frame(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
@@ -103,7 +103,7 @@ class MQPeakDetection : public PeakDetection {
void hop_size(int new_hop_size);
using PeakDetection::max_peaks;
void max_peaks(int new_max_peaks);
- Peaks find_peaks_in_frame(Frame* frame);
+ void find_peaks_in_frame(Frame* frame);
};
@@ -127,7 +127,7 @@ class SMSPeakDetection : public PeakDetection {
void max_peaks(int new_max_peaks);
int realtime();
void realtime(int new_realtime);
- Peaks find_peaks_in_frame(Frame* frame);
+ void find_peaks_in_frame(Frame* frame);
Frames find_peaks(int audio_size, sample* audio);
};
@@ -153,7 +153,7 @@ class SndObjPeakDetection : public PeakDetection {
void hop_size(int new_hop_size);
using PeakDetection::max_peaks;
void max_peaks(int new_max_peaks);
- Peaks find_peaks_in_frame(Frame* frame);
+ void find_peaks_in_frame(Frame* frame);
};
@@ -193,7 +193,7 @@ class LorisPeakDetection : public PeakDetection {
void hop_size(int new_hop_size);
using PeakDetection::max_peaks;
void max_peaks(int new_max_peaks);
- Peaks find_peaks_in_frame(Frame* frame);
+ void find_peaks_in_frame(Frame* frame);
};