Skip to content

Commit

Permalink
Merge pull request #395 from xenharmonic-devs/fix-korg-limit
Browse files Browse the repository at this point in the history
Remove an extra zero from Korg maximum cents limit
  • Loading branch information
SeanArchibald authored Apr 23, 2023
2 parents 0e29bf3 + 53b798d commit e0e009b
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export const KORG = {
mnlg: {
octaveSize: 12,
scaleSize: 128,
maxCents: 128000,
maxCents: 12800,
refA: { val: 6900, ind: 69, freq: 440.0 },
refC: { val: 6000, ind: 60, freq: 261.6255653 },
},
Expand Down
40 changes: 38 additions & 2 deletions src/exporters/__tests__/korg.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,48 @@
import { createHash } from "crypto";
import type { JSZipObject } from "jszip";
import { DEFAULT_NUMBER_OF_COMPONENTS } from "../../constants";
import { Scale } from "scale-workshop-core";
import { describe, it, expect } from "vitest";

import { MnlgtunsExporter, MnlgtunoExporter } from "../korg";

import { getTestData } from "./test-data";

describe("Korg exporters", () => {
it("can export a scale encountered while debugging issue #393", async () => {
const params = getTestData("Korg 'logue exporter unit test");
params.baseMidiNote = 60;
params.scale = Scale.fromSubharmonicSeries(
24,
12,
DEFAULT_NUMBER_OF_COMPONENTS,
256
);

const exporter = new MnlgtunsExporter(params);
const [zip, fileType] = exporter.getFileContents();

expect(fileType).toBe(".mnlgtuns");

// Await inside forEach doesn't reach vitest so we unpack
const files: [string, JSZipObject][] = [];
zip.forEach((path, file) => {
files.push([path, file]);
});

// Generated zipfiles have timestamps that interfere with testing so we extract the contents
for (let i = 0; i < files.length; ++i) {
const [path, file] = files[i];
if (path.endsWith("bin")) {
const content = await file.async("uint8array");
expect(createHash("sha256").update(content).digest("base64")).toBe(
"/st0f/90q1FNXxNnw2S+SCFeu9TkbXIydn85+qrqnrg="
);
}
// Other contents didn't seem to have issues so we ignore them here.
}
});

it("can handle all line types (mnlgtuns)", async () => {
const params = getTestData("Korg 'logue exporter unit test v0.0.0");
const exporter = new MnlgtunsExporter(params);
Expand All @@ -26,7 +62,7 @@ describe("Korg exporters", () => {
if (path.endsWith("bin")) {
const content = await file.async("uint8array");
expect(createHash("sha256").update(content).digest("base64")).toBe(
"uuwCgV4PYizHsTL1D8ST8Msxb6rPhcCKONeGCtw2sGQ="
"nuHoVQKeaJlIHrNsaAcxFfoepmtzy+NN48LcfoU4fqE="
);
} else {
const content = await file.async("string");
Expand Down Expand Up @@ -64,7 +100,7 @@ describe("Korg exporters", () => {
if (path.endsWith("bin")) {
const content = await file.async("uint8array");
expect(createHash("sha256").update(content).digest("base64")).toBe(
"02IsbILhm1WdhAR2l5rdvbDLD9/uL5/o7OYQECwMlhY="
"dQWlBBzfHE/LLvEhmAQqM1AppQg5YsoQ2GQbK6KTUeM="
);
} else {
const content = await file.async("string");
Expand Down
23 changes: 14 additions & 9 deletions src/exporters/korg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,15 +109,20 @@ class KorgExporter extends BaseExporter {
KORG.mnlg.refA.val +
valueToCents(scale.baseFrequency / KORG.mnlg.refA.freq);

// offset cents array for binary conversion
let centsTable = [];

for (let i = 0; i < KORG.mnlg.scaleSize; ++i) {
const cents =
frequencyToCentOffset(scale.getFrequency(i - baseMidiNote)) +
refOffsetCents;
centsTable.push(Math.round(cents * 1000) / 1000); // Round to 3 decimals
}
// This should be similar to Scale Workshop 1 tuning_table.freq
const frequencies = scale.getFrequencyRange(
-baseMidiNote,
KORG.mnlg.scaleSize - baseMidiNote
);
// This should be similar to Scale Workshop 1 tuning_table.cents
const centss = frequencies.map((freq: number) =>
frequencyToCentOffset(freq, scale.baseFrequency)
);

// offset cents array for binary conversion (rounded to 3 decimals)
let centsTable = centss.map(
(cents: number) => Math.round((cents + refOffsetCents) * 1000) / 1000
);

if (!this.useScaleFormat) {
// normalize around root, truncate to 12 notes, and wrap flattened Cs
Expand Down

0 comments on commit e0e009b

Please sign in to comment.