Add audio module: meeting minutes with Spring AI speech-to-text, structured output and text-to-speech
OpenAiAudioTranscriptionModel -> ChatClient.entity(Minutes.class) -> OpenAiAudioSpeechModel, WAV chunking at a silence under the 25 MB upload limit, SDK retry behaviour, the 4096-character speech limit and a cost model. Audio endpoints are a local mock and the chat model a stub; no real model was called. Spring Boot 4.1.2, Spring AI 2.0.2, Java 25. Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01CifR2BE7PBvRy5hT8TS9oz
This commit is contained in:
@@ -0,0 +1,98 @@
|
||||
package com.ankurm.audio;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Splits a WAV file into pieces that each fit under an upload limit, cutting in the middle of the
|
||||
* longest silence it can find instead of at an arbitrary byte. The OpenAI speech-to-text guide
|
||||
* says to avoid "splitting in the middle of a sentence, which can remove context and reduce
|
||||
* accuracy"; this is that advice as code.
|
||||
*/
|
||||
public final class SilenceChunker {
|
||||
|
||||
/** One piece: where it starts in the original recording, and its own valid WAV bytes. */
|
||||
public record Chunk(int index, double startSeconds, double endSeconds, byte[] wav, String cutKind) {
|
||||
public int bytes() { return wav.length; }
|
||||
}
|
||||
|
||||
private static final int WINDOW_MS = 100;
|
||||
|
||||
private final long maxBytes;
|
||||
private final double minSilenceSeconds;
|
||||
private final int silenceRms;
|
||||
|
||||
/**
|
||||
* @param maxBytes hard ceiling for each chunk, header included
|
||||
* @param minSilenceSeconds shortest quiet stretch worth cutting in
|
||||
* @param silenceRms windows whose RMS is below this count as quiet (16-bit scale)
|
||||
*/
|
||||
public SilenceChunker(long maxBytes, double minSilenceSeconds, int silenceRms) {
|
||||
this.maxBytes = maxBytes;
|
||||
this.minSilenceSeconds = minSilenceSeconds;
|
||||
this.silenceRms = silenceRms;
|
||||
}
|
||||
|
||||
public List<Chunk> split(byte[] wav) {
|
||||
Wav.Info info = Wav.parse(wav);
|
||||
if (wav.length <= maxBytes) {
|
||||
return List.of(new Chunk(0, 0, info.seconds(), wav, "none (fits)"));
|
||||
}
|
||||
int maxFrames = (int) ((maxBytes - 44) / info.bytesPerFrame());
|
||||
int win = info.sampleRate() * WINDOW_MS / 1000;
|
||||
int total = info.frames();
|
||||
List<Chunk> out = new ArrayList<>();
|
||||
int start = 0;
|
||||
while (start < total) {
|
||||
int limit = Math.min(total, start + maxFrames);
|
||||
if (limit == total) {
|
||||
out.add(chunk(out.size(), wav, info, start, total, "end of file"));
|
||||
break;
|
||||
}
|
||||
int cut = quietCut(wav, info, win, start + maxFrames / 2, limit);
|
||||
String kind = "mid-silence";
|
||||
if (cut < 0) { cut = limit; kind = "HARD CUT (no silence found)"; }
|
||||
out.add(chunk(out.size(), wav, info, start, cut, kind));
|
||||
start = cut;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
/** Naive baseline: every {@code maxBytes}, wherever that lands. */
|
||||
public List<Chunk> splitBlindly(byte[] wav) {
|
||||
Wav.Info info = Wav.parse(wav);
|
||||
int maxFrames = (int) ((maxBytes - 44) / info.bytesPerFrame());
|
||||
List<Chunk> out = new ArrayList<>();
|
||||
for (int s = 0; s < info.frames(); s += maxFrames) {
|
||||
out.add(chunk(out.size(), wav, info, s, Math.min(info.frames(), s + maxFrames), "fixed size"));
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
private Chunk chunk(int i, byte[] wav, Wav.Info info, int from, int to, String kind) {
|
||||
return new Chunk(i, (double) from / info.sampleRate(), (double) to / info.sampleRate(),
|
||||
Wav.slice(wav, info, from, to), kind);
|
||||
}
|
||||
|
||||
/** Middle frame of the LAST quiet run (at least minSilenceSeconds long) inside [from, limit), or -1. */
|
||||
private int quietCut(byte[] wav, Wav.Info info, int win, int from, int limit) {
|
||||
int minWindows = (int) Math.ceil(minSilenceSeconds * 1000 / WINDOW_MS);
|
||||
int runStart = -1, bestMid = -1;
|
||||
for (int f = from; f + win <= limit; f += win) {
|
||||
boolean quiet = rms(wav, info, f, win) < silenceRms;
|
||||
if (quiet && runStart < 0) runStart = f;
|
||||
if ((!quiet || f + 2 * win > limit) && runStart >= 0) {
|
||||
int end = quiet ? f + win : f;
|
||||
if ((end - runStart) / win >= minWindows) bestMid = runStart + (end - runStart) / 2;
|
||||
runStart = -1;
|
||||
}
|
||||
}
|
||||
return bestMid;
|
||||
}
|
||||
|
||||
private static double rms(byte[] wav, Wav.Info info, int from, int n) {
|
||||
double sum = 0;
|
||||
for (int i = 0; i < n; i++) { double s = Wav.sample(wav, info, from + i); sum += s * s; }
|
||||
return Math.sqrt(sum / n);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user