Skip to content

Commit cbcd477

Browse files
refactor: implement intelligent sound effect caching and reuse
1 parent 65faaf6 commit cbcd477

File tree

2 files changed

+36
-13
lines changed

2 files changed

+36
-13
lines changed

app/src/main/java/com/osfans/trime/data/soundeffect/SoundEffectManager.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import com.charleskorn.kaml.Yaml
88
import com.charleskorn.kaml.YamlConfiguration
99
import com.osfans.trime.data.base.DataManager
1010
import com.osfans.trime.data.prefs.AppPrefs
11+
import com.osfans.trime.ime.keyboard.InputFeedbackManager
1112
import com.osfans.trime.util.FileUtils
1213
import timber.log.Timber
1314
import java.io.File
@@ -62,6 +63,7 @@ object SoundEffectManager {
6263
}
6364
activeSoundEffect = effect
6465
soundEffectPref = name
66+
InputFeedbackManager.reloadSoundEffects()
6567
}
6668

6769
fun init() {

app/src/main/java/com/osfans/trime/ime/keyboard/InputFeedbackManager.kt

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import com.osfans.trime.data.soundeffect.SoundEffectManager
2222
import splitties.systemservices.audioManager
2323
import splitties.systemservices.vibrator
2424
import 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

Comments
 (0)