OscNode.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import Node from '../core/Node.js';
  2. import TimerNode from './TimerNode.js';
  3. import { abs, fract, round, sin, add, sub, mul, PI2 } from '../ShaderNode.js';
  4. class OscNode extends Node {
  5. static SINE = 'sine';
  6. static SQUARE = 'square';
  7. static TRIANGLE = 'triangle';
  8. static SAWTOOTH = 'sawtooth';
  9. constructor( method = OscNode.SINE, timeNode = new TimerNode() ) {
  10. super();
  11. this.method = method;
  12. this.timeNode = timeNode;
  13. }
  14. getNodeType( builder ) {
  15. return this.timeNode.getNodeType( builder );
  16. }
  17. generate( builder ) {
  18. const method = this.method;
  19. const timeNode = this.timeNode;
  20. let outputNode = null;
  21. if ( method === OscNode.SINE ) {
  22. outputNode = add( mul( sin( mul( add( timeNode, .75 ), PI2 ) ), .5 ), .5 );
  23. } else if ( method === OscNode.SQUARE ) {
  24. outputNode = round( fract( timeNode ) );
  25. } else if ( method === OscNode.TRIANGLE ) {
  26. outputNode = abs( sub( 1, mul( fract( add( timeNode, .5 ) ), 2 ) ) );
  27. } else if ( method === OscNode.SAWTOOTH ) {
  28. outputNode = fract( timeNode );
  29. }
  30. return outputNode.build( builder );
  31. }
  32. }
  33. export default OscNode;