Skip to content

Commit 4973036

Browse files
committed
feat(cli): beats analyses an audio or video file given directly
1 parent 71b6680 commit 4973036

3 files changed

Lines changed: 58 additions & 17 deletions

File tree

‎docs/packages/cli.mdx‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -716,10 +716,11 @@ them.
716716
```bash
717717
npx hyperframes beats [dir]
718718
npx hyperframes beats [dir] --json # { ok, file, count, bpm }
719+
npx hyperframes beats <file> --json # { ok, file, count, bpm, beatTimes }
719720
```
720721

721-
`beats` takes the project folder, not a media file. For the beats of one audio
722-
or video file, run `npx hyperframes audio <file> --only beats --json`.
722+
Given an audio or video file instead of a folder, `beats` analyses that file
723+
with the same tracker and prints the result; it writes nothing.
723724

724725
`beats` finds the music track — an `<audio>` element with
725726
`data-timeline-role="music"`, or an id like `music`, `bgm`, or `soundtrack` —

‎packages/cli/src/commands/beats.test.ts‎

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,21 @@ describe.skipIf(!canDecode)("hyperframes beats", () => {
9090
expect(Math.abs(report.bpm - 120)).toBeLessThanOrEqual(1);
9191
});
9292

93-
it("points a media file argument to hyperframes audio instead of reading it as a project", async () => {
94-
const song = join(project("file-arg", 120), "assets", "song.wav");
95-
const { exitCode, out } = await runBeats(song);
96-
expect(exitCode).toBe(1);
97-
expect(out).toContain(`hyperframes audio ${JSON.stringify(song)} --only beats --json`);
93+
it("analyses an audio file given directly and writes nothing", async () => {
94+
const dir = project("file-arg", 120);
95+
const song = join(dir, "assets", "song.wav");
96+
const { exitCode, out } = await runBeats(song, true);
97+
expect(exitCode).toBe(0);
98+
const report = JSON.parse(out) as {
99+
ok: boolean;
100+
file: string;
101+
bpm: number;
102+
beatTimes: number[];
103+
};
104+
expect(report).toMatchObject({ ok: true, file: song });
105+
expect(report.beatTimes.length).toBeGreaterThan(18);
106+
expect(Math.abs(report.bpm - 120)).toBeLessThanOrEqual(1);
107+
expect(existsSync(join(dir, "beats"))).toBe(false);
98108
});
99109

100110
it("tells a composition file argument to pass its folder", async () => {

‎packages/cli/src/commands/beats.ts‎

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import { c } from "../ui/colors.js";
2121
export const examples: Example[] = [
2222
["Generate the beat file for the current project", "hyperframes beats"],
2323
["Generate for a specific directory", "hyperframes beats ./my-video"],
24-
["Beats of a single audio file", "hyperframes audio music.mp3 --only beats --json"],
24+
["Beats of a single audio or video file", "hyperframes beats music.mp3 --json"],
2525
];
2626

2727
function fail(message: string): never {
@@ -96,24 +96,54 @@ function report(file: string, result: MusicBeatAnalysis, json: boolean): void {
9696
);
9797
}
9898

99+
async function analyzeFile(audioPath: string, json: boolean): Promise<void> {
100+
const result = await analyzeMusicFromSignal(await decode(audioPath));
101+
if (result.beatTimes.length === 0) fail(`No beats detected in ${audioPath}.`);
102+
if (json) {
103+
console.log(
104+
JSON.stringify(
105+
{
106+
ok: true,
107+
file: audioPath,
108+
count: result.beatTimes.length,
109+
bpm: result.bpm,
110+
beatTimes: result.beatTimes,
111+
},
112+
null,
113+
2,
114+
),
115+
);
116+
return;
117+
}
118+
console.log(
119+
c.success(
120+
`✓ ${result.beatTimes.length} beats in ${audioPath} (bpm ${result.bpm ?? "?"}, ${result.bpmConfidence})`,
121+
),
122+
);
123+
}
124+
99125
export default defineCommand({
100126
meta: {
101127
name: "beats",
102-
description: "Detect beats in the music track and write beats/<audio>.json",
128+
description:
129+
"Detect beats in the project music track (writes beats/<audio>.json) or in one audio file",
103130
},
104131
args: {
105-
dir: { type: "positional", description: "Project directory", required: false },
132+
dir: {
133+
type: "positional",
134+
description: "Project directory, or one audio or video file to analyse",
135+
required: false,
136+
},
106137
json: { type: "boolean", description: "Output result as JSON", default: false },
107138
},
108139
async run({ args }) {
109140
if (args.dir && existsSync(resolve(args.dir)) && statSync(resolve(args.dir)).isFile()) {
110-
const file = JSON.stringify(args.dir);
111-
fail(
112-
/\.html?$/i.test(args.dir)
113-
? `${args.dir} is a file; beats takes the project folder: hyperframes beats ${JSON.stringify(dirname(args.dir))}`
114-
: `${args.dir} is a file; beats takes a project folder. ` +
115-
`For one audio or video file run: hyperframes audio ${file} --only beats --json`,
116-
);
141+
if (/\.html?$/i.test(args.dir)) {
142+
fail(
143+
`${args.dir} is a file; beats takes the project folder: hyperframes beats ${JSON.stringify(dirname(args.dir))}`,
144+
);
145+
}
146+
return await analyzeFile(resolve(args.dir), Boolean(args.json));
117147
}
118148
const project = resolveProject(args.dir);
119149
const target = resolveMusicTarget(project);

0 commit comments

Comments
 (0)