[9f060d1] | 1 | /* |
---|
| 2 | * Copyright (C) 2003-2015 Paul Brossier <piem@aubio.org> |
---|
| 3 | * |
---|
| 4 | * This file is part of aubio. |
---|
| 5 | * |
---|
| 6 | * aubio is free software: you can redistribute it and/or modify it under the |
---|
| 7 | * terms of the GNU General Public License as published by the Free Software |
---|
| 8 | * Foundation, either version 3 of the License, or (at your option) any later |
---|
| 9 | * version. |
---|
| 10 | * |
---|
| 11 | * aubio is distributed in the hope that it will be useful, but WITHOUT ANY |
---|
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
---|
| 13 | * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
---|
| 14 | * details. |
---|
| 15 | * |
---|
| 16 | * You should have received a copy of the GNU General Public License along with |
---|
| 17 | * aubio. If not, see <http://www.gnu.org/licenses/>. |
---|
| 18 | * |
---|
| 19 | */ |
---|
| 20 | |
---|
| 21 | #include "aubio_priv.h" |
---|
| 22 | #include "fvec.h" |
---|
| 23 | #include "cvec.h" |
---|
| 24 | #include "mathutils.h" |
---|
| 25 | #include "spectral/awhitening.h" |
---|
| 26 | |
---|
| 27 | #define aubio_spectral_whitening_default_relax_time 30 // in seconds |
---|
| 28 | #define aubio_spectral_whitening_default_decay 0.001 // -60dB attenuation |
---|
| 29 | |
---|
| 30 | /** structure to store object state */ |
---|
| 31 | struct _aubio_spectral_whitening_t { |
---|
| 32 | uint_t buf_size; |
---|
| 33 | uint_t hop_size; |
---|
| 34 | uint_t samplerate; |
---|
| 35 | smpl_t relax_time; |
---|
| 36 | smpl_t r_decay; |
---|
| 37 | smpl_t floor; |
---|
| 38 | fvec_t *peak_values; |
---|
| 39 | }; |
---|
| 40 | |
---|
| 41 | void |
---|
| 42 | aubio_spectral_whitening_do (aubio_spectral_whitening_t * o, cvec_t * fftgrain) |
---|
| 43 | { |
---|
| 44 | uint_t i = 0; |
---|
| 45 | for (i = 0; i < o->peak_values->length; i++) { |
---|
| 46 | o->peak_values->data[i] = |
---|
| 47 | MAX(fftgrain->norm[i], o->r_decay * o->peak_values->data[i]); |
---|
| 48 | fftgrain->norm[i] /= o->peak_values->data[i]; |
---|
| 49 | } |
---|
| 50 | } |
---|
| 51 | |
---|
| 52 | aubio_spectral_whitening_t * |
---|
| 53 | new_aubio_spectral_whitening (uint_t buf_size, uint_t hop_size, uint_t samplerate) |
---|
| 54 | { |
---|
| 55 | aubio_spectral_whitening_t *o = AUBIO_NEW (aubio_spectral_whitening_t); |
---|
| 56 | if ((sint_t)buf_size < 1) { |
---|
| 57 | AUBIO_ERR("spectral_whitening: got buffer_size %d, but can not be < 1\n", buf_size); |
---|
| 58 | goto beach; |
---|
| 59 | } else if ((sint_t)hop_size < 1) { |
---|
| 60 | AUBIO_ERR("spectral_whitening: got hop_size %d, but can not be < 1\n", hop_size); |
---|
| 61 | goto beach; |
---|
| 62 | } else if ((sint_t)samplerate < 1) { |
---|
| 63 | AUBIO_ERR("spectral_whitening: got samplerate %d, but can not be < 1\n", samplerate); |
---|
| 64 | goto beach; |
---|
| 65 | } |
---|
| 66 | o->peak_values = new_fvec (buf_size / 2 + 1); |
---|
| 67 | o->buf_size = buf_size; |
---|
| 68 | o->hop_size = hop_size; |
---|
| 69 | o->samplerate = samplerate; |
---|
| 70 | o->floor = 1.e-6; // from 1.e-6 to 0.2 |
---|
| 71 | aubio_spectral_whitening_set_relax_time (o, aubio_spectral_whitening_default_relax_time); |
---|
| 72 | aubio_spectral_whitening_reset (o); |
---|
| 73 | return o; |
---|
| 74 | |
---|
| 75 | beach: |
---|
| 76 | AUBIO_FREE(o); |
---|
| 77 | return NULL; |
---|
| 78 | } |
---|
| 79 | |
---|
| 80 | void |
---|
| 81 | aubio_spectral_whitening_set_relax_time (aubio_spectral_whitening_t * o, smpl_t relax_time) |
---|
| 82 | { |
---|
| 83 | o->relax_time = relax_time; |
---|
| 84 | o->r_decay = POW (aubio_spectral_whitening_default_decay, |
---|
| 85 | (o->hop_size / (float) o->samplerate) / o->relax_time); |
---|
| 86 | } |
---|
| 87 | |
---|
| 88 | smpl_t |
---|
| 89 | aubio_spectral_whitening_get_relax_time (aubio_spectral_whitening_t * o) |
---|
| 90 | { |
---|
| 91 | return o->relax_time; |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | void |
---|
| 95 | aubio_spectral_whitening_reset (aubio_spectral_whitening_t * o) |
---|
| 96 | { |
---|
| 97 | /* cover the case n == 0. */ |
---|
| 98 | fvec_set_all (o->peak_values, o->floor); |
---|
| 99 | } |
---|
| 100 | |
---|
| 101 | void |
---|
| 102 | del_aubio_spectral_whitening (aubio_spectral_whitening_t * o) |
---|
| 103 | { |
---|
| 104 | del_fvec (o->peak_values); |
---|
| 105 | AUBIO_FREE (o); |
---|
| 106 | } |
---|