forked from euphony-io/euphony
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EuFreqGenerator.java
203 lines (162 loc) · 4.92 KB
/
EuFreqGenerator.java
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package euphony.lib.transmitter;
import euphony.lib.util.EuOption;
public class EuFreqGenerator {
// FIXED ACOUSTIC DATA
private final double PI = Math.PI;
private final double PI2 = PI * 2;
public enum CrossfadeType {
FRONT, END, BOTH
}
// Member for Frequency point
// DEFAULT DEFINITION
private short[] mZeroSource;
private int mBufferSize;
private int mSampleRate;
private int mCpIndex;
private double mCpLastTheta;
private EuOption mTxOption;
public EuFreqGenerator() {
mTxOption = new EuOption();
initialize();
}
public EuFreqGenerator(EuOption option) {
mTxOption = option;
initialize();
}
private void initialize() {
mBufferSize = mTxOption.getBufferSize();
mSampleRate = mTxOption.getSampleRate();
mZeroSource = new short[mBufferSize];
mCpIndex = 0;
mCpLastTheta = 0;
}
public short[] makeStaticFrequency(int freq, int degree)
{
double[] double_source = new double[mBufferSize];
short[] source = new short[mBufferSize];
double time, phase;
for(int i = 0; i < mBufferSize; i++)
{
time = (double)i / (double)mSampleRate;
double_source[i] = Math.sin(PI2 * (double)freq * time);
source[i] = (short)(32767 * double_source[i]);
}
return source;
}
public short[] makeFrequencyWithCP(int freq) {
double[] double_source = new double[mBufferSize];
short[] source = new short[mBufferSize];
double x = PI2 * freq;
double thetaDiff = x * (mCpIndex / mSampleRate) - mCpLastTheta;
int bufferIdx = 0;
double theta = 0;
int i = mCpIndex;
int bufferSize = mCpIndex + mBufferSize;
for(; i < bufferSize; i++) {
theta = x * i / mSampleRate - thetaDiff;
double_source[bufferIdx] = Math.sin(theta);
source[bufferIdx] = (short)(32767 * double_source[bufferIdx]);
bufferIdx++;
}
mCpIndex = i;
mCpLastTheta = x * mCpIndex / mSampleRate - thetaDiff;
return source;
}
public void euMakeFrequency(short[] source, int freq)
{
source = makeStaticFrequency(freq, 0);
}
public short[] makeFrequencyWithCrossFade(int freq)
{
return applyCrossFade(makeStaticFrequency(freq, 0), CrossfadeType.BOTH);
}
public short[] makeFrequencyWithValue(int value) {
return applyCrossFade(makeStaticFrequency(mTxOption.getControlPoint() + mTxOption.getDataInterval() * value, 0), CrossfadeType.BOTH);
}
public short[] applyCrossFade(short[] source, CrossfadeType type)
{
double mini_window;
int fade_section = mTxOption.getFadeRange();
for(int i = 0; i < fade_section; i++)
{
mini_window = (double)i / (double)fade_section;
switch(type) {
case FRONT:
source[i] *= mini_window;
break;
case END:
source[mBufferSize-1-i] *= mini_window;
break;
case BOTH:
source[i] *= mini_window;
source[mBufferSize-1-i] *= mini_window;
break;
}
}
return source;
}
public short[] appendRawData(short[] src, short[] objective)
{
int SRC_LENGTH, TOTAL_LENGTH;
if(src == null)
SRC_LENGTH = 0;
else
SRC_LENGTH = src.length;
TOTAL_LENGTH = SRC_LENGTH + objective.length;
short[] dest = new short[TOTAL_LENGTH];
for(int i = 0; i < SRC_LENGTH; i++)
dest[i] = src[i];
for(int i = SRC_LENGTH; i < TOTAL_LENGTH; i++)
dest[i] = objective[i - SRC_LENGTH];
return dest;
}
public short[] euLinkRawData(short[]... sources)
{
final int bufferSize = mTxOption.getBufferSize();
short[] dest = new short[sources.length * bufferSize];
for(int i = 0; i < sources.length; i++)
for(int j = 0; j < sources[i].length; j++)
dest[j + i * bufferSize] = sources[i][j];
return dest;
}
public short[] euLinkRawData(boolean isCrossfaded, short[]... sources)
{
final int bufferSize = mTxOption.getBufferSize();
short[] dest = new short[sources.length * bufferSize];
for(int i = 0; i < sources.length; i++)
{
if(isCrossfaded)
sources[i] = applyCrossFade(sources[i], CrossfadeType.BOTH);
for(int j = 0; j < sources[i].length; j++)
dest[j + i * bufferSize] = sources[i][j];
}
return dest;
}
public short[] mixingRawData(short[]... sources)
{
short[] dest = sources[0].clone();
for(int i = 1; i < sources.length; i++)
{
for(int j = 0; j < sources[i].length; j++){
dest[j] = (short) (((int)dest[j] + (int)sources[i][j])/2);
}
}
return dest;
}
public short[] euMakeMaximumVolume(short[] source)
{
int max = 0;
//SCAN FOR VOLUME UP
for (short i1 : source)
if (max < Math.abs(i1))
max = i1;
if(32767 == max)
return source;
for(int i = 0; i < source.length; i++)
source[i] *= 32767/max;
return source;
}
public short[] getZeroSource() {
return mZeroSource;
}
}