samer@0: /* samer@0: * Copyright (c) 2001, 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: samer@0: import samer.core.*; samer@0: import samer.core.types.*; samer@0: import samer.core.util.*; samer@0: import samer.maths.*; samer@0: import samer.tools.*; samer@0: import java.util.*; samer@0: samer@0: public class OnsetMap extends Viewable implements Task, Observer { samer@0: VInteger I1, I2; samer@0: VMap vmap; samer@0: Matrix bins; samer@0: IMap map; samer@0: int i1, i2; samer@0: double[] xArray; samer@0: double[][] binArray; samer@0: VDouble L; samer@0: boolean ison; samer@0: int vel; samer@0: VDouble thresh; samer@0: samer@0: public OnsetMap(Vec x) samer@0: { samer@0: super("onsetmap"); samer@0: samer@0: xArray=x.array(); samer@0: if (xArray==null) throw new Error("vec array not accessible"); samer@0: samer@0: Shell.push(node); samer@0: samer@0: int M=Shell.getInt("bins",32); samer@0: int N=x.size(); samer@0: samer@0: I1=new VInteger("i1",1,0); samer@0: I2=new VInteger("i2",2,0); samer@0: I1.setRange(0,N-1); samer@0: I2.setRange(0,N-1); samer@0: samer@0: vmap=new VMap(new LinearMap(-1,1,M)); samer@0: map=vmap.getMap(); samer@0: samer@0: bins = new Matrix("bins",M,M,0); // no register samer@0: L = new VDouble("velocity"); samer@0: thresh = new VDouble("threshold",1,0); samer@0: samer@0: Shell.pop(); samer@0: samer@0: setAgent(vmap); samer@0: binArray=bins.getArray(); samer@0: I1.addObserver(this); samer@0: I2.addObserver(this); samer@0: vmap.addObserver(this); samer@0: Shell.registerViewable(this); samer@0: } samer@0: samer@0: public Matrix getBinMatrix() { return bins; } samer@0: public VDouble getVelocitySignal() { return L; } samer@0: public boolean isOnset() { return ison; } samer@0: public int getVelocity() { return vel; } samer@0: samer@0: public void starting() {} samer@0: public void stopping() {} samer@0: public void run() { samer@0: int j=map.clipInt(xArray[i1]); samer@0: int k=map.clipInt(xArray[i2]); samer@0: if (binArray[j][k]>thresh.value) { samer@0: ison=true; samer@0: vel=(int)(L.value=binArray[j][k]); samer@0: } else { samer@0: ison=false; samer@0: L.value=0; samer@0: } samer@0: L.changed(); samer@0: } samer@0: samer@0: public void dispose() { samer@0: I1.dispose(); samer@0: I2.dispose(); samer@0: bins.dispose(); samer@0: } samer@0: samer@0: public void update(Observable o, Object a) { samer@0: i1=I1.value; samer@0: i2=I2.value; samer@0: if (a==VMap.NEW_MAP) map=vmap.getMap(); samer@0: } samer@0: samer@0: public Viewer getViewer() samer@0: { samer@0: DefaultViewer vwr=new DefaultViewer(this); samer@0: samer@0: vwr.add(I1); samer@0: vwr.add(I2); samer@0: vwr.add(bins.viewable()); samer@0: vwr.add(L); samer@0: vwr.add(thresh); samer@0: samer@0: return vwr; samer@0: } samer@0: } samer@0: samer@0: