Source: common/operator/Multiplier.js

  1. import BaseLfo from '../../core/BaseLfo';
  2. const definitions = {
  3. // float or array
  4. factor: {
  5. type: 'any',
  6. default: 1,
  7. }
  8. };
  9. /**
  10. * Multiply a given signal or vector by a given factor. On vector
  11. * streams, `factor` can be an array of values to apply on each dimension of the
  12. * vector frames.
  13. *
  14. * _support `standalone` usage_
  15. *
  16. * @param {Object} options - override default values
  17. * @param {Number|Array} [options.factor=1] - factor or array of factor to
  18. * apply on the incomming frame. Setting an array is only defined in case of
  19. * a vector stream.
  20. *
  21. * @memberof module:common.operator
  22. *
  23. * @example
  24. * import * as lfo from 'waves-lfo/common';
  25. *
  26. * const eventIn = new lfo.operator.EventIn({
  27. * type: 'vector',
  28. * frameSize: 2,
  29. * frameRate: 0,
  30. * });
  31. * const scaler = new lfo.operator.Multiplier({ factor: 0.1 });
  32. *
  33. * eventIn.connect(scaler);
  34. *
  35. * eventIn.process(null, [2, 3]);
  36. * > [0.2, 0.3]
  37. */
  38. class Multiplier extends BaseLfo {
  39. constructor(options) {
  40. super(definitions, options);
  41. }
  42. /**
  43. * Use the `Multiplier` operator in standalone mode.
  44. *
  45. * @param {Float32Array|Array} data - Input vector
  46. * @return {Array} - Scaled values
  47. *
  48. * @example
  49. * const scaler = new Multiplier({ factor: [2, 4] });
  50. * scaler.initStream({ frameType: 'vector', frameSize: 2 });
  51. *
  52. * scaler.inputVector([3, 2]);
  53. * > [6, 8]
  54. */
  55. inputVector(data) {
  56. const output = this.frame.data;
  57. const frameSize = this.streamParams.frameSize;
  58. const factor = this.params.get('factor');
  59. if (Array.isArray(factor)) {
  60. for (let i = 0; i < frameSize; i++)
  61. output[i] = data[i] * factor[i];
  62. } else {
  63. for (let i = 0; i < frameSize; i++)
  64. output[i] = data[i] * factor;
  65. }
  66. return output;
  67. }
  68. /** @private */
  69. processVector(frame) {
  70. this.frame.data = this.inputVector(frame.data);
  71. }
  72. /**
  73. * Use the `Multiplier` operator in standalone mode.
  74. *
  75. * @param {Float32Array|Array} data - Input signal.
  76. * @return {Array} - Scaled signal.
  77. *
  78. * @example
  79. * const scaler = new Multiplier({ factor: 0.1 });
  80. * scaler.initStream({ frameType: 'signal', frameSize: 2 });
  81. *
  82. * scaler.inputVector([1, 2]);
  83. * > [0.1, 0.2]
  84. */
  85. inputSignal(data) {
  86. const output = this.frame.data;
  87. const frameSize = this.streamParams.frameSize;
  88. const factor = this.params.get('factor');
  89. for (let i = 0; i < frameSize; i++)
  90. output[i] = data[i] * factor;
  91. return output;
  92. }
  93. /** @private */
  94. processSignal(frame) {
  95. this.frame.data = this.inputSignal(frame.data);
  96. }
  97. }
  98. export default Multiplier;