You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
58 lines
1.1 KiB
58 lines
1.1 KiB
import Node from '../core/Node.js';
|
|
import TimerNode from './TimerNode.js';
|
|
import { abs, fract, round, sin, add, sub, mul, PI2 } from '../ShaderNode.js';
|
|
|
|
class OscNode extends Node {
|
|
|
|
static SINE = 'sine';
|
|
static SQUARE = 'square';
|
|
static TRIANGLE = 'triangle';
|
|
static SAWTOOTH = 'sawtooth';
|
|
|
|
constructor( method = OscNode.SINE, timeNode = new TimerNode() ) {
|
|
|
|
super();
|
|
|
|
this.method = method;
|
|
this.timeNode = timeNode;
|
|
|
|
}
|
|
|
|
getNodeType( builder ) {
|
|
|
|
return this.timeNode.getNodeType( builder );
|
|
|
|
}
|
|
|
|
generate( builder ) {
|
|
|
|
const method = this.method;
|
|
const timeNode = this.timeNode;
|
|
|
|
let outputNode = null;
|
|
|
|
if ( method === OscNode.SINE ) {
|
|
|
|
outputNode = add( mul( sin( mul( add( timeNode, .75 ), PI2 ) ), .5 ), .5 );
|
|
|
|
} else if ( method === OscNode.SQUARE ) {
|
|
|
|
outputNode = round( fract( timeNode ) );
|
|
|
|
} else if ( method === OscNode.TRIANGLE ) {
|
|
|
|
outputNode = abs( sub( 1, mul( fract( add( timeNode, .5 ) ), 2 ) ) );
|
|
|
|
} else if ( method === OscNode.SAWTOOTH ) {
|
|
|
|
outputNode = fract( timeNode );
|
|
|
|
}
|
|
|
|
return outputNode.build( builder );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
export default OscNode;
|
|
|