Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { Oscillator, OscillatorArgs, OscillatorMode } from "./Oscillator";
- export class PolyBLEPOscillator extends Oscillator {
- last_output: number = 0;
- constructor(oscillator_args: OscillatorArgs) {
- super(oscillator_args);
- this.update_increment();
- }
- poly_blep(t: number) {
- const dt = this.phase_increment / (2 * Math.PI);
- if (t < dt) {
- t /= dt;
- return t + t - t * t - 1;
- } else if (t > 1 - dt) {
- t = (t - 1) / dt;
- return t + t - t * t - 1;
- } else {
- return 0;
- }
- }
- next_sample(): number {
- let value = 0;
- const t = this.phase / (2 * Math.PI);
- if (this.oscillator_mode == OscillatorMode.OSCILLATOR_MODE_SINE) {
- value = this.naive_wave_form(OscillatorMode.OSCILLATOR_MODE_SINE);
- } else if (this.oscillator_mode == OscillatorMode.OSCILLATOR_MODE_SAW) {
- value = this.naive_wave_form(OscillatorMode.OSCILLATOR_MODE_SAW);
- value -= this.poly_blep(t);
- } else {
- value = this.naive_wave_form(OscillatorMode.OSCILLATOR_MODE_SQUARE);
- value += this.poly_blep(t);
- value -= this.poly_blep((t + 0.5) % 1);
- if (this.oscillator_mode == OscillatorMode.OSCILLATOR_MODE_TRIANGLE) {
- value =
- this.phase_increment * value +
- (1 - this.phase_increment) * this.last_output;
- this.last_output = value;
- } else {
- value =
- this.naive_wave_form(OscillatorMode.OSCILLATOR_MODE_SQUARE) - value;
- }
- }
- this.phase += this.phase_increment;
- while (this.phase >= 2 * Math.PI) {
- this.phase -= 2 * Math.PI;
- }
- return value;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment