Skip to content

Commit

Permalink
Rewrite tests to not write on the file system
Browse files Browse the repository at this point in the history
  • Loading branch information
tassoevan committed Apr 19, 2023
1 parent f8c886f commit b06db31
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 51 deletions.
125 changes: 74 additions & 51 deletions packages/mp3-encoder/src/lame/Mp3Encoder.spec.ts
Original file line number Diff line number Diff line change
@@ -1,79 +1,102 @@
import { readFile, open } from 'fs/promises';
import { createHash } from 'crypto';
import { readFile } from 'fs/promises';
import { join } from 'path';

import { Mp3Encoder } from './Mp3Encoder';
import { WavHeader } from './WavHeader';

const leftPath = join('testdata', 'Left44100.wav');
const rightPath = join('testdata', 'Right44100.wav');
let leftSampleBuffer: ArrayBufferLike;
let rightSampleBuffer: ArrayBufferLike;

test('stereo 44100 kHz', async () => {
const r1 = await readFile(leftPath);
const r2 = await readFile(rightPath);
const fd = await open(join('testdata', 'stereo.mp3'), 'w');
beforeAll(async () => {
const leftPath = join('testdata', 'Left44100.wav');
const rightPath = join('testdata', 'Right44100.wav');

const sampleBuf1 = new Uint8Array(r1).buffer;
const sampleBuf2 = new Uint8Array(r2).buffer;
const w1 = WavHeader.readHeader(new DataView(sampleBuf1));
const w2 = WavHeader.readHeader(new DataView(sampleBuf2));
leftSampleBuffer = new Uint8Array(await readFile(leftPath)).buffer;
rightSampleBuffer = new Uint8Array(await readFile(rightPath)).buffer;
});

test('mono', async () => {
const waveHeader = WavHeader.readHeader(new DataView(leftSampleBuffer));
const samples = new Int16Array(
leftSampleBuffer,
waveHeader.dataOffset,
waveHeader.dataLen / 2
);

const hash = createHash('sha1');
hash.setEncoding('hex');

const samples1 = new Int16Array(sampleBuf1, w1.dataOffset, w1.dataLen / 2);
const samples2 = new Int16Array(sampleBuf2, w2.dataOffset, w2.dataLen / 2);
let remaining1 = samples1.length;
const remaining2 = samples2.length;
expect(remaining1).toBe(remaining2);
expect(w1.sampleRate).toBe(w2.sampleRate);
let remainingSamples = samples.length;

const lameEnc = new Mp3Encoder(2, w1.sampleRate, 128);
const encoder = new Mp3Encoder();
const maxSamples = 1152;

let time = new Date().getTime();
for (let i = 0; remaining1 >= maxSamples; i += maxSamples) {
const left = samples1.subarray(i, i + maxSamples);
const right = samples2.subarray(i, i + maxSamples);
for (let i = 0; remainingSamples >= maxSamples; i += maxSamples) {
const left = samples.subarray(i, i + maxSamples);
const right = samples.subarray(i, i + maxSamples);

const mp3buf = lameEnc.encodeBuffer(left, right);
const mp3buf = encoder.encodeBuffer(left, right);
if (mp3buf.length > 0) {
// eslint-disable-next-line no-await-in-loop
await fd.write(Buffer.from(mp3buf), 0, mp3buf.length);
hash.write(Buffer.from(mp3buf));
}
remaining1 -= maxSamples;
remainingSamples -= maxSamples;
}
const mp3buf = lameEnc.flush();

const mp3buf = encoder.flush();
if (mp3buf.length > 0) {
await fd.write(Buffer.from(mp3buf), 0, mp3buf.length);
hash.write(Buffer.from(mp3buf));
}
await fd.close();
time = new Date().getTime() - time;
console.log(`done in ${time}msec`);

hash.end();

expect(hash.read()).toBe('ca9292fc5fea3ba4cb07c4a0ba60cf0c267b783b');
});

test('full length', async () => {
const r = await readFile(leftPath);
const sampleBuf = new Uint8Array(r).buffer;
const w = WavHeader.readHeader(new DataView(sampleBuf));
const samples = new Int16Array(sampleBuf, w.dataOffset, w.dataLen / 2);
let remaining = samples.length;
const lameEnc = new Mp3Encoder();
test('stereo', async () => {
const leftWaveHeader = WavHeader.readHeader(new DataView(leftSampleBuffer));
const rightWaveHeader = WavHeader.readHeader(new DataView(rightSampleBuffer));

expect(leftWaveHeader.sampleRate).toBe(rightWaveHeader.sampleRate);

const leftSamples = new Int16Array(
leftSampleBuffer,
leftWaveHeader.dataOffset,
leftWaveHeader.dataLen / 2
);
const rightSamples = new Int16Array(
rightSampleBuffer,
rightWaveHeader.dataOffset,
rightWaveHeader.dataLen / 2
);

expect(leftSamples.length).toBe(rightSamples.length);

const hash = createHash('sha1');
hash.setEncoding('hex');

let remainingSamples = leftSamples.length;

const encoder = new Mp3Encoder(2, leftWaveHeader.sampleRate, 128);
const maxSamples = 1152;

const fd = await open(join('testdata', 'testjs2.mp3'), 'w');
let time = new Date().getTime();
for (let i = 0; remaining >= maxSamples; i += maxSamples) {
const left = samples.subarray(i, i + maxSamples);
const right = samples.subarray(i, i + maxSamples);
for (let i = 0; remainingSamples >= maxSamples; i += maxSamples) {
const left = leftSamples.subarray(i, i + maxSamples);
const right = rightSamples.subarray(i, i + maxSamples);

const mp3buf = lameEnc.encodeBuffer(left, right);
const mp3buf = encoder.encodeBuffer(left, right);
if (mp3buf.length > 0) {
fd.write(Buffer.from(mp3buf), 0, mp3buf.length);
hash.write(Buffer.from(mp3buf));
}
remaining -= maxSamples;
remainingSamples -= maxSamples;
}
const mp3buf = lameEnc.flush();

const mp3buf = encoder.flush();
if (mp3buf.length > 0) {
fd.write(Buffer.from(mp3buf), 0, mp3buf.length);
hash.write(Buffer.from(mp3buf));
}
fd.close();
time = new Date().getTime() - time;
console.log(`done in ${time}msec`);

hash.end();

expect(hash.read()).toBe('ab6daeb1c563389cafacc0ec4ed963ee8ae8e8d7');
});
Binary file removed packages/mp3-encoder/testdata/stereo.mp3
Binary file not shown.
Binary file removed packages/mp3-encoder/testdata/testjs2.mp3
Binary file not shown.

0 comments on commit b06db31

Please sign in to comment.