annotate src/samer/units/Trigger.java @ 8:5e3cbbf173aa tip

Reorganise some more
author samer
date Fri, 05 Apr 2019 22:41:58 +0100
parents bf79fb79ee13
children
rev   line source
samer@0 1 /*
samer@0 2 * Copyright (c) 2000, Samer Abdallah, King's College London.
samer@0 3 * All rights reserved.
samer@0 4 *
samer@0 5 * This software is provided AS iS and WITHOUT ANY WARRANTY;
samer@0 6 * without even the implied warranty of MERCHANTABILITY or
samer@0 7 * FITNESS FOR A PARTICULAR PURPOSE.
samer@0 8 */
samer@0 9
samer@0 10 package samer.units;
samer@0 11 import samer.tools.*;
samer@0 12 import samer.maths.*;
samer@0 13 import samer.core.types.*;
samer@0 14
samer@0 15 /**
samer@0 16 Generates a 1 for each element that rises above threshold,
samer@0 17 and a -1 for when it drops back below. Can be used to
samer@0 18 drive MidiSynth, which interprets +1 as note-on and -1
samer@0 19 as note-off.
samer@0 20 */
samer@0 21
samer@0 22 public class Trigger extends AnonymousTask
samer@0 23 {
samer@0 24 boolean [] states;
samer@0 25 VDouble thresh;
samer@0 26 VVector output;
samer@0 27 double[] in,out;
samer@0 28 int n;
samer@0 29
samer@0 30 public Trigger(VVector input, double th)
samer@0 31 {
samer@0 32 n = input.size();
samer@0 33 states = new boolean[n];
samer@0 34 in=input.array();
samer@0 35 thresh=new VDouble("thresh",th);
samer@0 36 output=new VVector("trigger",n);
samer@0 37 out=output.array();
samer@0 38 }
samer@0 39
samer@0 40 public VVector output() { return output; }
samer@0 41
samer@0 42 public void run()
samer@0 43 {
samer@0 44 double th=thresh.value;
samer@0 45
samer@0 46 for (int i=0; i<n; i++) {
samer@0 47 if (in[i]>th) {
samer@0 48 if (!states[i]) { out[i]=in[i]; states[i]=true; }
samer@0 49 else out[i]=0;
samer@0 50 } else {
samer@0 51 if (states[i]) { out[i]=-1; states[i]=false; }
samer@0 52 else out[i]=0;
samer@0 53 }
samer@0 54 }
samer@0 55 output.changed();
samer@0 56 }
samer@0 57 }