view src/samer/units/OnsetMap.java @ 8:5e3cbbf173aa tip

Reorganise some more
author samer
date Fri, 05 Apr 2019 22:41:58 +0100
parents bf79fb79ee13
children
line wrap: on
line source
/*
 *	Copyright (c) 2001, Samer Abdallah, King's College London.
 *	All rights reserved.
 *
 *	This software is provided AS iS and WITHOUT ANY WARRANTY;
 *	without even the implied warranty of MERCHANTABILITY or
 *	FITNESS FOR A PARTICULAR PURPOSE.
 */

package samer.units;

import samer.core.*;
import samer.core.types.*;
import samer.core.util.*;
import samer.maths.*;
import samer.tools.*;
import java.util.*;

public class OnsetMap extends Viewable implements Task, Observer {
	VInteger		I1, I2;
	VMap			vmap;
	Matrix			bins;
	IMap			map;
	int				i1, i2;
	double[]		xArray;
	double[][]		binArray;
	VDouble			L;
	boolean		ison;
	int				vel;
	VDouble		thresh;

	public OnsetMap(Vec x)
	{
		super("onsetmap");

		xArray=x.array();
		if (xArray==null) throw new Error("vec array not accessible");

		Shell.push(node);

		int M=Shell.getInt("bins",32);
		int N=x.size();

		I1=new VInteger("i1",1,0);
		I2=new VInteger("i2",2,0);
		I1.setRange(0,N-1);
		I2.setRange(0,N-1);

		vmap=new VMap(new LinearMap(-1,1,M));
		map=vmap.getMap();

		bins = new Matrix("bins",M,M,0); // no register
		L = new VDouble("velocity");
		thresh = new VDouble("threshold",1,0);

		Shell.pop();

		setAgent(vmap);
		binArray=bins.getArray();
		I1.addObserver(this);
		I2.addObserver(this);
		vmap.addObserver(this);
		Shell.registerViewable(this);
	}

	public Matrix getBinMatrix() { return bins; }
	public VDouble getVelocitySignal() { return L; }
	public boolean isOnset() { return ison; }
	public int getVelocity() { return vel; }

	public void starting() {}
	public void stopping() {}
	public void run()	{
		int j=map.clipInt(xArray[i1]);
		int k=map.clipInt(xArray[i2]);
		if (binArray[j][k]>thresh.value) {
			ison=true;
			vel=(int)(L.value=binArray[j][k]);
		} else {
			ison=false;
			L.value=0;
		}
		L.changed();
	}

	public void dispose() {
		I1.dispose();
		I2.dispose();
		bins.dispose();
	}

	public void update(Observable o, Object a) {
		i1=I1.value;
		i2=I2.value;
		if (a==VMap.NEW_MAP) map=vmap.getMap();
	}

	public Viewer getViewer()
	{
		DefaultViewer vwr=new DefaultViewer(this);

		vwr.add(I1);
		vwr.add(I2);
		vwr.add(bins.viewable());
		vwr.add(L);
		vwr.add(thresh);

		return vwr;
	}
}