@@ -22,6 +22,7 @@ import com.osfans.trime.data.soundeffect.SoundEffectManager
2222import splitties.systemservices.audioManager
2323import splitties.systemservices.vibrator
2424import timber.log.Timber
25+ import java.util.concurrent.ConcurrentHashMap
2526
2627/* *
2728 * Manage the key press effects, such as vibration, sound, speaking and so on.
@@ -33,18 +34,19 @@ object InputFeedbackManager {
3334 private var soundPool: SoundPool ? = null
3435
3536 private var effectPlayProgress = 0
36- private val cachedSoundIds = SparseIntArray (30 )
37+ private val cachedSoundIds = SparseIntArray ()
38+
39+ private val loadedSounds = ConcurrentHashMap <String , Int >()
40+ private var effectHash = 0
3741
3842 fun init (context : Context ) {
3943 try {
4044 tts = TextToSpeech (context, null )
4145 soundPool =
42- SoundPool
43- .Builder ()
46+ SoundPool .Builder ()
4447 .setMaxStreams(3 )
4548 .setAudioAttributes(
46- AudioAttributes
47- .Builder ()
49+ AudioAttributes .Builder ()
4850 .setUsage(AudioAttributes .USAGE_ASSISTANCE_SONIFICATION )
4951 .setContentType(AudioAttributes .CONTENT_TYPE_SONIFICATION )
5052 .build(),
@@ -55,19 +57,35 @@ object InputFeedbackManager {
5557 }
5658
5759 private fun cacheSoundId () {
60+ if (! soundEffectEnabled) return
61+
62+ if (SoundEffectManager .activeSoundEffect == null ) {
63+ SoundEffectManager .init ()
64+ }
65+
66+ val paths = SoundEffectManager .activeAudioPaths
67+ val hash = paths.hashCode()
68+
69+ if (hash == effectHash) return
70+
5871 cachedSoundIds.clear()
59- SoundEffectManager .activeAudioPaths.forEachIndexed { i, path ->
60- val id = soundPool?.load(path, 1 ) ? : 0
61- if (id != 0 && ! cachedSoundIds.containsValue(id)) {
62- cachedSoundIds.put(i, id)
72+
73+ paths.forEachIndexed { i, path ->
74+ val soundId = loadedSounds.getOrPut(path) { soundPool?.load(path, 1 ) ? : 0 }
75+ if (soundId != 0 && ! cachedSoundIds.containsValue(soundId)) {
76+ cachedSoundIds.put(i, soundId)
6377 }
6478 }
79+
80+ effectHash = hash
6581 }
6682
6783 fun startInput () {
68- if (SoundEffectManager .activeSoundEffect == null ) {
69- SoundEffectManager .init ()
70- }
84+ cacheSoundId()
85+ }
86+
87+ fun reloadSoundEffects () {
88+ effectHash = 0
7189 cacheSoundId()
7290 }
7391
@@ -131,7 +149,6 @@ object InputFeedbackManager {
131149 break
132150 }
133151 }
134- Timber .d(" without melody: index: $index , sounds.size=${sounds.size} " )
135152 index
136153 }
137154 }
@@ -212,5 +229,9 @@ object InputFeedbackManager {
212229 tts = null
213230 soundPool?.release()
214231 soundPool = null
232+
233+ loadedSounds.clear()
234+ cachedSoundIds.clear()
235+ effectHash = 0
215236 }
216237}
0 commit comments