Skip to content

Commit

Permalink
Add scaling to LFOs
Browse files Browse the repository at this point in the history
  • Loading branch information
Bubobubobubobubo committed Oct 14, 2023
1 parent 356e3c8 commit a7a38a4
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 27 deletions.
34 changes: 18 additions & 16 deletions src/API.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1558,7 +1558,7 @@ export class UserAPI {
return result;
};

sine = (freq: number = 1, offset: number = 0): number => {
sine = (freq: number = 1, times: number = 1, offset: number = 0): number => {
/**
* Returns a sine wave between -1 and 1.
*
Expand All @@ -1567,11 +1567,11 @@ export class UserAPI {
* @returns A sine wave between -1 and 1
*/
return (
Math.sin(this.app.clock.ctx.currentTime * Math.PI * 2 * freq) + offset
(Math.sin(this.app.clock.ctx.currentTime * Math.PI * 2 * freq) + offset) * times
);
};

usine = (freq: number = 1, offset: number = 0): number => {
usine = (freq: number = 1, times: number = 1, offset: number = 0): number => {
/**
* Returns a sine wave between 0 and 1.
*
Expand All @@ -1580,10 +1580,10 @@ export class UserAPI {
* @returns A sine wave between 0 and 1
* @see sine
*/
return (this.sine(freq, offset) + 1) / 2;
return ((this.sine(freq, offset) + 1) / 2) * times
};

saw = (freq: number = 1, offset: number = 0): number => {
saw = (freq: number = 1, times: number = 1, offset: number = 0): number => {
/**
* Returns a saw wave between -1 and 1.
*
Expand All @@ -1595,10 +1595,10 @@ export class UserAPI {
* @see sine
* @see noise
*/
return ((this.app.clock.ctx.currentTime * freq) % 1) * 2 - 1 + offset;
return (((this.app.clock.ctx.currentTime * freq) % 1) * 2 - 1 + offset) * times;
};

usaw = (freq: number = 1, offset: number = 0): number => {
usaw = (freq: number = 1, times: number = 1, offset: number = 0): number => {
/**
* Returns a saw wave between 0 and 1.
*
Expand All @@ -1607,10 +1607,10 @@ export class UserAPI {
* @returns A saw wave between 0 and 1
* @see saw
*/
return (this.saw(freq, offset) + 1) / 2;
return ((this.saw(freq, offset) + 1) / 2) * times;
};

triangle = (freq: number = 1, offset: number = 0): number => {
triangle = (freq: number = 1, times: number = 1, offset: number = 0): number => {
/**
* Returns a triangle wave between -1 and 1.
*
Expand All @@ -1620,10 +1620,10 @@ export class UserAPI {
* @see sine
* @see noise
*/
return Math.abs(this.saw(freq, offset)) * 2 - 1;
return (Math.abs(this.saw(freq, offset)) * 2 - 1) * times;
};

utriangle = (freq: number = 1, offset: number = 0): number => {
utriangle = (freq: number = 1, times: number = 1, offset: number = 0): number => {
/**
* Returns a triangle wave between 0 and 1.
*
Expand All @@ -1632,11 +1632,12 @@ export class UserAPI {
* @returns A triangle wave between 0 and 1
* @see triangle
*/
return (this.triangle(freq, offset) + 1) / 2;
return ((this.triangle(freq, offset) + 1) / 2) * times;
};

square = (
freq: number = 1,
times: number = 1,
offset: number = 0,
duty: number = 0.5
): number => {
Expand All @@ -1651,11 +1652,12 @@ export class UserAPI {
*/
const period = 1 / freq;
const t = (Date.now() / 1000 + offset) % period;
return t / period < duty ? 1 : -1;
return (t / period < duty ? 1 : -1) * times;
};

usquare = (
freq: number = 1,
times: number = 1,
offset: number = 0,
duty: number = 0.5
): number => {
Expand All @@ -1667,10 +1669,10 @@ export class UserAPI {
* @returns A square wave between 0 and 1
* @see square
*/
return (this.square(freq, offset, duty) + 1) / 2;
return ((this.square(freq, offset, duty) + 1) / 2) * times;
};

noise = (): number => {
noise = (times: number = 1): number => {
/**
* Returns a random value between -1 and 1.
*
Expand All @@ -1681,7 +1683,7 @@ export class UserAPI {
* @see sine
* @see noise
*/
return this.randomGen() * 2 - 1;
return (this.randomGen() * 2 - 1) * times;
};

// =============================================================
Expand Down
23 changes: 12 additions & 11 deletions src/documentation/lfos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,43 +8,46 @@ export const lfos = (application: Editor): string => {
Low Frequency Oscillators (_LFOs_) are an important piece in any digital audio workstation or synthesizer. Topos implements some basic waveforms you can play with to automatically modulate your paremeters.
- <ic>sine(freq: number = 1, offset: number= 0): number</ic>: returns a sinusoïdal oscillation between <ic>-1</ic> and <ic>1</ic>.
- <ic>usine(freq: number = 1, offset: number= 0): number</ic>: returns a sinusoïdal oscillation between <ic>0</ic> and <ic>1</ic>. The <ic>u</ic> stands for _unipolar_.
- <ic>sine(freq: number = 1, times: number = 1, offset: number= 0): number</ic>: returns a sinusoïdal oscillation between <ic>-1</ic> and <ic>1</ic>.
- <ic>freq</ic> : frequency in hertz.
- <ic>times</ic> : output value multiplier.
- <ic>offset</ic>: linear offset.
- <ic>usine(freq: number = 1, times: number = 1, offset: number= 0): number</ic>: returns a sinusoïdal oscillation between <ic>0</ic> and <ic>1</ic>. The <ic>u</ic> stands for _unipolar_.
${makeExample(
"Modulating the speed of a sample player using a sine LFO",
`beat(.25) && snd('cp').speed(1 + usine(0.25) * 2).out()`,
true
)};
- <ic>triangle(freq: number = 1, offset: number= 0): number</ic>: returns a triangle oscillation between <ic>-1</ic> and <ic>1</ic>.
- <ic>utriangle(freq: number = 1, offset: number= 0): number</ic>: returns a triangle oscillation between <ic>0</ic> and <ic>1</ic>. The <ic>u</ic> stands for _unipolar_.
- <ic>triangle(freq: number = 1, times: number = 1, offset: number= 0): number</ic>: returns a triangle oscillation between <ic>-1</ic> and <ic>1</ic>.
- <ic>utriangle(freq: number = 1, times: number = 1, offset: number= 0): number</ic>: returns a triangle oscillation between <ic>0</ic> and <ic>1</ic>. The <ic>u</ic> stands for _unipolar_.
${makeExample(
"Modulating the speed of a sample player using a triangle LFO",
`beat(.25) && snd('cp').speed(1 + utriangle(0.25) * 2).out()`,
true
)}
- <ic>saw(freq: number = 1, offset: number= 0): number</ic>: returns a sawtooth-like oscillation between <ic>-1</ic> and <ic>1</ic>.
- <ic>usaw(freq: number = 1, offset: number= 0): number</ic>: returns a sawtooth-like oscillation between <ic>0</ic> and <ic>1</ic>. The <ic>u</ic> stands for _unipolar_.
- <ic>saw(freq: number = 1, times: number = 1, offset: number= 0): number</ic>: returns a sawtooth-like oscillation between <ic>-1</ic> and <ic>1</ic>.
- <ic>usaw(freq: number = 1, times: number = 1, offset: number= 0): number</ic>: returns a sawtooth-like oscillation between <ic>0</ic> and <ic>1</ic>. The <ic>u</ic> stands for _unipolar_.
${makeExample(
"Modulating the speed of a sample player using a saw LFO",
`beat(.25) && snd('cp').speed(1 + usaw(0.25) * 2).out()`,
true
)}
- <ic>square(freq: number = 1, offset: number= 0, duty: number = .5): number</ic>: returns a square wave oscillation between <ic>-1</ic> and <ic>1</ic>. You can also control the duty cycle using the <ic>duty</ic> parameter.
- <ic>usquare(freq: number = 1, offset: number= 0, duty: number = .5): number</ic>: returns a square wave oscillation between <ic>0</ic> and <ic>1</ic>. The <ic>u</ic> stands for _unipolar_. You can also control the duty cycle using the <ic>duty</ic> parameter.
- <ic>square(freq: number = 1, times: number = 1, offset: number= 0, duty: number = .5): number</ic>: returns a square wave oscillation between <ic>-1</ic> and <ic>1</ic>. You can also control the duty cycle using the <ic>duty</ic> parameter.
- <ic>usquare(freq: number = 1, times: number = 1, offset: number= 0, duty: number = .5): number</ic>: returns a square wave oscillation between <ic>0</ic> and <ic>1</ic>. The <ic>u</ic> stands for _unipolar_. You can also control the duty cycle using the <ic>duty</ic> parameter.
${makeExample(
"Modulating the speed of a sample player using a square LFO",
`beat(.25) && snd('cp').speed(1 + usquare(0.25, 0, 0.25) * 2).out()`,
true
)};
- <ic>noise()</ic>: returns a random value between -1 and 1.
- <ic>noise(times: number = 1)</ic>: returns a random value between -1 and 1.
${makeExample(
"Modulating the speed of a sample player using noise",
Expand All @@ -54,5 +57,3 @@ ${makeExample(
`
}


0 comments on commit a7a38a4

Please sign in to comment.