UNPKG

xen-dev-utils

Version:

Utility functions used by the Scale Workshop ecosystem

157 lines 12.1 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const vitest_1 = require("vitest"); const conversion_1 = require("../conversion"); (0, vitest_1.describe)('Ratio to cents converter', () => { (0, vitest_1.it)('converts a known value', () => { (0, vitest_1.expect)((0, conversion_1.valueToCents)(3 / 2)).toBeCloseTo(701.9550008653874); }); }); (0, vitest_1.describe)('Cents to ratio converter', () => { (0, vitest_1.it)('converts a known value', () => { (0, vitest_1.expect)((0, conversion_1.centsToValue)(1200)).toBeCloseTo(2); }); }); (0, vitest_1.describe)('Frequency to cents converter', () => { (0, vitest_1.it)('converts a known value', () => { (0, vitest_1.expect)((0, conversion_1.frequencyToCentOffset)(220)).toBeCloseTo(-1200); }); }); (0, vitest_1.describe)('Cents to frequency converter', () => { (0, vitest_1.it)('converts a known value', () => { (0, vitest_1.expect)((0, conversion_1.centOffsetToFrequency)(1200)).toBeCloseTo(880); }); }); (0, vitest_1.describe)('MIDI to frequency converter', () => { (0, vitest_1.it)('converts a known value', () => { (0, vitest_1.expect)((0, conversion_1.mtof)(60)).toBeCloseTo(261.625565); }); }); (0, vitest_1.describe)('Frequency to MTS converter', () => { (0, vitest_1.it)('converts known frequency values (based on MIDI Tuning Spec)', () => { (0, vitest_1.expect)((0, conversion_1.ftomts)(8.175799)).toBe(0); (0, vitest_1.expect)((0, conversion_1.ftomts)(8.175828)).toBe(0.000062); (0, vitest_1.expect)((0, conversion_1.ftomts)(8.661957)).toBe(1); (0, vitest_1.expect)((0, conversion_1.ftomts)(16.351598)).toBe(12); (0, vitest_1.expect)((0, conversion_1.ftomts)(261.625565)).toBe(60); (0, vitest_1.expect)((0, conversion_1.ftomts)(277.182631)).toBe(61); (0, vitest_1.expect)((0, conversion_1.ftomts)(439.998449)).toBe(68.999939); (0, vitest_1.expect)((0, conversion_1.ftomts)(440)).toBe(69); (0, vitest_1.expect)((0, conversion_1.ftomts)(440.001551)).toBe(69.000061); (0, vitest_1.expect)((0, conversion_1.ftomts)(8372.01809)).toBe(120); (0, vitest_1.expect)((0, conversion_1.ftomts)(8372.047605)).toBe(120.000061); (0, vitest_1.expect)((0, conversion_1.ftomts)(12543.853951)).toBe(127); (0, vitest_1.expect)((0, conversion_1.ftomts)(12543.898175)).toBe(127.000061); (0, vitest_1.expect)((0, conversion_1.ftomts)(13289.656616)).toBe(127.999878); (0, vitest_1.expect)((0, conversion_1.ftomts)(13289.656616, true)).toBe(127.999878); }); (0, vitest_1.it)('clamps values beyond the specified MTS frequency range', () => { (0, vitest_1.expect)((0, conversion_1.ftomts)(-100, false)).toBe(0); (0, vitest_1.expect)((0, conversion_1.ftomts)(14080, false)).toBe(127.999878); (0, vitest_1.expect)((0, conversion_1.ftomts)(28980, false)).toBe(127.999878); }); (0, vitest_1.it)('does not clamp values above the specified MTS frequency range if ignoreLimit is true', () => { (0, vitest_1.expect)((0, conversion_1.ftomts)(-100, true)).toBe(0); (0, vitest_1.expect)((0, conversion_1.ftomts)(14080, true)).toBe(129); (0, vitest_1.expect)((0, conversion_1.ftomts)(28980, true)).toBeCloseTo(141.496923, 4); }); }); (0, vitest_1.describe)('MTS to MTS sysex value', () => { (0, vitest_1.it)('converts known values from MIDI Tuning spec', () => { (0, vitest_1.expect)((0, conversion_1.mtsToMtsBytes)(0)).toMatchObject(new Uint8Array([0, 0, 0])); (0, vitest_1.expect)((0, conversion_1.mtsToMtsBytes)(0.00006)).toMatchObject(new Uint8Array([0, 0, 1])); (0, vitest_1.expect)((0, conversion_1.mtsToMtsBytes)(1)).toMatchObject(new Uint8Array([1, 0, 0])); (0, vitest_1.expect)((0, conversion_1.mtsToMtsBytes)(12)).toMatchObject(new Uint8Array([12, 0, 0])); (0, vitest_1.expect)((0, conversion_1.mtsToMtsBytes)(60)).toMatchObject(new Uint8Array([60, 0, 0])); (0, vitest_1.expect)((0, conversion_1.mtsToMtsBytes)(61)).toMatchObject(new Uint8Array([61, 0, 0])); (0, vitest_1.expect)((0, conversion_1.mtsToMtsBytes)(68.99994)).toMatchObject(new Uint8Array([68, 127, 127])); (0, vitest_1.expect)((0, conversion_1.mtsToMtsBytes)(69)).toMatchObject(new Uint8Array([69, 0, 0])); (0, vitest_1.expect)((0, conversion_1.mtsToMtsBytes)(69.000061)).toMatchObject(new Uint8Array([69, 0, 1])); (0, vitest_1.expect)((0, conversion_1.mtsToMtsBytes)(120)).toMatchObject(new Uint8Array([120, 0, 0])); (0, vitest_1.expect)((0, conversion_1.mtsToMtsBytes)(120.000061)).toMatchObject(new Uint8Array([120, 0, 1])); (0, vitest_1.expect)((0, conversion_1.mtsToMtsBytes)(127)).toMatchObject(new Uint8Array([127, 0, 0])); (0, vitest_1.expect)((0, conversion_1.mtsToMtsBytes)(127.000061)).toMatchObject(new Uint8Array([127, 0, 1])); (0, vitest_1.expect)((0, conversion_1.mtsToMtsBytes)(127.999878)).toMatchObject(new Uint8Array([127, 127, 126])); }); (0, vitest_1.it)('clamps values beyond the specified MTS frequency range', () => { (0, vitest_1.expect)((0, conversion_1.mtsToMtsBytes)(-1)).toMatchObject(new Uint8Array([0, 0, 0])); (0, vitest_1.expect)((0, conversion_1.mtsToMtsBytes)(127.9999)).toMatchObject(new Uint8Array([127, 127, 126])); (0, vitest_1.expect)((0, conversion_1.mtsToMtsBytes)(128)).toMatchObject(new Uint8Array([127, 127, 126])); }); }); (0, vitest_1.describe)('Frequency to MTS sysex value', () => { (0, vitest_1.it)('converts known values from MIDI Tuning spec', () => { (0, vitest_1.expect)((0, conversion_1.frequencyToMtsBytes)(8.175799)).toMatchObject(new Uint8Array([0, 0, 0])); (0, vitest_1.expect)((0, conversion_1.frequencyToMtsBytes)(8.175828)).toMatchObject(new Uint8Array([0, 0, 1])); (0, vitest_1.expect)((0, conversion_1.frequencyToMtsBytes)(8.661957)).toMatchObject(new Uint8Array([1, 0, 0])); (0, vitest_1.expect)((0, conversion_1.frequencyToMtsBytes)(16.351598)).toMatchObject(new Uint8Array([12, 0, 0])); (0, vitest_1.expect)((0, conversion_1.frequencyToMtsBytes)(261.625565)).toMatchObject(new Uint8Array([60, 0, 0])); (0, vitest_1.expect)((0, conversion_1.frequencyToMtsBytes)(277.182631)).toMatchObject(new Uint8Array([61, 0, 0])); (0, vitest_1.expect)((0, conversion_1.frequencyToMtsBytes)(439.998449)).toMatchObject(new Uint8Array([68, 127, 127])); (0, vitest_1.expect)((0, conversion_1.frequencyToMtsBytes)(440)).toMatchObject(new Uint8Array([69, 0, 0])); (0, vitest_1.expect)((0, conversion_1.frequencyToMtsBytes)(440.001551)).toMatchObject(new Uint8Array([69, 0, 1])); (0, vitest_1.expect)((0, conversion_1.frequencyToMtsBytes)(8372.01809)).toMatchObject(new Uint8Array([120, 0, 0])); (0, vitest_1.expect)((0, conversion_1.frequencyToMtsBytes)(8372.047605)).toMatchObject(new Uint8Array([120, 0, 1])); (0, vitest_1.expect)((0, conversion_1.frequencyToMtsBytes)(12543.853951)).toMatchObject(new Uint8Array([127, 0, 0])); (0, vitest_1.expect)((0, conversion_1.frequencyToMtsBytes)(12543.898175)).toMatchObject(new Uint8Array([127, 0, 1])); (0, vitest_1.expect)((0, conversion_1.frequencyToMtsBytes)(13289.656616)).toMatchObject(new Uint8Array([127, 127, 126])); }); (0, vitest_1.it)('converts other known values', () => { (0, vitest_1.expect)((0, conversion_1.frequencyToMtsBytes)(255.999612)).toMatchObject(new Uint8Array([59, 79, 106])); (0, vitest_1.expect)((0, conversion_1.frequencyToMtsBytes)(256)).toMatchObject(new Uint8Array([59, 79, 106])); (0, vitest_1.expect)((0, conversion_1.frequencyToMtsBytes)(441.999414)).toMatchObject(new Uint8Array([69, 10, 6])); (0, vitest_1.expect)((0, conversion_1.frequencyToMtsBytes)(442)).toMatchObject(new Uint8Array([69, 10, 6])); }); (0, vitest_1.it)('clamps values beyond supported MTS range', () => { (0, vitest_1.expect)((0, conversion_1.frequencyToMtsBytes)(-1)).toMatchObject(new Uint8Array([0, 0, 0])); (0, vitest_1.expect)((0, conversion_1.frequencyToMtsBytes)(14000)).toMatchObject(new Uint8Array([127, 127, 126])); }); }); (0, vitest_1.describe)('MTS sysex value to frequency ', () => { (0, vitest_1.it)('converts known values from MIDI Tuning spec', () => { (0, vitest_1.expect)((0, conversion_1.mtsBytesToFrequency)(new Uint8Array([0, 0, 0]))).toBeCloseTo(8.175799, 4); (0, vitest_1.expect)((0, conversion_1.mtsBytesToFrequency)(new Uint8Array([0, 0, 1]))).toBeCloseTo(8.175828, 4); (0, vitest_1.expect)((0, conversion_1.mtsBytesToFrequency)(new Uint8Array([1, 0, 0]))).toBeCloseTo(8.661957, 4); (0, vitest_1.expect)((0, conversion_1.mtsBytesToFrequency)(new Uint8Array([12, 0, 0]))).toBeCloseTo(16.351598, 4); (0, vitest_1.expect)((0, conversion_1.mtsBytesToFrequency)(new Uint8Array([60, 0, 0]))).toBeCloseTo(261.625565, 4); (0, vitest_1.expect)((0, conversion_1.mtsBytesToFrequency)(new Uint8Array([61, 0, 0]))).toBeCloseTo(277.182631, 4); (0, vitest_1.expect)((0, conversion_1.mtsBytesToFrequency)(new Uint8Array([68, 127, 127]))).toBeCloseTo(439.998449, 4); (0, vitest_1.expect)((0, conversion_1.mtsBytesToFrequency)(new Uint8Array([69, 0, 0]))).toBe(440); (0, vitest_1.expect)((0, conversion_1.mtsBytesToFrequency)(new Uint8Array([69, 0, 1]))).toBeCloseTo(440.001551, 4); (0, vitest_1.expect)((0, conversion_1.mtsBytesToFrequency)(new Uint8Array([120, 0, 0]))).toBeCloseTo(8372.01809, 4); (0, vitest_1.expect)((0, conversion_1.mtsBytesToFrequency)(new Uint8Array([120, 0, 1]))).toBeCloseTo(8372.047605, 4); (0, vitest_1.expect)((0, conversion_1.mtsBytesToFrequency)(new Uint8Array([127, 0, 0]))).toBeCloseTo(12543.853951, 4); (0, vitest_1.expect)((0, conversion_1.mtsBytesToFrequency)(new Uint8Array([127, 0, 1]))).toBeCloseTo(12543.898175, 4); (0, vitest_1.expect)((0, conversion_1.mtsBytesToFrequency)(new Uint8Array([127, 127, 126]))).toBeCloseTo(13289.656616, 4); }); (0, vitest_1.it)('converts other known values', () => { (0, vitest_1.expect)((0, conversion_1.mtsBytesToFrequency)(new Uint8Array([59, 79, 106]))).toBeCloseTo(255.999612, 4); (0, vitest_1.expect)((0, conversion_1.mtsBytesToFrequency)(new Uint8Array([69, 10, 6]))).toBeCloseTo(441.999414, 4); }); (0, vitest_1.it)('clamps values beyond supported MTS range', () => { (0, vitest_1.expect)((0, conversion_1.mtsBytesToFrequency)(new Uint8Array([68, 199, 199]))).toBeCloseTo(439.998449, 4); (0, vitest_1.expect)((0, conversion_1.mtsBytesToFrequency)(new Uint8Array([199, 199, 199]))).toBeCloseTo(13289.656616, 4); }); }); (0, vitest_1.describe)('MTS data hex string converter', () => { (0, vitest_1.it)('converts other known values', () => { (0, vitest_1.expect)((0, conversion_1.mtsBytesToHex)(new Uint8Array([60, 0, 0]))).toEqual('3c0000'); (0, vitest_1.expect)((0, conversion_1.mtsBytesToHex)(new Uint8Array([69, 0, 0]))).toEqual('450000'); (0, vitest_1.expect)((0, conversion_1.mtsBytesToHex)(new Uint8Array([69, 10, 6]))).toEqual('450a06'); }); (0, vitest_1.it)('clamps int values above 0x7f to 0x7f', () => { (0, vitest_1.expect)((0, conversion_1.mtsBytesToHex)(new Uint8Array([69, 240, 6]))).toEqual('457f06'); (0, vitest_1.expect)((0, conversion_1.mtsBytesToHex)(new Uint8Array([128, 255, 128]))).toEqual('7f7f7f'); }); (0, vitest_1.it)('allow value reserved for "no tuning change"', () => { (0, vitest_1.expect)((0, conversion_1.mtsBytesToHex)(new Uint8Array([127, 127, 127]))).toEqual('7f7f7f'); }); }); (0, vitest_1.describe)('Frequency to MIDI converter', () => { (0, vitest_1.it)('converts a known value', () => { const [index, offset] = (0, conversion_1.ftom)(261.625565); (0, vitest_1.expect)(index).toBe(60); (0, vitest_1.expect)(offset).toBeCloseTo(0); }); }); //# sourceMappingURL=conversion.spec.js.map