Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/long_audio.js
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,7 @@ async function runAutoSentenceWindowing({
* @param {number} [opts.timeOffset=0]
* @returns {Promise<{text: string, chunks?: Array<{ text: string, timestamp: [number, number] }>, words?: Array<{ text: string, start_time: number, end_time: number, confidence?: number }>}>}
*/
export { validateAudio };
export async function transcribeLongAudioWithChunks(model, audio, sampleRate = 16000, opts = {}) {
validateAudio(audio);
if (typeof sampleRate !== 'number' || !Number.isFinite(sampleRate) || sampleRate <= 0) {
Expand Down
35 changes: 34 additions & 1 deletion tests/long_audio_chunking.test.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, it, expect, vi } from 'vitest';
import { ParakeetModel } from '../src/parakeet.js';
import { transcribeLongAudioWithChunks } from '../src/long_audio.js';
import { transcribeLongAudioWithChunks, validateAudio } from '../src/long_audio.js';

describe('long-audio chunking helpers', () => {
it('returns a simple text result when timestamps are not requested', async () => {
Expand Down Expand Up @@ -504,3 +504,36 @@ describe('long-audio chunking helpers', () => {
]);
});
});

describe('validateAudio', () => {
it('throws TypeError for invalid types', () => {
const invalidInputs = [
[],
[1, 2, 3],
new Uint8Array([1, 2, 3]),
'string',
null,
undefined,
{},
];
for (const input of invalidInputs) {
expect(() => validateAudio(input)).toThrow(TypeError);
}
});

it('throws Error if audio contains non-finite values', () => {
expect(() => validateAudio(Float32Array.from([1, NaN, 3]))).toThrow(/finite audio samples/);
expect(() => validateAudio(Float64Array.from([1, Infinity, 3]))).toThrow(/finite audio samples/);
expect(() => validateAudio(Float32Array.from([-Infinity]))).toThrow(/finite audio samples/);
});

it('passes for empty typed arrays', () => {
expect(() => validateAudio(new Float32Array(0))).not.toThrow();
expect(() => validateAudio(new Float64Array(0))).not.toThrow();
});

it('passes for valid finite typed arrays', () => {
expect(() => validateAudio(Float32Array.from([1.5, -2.5, 0]))).not.toThrow();
expect(() => validateAudio(Float64Array.from([1.5, -2.5, 0]))).not.toThrow();
});
});