Skip to content
Merged
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mmx-cli",
"version": "1.0.12",
"version": "1.0.13",
"description": "CLI for the MiniMax AI Platform",
"type": "module",
"engines": {
Expand Down
4 changes: 2 additions & 2 deletions src/commands/speech/synthesize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { detectOutputFormat, formatOutput } from '../../output/formatter';
import { saveAudioOutput } from '../../output/audio';
import { writeFileSync } from 'fs';
import { readTextFromPathOrStdin } from '../../utils/fs';
import { T2A_FORMATS, formatList, validateAudioFormat, validateT2AStreaming } from '../../utils/audio-formats';
import { T2A_FORMATS, formatList, validateAudioFormat, validateT2AStreaming, t2aDefaultSampleRate } from '../../utils/audio-formats';
import type { Config } from '../../config/schema';
import type { GlobalFlags } from '../../types/flags';
import type { SpeechRequest, SpeechResponse } from '../../types/api';
Expand Down Expand Up @@ -81,7 +81,7 @@ export default defineCommand({
},
audio_setting: {
format: (flags.format as string) || 'mp3',
sample_rate: (flags.sampleRate as number) ?? 32000,
sample_rate: (flags.sampleRate as number) ?? t2aDefaultSampleRate(ext, 32000),
bitrate: (flags.bitrate as number) ?? 128000,
channel: (flags.channels as number) ?? 1,
},
Expand Down
12 changes: 11 additions & 1 deletion src/utils/audio-formats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { CLIError } from '../errors/base';
import { ExitCode } from '../errors/codes';

export const T2A_FORMATS = ['mp3', 'pcm', 'flac', 'wav', 'pcmu_raw', 'pcmu_wav', 'opus'] as const;
export const MUSIC_FORMATS = ['mp3', 'wav', 'pcm', 'flac'] as const;
export const MUSIC_FORMATS = ['mp3', 'wav', 'pcm'] as const;

export type T2AFormat = (typeof T2A_FORMATS)[number];
export type MusicFormat = (typeof MUSIC_FORMATS)[number];
Expand All @@ -20,6 +20,16 @@ export function validateAudioFormat(format: string, formats: readonly string[]):
}
}

const T2A_SAMPLE_RATE: Partial<Record<T2AFormat, number>> = {
opus: 24000,
pcmu_raw: 8000,
pcmu_wav: 8000,
};

export function t2aDefaultSampleRate(format: string, fallback: number): number {
return T2A_SAMPLE_RATE[format as T2AFormat] ?? fallback;
}

export function validateT2AStreaming(format: string, stream: boolean): void {
if (stream && format === 'wav') {
throw new CLIError(
Expand Down
2 changes: 1 addition & 1 deletion test/commands/music/generate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ describe('music generate command', () => {
).rejects.toThrow('Invalid audio format "opus"');
});

it.each(['mp3', 'wav', 'pcm', 'flac'])(
it.each(['mp3', 'wav', 'pcm'])(
'accepts %s format in dry-run',
async (fmt) => {
const origLog = console.log;
Expand Down
39 changes: 39 additions & 0 deletions test/commands/speech/synthesize.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,4 +259,43 @@ describe('speech synthesize format validation', () => {
synthesizeCommand.execute(config, { ...flags, format: 'wav', stream: true }),
).rejects.toThrow('wav format is not supported in streaming');
});

it('defaults opus sample rate to 24000', async () => {
const originalLog = console.log;
let output = '';
console.log = (msg: string) => { output += msg; };
try {
await synthesizeCommand.execute(config, { ...flags, format: 'opus' });
const parsed = JSON.parse(output);
expect(parsed.request.audio_setting.sample_rate).toBe(24000);
} finally {
console.log = originalLog;
}
});

it('defaults pcmu_wav sample rate to 8000', async () => {
const originalLog = console.log;
let output = '';
console.log = (msg: string) => { output += msg; };
try {
await synthesizeCommand.execute(config, { ...flags, format: 'pcmu_wav' });
const parsed = JSON.parse(output);
expect(parsed.request.audio_setting.sample_rate).toBe(8000);
} finally {
console.log = originalLog;
}
});

it('respects explicit --sample-rate even for opus', async () => {
const originalLog = console.log;
let output = '';
console.log = (msg: string) => { output += msg; };
try {
await synthesizeCommand.execute(config, { ...flags, format: 'opus', sampleRate: 16000 });
const parsed = JSON.parse(output);
expect(parsed.request.audio_setting.sample_rate).toBe(16000);
} finally {
console.log = originalLog;
}
});
});
23 changes: 21 additions & 2 deletions test/utils/audio-formats.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
formatList,
validateAudioFormat,
validateT2AStreaming,
t2aDefaultSampleRate,
} from '../../src/utils/audio-formats';

describe('audio-formats', () => {
Expand All @@ -21,12 +22,12 @@ describe('audio-formats', () => {
});

describe('MUSIC_FORMATS', () => {
it.each(['mp3', 'wav', 'pcm', 'flac'] as const)(
it.each(['mp3', 'wav', 'pcm'] as const)(
'accepts %s',
(fmt) => expect(() => validateAudioFormat(fmt, MUSIC_FORMATS)).not.toThrow(),
);

it.each(['opus', 'pcmu_raw', 'pcmu_wav', 'aac'])(
it.each(['opus', 'pcmu_raw', 'pcmu_wav', 'flac', 'aac'])(
'rejects %s',
(fmt) => expect(() => validateAudioFormat(fmt, MUSIC_FORMATS)).toThrow(/Invalid audio format/),
);
Expand All @@ -52,4 +53,22 @@ describe('audio-formats', () => {
expect(formatList(['a', 'b', 'c'])).toBe('a, b, c');
});
});

describe('t2aDefaultSampleRate', () => {
it('returns 24000 for opus', () => {
expect(t2aDefaultSampleRate('opus', 32000)).toBe(24000);
});

it('returns 8000 for pcmu_raw', () => {
expect(t2aDefaultSampleRate('pcmu_raw', 32000)).toBe(8000);
});

it('returns 8000 for pcmu_wav', () => {
expect(t2aDefaultSampleRate('pcmu_wav', 32000)).toBe(8000);
});

it('returns fallback for mp3', () => {
expect(t2aDefaultSampleRate('mp3', 32000)).toBe(32000);
});
});
});
Loading