revorbis/
scales.rs

1#![allow(dead_code)]
2use std::mem::transmute;
3
4#[inline(always)]
5pub fn unitnorm(x: f32) -> f32 {
6	let mut i: u32 = unsafe {transmute(x)};
7	i = (i & 0x80000000) | 0x3f800000;
8	unsafe {transmute(i)}
9}
10
11/// * Convert dB to gain
12#[inline(always)]
13#[allow(non_snake_case)]
14pub fn todB(x: f32) -> f32 {
15	let mut i: u32 = unsafe {transmute(x)};
16	i &= 0x7FFFFFFF;
17	i as f32 * 7.17711438e-7 - 764.6161886
18}
19
20/// * Convert gain to dB
21#[inline(always)]
22#[allow(non_snake_case)]
23pub fn fromdB(x: f32) -> f32 {
24    (x * 0.11512925).exp()
25}
26
27/* The bark scale equations are approximations, since the original
28   table was somewhat hand rolled.  The below are chosen to have the
29   best possible fit to the rolled tables, thus their somewhat odd
30   appearance (these are more accurate and over a longer range than
31   the oft-quoted bark equations found in the texts I have).  The
32   approximations are valid from 0 - 30kHz (nyquist) or so.
33
34   all f in Hz, z in Bark */
35
36#[inline(always)]
37#[allow(non_snake_case)]
38pub fn toBARK(n: f32) -> f32 {
39	13.1 * (n * 0.00074).atan() + 2.24 * (n * n * 1.85e-8).atan() + 1e-4 * n
40}
41
42#[inline(always)]
43#[allow(non_snake_case)]
44pub fn fromBARK(z: f32) -> f32 {
45	102.0 * z - 2.0 * z.powf(2.0) + 0.4 * z.powf(3.0) + 1.46_f32.powf(z) - 1.0
46}
47
48#[inline(always)]
49#[allow(non_snake_case)]
50pub fn toMEL(n: f32) -> f32 {
51	(1.0 + n * 0.001).ln() * 1442.695
52}
53
54#[inline(always)]
55#[allow(non_snake_case)]
56pub fn fromMEL(m: f32) -> f32 {
57	1000.0 * (m / 1442.695).exp() - 1000.0
58}
59
60/* Frequency to octave.  We arbitrarily declare 63.5 Hz to be octave
61   0.0 */
62
63#[inline(always)]
64#[allow(non_snake_case)]
65pub fn toOC(n: f32) -> f32 {
66	n.ln() * 1.442695 - 5.965784
67}
68
69#[inline(always)]
70#[allow(non_snake_case)]
71pub fn fromOC(o: f32) -> f32 {
72	((o + 5.965784) * 0.693147).exp()
73}
74
75#[macro_export]
76macro_rules! unitnorm {
77	($x:expr) => {
78		unitnorm($x as f32)
79	}
80}
81
82#[macro_export]
83macro_rules! todB {
84	($x:expr) => {
85		todB($x as f32)
86	}
87}
88
89#[macro_export]
90macro_rules! fromdB {
91	($x:expr) => {
92		fromdB($x as f32)
93	}
94}
95
96#[macro_export]
97macro_rules! toBARK {
98	($x:expr) => {
99		toBARK($x as f32)
100	}
101}
102
103#[macro_export]
104macro_rules! fromBARK {
105	($x:expr) => {
106		fromBARK($x as f32)
107	}
108}
109
110#[macro_export]
111macro_rules! toMEL {
112	($x:expr) => {
113		toMEL($x as f32)
114	}
115}
116
117#[macro_export]
118macro_rules! fromMEL {
119	($x:expr) => {
120		fromMEL($x as f32)
121	}
122}
123
124#[macro_export]
125macro_rules! toOC {
126	($x:expr) => {
127		toOC($x as f32)
128	}
129}
130
131#[macro_export]
132macro_rules! fromOC {
133	($x:expr) => {
134		fromOC($x as f32)
135	}
136}