package de.maramuse.soundcomp.generator;
/*
* Copyright 2010 Jan Schmidt-Reinisch
*
* SoundComp - a sound processing library
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; in version 2.1
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* A saw tooth generator generating asymmetric sawtooth/triangle signals.
* The asymmetry (duty cycle) can be changed.
* Valid duty cycle values are [0,1] - excess values will result in the same output as the respective
* limit, i.e. sawtooth output.
* Additionally, it can be phase modulated and synchronized as the UnitySawtooth.
*/
import java.util.Map;
import java.util.TreeMap;
import de.maramuse.soundcomp.process.NamedSource;
import de.maramuse.soundcomp.process.ParameterMap;
import de.maramuse.soundcomp.process.ProcessElement;
import de.maramuse.soundcomp.process.SourceStore;
import de.maramuse.soundcomp.process.StandardParameters;
import de.maramuse.soundcomp.process.TypeMismatchException;
import de.maramuse.soundcomp.process.UnknownConnectionException;
import de.maramuse.soundcomp.process.ValueType;
import de.maramuse.soundcomp.process.StandardParameters.Parameter;
import de.maramuse.soundcomp.util.NativeObjects;
import de.maramuse.soundcomp.util.ReadOnlyMap;
public class SawTooth extends UnitySawTooth {
public SawTooth() {
NativeObjects.registerNativeObject(this);
}
SawTooth(boolean s) {
}
private static final Map<Integer, ValueType> destTypeMap=new TreeMap<Integer, ValueType>();
static{
destTypeMap.put(StandardParameters.FREQUENCY.i, ValueType.STREAM);
destTypeMap.put(StandardParameters.DUTYCYCLE.i, ValueType.STREAM);
destTypeMap.put(StandardParameters.SYNC.i, ValueType.STREAM);
destTypeMap.put(StandardParameters.SYNCPHASE.i, ValueType.STREAM);
destTypeMap.put(StandardParameters.PHASEOFFSET.i, ValueType.STREAM);
}
protected static ParameterMap inputsMap=new ParameterMap(); // protected as all descendend classes might want to inherit the same set of inputs
private static ParameterMap outputsMap=new ParameterMap();
static{
inputsMap.put(StandardParameters.FREQUENCY);
inputsMap.put(StandardParameters.SYNC);
inputsMap.put(StandardParameters.SYNCPHASE);
inputsMap.put(StandardParameters.DUTYCYCLE);
inputsMap.put(StandardParameters.PHASEOFFSET);
outputsMap.put(StandardParameters.OUT);
}
protected double dutycycle=0;
protected double phaseoffset=0;
protected double effectivePhase;
protected SourceStore dc=null;
protected SourceStore po=null;
@Override
public void setSource(int connection, NamedSource source,
int sourceIndex) throws UnknownConnectionException,
TypeMismatchException {
if(connection==StandardParameters.DUTYCYCLE.i){
if(source.getSourceTypes().containsKey(sourceIndex)){
dc=new SourceStore(source, sourceIndex);
}else
throw new UnknownConnectionException("source for SawTooth dutycycle unknown");
}else if(connection==StandardParameters.PHASEOFFSET.i){
if(source.getSourceTypes().containsKey(sourceIndex)){
po=new SourceStore(source, sourceIndex);
}else
throw new UnknownConnectionException("source for SawTooth phaseoffset unknown");
}else
super.setSource(connection, source, sourceIndex);
}
@Override
public void advanceState() {
super.advanceState();
if(dc!=null)
dutycycle=dc.getValue();
if(po!=null)
phaseoffset=po.getValue();
effectivePhase=phase+phaseoffset-Math.floor(phase+phaseoffset);
_val=(effectivePhase<dutycycle ? (2*effectivePhase/dutycycle-1) : (2*(1-effectivePhase)/(1-dutycycle)-1));
}
/**
* @see ProcessElement#clone()
*/
@Override
public SawTooth clone() {
return new SawTooth();
}
/*
* (non-Javadoc)
*
* @see de.maramuse.soundcomp.process.ProcessElement#inputsByName()
*/
@Override
public ReadOnlyMap<String, Parameter> inputsByName() {
return inputsMap;
}
/*
* (non-Javadoc)
*
* @see de.maramuse.soundcomp.process.ProcessElement#outputsByName()
*/
@Override
public ReadOnlyMap<String, Parameter> outputsByName() {
return outputsMap;
}
}