package org.example;

import org.vosk.LibVosk;
import org.vosk.LogLevel;
import org.vosk.Model;
import org.vosk.Recognizer;
import javax.sound.sampled.*;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

// 语音识别示例 对话
public class demo5 {

    private static final String WAKE_WORD = "小爱同学"; // 唤醒词

    public static void main(String[] args) {
        // 设置日志级别
        LibVosk.setLogLevel(LogLevel.DEBUG);

        // 配置音频格式
        AudioFormat format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, 16000, 16, 1, 2, 16000, true);
        DataLine.Info inputInfo = new DataLine.Info(TargetDataLine.class, format);
        DataLine.Info outputInfo = new DataLine.Info(SourceDataLine.class, format);

        try (Model model = new Model("src/main/resources/model/vosk-model-cn-0.22");
             Recognizer recognizer = new Recognizer(model, 16000)) {

            // 打开并启动麦克风
            try (TargetDataLine microphone = (TargetDataLine) AudioSystem.getLine(inputInfo)) {
                microphone.open(format);
                microphone.start();

                // 准备音频输出（用于测试）
                try (SourceDataLine speakers = (SourceDataLine) AudioSystem.getLine(outputInfo)) {
                    speakers.open(format);
                    speakers.start();

                    ByteArrayOutputStream out = new ByteArrayOutputStream();
                    byte[] buffer = new byte[4096];
                    int bytesRead;
                    boolean wakeWordDetected = false;

                    System.out.println("等待唤醒词...");

                    while (true) {
                        bytesRead = microphone.read(buffer, 0, buffer.length);
                        if (bytesRead == -1) break; // 检测到流结束

                        // 写入扬声器用于播放（可选）
                        speakers.write(buffer, 0, bytesRead);

                        // 简单的唤醒词检测
                        if (!wakeWordDetected) {
                            String partialResult = recognizer.getPartialResult();
                            if (partialResult.replaceAll("\\s+", "").contains(WAKE_WORD)) {
                                System.out.println("唤醒词检测到，开始语音识别...");
                                wakeWordDetected = true;
                            }
                        }

                        // 启动语音识别
                        if (wakeWordDetected) {
                            if (recognizer.acceptWaveForm(buffer, bytesRead)) {
                                System.out.println("识别结果: " + recognizer.getResult());
                            } else {
                                System.out.println("部分结果: " + recognizer.getPartialResult());
                            }
                        }
                    }

                    System.out.println("最终结果: " + recognizer.getFinalResult());
                    speakers.drain();
                } catch (Exception e) {
                    System.err.println("音频播放错误: " + e.getMessage());
                }
            } catch (Exception e) {
                System.err.println("音频输入错误: " + e.getMessage());
            }

        } catch (IOException e) {
            System.err.println("模型加载错误: " + e.getMessage());
        }
    }
}
