samer@0: /* samer@0: * Copyright (c) 2000, Samer Abdallah, King's College London. samer@0: * All rights reserved. samer@0: * samer@0: * This software is provided AS iS and WITHOUT ANY WARRANTY; samer@0: * without even the implied warranty of MERCHANTABILITY or samer@0: * FITNESS FOR A PARTICULAR PURPOSE. samer@0: */ samer@0: samer@0: package samer.units; samer@0: import samer.tools.*; samer@0: import samer.maths.*; samer@0: import samer.core.types.*; samer@0: samer@0: /** samer@0: Generates a 1 for each element that rises above threshold, samer@0: and a -1 for when it drops back below. Can be used to samer@0: drive MidiSynth, which interprets +1 as note-on and -1 samer@0: as note-off. samer@0: */ samer@0: samer@0: public class Trigger extends AnonymousTask samer@0: { samer@0: boolean [] states; samer@0: VDouble thresh; samer@0: VVector output; samer@0: double[] in,out; samer@0: int n; samer@0: samer@0: public Trigger(VVector input, double th) samer@0: { samer@0: n = input.size(); samer@0: states = new boolean[n]; samer@0: in=input.array(); samer@0: thresh=new VDouble("thresh",th); samer@0: output=new VVector("trigger",n); samer@0: out=output.array(); samer@0: } samer@0: samer@0: public VVector output() { return output; } samer@0: samer@0: public void run() samer@0: { samer@0: double th=thresh.value; samer@0: samer@0: for (int i=0; ith) { samer@0: if (!states[i]) { out[i]=in[i]; states[i]=true; } samer@0: else out[i]=0; samer@0: } else { samer@0: if (states[i]) { out[i]=-1; states[i]=false; } samer@0: else out[i]=0; samer@0: } samer@0: } samer@0: output.changed(); samer@0: } samer@0: }