From 83e63d57c973be5627724dd851564d0b6581c19c Mon Sep 17 00:00:00 2001 From: Vincenzo Sicurella Date: Thu, 16 Nov 2023 12:25:08 -0500 Subject: [PATCH] fix mtsBytesToHex style and tests --- src/__tests__/conversion.spec.ts | 9 ++++++--- src/conversion.ts | 6 +++--- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/__tests__/conversion.spec.ts b/src/__tests__/conversion.spec.ts index 7a6f5bb..72c877b 100644 --- a/src/__tests__/conversion.spec.ts +++ b/src/__tests__/conversion.spec.ts @@ -219,10 +219,13 @@ describe('MTS data hex string converter', () => { expect(mtsBytesToHex(new Uint8Array([69, 0, 0]))).toEqual('450000'); expect(mtsBytesToHex(new Uint8Array([69, 10, 6]))).toEqual('450a06'); }); - it('masks int values above 0x7f by 0x7f', () => { - expect(mtsBytesToHex(new Uint8Array([69, 240, 6]))).toEqual('457006'); - expect(mtsBytesToHex(new Uint8Array([69, 255, 6]))).toEqual('457f06'); + it('clamps int values above 0x7f to 0x7f', () => { + expect(mtsBytesToHex(new Uint8Array([69, 240, 6]))).toEqual('457f06'); + expect(mtsBytesToHex(new Uint8Array([128, 255, 128]))).toEqual('7f7f7f'); }); + it ('allow value reserved for "no tuning change"', () => { + expect(mtsBytesToHex(new Uint8Array([127, 127, 127]))).toEqual('7f7f7f') + }) }); describe('Frequency to MIDI converter', () => { diff --git a/src/conversion.ts b/src/conversion.ts index bee8a67..34d1fae 100644 --- a/src/conversion.ts +++ b/src/conversion.ts @@ -147,9 +147,9 @@ export function mtsBytesToHex(mtsBytes: Uint8Array): String { const msb = mtsBytes[1] > 0x7f ? 0x7f : mtsBytes[1]; const lsb = mtsBytes[2] > 0x7f ? 0x7f : mtsBytes[2]; return ( - (noteNumber).toString(16).padStart(2, '0') + - (msb).toString(16).padStart(2, '0') + - (lsb).toString(16).padStart(2, '0') + noteNumber.toString(16).padStart(2, '0') + + msb.toString(16).padStart(2, '0') + + lsb.toString(16).padStart(2, '0') ); }