VoiceStudio is the open-source, fully-local ElevenLabs alternative — voice cloning, voice design, video dubbing, dictation, transcription & audiobook creation in 646 languages.
Python aiaudiobookcudadubbingelevenlabs-alternativehuggingfacelocal-firstmlxomnivoice-studiospeech-to-texttext-to-speechtranscriptiontranslatettsvoice-aivoice-cloningvoice-generationvoicestudioworkflow
import { describe, expect, it, vi } from 'vitest';
import {
buildAudioInputConstraints,
createInputLevelStore,
listAudioInputs,
startInputLevelMonitor,
} from './audioInput';
describe('audio input utilities', () => {
it('publishes microphone levels without rerendering the app root', () => {
const store = createInputLevelStore();
const listener = vi.fn();
const unsubscribe = store.subscribe(listener);
store.set(0.3);
expect(store.getSnapshot()).toBe(0.3);
expect(listener).toHaveBeenCalledOnce();
unsubscribe();
store.set(0.6);
expect(listener).toHaveBeenCalledOnce();
});
it('builds portable default, device, and channel constraints', () => {
expect(buildAudioInputConstraints()).toEqual({ audio: true });
expect(buildAudioInputConstraints('mic-2', 'mono')).toEqual({
audio: { deviceId: { exact: 'mic-2' }, channelCount: { ideal: 1 } },
});
expect(buildAudioInputConstraints('', 'stereo')).toEqual({
audio: { channelCount: { ideal: 2 } },
});
});
it('lists only audio input devices', async () => {
const audioInputs = await listAudioInputs({
enumerateDevices: vi.fn(async () => [
{ kind: 'audiooutput', deviceId: 'speaker' },
{ kind: 'audioinput', deviceId: 'mic', label: 'Desk mic' },
]),
});
expect(audioInputs).toEqual([{ kind: 'audioinput', deviceId: 'mic', label: 'Desk mic' }]);
});
it('reports live input energy without audible monitoring and cleans up', async () => {
const frames = [];
const cancelled = vi.fn();
const source = { connect: vi.fn(), disconnect: vi.fn() };
const analyser = {
connect: vi.fn(),
disconnect: vi.fn(),
getFloatTimeDomainData: vi.fn((samples) => samples.fill(0.1)),
};
const silentGain = {
gain: { value: 1 },
connect: vi.fn(),
disconnect: vi.fn(),
};
const context = {
destination: {},
createMediaStreamSource: vi.fn(() => source),
createAnalyser: vi.fn(() => analyser),
createGain: vi.fn(() => silentGain),
resume: vi.fn(async () => {}),
close: vi.fn(async () => {}),
};
const levels = [];
function MockAudioContext() {
return context;
}
const stop = startInputLevelMonitor({}, (level) => levels.push(level), {
AudioContextClass: MockAudioContext,
requestFrame: vi.fn((callback) => {
frames.push(callback);
return frames.length;
}),
cancelFrame: cancelled,
});
expect(silentGain.gain.value).toBe(0);
frames.shift()();
expect(levels.at(-1)).toBeCloseTo(0.4);
stop();
expect(cancelled).toHaveBeenCalled();
expect(source.disconnect).toHaveBeenCalled();
expect(context.close).toHaveBeenCalled();
expect(levels.at(-1)).toBe(0);
});
describe('setup failure', () => {
const makeContext = () => {
const node = () => ({ connect: vi.fn(), disconnect: vi.fn() });
return {
destination: {},
createMediaStreamSource: vi.fn(node),
createAnalyser: vi.fn(() => ({
...node(),
getFloatTimeDomainData: vi.fn(),
})),
createGain: vi.fn(() => ({ ...node(), gain: { value: 1 } })),
resume: vi.fn(async () => {}),
close: vi.fn(async () => {}),
};
};
it('closes the AudioContext when graph setup throws', () => {
const context = makeContext();
context.createMediaStreamSource.mockImplementation(() => {
throw new Error('bad stream');
});
expect(() =>
startInputLevelMonitor({}, vi.fn(), {
AudioContextClass: function Ctx() {
return context;
},
requestFrame: vi.fn(),
cancelFrame: vi.fn(),
}),
).toThrow('bad stream');
expect(context.close).toHaveBeenCalledOnce();
});
it('closes the AudioContext when frame scheduling throws', () => {
const context = makeContext();
expect(() =>
startInputLevelMonitor({}, vi.fn(), {
AudioContextClass: function Ctx() {
return context;
},
requestFrame: vi.fn(() => {
throw new Error('no frames');
}),
cancelFrame: vi.fn(),
}),
).toThrow('no frames');
expect(context.close).toHaveBeenCalledOnce();
});
it('still closes the context when teardown disconnect throws', () => {
const context = makeContext();
const source = context.createMediaStreamSource();
source.disconnect.mockImplementation(() => {
throw new Error('already disconnected');
});
context.createMediaStreamSource.mockImplementation(() => source);
const stop = startInputLevelMonitor({}, vi.fn(), {
AudioContextClass: function Ctx() {
return context;
},
requestFrame: vi.fn(() => 1),
cancelFrame: vi.fn(),
});
expect(() => stop()).not.toThrow();
expect(context.close).toHaveBeenCalledOnce();
});
});
});