Chris@224
|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
Chris@224
|
2
|
Chris@224
|
3 /*
|
Chris@224
|
4 Sonic Visualiser
|
Chris@224
|
5 An audio file viewer and annotation editor.
|
Chris@224
|
6 Centre for Digital Music, Queen Mary, University of London.
|
Chris@224
|
7 This file copyright 2006 Chris Cannam.
|
Chris@224
|
8
|
Chris@224
|
9 This program is free software; you can redistribute it and/or
|
Chris@224
|
10 modify it under the terms of the GNU General Public License as
|
Chris@224
|
11 published by the Free Software Foundation; either version 2 of the
|
Chris@224
|
12 License, or (at your option) any later version. See the file
|
Chris@224
|
13 COPYING included with this distribution for more information.
|
Chris@224
|
14 */
|
Chris@224
|
15
|
Chris@224
|
16 #include "LogRange.h"
|
Chris@224
|
17
|
Chris@224
|
18 #include <algorithm>
|
Chris@464
|
19 #include <iostream>
|
Chris@224
|
20 #include <cmath>
|
Chris@224
|
21
|
Chris@224
|
22 void
|
Chris@224
|
23 LogRange::mapRange(float &min, float &max, float logthresh)
|
Chris@224
|
24 {
|
Chris@224
|
25 if (min > max) std::swap(min, max);
|
Chris@224
|
26 if (max == min) max = min + 1;
|
Chris@224
|
27
|
Chris@464
|
28 // std::cerr << "LogRange::mapRange: min = " << min << ", max = " << max << std::endl;
|
Chris@464
|
29
|
Chris@224
|
30 if (min >= 0.f) {
|
Chris@224
|
31
|
Chris@224
|
32 max = log10f(max); // we know max != 0
|
Chris@224
|
33
|
Chris@224
|
34 if (min == 0.f) min = std::min(logthresh, max);
|
Chris@224
|
35 else min = log10f(min);
|
Chris@224
|
36
|
Chris@464
|
37 // std::cerr << "LogRange::mapRange: positive: min = " << min << ", max = " << max << std::endl;
|
Chris@464
|
38
|
Chris@224
|
39 } else if (max <= 0.f) {
|
Chris@224
|
40
|
Chris@224
|
41 min = log10f(-min); // we know min != 0
|
Chris@224
|
42
|
Chris@224
|
43 if (max == 0.f) max = std::min(logthresh, min);
|
Chris@224
|
44 else max = log10f(-max);
|
Chris@224
|
45
|
Chris@224
|
46 std::swap(min, max);
|
Chris@224
|
47
|
Chris@464
|
48 // std::cerr << "LogRange::mapRange: negative: min = " << min << ", max = " << max << std::endl;
|
Chris@464
|
49
|
Chris@224
|
50 } else {
|
Chris@224
|
51
|
Chris@224
|
52 // min < 0 and max > 0
|
Chris@224
|
53
|
Chris@224
|
54 max = log10f(std::max(max, -min));
|
Chris@224
|
55 min = std::min(logthresh, max);
|
Chris@464
|
56
|
Chris@464
|
57 // std::cerr << "LogRange::mapRange: spanning: min = " << min << ", max = " << max << std::endl;
|
Chris@224
|
58 }
|
Chris@224
|
59
|
Chris@224
|
60 if (min == max) min = max - 1;
|
Chris@224
|
61 }
|
Chris@224
|
62
|
Chris@224
|
63 float
|
Chris@224
|
64 LogRange::map(float value, float thresh)
|
Chris@224
|
65 {
|
Chris@224
|
66 if (value == 0.f) return thresh;
|
Chris@224
|
67 return log10f(fabsf(value));
|
Chris@224
|
68 }
|
Chris@224
|
69
|
Chris@266
|
70 float
|
Chris@266
|
71 LogRange::unmap(float value)
|
Chris@266
|
72 {
|
Chris@266
|
73 return powf(10.0, value);
|
Chris@266
|
74 }
|