-
Notifications
You must be signed in to change notification settings - Fork 16
/
MathUtil.cs
170 lines (150 loc) · 4.55 KB
/
MathUtil.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
using System;
using System.Collections.Generic;
using System.Text;
// Copyright (c) 2006, 2009 by Hugh Pyle, inguzaudio.com
namespace DSPUtil
{
// Package constants
public class DSPUtil
{
public const int BUFSIZE = 8192; // 512;
public static Version VERSION { get { return new Version("0.9.32"); } }
public static DateTime EXPIRY { get { return DateTime.MaxValue; } } // expire just about never
public static String GetVersionInfo()
{
string s = String.Format("version {0}{1}", VERSION, IsMono() ? " on Mono" : "");
return s;
}
public static bool IsMono()
{
Type t = Type.GetType("Mono.Runtime");
return (t != null) ? true : false;
}
}
// flags (bit)
public enum ChannelFlag
{
NONE = 0x00,
LEFT = 0x01,
RIGHT = 0x02,
BOTH = 0x03
}
// Math utils
public sealed class MathUtil
{
public static double SQRT2 = Math.Sqrt(2);
public static double INVSQRT2 = 1/Math.Sqrt(2);
public static bool IsPowerOfTwo(int n)
{
// http://graphics.stanford.edu/~seander/bithacks.html#DetermineIfPowerOf2
return ((n & (n - 1)) == 0) && n>0;
}
public static int NextPowerOfTwo(int n)
{
// http://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
n--;
n |= n >> 1;
n |= n >> 2;
n |= n >> 4;
n |= n >> 8;
n |= n >> 16;
n++;
return n;
}
public static double dB(double gain)
{
return (20 * Math.Log(gain,10));
}
public static double gain(double dB)
{
return (Math.Pow(10, dB / 20));
}
public static double Feet(int samples, uint sampleRate)
{
return 1116.43701 * samples / sampleRate;
}
public static int FSamples(double feet, uint sampleRate)
{
return (int)((feet / 1116.43701) * sampleRate);
}
public static double Metres(int samples, uint sampleRate)
{
return 340.29 * samples / sampleRate;
}
public static int MSamples(double metres, uint sampleRate)
{
return (int)((metres / 340.29) * sampleRate);
}
public static double FcFromFeet(double r)
{
return 1116.43701 / (2 * Math.PI * r);
}
public static double FcFromMetres(double r)
{
return 340.29 / (2 * Math.PI * r);
}
/// <summary>
/// Compute the Bark critical band rate z at frequency f (Hz)
/// </summary>
/// <param name="f">Frequency (Hz)</param>
/// <returns>z</returns>
public static double Bark(double f)
{
// from http://www.ling.su.se/STAFF/hartmut/bark.htm
double z = (26.81 / (1 + 1960 / f)) - 0.53;
return z;
}
/// <summary>
/// Compute the Bark critical bandwidth Cb at frequency f (Hz)
/// </summary>
/// <param name="f">Frequency (Hz)</param>
/// <returns>Cb</returns>
public static double BarkCb(double f)
{
// from http://www.ling.su.se/STAFF/hartmut/bark.htm
double z = Bark(f);
double Cb = 52548 / ((z * z) - (52.56 * z) + 690.39);
return Cb;
}
public delegate double InvertDelegate(double f);
public static double invert(InvertDelegate fn, double low, double high, double i)
{
double probe;
while (high - low > 0.001)
{
probe = (low + high) / 2;
if (fn(probe) > i)
high = probe;
else
low = probe;
}
return low;
}
// Greatest common divisor
public static uint gcd(uint a, uint b)
{
while (b != 0)
{
uint t = b;
b = a % b;
a = t;
}
return a;
}
// Least common multiple
public static uint lcm(uint a, uint b)
{
long g = gcd(a, b);
uint l = (uint)(a / g) * b;
return l;
}
public static double Radians(double degrees)
{
return degrees * Math.PI / 180;
}
public static double Degrees(double radians)
{
return radians * 180 / Math.PI;
}
}
}