soundDirector.ts 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. import { type GameEffect } from '../core/gameResult'
  2. import { DEFAULT_GAME_AUDIO_CONFIG, type AudioCueKey, type GameAudioConfig } from './audioConfig'
  3. export class SoundDirector {
  4. enabled: boolean
  5. config: GameAudioConfig
  6. contexts: Partial<Record<AudioCueKey, WechatMiniprogram.InnerAudioContext>>
  7. loopTimers: Partial<Record<AudioCueKey, number>>
  8. backgroundLoopTimer: number
  9. activeGuidanceCue: AudioCueKey | null
  10. backgroundManager: WechatMiniprogram.BackgroundAudioManager | null
  11. appAudioMode: 'foreground' | 'background'
  12. constructor(config: GameAudioConfig = DEFAULT_GAME_AUDIO_CONFIG) {
  13. this.enabled = true
  14. this.config = config
  15. this.contexts = {}
  16. this.loopTimers = {}
  17. this.backgroundLoopTimer = 0
  18. this.activeGuidanceCue = null
  19. this.backgroundManager = null
  20. this.appAudioMode = 'foreground'
  21. }
  22. configure(config: GameAudioConfig): void {
  23. this.config = config
  24. this.resetContexts()
  25. }
  26. setEnabled(enabled: boolean): void {
  27. this.enabled = enabled
  28. }
  29. resetContexts(): void {
  30. const timerKeys = Object.keys(this.loopTimers) as AudioCueKey[]
  31. for (const key of timerKeys) {
  32. const timer = this.loopTimers[key]
  33. if (timer) {
  34. clearTimeout(timer)
  35. }
  36. }
  37. this.loopTimers = {}
  38. this.clearBackgroundLoopTimer()
  39. const keys = Object.keys(this.contexts) as AudioCueKey[]
  40. for (const key of keys) {
  41. const context = this.contexts[key]
  42. if (!context) {
  43. continue
  44. }
  45. context.stop()
  46. context.destroy()
  47. }
  48. this.contexts = {}
  49. this.activeGuidanceCue = null
  50. this.stopBackgroundGuidance()
  51. }
  52. destroy(): void {
  53. this.resetContexts()
  54. }
  55. handleEffects(effects: GameEffect[]): void {
  56. if (!this.enabled || !this.config.enabled || !effects.length) {
  57. return
  58. }
  59. const hasFinishCompletion = effects.some((effect) => effect.type === 'control_completed' && effect.controlKind === 'finish')
  60. for (const effect of effects) {
  61. if (effect.type === 'session_started') {
  62. this.play('session_started')
  63. continue
  64. }
  65. if (effect.type === 'session_cancelled') {
  66. this.stopGuidanceLoop()
  67. this.play('control_completed:finish')
  68. continue
  69. }
  70. if (effect.type === 'punch_feedback' && effect.tone === 'warning') {
  71. this.play('punch_feedback:warning')
  72. continue
  73. }
  74. if (effect.type === 'guidance_state_changed') {
  75. if (effect.guidanceState === 'searching') {
  76. this.startGuidanceLoop('guidance:searching')
  77. continue
  78. }
  79. if (effect.guidanceState === 'approaching') {
  80. this.startGuidanceLoop('guidance:approaching')
  81. continue
  82. }
  83. this.startGuidanceLoop('guidance:ready')
  84. continue
  85. }
  86. if (effect.type === 'control_completed') {
  87. this.stopGuidanceLoop()
  88. if (effect.controlKind === 'start') {
  89. this.play('control_completed:start')
  90. continue
  91. }
  92. if (effect.controlKind === 'finish') {
  93. this.play('control_completed:finish')
  94. continue
  95. }
  96. this.play('control_completed:control')
  97. continue
  98. }
  99. if (effect.type === 'session_finished') {
  100. this.stopGuidanceLoop()
  101. if (!hasFinishCompletion) {
  102. this.play('control_completed:finish')
  103. }
  104. }
  105. }
  106. }
  107. setAppAudioMode(mode: 'foreground' | 'background'): void {
  108. if (this.appAudioMode === mode) {
  109. return
  110. }
  111. this.appAudioMode = mode
  112. const activeGuidanceCue = this.activeGuidanceCue
  113. if (!activeGuidanceCue) {
  114. this.stopBackgroundGuidance()
  115. return
  116. }
  117. if (mode === 'background') {
  118. this.stopForegroundCue(activeGuidanceCue)
  119. this.startBackgroundGuidance(activeGuidanceCue)
  120. return
  121. }
  122. this.stopBackgroundGuidance()
  123. this.playForeground(activeGuidanceCue)
  124. }
  125. play(key: AudioCueKey): void {
  126. if (this.appAudioMode === 'background') {
  127. const cue = this.config.cues[key]
  128. if (!cue || cue.backgroundMode !== 'guidance' || !this.isGuidanceCue(key)) {
  129. return
  130. }
  131. this.startBackgroundGuidance(key)
  132. return
  133. }
  134. this.playForeground(key)
  135. }
  136. playForeground(key: AudioCueKey): void {
  137. const cue = this.config.cues[key]
  138. if (!cue || !cue.src) {
  139. return
  140. }
  141. this.clearLoopTimer(key)
  142. const context = this.getContext(key)
  143. context.stop()
  144. if (typeof context.seek === 'function') {
  145. context.seek(0)
  146. }
  147. context.volume = Math.max(0, Math.min(1, this.config.masterVolume * cue.volume))
  148. context.play()
  149. }
  150. startGuidanceLoop(key: AudioCueKey): void {
  151. if (this.activeGuidanceCue === key) {
  152. return
  153. }
  154. this.stopGuidanceLoop()
  155. this.activeGuidanceCue = key
  156. if (this.appAudioMode === 'background') {
  157. this.startBackgroundGuidance(key)
  158. return
  159. }
  160. this.playForeground(key)
  161. }
  162. stopGuidanceLoop(): void {
  163. if (!this.activeGuidanceCue) {
  164. this.stopBackgroundGuidance()
  165. return
  166. }
  167. this.clearLoopTimer(this.activeGuidanceCue)
  168. const context = this.contexts[this.activeGuidanceCue]
  169. if (context) {
  170. context.stop()
  171. if (typeof context.seek === 'function') {
  172. context.seek(0)
  173. }
  174. }
  175. this.stopBackgroundGuidance()
  176. this.activeGuidanceCue = null
  177. }
  178. clearLoopTimer(key: AudioCueKey): void {
  179. const timer = this.loopTimers[key]
  180. if (timer) {
  181. clearTimeout(timer)
  182. delete this.loopTimers[key]
  183. }
  184. }
  185. handleCueEnded(key: AudioCueKey): void {
  186. const cue = this.config.cues[key]
  187. if (!cue.loop || this.activeGuidanceCue !== key || !this.enabled || !this.config.enabled) {
  188. return
  189. }
  190. this.clearLoopTimer(key)
  191. this.loopTimers[key] = setTimeout(() => {
  192. delete this.loopTimers[key]
  193. if (this.activeGuidanceCue === key && this.enabled && this.config.enabled) {
  194. this.play(key)
  195. }
  196. }, cue.loopGapMs) as unknown as number
  197. }
  198. handleBackgroundCueEnded(): void {
  199. const key = this.activeGuidanceCue
  200. if (!key || !this.enabled || !this.config.enabled || this.appAudioMode !== 'background') {
  201. return
  202. }
  203. const cue = this.config.cues[key]
  204. if (!cue || !cue.loop) {
  205. return
  206. }
  207. this.clearBackgroundLoopTimer()
  208. this.backgroundLoopTimer = setTimeout(() => {
  209. this.backgroundLoopTimer = 0
  210. if (this.activeGuidanceCue === key && this.appAudioMode === 'background' && this.enabled && this.config.enabled) {
  211. this.playBackgroundCue(key)
  212. }
  213. }, cue.loopGapMs) as unknown as number
  214. }
  215. clearBackgroundLoopTimer(): void {
  216. if (this.backgroundLoopTimer) {
  217. clearTimeout(this.backgroundLoopTimer)
  218. this.backgroundLoopTimer = 0
  219. }
  220. }
  221. stopForegroundCue(key: AudioCueKey): void {
  222. this.clearLoopTimer(key)
  223. const context = this.contexts[key]
  224. if (!context) {
  225. return
  226. }
  227. context.stop()
  228. if (typeof context.seek === 'function') {
  229. context.seek(0)
  230. }
  231. }
  232. isGuidanceCue(key: AudioCueKey): boolean {
  233. return key === 'guidance:searching'
  234. || key === 'guidance:approaching'
  235. || key === 'guidance:ready'
  236. }
  237. startBackgroundGuidance(key: AudioCueKey): void {
  238. if (!this.enabled || !this.config.enabled || !this.config.backgroundAudioEnabled) {
  239. return
  240. }
  241. const cue = this.config.cues[key]
  242. if (!cue || cue.backgroundMode !== 'guidance' || !cue.src) {
  243. return
  244. }
  245. this.playBackgroundCue(key)
  246. }
  247. playBackgroundCue(key: AudioCueKey): void {
  248. const cue = this.config.cues[key]
  249. if (!cue || !cue.src) {
  250. return
  251. }
  252. const manager = this.getBackgroundManager()
  253. this.clearBackgroundLoopTimer()
  254. manager.stop()
  255. manager.title = 'ColorMapRun 引导音'
  256. manager.epname = 'ColorMapRun'
  257. manager.singer = 'ColorMapRun'
  258. manager.coverImgUrl = ''
  259. manager.src = cue.src
  260. manager.play()
  261. }
  262. stopBackgroundGuidance(): void {
  263. this.clearBackgroundLoopTimer()
  264. if (!this.backgroundManager) {
  265. return
  266. }
  267. this.backgroundManager.stop()
  268. }
  269. getBackgroundManager(): WechatMiniprogram.BackgroundAudioManager {
  270. if (this.backgroundManager) {
  271. return this.backgroundManager
  272. }
  273. const manager = wx.getBackgroundAudioManager()
  274. if (typeof manager.onEnded === 'function') {
  275. manager.onEnded(() => {
  276. this.handleBackgroundCueEnded()
  277. })
  278. }
  279. this.backgroundManager = manager
  280. return manager
  281. }
  282. getContext(key: AudioCueKey): WechatMiniprogram.InnerAudioContext {
  283. const existing = this.contexts[key]
  284. if (existing) {
  285. return existing
  286. }
  287. const cue = this.config.cues[key]
  288. const context = wx.createInnerAudioContext()
  289. context.src = cue.src
  290. context.autoplay = false
  291. context.loop = false
  292. context.obeyMuteSwitch = this.config.obeyMuteSwitch
  293. if (typeof context.onEnded === 'function') {
  294. context.onEnded(() => {
  295. this.handleCueEnded(key)
  296. })
  297. }
  298. context.volume = Math.max(0, Math.min(1, this.config.masterVolume * cue.volume))
  299. this.contexts[key] = context
  300. return context
  301. }
  302. }