Guest User

PolyBLEPOscillator.ts

a guest
Nov 12th, 2025
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { Oscillator, OscillatorArgs, OscillatorMode } from "./Oscillator";
  2.  
  3. export class PolyBLEPOscillator extends Oscillator {
  4.   last_output: number = 0;
  5.   constructor(oscillator_args: OscillatorArgs) {
  6.     super(oscillator_args);
  7.     this.update_increment();
  8.   }
  9.   poly_blep(t: number) {
  10.     const dt = this.phase_increment / (2 * Math.PI);
  11.     if (t < dt) {
  12.       t /= dt;
  13.       return t + t - t * t - 1;
  14.     } else if (t > 1 - dt) {
  15.       t = (t - 1) / dt;
  16.       return t + t - t * t - 1;
  17.     } else {
  18.       return 0;
  19.     }
  20.   }
  21.   next_sample(): number {
  22.     let value = 0;
  23.     const t = this.phase / (2 * Math.PI);
  24.     if (this.oscillator_mode == OscillatorMode.OSCILLATOR_MODE_SINE) {
  25.       value = this.naive_wave_form(OscillatorMode.OSCILLATOR_MODE_SINE);
  26.     } else if (this.oscillator_mode == OscillatorMode.OSCILLATOR_MODE_SAW) {
  27.       value = this.naive_wave_form(OscillatorMode.OSCILLATOR_MODE_SAW);
  28.       value -= this.poly_blep(t);
  29.     } else {
  30.       value = this.naive_wave_form(OscillatorMode.OSCILLATOR_MODE_SQUARE);
  31.       value += this.poly_blep(t);
  32.       value -= this.poly_blep((t + 0.5) % 1);
  33.       if (this.oscillator_mode == OscillatorMode.OSCILLATOR_MODE_TRIANGLE) {
  34.         value =
  35.           this.phase_increment * value +
  36.           (1 - this.phase_increment) * this.last_output;
  37.         this.last_output = value;
  38.       } else {
  39.         value =
  40.           this.naive_wave_form(OscillatorMode.OSCILLATOR_MODE_SQUARE) - value;
  41.       }
  42.     }
  43.     this.phase += this.phase_increment;
  44.     while (this.phase >= 2 * Math.PI) {
  45.       this.phase -= 2 * Math.PI;
  46.     }
  47.     return value;
  48.   }
  49. }
  50.  
Advertisement
Add Comment
Please, Sign In to add comment