#pragma once #include "audio_service.h" #include #include class Protocol; // fwd-decl to avoid pulling protocol.h enum PromptGroup { kPromptWelcome, kPromptShutdown, kPromptNoNetwork, kPromptError, kPromptWifiConfig, kPromptAlarmSet, kPromptAlarmWake, kPromptDeviceAlreadyBound, kPromptBoot, // 长按开机 - 立即播放,不等 WebSocket kPromptServerError, // 服务器连接失败(404 / 超时) kPromptStoryExpired, // 故事断点过期提示 kPromptWakeResponse, // 唤醒应答 - 每次 wake word 触发后播放("我在。") kPromptIdleCheck, // 空闲提醒 - T+3min 无活动时播放("你还在吗?小朋友") kPromptIdleGoodbye, // 空闲告别 - T+10min 无活动时播放("那我先退下来…"),播完进 light sleep kPromptWifiSuccess, // 配网成功 - BLE 配网联通 WiFi 瞬间播放("配网成功啦。") kPromptLowBattery20, // 低电量提醒 - 电量降到 20% 时播放(每 10 分钟提醒一次) kPromptLowBattery10, // 低电量关机 - 电量降到 10% 时播放后关机 // Activation-code sequence playback: spoken prompt + per-digit OGGs kPromptActivationDevice, kPromptDigit0, kPromptDigit1, kPromptDigit2, kPromptDigit3, kPromptDigit4, kPromptDigit5, kPromptDigit6, kPromptDigit7, kPromptDigit8, kPromptDigit9, }; class AudioPrompt { public: // Check if local prompts are available bool HasPrompts(PromptGroup group) const; // Play a random prompt from the group through the audio pipeline bool PlayRandom(PromptGroup group, AudioService& audio); // Play a specific prompt by index (0-based). Falls back to PlayRandom if index invalid. bool PlayByIndex(PromptGroup group, int index, AudioService& audio); // Parse num_frames from the embedded-opus header so callers can derive // playback duration (each frame = 20ms audio). Returns 0 on invalid // group/index — caller must skip that item. int GetFrameCount(PromptGroup group, int index) const; // Parse embedded Opus frames and push to decode queue. `my_generation` is // captured at task launch (right after BeginNewPlayback bumped it) and // checked each frame; mismatch with AudioService::CurrentPlaybackGeneration // means a newer playback cycle started and this task must self-exit. bool PlayEmbeddedOpus(const uint8_t* data, size_t size, AudioService& audio, int my_generation); // === ADR-004 anti-mixing: unified local playback with server signalling === // // PlayLocal wraps PlayRandom/PlayByIndex and sends // {"type":"play_local","state":"start","group":""} // to the server so the server can stop any in-flight TTS / music stream, // preventing overlap with the local OGG. Caller must call NotifyDone() // after WaitForPlaybackComplete (PlayTask is async, so "done" must be // emitted on completion, not here). // // If protocol is null or WS not open, falls back silently to PlayRandom — // the local prompt still plays, just without the server hand-shake. This // covers boot prompt / connection-failure prompts where WS doesn't exist // yet (no server in flight = no mixing risk anyway). // // group_name: the string server would see (e.g. "welcome", "alarm_wake"). // Must match the PromptGroup argument semantically — used only for the // server message body. bool PlayLocal(PromptGroup group, AudioService& audio, Protocol* protocol, const std::string& group_name); // Variant that picks a specific index inside the group. bool PlayLocalByIndex(PromptGroup group, int index, AudioService& audio, Protocol* protocol, const std::string& group_name); // Send the "done" notification. Must be called by the caller after // WaitForPlaybackComplete or equivalent, so server unlocks TTS lane. // Safe to call with null/closed protocol (no-op). void NotifyLocalDone(Protocol* protocol, const std::string& group_name); };