J021 Iterating samples
If you have a single signal source (IReadableSamples), its ok to iterate over the sample index (0..count-1). If you have multiple inputs you cant do. Using a sample iterator, you can easily iterate over all changes of all sources.
The interface ISamplesIterator works with sources of type ISamplePointer. Each pointer contains the current index position of a IReadableSamples source. A samples iterator is used to iterate over all changes that are found in a given set of sources:
- Return the information if there is a next or previous sample change available (hasNext(), hasPrev()).
- Step to the next or previous change (prev(), next()).
- Return the current domain position (current()).
- Fetch the domain information (start, end, domain base).
public interface ISamplesIterator extends Iterator<Long> { // step next/prev boolean hasNext(); boolean hasPrev(); Long prev(); Long next(); // current position long current(); DomainValue getCurrentPosition(); void setCurrent(long units, boolean iterationStart); // domain base/range IDomainBase getDomainBase(); long getStart(); long getEnd(); DomainValue getStartPosition(); DomainValue getEndPosition(); }Open JavaDoc Reference
Sample Pointer
A sample pointer object (ISamplePointer) is similar to a IReadableSamples object. The difference is that the object additionally contains the current index/position. Therefor instead of calling floatValueAt(int idx) to retrieve the float sample value at index idx, you just call floatValue() or val() for valueAt(int idx). Also you can retrieve member values as explained in the previous article.
public interface ISamplePointer extends IReadableSample, IReadableMembers, IPointer { ... // access current sample value Object val(); CompoundValue compound(); boolean isNone(); boolean isConflict(); Logic logicValue(); ... // access members of current sample value Object valueOf(Object member); Logic logicValueOf(Object member); int logicStateOf(Object member); Number numberValueOf(Object member); ... } Open JavaDoc Reference
Simple iteration
The example below is a typical way of iterating over multiple inputs. You will find it when adding a Signal Script production example (e.g. "Float Add"). The production creates the iterator and the input pointers automatically based on the plot configuration:
while ( iter.hasNext()) { var current <:Long:> = iter.next(out); out.write(current, false, input[0].floatValue() + input[1].floatValue()); }The hasNext () methods checks if there are additonal changes/samples available. The return value of next () contains the current domain position (e.g time).
With input [n]. floatValue () i can get the float value of the current sample.