OscNode.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import Node, { addNodeClass } from '../core/Node.js';
  2. import { timerLocal } from './TimerNode.js';
  3. import { nodeObject, nodeProxy } from '../shadernode/ShaderNode.js';
  4. class OscNode extends Node {
  5. constructor( method = OscNode.SINE, timeNode = timerLocal() ) {
  6. super();
  7. this.method = method;
  8. this.timeNode = timeNode;
  9. }
  10. getNodeType( builder ) {
  11. return this.timeNode.getNodeType( builder );
  12. }
  13. setup() {
  14. const method = this.method;
  15. const timeNode = nodeObject( this.timeNode );
  16. let outputNode = null;
  17. if ( method === OscNode.SINE ) {
  18. outputNode = timeNode.add( 0.75 ).mul( Math.PI * 2 ).sin().mul( 0.5 ).add( 0.5 );
  19. } else if ( method === OscNode.SQUARE ) {
  20. outputNode = timeNode.fract().round();
  21. } else if ( method === OscNode.TRIANGLE ) {
  22. outputNode = timeNode.add( 0.5 ).fract().mul( 2 ).sub( 1 ).abs();
  23. } else if ( method === OscNode.SAWTOOTH ) {
  24. outputNode = timeNode.fract();
  25. }
  26. return outputNode;
  27. }
  28. serialize( data ) {
  29. super.serialize( data );
  30. data.method = this.method;
  31. }
  32. deserialize( data ) {
  33. super.deserialize( data );
  34. this.method = data.method;
  35. }
  36. }
  37. OscNode.SINE = 'sine';
  38. OscNode.SQUARE = 'square';
  39. OscNode.TRIANGLE = 'triangle';
  40. OscNode.SAWTOOTH = 'sawtooth';
  41. export default OscNode;
  42. export const oscSine = nodeProxy( OscNode, OscNode.SINE );
  43. export const oscSquare = nodeProxy( OscNode, OscNode.SQUARE );
  44. export const oscTriangle = nodeProxy( OscNode, OscNode.TRIANGLE );
  45. export const oscSawtooth = nodeProxy( OscNode, OscNode.SAWTOOTH );
  46. addNodeClass( 'OscNode', OscNode );