16
16
package curves
17
17
18
18
import java.util.*
19
+ import kotlin.math.abs
20
+ import kotlin.math.cos
21
+ import kotlin.math.sign
22
+ import kotlin.math.sin
19
23
20
24
/* *
21
25
* This generates variable frequency oscillation curves
@@ -26,7 +30,7 @@ class Cycles {
26
30
private var mPeriod = floatArrayOf()
27
31
private var mPosition = floatArrayOf()
28
32
private lateinit var mArea: FloatArray
29
- private var mCustomType: String ? = null
33
+ private var mCustomType: FloatArray ? = null
30
34
private var mCustomCurve: MonoSpline ? = null
31
35
private var mType = 0
32
36
private var mPI2 = Math .PI .toFloat() * 2
@@ -36,10 +40,10 @@ class Cycles {
36
40
}
37
41
38
42
// @TODO: add description
39
- fun setType (type : Int , customType : String ) {
43
+ fun setType (type : Int , customType : FloatArray? ) {
40
44
mType = type
41
45
mCustomType = customType
42
- if (mCustomType != null ) {
46
+ if (customType != null ) {
43
47
mCustomCurve = buildWave(customType)
44
48
}
45
49
}
@@ -87,7 +91,7 @@ class Cycles {
87
91
mNormalized = true
88
92
}
89
93
90
- fun getP (time : Float ): Float {
94
+ private fun getP (time : Float ): Float {
91
95
var time = time
92
96
if (time < 0 ) {
93
97
time = 0f
@@ -103,7 +107,8 @@ class Cycles {
103
107
val t = time
104
108
val m = ((mPeriod[index] - mPeriod[index - 1 ])
105
109
/ (mPosition[index] - mPosition[index - 1 ]))
106
- p = mArea[index - 1 ] + (mPeriod[index - 1 ] - m * mPosition[index - 1 ]) * (t - mPosition[index - 1 ]) + m * (t * t - mPosition[index - 1 ] * mPosition[index - 1 ]) / 2
110
+ p =
111
+ mArea[index - 1 ] + (mPeriod[index - 1 ] - m * mPosition[index - 1 ]) * (t - mPosition[index - 1 ]) + m * (t * t - mPosition[index - 1 ] * mPosition[index - 1 ]) / 2
107
112
}
108
113
return p
109
114
}
@@ -112,19 +117,19 @@ class Cycles {
112
117
fun getValue (time : Float , phase : Float ): Float {
113
118
val angle = phase + getP(time) // angle is / by 360
114
119
return when (mType) {
115
- SIN_WAVE -> Math . sin(( mPI2 * angle).toDouble()).toFloat( )
116
- SQUARE_WAVE -> Math .signum (0.5 - angle % 1 ).toFloat( )
117
- TRIANGLE_WAVE -> 1 - Math . abs((angle * 4 + 1 ) % 4 - 2 )
120
+ SIN_WAVE -> sin(mPI2 * angle)
121
+ SQUARE_WAVE -> sign (0.5f - angle % 1 )
122
+ TRIANGLE_WAVE -> 1 - abs((angle * 4 + 1 ) % 4 - 2 )
118
123
SAW_WAVE -> (angle * 2 + 1 ) % 2 - 1
119
124
REVERSE_SAW_WAVE -> 1 - (angle * 2 + 1 ) % 2
120
- COS_WAVE -> Math . cos(( mPI2 * (phase + angle)).toDouble()).toFloat( )
125
+ COS_WAVE -> cos(mPI2 * (phase + angle))
121
126
BOUNCE -> {
122
127
val x = 1 - Math .abs(angle * 4 % 4 - 2 )
123
128
1 - x * x
124
129
}
125
130
126
131
CUSTOM -> mCustomCurve!! .getPos((angle % 1 ), 0 )
127
- else -> Math . sin(( mPI2 * angle).toDouble()).toFloat( )
132
+ else -> sin(mPI2 * angle)
128
133
}
129
134
}
130
135
@@ -155,20 +160,19 @@ class Cycles {
155
160
val angle = phase + getP(time)
156
161
val dangle_dtime = getDP(time) + dphase
157
162
return when (mType) {
158
- SIN_WAVE -> (mPI2 * dangle_dtime * Math . cos((mPI2 * angle).toDouble())).toFloat( )
163
+ SIN_WAVE -> (mPI2 * dangle_dtime * cos((mPI2 * angle)) )
159
164
SQUARE_WAVE -> 0f
160
165
TRIANGLE_WAVE -> 4 * dangle_dtime * Math .signum((angle * 4 + 3 ) % 4 - 2 )
161
166
SAW_WAVE -> dangle_dtime * 2
162
167
REVERSE_SAW_WAVE -> - dangle_dtime * 2
163
- COS_WAVE -> ( - mPI2 * dangle_dtime * Math . sin(( mPI2 * angle).toDouble())).toFloat( )
168
+ COS_WAVE -> - mPI2 * dangle_dtime * sin(mPI2 * angle)
164
169
BOUNCE -> 4 * dangle_dtime * ((angle * 4 + 2 ) % 4 - 2 )
165
170
CUSTOM -> mCustomCurve!! .getSlope((angle % 1 ), 0 )
166
- else -> ( mPI2 * dangle_dtime * Math . cos((mPI2 * angle).toDouble())).toFloat( )
171
+ else -> mPI2 * dangle_dtime * cos((mPI2 * angle))
167
172
}
168
173
}
169
174
170
175
companion object {
171
- var TAG = " Oscillator"
172
176
const val SIN_WAVE = 0 // theses must line up with attributes
173
177
const val SQUARE_WAVE = 1
174
178
const val TRIANGLE_WAVE = 2
@@ -177,45 +181,33 @@ class Cycles {
177
181
const val COS_WAVE = 5
178
182
const val BOUNCE = 6
179
183
const val CUSTOM = 7
184
+ }
185
+
186
+ /* *
187
+ * This builds a monotonic spline to be used as a wave function
188
+ */
180
189
181
- /* *
182
- * This builds a monotonic spline to be used as a wave function
183
- */
184
- fun buildWave (configString : String ): MonoSpline {
185
- // done this way for efficiency
186
- val values = FloatArray (configString.length / 2 )
187
- var start = configString.indexOf(' (' ) + 1
188
- var off1 = configString.indexOf(' ,' , start)
189
- var count = 0
190
- while (off1 != - 1 ) {
191
- val tmp = configString.substring(start, off1).trim { it <= ' ' }
192
- values[count++ ] = tmp.toFloat()
193
- off1 = configString.indexOf(' ,' , off1 + 1 .also { start = it })
194
- }
195
- off1 = configString.indexOf(' )' , start)
196
- val tmp = configString.substring(start, off1).trim { it <= ' ' }
197
- values[count++ ] = tmp.toFloat()
198
- return buildWave(Arrays .copyOf(values, count))
199
- }
200
190
201
- private fun buildWave (values : FloatArray ): MonoSpline {
202
- val length = values.size * 3 - 2
203
- val len = values.size - 1
204
- val gap = 1.0f / len
205
- val points = ArrayList <FloatArray >(length)
206
- val time = FloatArray (length)
207
- for (i in values.indices) {
208
- val v = values[i]
209
- points[i + len][0 ] = v
210
- time[i + len] = i * gap
211
- if (i > 0 ) {
212
- points[i + len * 2 ][0 ] = v + 1
213
- time[i + len * 2 ] = i * gap + 1
214
- points[i - 1 ][0 ] = v - 1 - gap
215
- time[i - 1 ] = i * gap + - 1 - gap
216
- }
191
+ private fun buildWave (values : FloatArray ): MonoSpline {
192
+ val length = values.size * 3 - 2
193
+ val len = values.size - 1
194
+ val gap = 1.0f / len
195
+ val points = ArrayList <FloatArray >(length)
196
+ for (i in 0 until length) {
197
+ points.add(FloatArray (1 ))
198
+ }
199
+ val time = FloatArray (length)
200
+ for (i in values.indices) {
201
+ val v = values[i]
202
+ points[i + len][0 ] = v
203
+ time[i + len] = i * gap
204
+ if (i > 0 ) {
205
+ points[i + len * 2 ][0 ] = v + 1
206
+ time[i + len * 2 ] = i * gap + 1
207
+ points[i - 1 ][0 ] = v - 1 - gap
208
+ time[i - 1 ] = i * gap + - 1 - gap
217
209
}
218
- return MonoSpline (time, points)
219
210
}
211
+ return MonoSpline (time, points)
220
212
}
221
- }
213
+ }
0 commit comments