[specdesc] improve error message
[aubio.git] / src / musicutils.c
1 /*
2   Copyright (C) 2018 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 "musicutils.h"
23
24 smpl_t
25 aubio_hztomel (smpl_t freq)
26 {
27   const smpl_t lin_space = 3./200.;
28   const smpl_t split_hz = 1000.;
29   const smpl_t split_mel = split_hz * lin_space;
30   const smpl_t log_space = 27./LOG(6400/1000.);
31   if (freq < 0) {
32     AUBIO_WRN("hztomel: input frequency should be >= 0\n");
33     return 0;
34   }
35   if (freq < split_hz)
36   {
37     return freq * lin_space;
38   } else {
39     return split_mel + log_space * LOG (freq / split_hz);
40   }
41
42 }
43
44 smpl_t
45 aubio_meltohz (smpl_t mel)
46 {
47   const smpl_t lin_space = 200./3.;
48   const smpl_t split_hz = 1000.;
49   const smpl_t split_mel = split_hz / lin_space;
50   const smpl_t logSpacing = POW(6400/1000., 1/27.);
51   if (mel < 0) {
52     AUBIO_WRN("meltohz: input mel should be >= 0\n");
53     return 0;
54   }
55   if (mel < split_mel) {
56     return lin_space * mel;
57   } else {
58     return split_hz * POW(logSpacing, mel - split_mel);
59   }
60 }
61
62 smpl_t
63 aubio_hztomel_htk (smpl_t freq)
64 {
65   const smpl_t split_hz = 700.;
66   const smpl_t log_space = 1127.;
67   if (freq < 0) {
68     AUBIO_WRN("hztomel_htk: input frequency should be >= 0\n");
69     return 0;
70   }
71   return log_space * LOG (1 + freq / split_hz);
72 }
73
74 smpl_t
75 aubio_meltohz_htk (smpl_t mel)
76 {
77   const smpl_t split_hz = 700.;
78   const smpl_t log_space = 1./1127.;
79   if (mel < 0) {
80     AUBIO_WRN("meltohz_htk: input frequency should be >= 0\n");
81     return 0;
82   }
83   return split_hz * ( EXP ( mel * log_space) - 1.);
84 }
85