Skip to content

Commit

Permalink
fix mtsBytesToHex style and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vsicurella committed Nov 16, 2023
1 parent b64b7c0 commit 10532a4
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
9 changes: 6 additions & 3 deletions src/__tests__/conversion.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,9 +219,12 @@ 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');
});
});

Expand Down
8 changes: 4 additions & 4 deletions src/conversion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ export function mtsBytesToMts(mtsBytes: Uint8Array): number {
let lsb = mtsBytes[2];

const noteNumber = mtsBytes[0] > 0x7f ? 0x7f : mtsBytes[0];
if (noteNumber == 0x7f) {
if (noteNumber === 0x7f) {
if (lsb >= 0x7f) lsb = 0x7e;
} else if (lsb > 0x7f) lsb = 0x7f;

Expand Down Expand Up @@ -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')
);
}

Expand Down

0 comments on commit 10532a4

Please sign in to comment.