feat(voice): 语音参数移入 config.js + 设置页男女声切换
- config: 新增 tts.female/male 两组参数(voiceType/volume/speed), 直接改数字即可调音色,无需改逻辑代码 - storage: getSettings 兜底 voiceGender(旧用户迁移为女声) - voice: 新增 _getTtsParams 读 config+性别;缓存 key 含完整参数, 改音色/音量/性别自动重新合成,无需手动清缓存 - settings: 训练偏好加"语音音色"segmented(女声/男声) - tts 云函数: _synthesize 用传入 opts 合成;_upload 路径含 voiceType (男/女不同文件);需重新部署 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
+30
-6
@@ -17,6 +17,8 @@
|
||||
* the user with toast errors mid-plank.
|
||||
*/
|
||||
|
||||
const config = require('../config')
|
||||
|
||||
const PROMPTS = {
|
||||
start: '训练已开始',
|
||||
halfway: '已完成一半啦,坚持就是胜利!',
|
||||
@@ -58,34 +60,56 @@ const _getCtx = () => {
|
||||
return _ctx
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve the TTS params (voiceType/volume/speed) for the user's chosen
|
||||
* voice gender from config.js. Cached fileIDs are keyed on these params,
|
||||
* so changing them (or switching gender) auto-invalidates stale audio.
|
||||
*/
|
||||
const _getTtsParams = () => {
|
||||
const s = wx.getStorageSync('user_settings') || {}
|
||||
const gender = s.voiceGender === 'male' ? 'male' : 'female'
|
||||
const params = (config.tts && config.tts[gender]) || config.tts.female
|
||||
return {
|
||||
voiceType: params.voiceType,
|
||||
volume: params.volume,
|
||||
speed: params.speed
|
||||
}
|
||||
}
|
||||
|
||||
const _fetchUrl = async (promptKey) => {
|
||||
const { voiceType, volume, speed } = _getTtsParams()
|
||||
// Key the cache on the full param set so changing voiceType/volume/speed
|
||||
// (or switching gender) automatically re-synthesizes instead of serving
|
||||
// stale audio. No manual cache clearing needed.
|
||||
const cacheKey = `${promptKey}:${voiceType}:${volume}:${speed}`
|
||||
|
||||
// 1. In-memory cache: same-session instant return
|
||||
if (_urlCache[promptKey]) return _urlCache[promptKey]
|
||||
if (_urlCache[cacheKey]) return _urlCache[cacheKey]
|
||||
|
||||
// 2. Persistent fileID cache: pass to cloud function for instant getTempFileURL
|
||||
_loadFileIDCache()
|
||||
const cachedFileID = _fileIDCache[promptKey] || null
|
||||
const cachedFileID = _fileIDCache[cacheKey] || null
|
||||
|
||||
try {
|
||||
const res = await wx.cloud.callFunction({
|
||||
name: 'tts',
|
||||
data: { promptKey, fileID: cachedFileID },
|
||||
data: { promptKey, fileID: cachedFileID, voiceType, volume, speed },
|
||||
// Timeout: 30s for first synthesis (cold start), <1s for cached
|
||||
config: { timeout: 30000 }
|
||||
})
|
||||
if (res && res.result && res.result.success && res.result.audioUrl) {
|
||||
_urlCache[promptKey] = res.result.audioUrl
|
||||
_urlCache[cacheKey] = res.result.audioUrl
|
||||
|
||||
// Persist the fileID if we got a new one (first synthesis or
|
||||
// re-synthesis after cache invalidation).
|
||||
if (res.result.fileID && res.result.fileID !== cachedFileID) {
|
||||
_fileIDCache[promptKey] = res.result.fileID
|
||||
_fileIDCache[cacheKey] = res.result.fileID
|
||||
_saveFileIDCache()
|
||||
}
|
||||
return res.result.audioUrl
|
||||
}
|
||||
} catch (e) {
|
||||
// cloud function not deployed or other error — silent fallback
|
||||
// cloud function not deployed or other error - silent fallback
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user