Revision bdb341bc
Added by Leszek Koltunski over 7 years ago
src/main/java/org/distorted/library/type/Dynamic.java | ||
---|---|---|
66 | 66 |
*/ |
67 | 67 |
public static final int MODE_JUMP = 2; |
68 | 68 |
|
69 |
/** |
|
70 |
* The default mode of access. When in this mode, we are able to call interpolate() with points in time |
|
71 |
* in any random order. This means one single Dynamic can be used in many effects simultaneously. |
|
72 |
* On the other hand, when in this mode, it is not possible to smoothly interpolate when mDuration suddenly |
|
73 |
* changes. |
|
74 |
*/ |
|
75 |
public static final int ACCESS_RANDOM = 0; |
|
76 |
/** |
|
77 |
* Set the mode to ACCESS_SEQUENTIAL if you need to change mDuration and you would rather have the Dynamic |
|
78 |
* keep on smoothly interpolating. |
|
79 |
* On the other hand, in this mode, a Dynamic can only be accessed in sequential manner, which means one |
|
80 |
* one Dynamic can only be used in one effect at a time. |
|
81 |
*/ |
|
82 |
public static final int ACCESS_SEQUENTIAL = 1; |
|
83 |
|
|
69 | 84 |
protected int mDimension; |
70 | 85 |
protected int numPoints; |
71 | 86 |
protected int mSegment; // between which pair of points are we currently? (in case of PATH this is a bit complicated!) |
... | ... | |
73 | 88 |
protected int mMode; // LOOP, PATH or JUMP |
74 | 89 |
protected long mDuration; // number of milliseconds it takes to do a full loop/path from first vector to the last and back to the first |
75 | 90 |
protected float mCount; // number of loops/paths we will do; mCount = 1.5 means we go from the first vector to the last, back to first, and to the last again. |
91 |
protected double mLastPos; |
|
92 |
protected int mAccessMode; |
|
76 | 93 |
|
77 | 94 |
protected class VectorNoise |
78 | 95 |
{ |
... | ... | |
160 | 177 |
mCount = count; |
161 | 178 |
mDimension = dimension; |
162 | 179 |
mSegment = -1; |
180 |
mLastPos = -1; |
|
181 |
mAccessMode= ACCESS_RANDOM; |
|
163 | 182 |
|
164 | 183 |
baseV = new float[mDimension][mDimension]; |
165 | 184 |
buf = new float[mDimension]; |
166 | 185 |
old = new float[mDimension]; |
167 | 186 |
} |
168 | 187 |
|
169 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
170 |
|
|
171 |
public void interpolateMain(float[] buffer, int offset, long currentDuration) |
|
172 |
{ |
|
173 |
if( mDuration<=0.0f ) |
|
174 |
{ |
|
175 |
interpolate(buffer,offset,mCount-(int)mCount); |
|
176 |
} |
|
177 |
else |
|
178 |
{ |
|
179 |
double x = (double)currentDuration/mDuration; |
|
180 |
|
|
181 |
if( x<=mCount || mCount<=0.0f ) |
|
182 |
{ |
|
183 |
interpolate(buffer,offset, (float)(x-(int)x) ); |
|
184 |
} |
|
185 |
} |
|
186 |
} |
|
187 |
|
|
188 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
189 |
|
|
190 |
public boolean interpolateMain(float[] buffer, int offset, long currentDuration, long step) |
|
191 |
{ |
|
192 |
if( mDuration<=0.0f ) |
|
193 |
{ |
|
194 |
interpolate(buffer,offset,mCount-(int)mCount); |
|
195 |
return false; |
|
196 |
} |
|
197 |
|
|
198 |
double x = (double)currentDuration/mDuration; |
|
199 |
|
|
200 |
if( x<=mCount || mCount<=0.0f ) |
|
201 |
{ |
|
202 |
interpolate(buffer,offset, (float)(x-(int)x) ); |
|
203 |
|
|
204 |
if( currentDuration+step > mDuration*mCount && mCount>0.0f ) |
|
205 |
{ |
|
206 |
interpolate(buffer,offset,mCount-(int)mCount); |
|
207 |
return true; |
|
208 |
} |
|
209 |
} |
|
210 |
|
|
211 |
return false; |
|
212 |
} |
|
213 |
|
|
214 | 188 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
215 | 189 |
|
216 | 190 |
protected float noise(float time,int vecNum) |
... | ... | |
626 | 600 |
mDuration = duration; |
627 | 601 |
} |
628 | 602 |
|
603 |
|
|
604 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
605 |
/** |
|
606 |
* Sets the access mode this Dynamic will be working in. |
|
607 |
* |
|
608 |
* @param mode ACCESS_RANDOM or ACCESS_SEQUENTIAL. |
|
609 |
* see {@link Dynamic#ACCESS_RANDOM}. |
|
610 |
* see {@link Dynamic#ACCESS_SEQUENTIAL}. |
|
611 |
*/ |
|
612 |
public void setAccessMode(int mode) |
|
613 |
{ |
|
614 |
mAccessMode = mode; |
|
615 |
mLastPos = -1; |
|
616 |
} |
|
617 |
|
|
618 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
619 |
/** |
|
620 |
* Writes the results of interpolation between the Points at time 'time' to the passed float buffer. |
|
621 |
* |
|
622 |
* @param buffer Float buffer we will write the resulting Static1D to. |
|
623 |
* @param offset Offset in the buffer where to write the result. |
|
624 |
* @param time Time of interpolation. Time=0.0 would return the first Point, Time=0.5 - the last, |
|
625 |
* time=1.0 - the first again, and time 0.1 would be 1/5 of the way between the first and the last Points. |
|
626 |
*/ |
|
627 |
|
|
628 |
public void interpolateMain(float[] buffer, int offset, long time) |
|
629 |
{ |
|
630 |
if( mDuration<=0.0f ) |
|
631 |
{ |
|
632 |
interpolate(buffer,offset,mCount-(int)mCount); |
|
633 |
} |
|
634 |
else |
|
635 |
{ |
|
636 |
double pos = (double)time/mDuration; |
|
637 |
|
|
638 |
if( pos<=mCount || mCount<=0.0f ) |
|
639 |
{ |
|
640 |
interpolate(buffer,offset, (float)(pos-(int)pos) ); |
|
641 |
} |
|
642 |
} |
|
643 |
} |
|
644 |
|
|
645 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
646 |
/** |
|
647 |
* Writes the results of interpolation between the Points at time 'time' to the passed float buffer. |
|
648 |
* <p> |
|
649 |
* This version differs from the previous in that it returns a boolean value which indicates whether |
|
650 |
* the interpolation is finished. |
|
651 |
* |
|
652 |
* @param buffer Float buffer we will write the resulting Static1D to. |
|
653 |
* @param offset Offset in the buffer where to write the result. |
|
654 |
* @param time Time of interpolation. Time=0.0 would return the first Point, Time=0.5 - the last, |
|
655 |
* time=1.0 - the first again, and time 0.1 would be 1/5 of the way between the first and the last Points. |
|
656 |
* @param step Time difference between now and the last time we called this function. Needed to figure out |
|
657 |
* if the previous time we were called the effect wasn't finished yet, but now it is. |
|
658 |
* @return true if the interpolation reached its end. |
|
659 |
*/ |
|
660 |
public boolean interpolateMain(float[] buffer, int offset, long time, long step) |
|
661 |
{ |
|
662 |
if( mDuration<=0.0f ) |
|
663 |
{ |
|
664 |
interpolate(buffer,offset,mCount-(int)mCount); |
|
665 |
return false; |
|
666 |
} |
|
667 |
|
|
668 |
double pos; |
|
669 |
|
|
670 |
if( mAccessMode==ACCESS_SEQUENTIAL ) |
|
671 |
{ |
|
672 |
pos = mLastPos<0 ? (double)time/mDuration : (double)step/mDuration + mLastPos; |
|
673 |
mLastPos = pos; |
|
674 |
} |
|
675 |
else |
|
676 |
{ |
|
677 |
pos = (double)time/mDuration; |
|
678 |
} |
|
679 |
|
|
680 |
if( pos<=mCount || mCount<=0.0f ) |
|
681 |
{ |
|
682 |
interpolate(buffer,offset, (float)(pos-(int)pos) ); |
|
683 |
|
|
684 |
if( time+step > mDuration*mCount && mCount>0.0f ) |
|
685 |
{ |
|
686 |
interpolate(buffer,offset,mCount-(int)mCount); |
|
687 |
return true; |
|
688 |
} |
|
689 |
} |
|
690 |
|
|
691 |
return false; |
|
692 |
} |
|
693 |
|
|
629 | 694 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
630 |
// end of DistortedInterpolator |
|
631 | 695 |
} |
Also available in: Unified diff
Dynamics: Introduce 2 Modes of operation:
- 'random access' mode, where we are able to call a single Dynamic from multiple thread simultaneously.
- 'sequential' mode, which only permits sequential interpolation from one client.
The second mode has an advantage when one needs to change mDuration: it keeps on interpolating smoothly. In the first mode, this is not possible.