package hk.gov.housingauthority.nco.cmp.common.captcha;
import javax.sound.sampled.*;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;
public class AudioCaptchaGenerator {
private static final int SAMPLE_RATE = 44100;
private static final int DURATION_MS_PER_CHAR = 500;
private static final int SILENCE_MS = 100;
private static final int BASE_FREQ = 220;
private static final Map<Character, int[]> CHAR_TO_FORMANTS = new HashMap<>();
static {
CHAR_TO_FORMANTS.put('0', new int[]{ 220, 400, 1000 }); // "Zero"
CHAR_TO_FORMANTS.put('1', new int[]{ 220, 500, 1500 }); // "One"
CHAR_TO_FORMANTS.put('2', new int[]{ 220, 600, 1800 }); // "Two"
CHAR_TO_FORMANTS.put('3', new int[]{ 220, 550, 2000 }); // "Three"
CHAR_TO_FORMANTS.put('4', new int[]{ 220, 700, 1600 }); // "Four"
CHAR_TO_FORMANTS.put('5', new int[]{ 220, 650, 1900 }); // "Five"
CHAR_TO_FORMANTS.put('6', new int[]{ 220, 750, 1700 }); // "Six"
CHAR_TO_FORMANTS.put('7', new int[]{ 220, 800, 2200 }); // "Seven"
CHAR_TO_FORMANTS.put('8', new int[]{ 220, 500, 1400 }); // "Eight"
CHAR_TO_FORMANTS.put('9', new int[]{ 220, 600, 2100 }); // "Nine"
CHAR_TO_FORMANTS.put('A', new int[]{ 220, 800, 1200 }); // "Ah"(如 "Father")
CHAR_TO_FORMANTS.put('E', new int[]{ 220, 500, 2300 }); // "Eh"(如 "Bed")
CHAR_TO_FORMANTS.put('I', new int[]{ 220, 300, 2500 }); // "Ee"(如 "See")
CHAR_TO_FORMANTS.put('O', new int[]{ 220, 500, 800 }); // "Oh"(如 "Boat")
CHAR_TO_FORMANTS.put('U', new int[]{ 220, 300, 700 }); // "Oo"(如 "Boot")
CHAR_TO_FORMANTS.put('B', new int[]{ 220, 400, 2000 }); // "Buh"(爆破音)
CHAR_TO_FORMANTS.put('C', new int[]{ 220, 500, 1800 }); // "Kuh"(硬颚音)
CHAR_TO_FORMANTS.put('D', new int[]{ 220, 300, 2200 }); // "Duh"
CHAR_TO_FORMANTS.put('F', new int[]{ 220, 400, 1700 }); // "Fuh"(摩擦音)
CHAR_TO_FORMANTS.put('G', new int[]{ 220, 300, 1900 }); // "Guh"
CHAR_TO_FORMANTS.put('H', new int[]{ 220, 1000, 1200 }); // "Huh"(气流音)
CHAR_TO_FORMANTS.put('J', new int[]{ 220, 600, 2400 }); // "Juh"(如 "Jump")
CHAR_TO_FORMANTS.put('K', new int[]{ 220, 800, 1800 }); // "Kuh"
CHAR_TO_FORMANTS.put('L', new int[]{ 220, 400, 1500 }); // "Luh"(舌侧音)
CHAR_TO_FORMANTS.put('M', new int[]{ 220, 200, 1000 }); // "Mmm"(鼻音)
CHAR_TO_FORMANTS.put('N', new int[]{ 220, 300, 1200 }); // "Nnn"(鼻音)
CHAR_TO_FORMANTS.put('P', new int[]{ 220, 500, 2000 }); // "Puh"
CHAR_TO_FORMANTS.put('Q', new int[]{ 220, 700, 1600 }); // "Kuh"(如 "Queen")
CHAR_TO_FORMANTS.put('R', new int[]{ 220, 600, 1400 }); // "Rrr"(卷舌音)
CHAR_TO_FORMANTS.put('S', new int[]{ 220, 5000, 6000 }); // "Sss"(高频摩擦音)
CHAR_TO_FORMANTS.put('T', new int[]{ 220, 400, 1800 }); // "Tuh"
CHAR_TO_FORMANTS.put('V', new int[]{ 220, 300, 1600 }); // "Vuh"
CHAR_TO_FORMANTS.put('W', new int[]{ 220, 200, 800 }); // "Wuh"(圆唇音)
CHAR_TO_FORMANTS.put('X', new int[]{ 220, 700, 2200 }); // "Ks"(如 "Box")
CHAR_TO_FORMANTS.put('Y', new int[]{ 220, 400, 1200 }); // "Yuh"(如 "Yes")
CHAR_TO_FORMANTS.put('Z', new int[]{ 220, 4500, 5500 }); // "Zzz"(高频摩擦音)
}
public static byte[] generateCaptchaAudio(String text) {
try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
AudioFormat format = new AudioFormat(SAMPLE_RATE, 16, 1, true, false);
for (char c : text.toUpperCase().toCharArray()) {
int[] formants = CHAR_TO_FORMANTS.getOrDefault(c, new int[]{ BASE_FREQ });
addVowelSound(baos, format, formants, DURATION_MS_PER_CHAR);
addSilence(baos, format, SILENCE_MS);
}
return convertToWav(baos.toByteArray(), format);
} catch (IOException e) {
throw new RuntimeException("Failed to generate audio captcha", e);
}
}
private static void addVowelSound(
ByteArrayOutputStream baos,
AudioFormat format,
int[] formants,
int durationMs
) throws IOException {
byte[] buffer = new byte[format.getFrameSize() * (int)(format.getSampleRate() * durationMs / 1000)];
for (int i = 0; i < buffer.length; i += 2) {
double sample = 0;
// 叠加共振峰频率
for (int j = 0; j < formants.length; j++) {
double freq = formants[j];
double volume = 1.0 / (j + 1); // 高频衰减
double angle = 2.0 * Math.PI * freq * (i / 2) / format.getSampleRate();
sample += volume * Math.sin(angle);
}
// 应用包络(ADSR)
double envelope = calculateEnvelope(i, buffer.length);
short sampleValue = (short)(Short.MAX_VALUE * sample * envelope * 0.8); // 避免削波
buffer[i] = (byte)(sampleValue & 0xFF);
buffer[i + 1] = (byte)((sampleValue >> 8) & 0xFF);
}
baos.write(buffer);
}
private static double calculateEnvelope(int currentPos, int totalLength) {
double attack = 0.1; // 10% 攻击时间
double release = 0.3; // 30% 释放时间
if (currentPos < attack * totalLength) {
return currentPos / (attack * totalLength); // 渐强
} else if (currentPos > (1 - release) * totalLength) {
return 1 - (currentPos - (1 - release) * totalLength) / (release * totalLength); // 渐弱
} else {
return 1.0; // 持续
}
}
public static String generateCaptchaAudioAsBase64(String text) {
try {
byte[] wavData = generateCaptchaAudio(text);
return Base64.getEncoder().encodeToString(wavData);
} catch (Exception e) {
throw new RuntimeException("Failed to generate audio captcha", e);
}
}
private static byte[] convertToWav(byte[] audioData, AudioFormat format) throws IOException {
try (ByteArrayOutputStream wavOutputStream = new ByteArrayOutputStream()) {
// WAV文件頭
wavOutputStream.write("RIFF".getBytes());
writeInt(wavOutputStream, 36 + audioData.length); // 文件總長度
wavOutputStream.write("WAVE".getBytes());
wavOutputStream.write("fmt ".getBytes());
writeInt(wavOutputStream, 16); // fmt塊長度
writeShort(wavOutputStream, (short) 1); // 音頻格式 (PCM)
writeShort(wavOutputStream, (short) format.getChannels());
writeInt(wavOutputStream, (int) format.getSampleRate());
writeInt(wavOutputStream, (int) (format.getSampleRate() * format.getFrameSize()));
writeShort(wavOutputStream, (short) format.getFrameSize());
writeShort(wavOutputStream, (short) format.getSampleSizeInBits());
wavOutputStream.write("data".getBytes());
writeInt(wavOutputStream, audioData.length);
wavOutputStream.write(audioData);
return wavOutputStream.toByteArray();
}
}
private static void writeInt(ByteArrayOutputStream out, int value) throws IOException {
out.write(value);
out.write(value >> 8);
out.write(value >> 16);
out.write(value >> 24);
}
private static void writeShort(ByteArrayOutputStream out, short value) throws IOException {
out.write(value);
out.write(value >> 8);
}
private static void addSilence(ByteArrayOutputStream baos, AudioFormat format, int durationMs) throws IOException {
byte[] buffer = new byte[format.getFrameSize() * (int) (format.getSampleRate() * durationMs / 1000)];
baos.write(buffer);
}
}
加入BASE 65 對應的CHAR_TO_FORMANTS
最新发布