src/io/source_sndfile.c: return NULL before trying to allocate memory, clean up
[aubio.git] / src / utils / scale.c
1 /*
2   Copyright (C) 2003-2009 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
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation, either version 3 of the License, or
9   (at your option) any later version.
10
11   aubio is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with aubio.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21 #include "aubio_priv.h"
22 #include "fvec.h"
23 #include "utils/scale.h"
24
25 struct _aubio_scale_t {
26   smpl_t ilow;
27   smpl_t ihig;
28   smpl_t olow;
29   smpl_t ohig;
30
31   smpl_t scaler;
32   smpl_t irange;
33
34   /* not implemented yet : type in/out data
35      bool inint;
36      bool outint;
37      */
38 };
39
40 aubio_scale_t * new_aubio_scale (smpl_t ilow, smpl_t ihig, 
41     smpl_t olow, smpl_t ohig) {
42   aubio_scale_t * s = AUBIO_NEW(aubio_scale_t);
43   aubio_scale_set_limits (s, ilow, ihig, olow, ohig);
44   return s;
45 }
46
47 void del_aubio_scale(aubio_scale_t *s) {
48   AUBIO_FREE(s);
49 }
50
51 uint_t aubio_scale_set_limits (aubio_scale_t *s, smpl_t ilow, smpl_t ihig,
52     smpl_t olow, smpl_t ohig) {
53   smpl_t inputrange = ihig - ilow;
54   smpl_t outputrange= ohig - olow;
55   s->ilow = ilow;
56   s->ihig = ihig;
57   s->olow = olow;
58   s->ohig = ohig;
59   if (inputrange == 0) {
60     s->scaler = 0.0;
61   } else {
62     s->scaler = outputrange/inputrange;
63     if (inputrange < 0) {
64       inputrange = inputrange * -1.0f;
65     }
66   }
67   return AUBIO_OK;
68 }
69
70 void aubio_scale_do (aubio_scale_t *s, fvec_t *input) 
71 {
72   uint_t j;
73   for (j=0;  j < input->length; j++){
74     input->data[j] -= s->ilow;
75     input->data[j] *= s->scaler;
76     input->data[j] += s->olow;
77   }
78 }
79