Skip to content

Commit

Permalink
optimized quarter sine a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
44100hertz committed Mar 6, 2023
1 parent 6aa6d2a commit 7cb65ee
Showing 1 changed file with 4 additions and 5 deletions.
9 changes: 4 additions & 5 deletions src/lib/audio/Voice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ class Oscillator {
}

getSample(): number {
const MAX_SINES = 200; // Maximum number of times to generate sine waves
const MAX_SINES = 60; // Maximum number of times to generate sine waves

this.phase += this.phaseadd;
const realPhaseAdd = Math.abs(this.phase - this.lastPhase);
Expand Down Expand Up @@ -159,17 +159,16 @@ class Oscillator {
// different phase relationships make this more useful.

// Generate absolute value sine
let absSine = 1/2;
for (let i=1; i < integerSines/2 && i < MAX_SINES/2; i++) {
let absSine = 0;
for (let i=1; i < integerSines && i < MAX_SINES/2; i++) {
absSine -= Math.cos(this.phase * i) / (i*i*4 - 1);
}
// Multiply by pulse wave to get pulsed sine
let square = 0;
for (let i=1; i < integerSines/2 && i < MAX_SINES/2; i++) {
square += Math.sin(this.phase * (i*2-1)) / (i*i);
}
out = absSine * (square / Math.PI * 2.0 + 0.5);
out = (out - 1/4) * 2.0;
out = (absSine + 0.5) * (square / Math.PI * 2.0 + 0.5) * 2 - 0.5;
} else if (this.wave === 'pulseSine') {
// Generate a square wave
for (let i=1; i < integerSines/2 && i < MAX_SINES; i++) {
Expand Down

0 comments on commit 7cb65ee

Please sign in to comment.