tuning-core 1.0.3 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -95,6 +95,15 @@ export declare class IntervalSet {
95
95
  */
96
96
  toSweepIntervals(): Fraction[];
97
97
  minMax(min: FractionInput, max: FractionInput): this;
98
+ /**
99
+ * Fill gaps between consecutive intervals by adding new intervals per gap,
100
+ * equally spaced in logarithmic (ratio) space. Adds at least 3 new intervals per gap.
101
+ * When maxGapCents is provided, subdivides further so each part is ≤ maxGapCents.
102
+ *
103
+ * @param maxGapCents - Optional maximum gap in cents. When set, each subdivision is ≤ this value.
104
+ * @returns this (for chaining)
105
+ */
106
+ interpolateLog(maxGapCents?: number): this;
98
107
  /**
99
108
  * Generate all unique ratios between min and max with denominators up to maxDenominator.
100
109
  *
@@ -182,6 +182,33 @@ export class IntervalSet {
182
182
  }
183
183
  return this;
184
184
  }
185
+ /**
186
+ * Fill gaps between consecutive intervals by adding new intervals per gap,
187
+ * equally spaced in logarithmic (ratio) space. Adds at least 3 new intervals per gap.
188
+ * When maxGapCents is provided, subdivides further so each part is ≤ maxGapCents.
189
+ *
190
+ * @param maxGapCents - Optional maximum gap in cents. When set, each subdivision is ≤ this value.
191
+ * @returns this (for chaining)
192
+ */
193
+ interpolateLog(maxGapCents) {
194
+ if (maxGapCents !== undefined && maxGapCents <= 0) {
195
+ throw new Error("maxGapCents must be a positive number");
196
+ }
197
+ const ratios = this.getRatios();
198
+ for (let i = 0; i < ratios.length - 1; i++) {
199
+ const left = ratios[i].valueOf();
200
+ const right = ratios[i + 1].valueOf();
201
+ const gapRatio = right / left;
202
+ const gapCents = 1200 * Math.log2(gapRatio);
203
+ const n = Math.max(4, Math.ceil(gapCents / (maxGapCents ?? gapCents)));
204
+ for (let k = 1; k < n; k++) {
205
+ const num = left * Math.pow(gapRatio, k / n);
206
+ const rounded = Math.round(num * 10000) / 10000;
207
+ this.add(rounded);
208
+ }
209
+ }
210
+ return this;
211
+ }
185
212
  /**
186
213
  * Generate all unique ratios between min and max with denominators up to maxDenominator.
187
214
  *
@@ -4,14 +4,14 @@ import type { HarmonicData } from './types';
4
4
  * Convert a frequency ratio to cents.
5
5
  * @param ratio - The ratio as FractionInput (number, string, array, object, or Fraction)
6
6
  * @param places - Number of decimal places to round to (default: 0, no decimals)
7
- * @returns The ratio in cents (1200 * log2(ratio)), rounded to the specified number of decimals
7
+ * @returns The ratio in cents (1200 * log2(ratio)), rounded to the specified number of decimals (default 0, no decimals 3/2 = 702 cents)
8
8
  */
9
9
  export declare function ratioToCents(ratio: FractionInput, places?: number): Fraction;
10
10
  /**
11
11
  * Convert cents to a frequency ratio.
12
12
  * @param cents - The interval in cents as FractionInput (number, string, array, object, or Fraction)
13
13
  * @param places - Number of decimal places to round to (default: 0, no decimals)
14
- * @returns The ratio as a Fraction object (2^(cents/1200)), rounded to the specified number of decimals
14
+ * @returns The ratio as a Fraction object (2^(cents/1200)), rounded to the specified number of decimals before contertion to fraction (default 3 decimals, 702 cents = 3/2)
15
15
  */
16
16
  export declare function centsToRatio(cents: FractionInput, places?: number): Fraction;
17
17
  /**
package/dist/lib/utils.js CHANGED
@@ -6,7 +6,7 @@ function round(num, places = 0) {
6
6
  * Convert a frequency ratio to cents.
7
7
  * @param ratio - The ratio as FractionInput (number, string, array, object, or Fraction)
8
8
  * @param places - Number of decimal places to round to (default: 0, no decimals)
9
- * @returns The ratio in cents (1200 * log2(ratio)), rounded to the specified number of decimals
9
+ * @returns The ratio in cents (1200 * log2(ratio)), rounded to the specified number of decimals (default 0, no decimals 3/2 = 702 cents)
10
10
  */
11
11
  export function ratioToCents(ratio, places = 0) {
12
12
  const val = new Fraction(ratio).valueOf();
@@ -18,9 +18,9 @@ export function ratioToCents(ratio, places = 0) {
18
18
  * Convert cents to a frequency ratio.
19
19
  * @param cents - The interval in cents as FractionInput (number, string, array, object, or Fraction)
20
20
  * @param places - Number of decimal places to round to (default: 0, no decimals)
21
- * @returns The ratio as a Fraction object (2^(cents/1200)), rounded to the specified number of decimals
21
+ * @returns The ratio as a Fraction object (2^(cents/1200)), rounded to the specified number of decimals before contertion to fraction (default 3 decimals, 702 cents = 3/2)
22
22
  */
23
- export function centsToRatio(cents, places = 0) {
23
+ export function centsToRatio(cents, places = 3) {
24
24
  const val = new Fraction(cents).valueOf();
25
25
  const num = Math.pow(2, val / 1200);
26
26
  const rounded = round(num, places);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tuning-core",
3
- "version": "1.0.3",
3
+ "version": "1.2.0",
4
4
  "type": "module",
5
5
  "module": "dist/index.js",
6
6
  "main": "dist/index.js",