| 1 |
/* |
| 2 |
Copyright (c) 2009, hkrn All rights reserved. |
| 3 |
|
| 4 |
Redistribution and use in source and binary forms, with or without |
| 5 |
modification, are permitted provided that the following conditions are met: |
| 6 |
|
| 7 |
Redistributions of source code must retain the above copyright notice, this |
| 8 |
list of conditions and the following disclaimer. Redistributions in binary |
| 9 |
form must reproduce the above copyright notice, this list of conditions and |
| 10 |
the following disclaimer in the documentation and/or other materials |
| 11 |
provided with the distribution. Neither the name of the hkrn nor |
| 12 |
the names of its contributors may be used to endorse or promote products |
| 13 |
derived from this software without specific prior written permission. |
| 14 |
|
| 15 |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 16 |
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 17 |
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 18 |
ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR |
| 19 |
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 20 |
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 21 |
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 22 |
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 23 |
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 24 |
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH |
| 25 |
DAMAGE. |
| 26 |
*/ |
| 27 |
|
| 28 |
// |
| 29 |
// $Id$ |
| 30 |
// |
| 31 |
|
| 32 |
namespace SlMML |
| 33 |
{ |
| 34 |
public enum FormantVowel |
| 35 |
{ |
| 36 |
Unknown = -1, |
| 37 |
A = 0, |
| 38 |
E, |
| 39 |
I, |
| 40 |
O, |
| 41 |
U |
| 42 |
} |
| 43 |
|
| 44 |
public class Formant |
| 45 |
{ |
| 46 |
#region ��������������������������������������������������������� |
| 47 |
public Formant() |
| 48 |
{ |
| 49 |
m_vowel = FormantVowel.A; |
| 50 |
m_power = false; |
| 51 |
m_leftMemory = new double[10] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; |
| 52 |
m_rightMemory = new double[10] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; |
| 53 |
} |
| 54 |
#endregion |
| 55 |
|
| 56 |
#region ��������������������������� |
| 57 |
public void Disable() |
| 58 |
{ |
| 59 |
m_power = false; |
| 60 |
Reset(); |
| 61 |
} |
| 62 |
|
| 63 |
public void Reset() |
| 64 |
{ |
| 65 |
for (int i = 0; i < 10; i++) |
| 66 |
m_leftMemory[i] = m_rightMemory[i] = 0; |
| 67 |
} |
| 68 |
|
| 69 |
public void GetSamples(ref double[] samples, int start, int end) |
| 70 |
{ |
| 71 |
if (!m_power || m_vowel == FormantVowel.Unknown) |
| 72 |
return; |
| 73 |
double[] coeff = m_coeff[(int)m_vowel]; |
| 74 |
for (int i = start; i < end; i++) |
| 75 |
{ |
| 76 |
double sample = samples[i]; |
| 77 |
double value = coeff[0] * sample |
| 78 |
+ coeff[1] * m_leftMemory[0] |
| 79 |
+ coeff[2] * m_leftMemory[1] |
| 80 |
+ coeff[3] * m_leftMemory[2] |
| 81 |
+ coeff[4] * m_leftMemory[3] |
| 82 |
+ coeff[5] * m_leftMemory[4] |
| 83 |
+ coeff[6] * m_leftMemory[5] |
| 84 |
+ coeff[7] * m_leftMemory[6] |
| 85 |
+ coeff[8] * m_leftMemory[7] |
| 86 |
+ coeff[9] * m_leftMemory[8] |
| 87 |
+ coeff[10] * m_leftMemory[9]; |
| 88 |
samples[i] = value; |
| 89 |
m_leftMemory[9] = m_leftMemory[8]; |
| 90 |
m_leftMemory[8] = m_leftMemory[7]; |
| 91 |
m_leftMemory[7] = m_leftMemory[6]; |
| 92 |
m_leftMemory[6] = m_leftMemory[5]; |
| 93 |
m_leftMemory[5] = m_leftMemory[4]; |
| 94 |
m_leftMemory[4] = m_leftMemory[3]; |
| 95 |
m_leftMemory[3] = m_leftMemory[2]; |
| 96 |
m_leftMemory[2] = m_leftMemory[1]; |
| 97 |
m_leftMemory[1] = m_leftMemory[0]; |
| 98 |
m_leftMemory[0] = value; |
| 99 |
} |
| 100 |
} |
| 101 |
#endregion |
| 102 |
|
| 103 |
#region ������������������������������ |
| 104 |
public FormantVowel Vowel |
| 105 |
{ |
| 106 |
set |
| 107 |
{ |
| 108 |
m_power = true; |
| 109 |
m_vowel = value; |
| 110 |
} |
| 111 |
} |
| 112 |
#endregion |
| 113 |
|
| 114 |
#region ��������������������������� |
| 115 |
private readonly double[][] m_coeff = new double[][] { |
| 116 |
new double[] { |
| 117 |
8.11044e-06, 8.943665402, -36.83889529, 92.01697887, -154.337906, 181.6233289, |
| 118 |
-151.8651235, 89.09614114, -35.10298511, 8.388101016, -0.923313471 |
| 119 |
}, |
| 120 |
new double[] { |
| 121 |
4.36215e-06, 8.90438318, -36.55179099, 91.05750846, -152.422234, 179.1170248, |
| 122 |
-149.6496211, 87.78352223, -34.60687431, 8.282228154, -0.914150747 |
| 123 |
}, |
| 124 |
new double[] { |
| 125 |
3.33819e-06, 8.893102966, -36.49532826, 90.96543286, -152.4545478, 179.4835618, |
| 126 |
-150.315433, 88.43409371, -34.98612086, 8.407803364, -0.932568035 |
| 127 |
}, |
| 128 |
new double[] { |
| 129 |
1.13572e-06, 8.994734087, -37.2084849, 93.22900521, -156.6929844, 184.596544, |
| 130 |
-154.3755513, 90.49663749, -35.58964535, 8.478996281, -0.929252233 |
| 131 |
}, |
| 132 |
new double[] { |
| 133 |
4.09431e-07, 8.997322763, -37.20218544, 93.11385476, -156.2530937, 183.7080141, |
| 134 |
-153.2631681, 89.59539726, -35.12454591, 8.338655623, -0.910251753 |
| 135 |
} |
| 136 |
}; |
| 137 |
private double[] m_leftMemory; |
| 138 |
private double[] m_rightMemory; |
| 139 |
private FormantVowel m_vowel; |
| 140 |
private bool m_power; |
| 141 |
#endregion |
| 142 |
} |
| 143 |
} |