Skip to content

Commit

Permalink
fix: broken log scaling
Browse files Browse the repository at this point in the history
  • Loading branch information
eye-wave committed Oct 23, 2024
1 parent d33a762 commit 437fc73
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "svelte-knobs",
"description": "Svelte component library for building customizable knob controls.",
"version": "0.1.0",
"version": "0.1.1",
"repository": {
"url": "https://github.com/eye-wave/svelte-knobs"
},
Expand Down
17 changes: 10 additions & 7 deletions src/lib/params/float-param.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,8 @@ export function unnormalize(value: number, param: FloatParam): number {
}
}

function log(value: number, base = 10): number {
if (value === 0) return 0;

const x = value < 0 ? -value : value;
export function log(value: number, base = 10): number {
const x = Math.abs(value) + 1;
const sign = Math.sign(value);

if (base === 10) return sign * Math.log10(x);
Expand All @@ -58,8 +56,13 @@ function log(value: number, base = 10): number {
return sign * (Math.log(x) / Math.log(base));
}

function exp(value: number, base = 10): number {
const x = value < 0 ? -value : value;
export function exp(value: number, base = 10): number {
const sign = Math.sign(value);
const expValue = Math.abs(value);

if (base === 10) return sign * (Math.pow(10, expValue) - 1);
if (base === 2) return sign * (Math.pow(2, expValue) - 1);
if (base === Math.E) return sign * (Math.exp(expValue) - 1);

return Math.pow(base, x) * Math.sign(value);
return sign * (Math.pow(base, expValue) - 1);
}
3 changes: 3 additions & 0 deletions src/routes/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
const basicParam = createFloatParam(createRange('lin', 0, 100));
const freqParam = createFloatParam(createRange('log', 20, 20_000));
const gainParam = createFloatParam(createRange('log', -30, 30, Math.E));
const qParam = createFloatParam(createRange('log', 0.01, 30, 2));
const smoothParam = createFloatParam(createRange('lin', 0, 100));
const snapParam = createFloatParam(createRange('lin', 0, 2000));
const enumParam = createEnumParam(['🍍', '🍉', '🍌', '🍎', '🥭', '🍇', '🥝', '🍋'] as const);
Expand All @@ -23,6 +24,7 @@
let basicValue = 0;
let freqValue = 20;
let gainValue = 20;
let qValue = 0;
let smoothValue = 0;
let snapValue = 0;
let enumValue: Variant<typeof enumParam> = '🍎';
Expand All @@ -44,6 +46,7 @@
<h2>Logarithmic</h2>
<Knob param={freqParam} bind:value={freqValue} label="Frequency" unit="hz" />
<Knob param={gainParam} bind:value={gainValue} label="Gain" unit="dB" />
<Knob param={qParam} bind:value={qValue} label="Q" unit="dB" decimalDigits={2} />

<p>A knob with logarithmic scaling (default base is 10).</p>
</div>
Expand Down

0 comments on commit 437fc73

Please sign in to comment.