Skip to content

Commit

Permalink
Merge pull request #33 from lucianodato/development
Browse files Browse the repository at this point in the history
Ensure fftwf memory alignment. Fixed some memory leaks. Right size allocation for critical bands
  • Loading branch information
lucianodato authored Apr 27, 2022
2 parents ddd9c27 + cef7d12 commit d53ec86
Show file tree
Hide file tree
Showing 15 changed files with 53 additions and 43 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ jobs:
shell: bash
run: |
meson build --buildtype release
ninja -v -C build
meson compile -v -C build
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ Installation:
git clone https://github.com/lucianodato/noise-repellent.git
cd noise-repellent
meson build --buildtype=release --prefix=/usr --libdir=lib (your-os-appropriate-location-fullpath)
ninja -C build -v
sudo ninja -C build install
meson compile -C build -v
sudo meson install -C build
```

## Example
Expand Down
2 changes: 1 addition & 1 deletion meson.build
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
project('libspecbleach', 'c', version: '0.1.2',default_options: ['c_std=c99'])
project('libspecbleach', 'c', version: '0.1.3',default_options: ['c_std=c99'])

#sources to compile
shared_sources = [
Expand Down
2 changes: 1 addition & 1 deletion src/denoiser/spectral_denoiser.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ typedef struct SbSpectralDenoiser {
NoiseProfile *noise_profile;
SpectralFeatures *spectral_features;
DenoiseMixer *mixer;

NoiseScalingCriterias *noise_scaling_criteria;
SpectralSmoother *spectrum_smoothing;
} SbSpectralDenoiser;
Expand Down Expand Up @@ -118,6 +117,7 @@ void spectral_denoiser_free(SpectralProcessorHandle instance) {
SbSpectralDenoiser *self = (SbSpectralDenoiser *)instance;

noise_estimation_free(self->noise_estimator);
noise_profile_free(self->noise_profile);
spectral_features_free(self->spectral_features);
spectral_smoothing_free(self->spectrum_smoothing);
noise_scaling_criterias_free(self->noise_scaling_criteria);
Expand Down
5 changes: 4 additions & 1 deletion src/shared/noise_estimation/noise_estimator.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ NoiseEstimator *noise_estimation_initialize(const uint32_t fft_size,
return self;
}

void noise_estimation_free(NoiseEstimator *self) { free(self); }
void noise_estimation_free(NoiseEstimator *self) {
noise_profile_free(self->noise_profile);
free(self);
}

bool noise_estimation_run(NoiseEstimator *self, float *signal_spectrum) {
if (!self || !signal_spectrum) {
Expand Down
2 changes: 1 addition & 1 deletion src/shared/post_estimation/postfilter.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#include <string.h>

struct PostFilter {
float *postfilter;
FftTransform *gain_fft_spectrum;
FftTransform *postfilter_fft_spectrum;
bool preserve_minimun;

float *postfilter;
float *pf_gain_spectrum;

float snr_threshold;
Expand Down
6 changes: 3 additions & 3 deletions src/shared/pre_estimation/absolute_hearing_thresholds.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ struct AbsoluteHearingThresholds {

SpectralFeatures *spectral_features;
FftTransform *fft_transform;
SpectrumType spectrum_type;

SpectrumType spectrum_type;
uint32_t fft_size;
uint32_t real_spectrum_size;
uint32_t sample_rate;
Expand All @@ -55,8 +55,6 @@ absolute_hearing_thresholds_initialize(const uint32_t sample_rate,
AbsoluteHearingThresholds *self = (AbsoluteHearingThresholds *)calloc(
1U, sizeof(AbsoluteHearingThresholds));

self->fft_transform = fft_transform_initialize_bins(fft_size);

self->fft_size = fft_size;
self->real_spectrum_size = self->fft_size / 2U + 1U;
self->sample_rate = sample_rate;
Expand All @@ -65,6 +63,8 @@ absolute_hearing_thresholds_initialize(const uint32_t sample_rate,
self->sine_wave_frequency = REFERENCE_SINE_WAVE_FREQ;
self->reference_level = REFERENCE_LEVEL;

self->fft_transform = fft_transform_initialize_bins(self->fft_size);

self->spl_reference_values =
(float *)calloc(self->real_spectrum_size, sizeof(float));

Expand Down
8 changes: 4 additions & 4 deletions src/shared/pre_estimation/critical_bands.c
Original file line number Diff line number Diff line change
Expand Up @@ -113,22 +113,22 @@ static void compute_mapping_spectrum(CriticalBands *self) {
switch (self->type) {
case BARK_SCALE: {
self->current_critical_bands = (float *)bark_bands;
self->number_bands = sizeof(bark_bands) / sizeof(float) + 1U;
self->number_bands = sizeof(bark_bands) / sizeof(float);
break;
}
case MEL_SCALE: {
self->current_critical_bands = (float *)mel_bands;
self->number_bands = sizeof(mel_bands) / sizeof(float) + 1U;
self->number_bands = sizeof(mel_bands) / sizeof(float);
break;
}
case OPUS_SCALE: {
self->current_critical_bands = (float *)opus_bands;
self->number_bands = sizeof(opus_bands) / sizeof(float) + 1U;
self->number_bands = sizeof(opus_bands) / sizeof(float);
break;
}
case OCTAVE_SCALE: {
self->current_critical_bands = (float *)octave_bands;
self->number_bands = sizeof(octave_bands) / sizeof(float) + 1U;
self->number_bands = sizeof(octave_bands) / sizeof(float);
break;
}
default:
Expand Down
25 changes: 13 additions & 12 deletions src/shared/pre_estimation/noise_scaling_criterias.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,16 @@ struct NoiseScalingCriterias {
float higher_snr;
float alpha_minimun;
float beta_minimun;
CriticalBandIndexes band_indexes;
CriticalBandType critical_band_type;

float *masking_thresholds;
float *clean_signal_estimation;
float *critical_bands_noise_profile;
float *critical_bands_reference_spectrum;

MaskingEstimator *masking_estimation;
CriticalBands *critical_bands;
CriticalBandIndexes band_indexes;
CriticalBandType critical_band_type;
float *bark_noise_profile;
float *bark_reference_spectrum;
};

NoiseScalingCriterias *noise_scaling_criterias_initialize(
Expand Down Expand Up @@ -89,9 +89,9 @@ NoiseScalingCriterias *noise_scaling_criterias_initialize(
self->number_critical_bands =
get_number_of_critical_bands(self->critical_bands);

self->bark_noise_profile =
self->critical_bands_noise_profile =
(float *)calloc(self->number_critical_bands, sizeof(float));
self->bark_reference_spectrum =
self->critical_bands_reference_spectrum =
(float *)calloc(self->number_critical_bands, sizeof(float));

self->masking_thresholds =
Expand All @@ -108,8 +108,8 @@ void noise_scaling_criterias_free(NoiseScalingCriterias *self) {

free(self->clean_signal_estimation);
free(self->masking_thresholds);
free(self->bark_noise_profile);
free(self->bark_reference_spectrum);
free(self->critical_bands_noise_profile);
free(self->critical_bands_reference_spectrum);

free(self);
}
Expand Down Expand Up @@ -149,9 +149,9 @@ static void a_posteriori_snr_critical_bands(NoiseScalingCriterias *self,
NoiseScalingParameters parameters) {

compute_critical_bands_spectrum(self->critical_bands, noise_spectrum,
self->bark_noise_profile);
self->critical_bands_noise_profile);
compute_critical_bands_spectrum(self->critical_bands, spectrum,
self->bark_reference_spectrum);
self->critical_bands_reference_spectrum);

float a_posteriori_snr = 20.F;
float oversustraction_factor = 1.F;
Expand All @@ -160,8 +160,9 @@ static void a_posteriori_snr_critical_bands(NoiseScalingCriterias *self,

self->band_indexes = get_band_indexes(self->critical_bands, j);

a_posteriori_snr = 10.F * log10f(self->bark_reference_spectrum[j] /
self->bark_noise_profile[j]);
a_posteriori_snr =
10.F * log10f(self->critical_bands_reference_spectrum[j] /
self->critical_bands_noise_profile[j]);

if (a_posteriori_snr >= self->lower_snr &&
a_posteriori_snr <= self->higher_snr) {
Expand Down
7 changes: 3 additions & 4 deletions src/shared/pre_estimation/spectral_smoother.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,12 @@ struct SpectralSmoother {
uint32_t real_spectrum_size;
float adaptive_coefficient;
float previous_adaptive_coefficient;
TimeSmoothingType type;

float *noise_spectrum;

float *smoothed_spectrum;
float *smoothed_spectrum_previous;

TimeSmoothingType type;
TransientDetector *transient_detection;
};

Expand Down Expand Up @@ -68,12 +67,12 @@ SpectralSmoother *spectral_smoothing_initialize(const uint32_t fft_size,
}

void spectral_smoothing_free(SpectralSmoother *self) {
transient_detector_free(self->transient_detection);

free(self->noise_spectrum);
free(self->smoothed_spectrum);
free(self->smoothed_spectrum_previous);

transient_detector_free(self->transient_detection);

free(self);
}

Expand Down
4 changes: 2 additions & 2 deletions src/shared/pre_estimation/transient_detector.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
struct TransientDetector {
uint32_t fft_size;
uint32_t real_spectrum_size;

float *previous_spectrum;
float rolling_mean;
bool transient_present;
uint32_t window_count;

float *previous_spectrum;
};

TransientDetector *transient_detector_initialize(const uint32_t fft_size) {
Expand Down
18 changes: 11 additions & 7 deletions src/shared/stft/fft_transform.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,15 @@ FftTransform *fft_transform_initialize(const uint32_t sample_rate,
FftTransform *self = (FftTransform *)calloc(1U, sizeof(FftTransform));

self->padding_type = padding_type;
self->frame_size = (uint32_t)((frame_size_ms / 1000.F) * (float)sample_rate);
self->zeropadding_amount = zeropadding_amount;
self->frame_size = (uint32_t)((frame_size_ms / 1000.F) * (float)sample_rate);

self->fft_size = calculate_fft_size(self);

self->input_fft_buffer = (float *)calloc(self->fft_size, sizeof(float));
self->output_fft_buffer = (float *)calloc(self->fft_size, sizeof(float));
self->input_fft_buffer =
(float *)fftwf_malloc(sizeof(float) * self->fft_size);
self->output_fft_buffer =
(float *)fftwf_malloc(sizeof(float) * self->fft_size);
self->forward =
fftwf_plan_r2r_1d((int)self->fft_size, self->input_fft_buffer,
self->output_fft_buffer, FFTW_FORWARD, FFTW_ESTIMATE);
Expand All @@ -71,8 +73,10 @@ FftTransform *fft_transform_initialize_bins(const uint32_t fft_size) {
self->fft_size = fft_size;
self->frame_size = self->fft_size;

self->input_fft_buffer = (float *)calloc(self->fft_size, sizeof(float));
self->output_fft_buffer = (float *)calloc(self->fft_size, sizeof(float));
self->input_fft_buffer =
(float *)fftwf_malloc(sizeof(float) * self->fft_size);
self->output_fft_buffer =
(float *)fftwf_malloc(sizeof(float) * self->fft_size);
self->forward =
fftwf_plan_r2r_1d((int)self->fft_size, self->input_fft_buffer,
self->output_fft_buffer, FFTW_FORWARD, FFTW_ESTIMATE);
Expand Down Expand Up @@ -105,8 +109,8 @@ static uint32_t calculate_fft_size(FftTransform *self) {
}

void fft_transform_free(FftTransform *self) {
free(self->input_fft_buffer);
free(self->output_fft_buffer);
fftwf_free(self->input_fft_buffer);
fftwf_free(self->output_fft_buffer);
fftwf_destroy_plan(self->forward);
fftwf_destroy_plan(self->backward);

Expand Down
8 changes: 4 additions & 4 deletions src/shared/stft/stft_processor.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,13 @@ StftProcessor *stft_processor_initialize(const uint32_t sample_rate,
}

void stft_processor_free(StftProcessor *self) {
free(self->output_accumulator);
free(self->tmp_output);

stft_buffer_free(self->stft_buffer);
fft_transform_free(self->fft_transform);
stft_buffer_free(self->stft_buffer);
stft_window_free(self->stft_windows);

free(self->output_accumulator);
free(self->tmp_output);

free(self);
}

Expand Down
1 change: 1 addition & 0 deletions src/shared/stft/stft_windows.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ static float get_windows_scale_factor(StftWindows *self,
struct StftWindows {
float *input_window;
float *output_window;

uint32_t stft_frame_size;
float scale_factor;
};
Expand Down
2 changes: 2 additions & 0 deletions src/shared/utils/denoise_mixer.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,10 @@ DenoiseMixer *denoise_mixer_initialize(uint32_t fft_size, uint32_t sample_rate,

void denoise_mixer_free(DenoiseMixer *self) {
spectral_whitening_free(self->whitener);

free(self->residual_spectrum);
free(self->denoised_spectrum);

free(self);
}

Expand Down

0 comments on commit d53ec86

Please sign in to comment.