Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import com.charleskorn.kaml.Yaml
import com.charleskorn.kaml.YamlConfiguration
import com.osfans.trime.data.base.DataManager
import com.osfans.trime.data.prefs.AppPrefs
import com.osfans.trime.ime.keyboard.InputFeedbackManager
import com.osfans.trime.util.FileUtils
import timber.log.Timber
import java.io.File
Expand Down Expand Up @@ -62,6 +63,7 @@ object SoundEffectManager {
}
activeSoundEffect = effect
soundEffectPref = name
InputFeedbackManager.reloadSoundEffects()
}

fun init() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import com.osfans.trime.data.soundeffect.SoundEffectManager
import splitties.systemservices.audioManager
import splitties.systemservices.vibrator
import timber.log.Timber
import java.util.concurrent.ConcurrentHashMap

/**
* Manage the key press effects, such as vibration, sound, speaking and so on.
Expand All @@ -33,18 +34,19 @@ object InputFeedbackManager {
private var soundPool: SoundPool? = null

private var effectPlayProgress = 0
private val cachedSoundIds = SparseIntArray(30)
private val cachedSoundIds = SparseIntArray()

private val loadedSounds = ConcurrentHashMap<String, Int>()
private var effectHash = 0

fun init(context: Context) {
try {
tts = TextToSpeech(context, null)
soundPool =
SoundPool
.Builder()
SoundPool.Builder()
.setMaxStreams(3)
.setAudioAttributes(
AudioAttributes
.Builder()
AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_ASSISTANCE_SONIFICATION)
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.build(),
Expand All @@ -55,19 +57,35 @@ object InputFeedbackManager {
}

private fun cacheSoundId() {
if (!soundEffectEnabled) return

if (SoundEffectManager.activeSoundEffect == null) {
SoundEffectManager.init()
}

val paths = SoundEffectManager.activeAudioPaths
val hash = paths.hashCode()

if (hash == effectHash) return

cachedSoundIds.clear()
SoundEffectManager.activeAudioPaths.forEachIndexed { i, path ->
val id = soundPool?.load(path, 1) ?: 0
if (id != 0 && !cachedSoundIds.containsValue(id)) {
cachedSoundIds.put(i, id)

paths.forEachIndexed { i, path ->
val soundId = loadedSounds.getOrPut(path) { soundPool?.load(path, 1) ?: 0 }
if (soundId != 0 && !cachedSoundIds.containsValue(soundId)) {
cachedSoundIds.put(i, soundId)
}
}

effectHash = hash
}

fun startInput() {
if (SoundEffectManager.activeSoundEffect == null) {
SoundEffectManager.init()
}
cacheSoundId()
}

fun reloadSoundEffects() {
effectHash = 0
cacheSoundId()
}

Expand Down Expand Up @@ -131,7 +149,6 @@ object InputFeedbackManager {
break
}
}
Timber.d("without melody: index: $index, sounds.size=${sounds.size}")
index
}
}
Expand Down Expand Up @@ -212,5 +229,9 @@ object InputFeedbackManager {
tts = null
soundPool?.release()
soundPool = null

loadedSounds.clear()
cachedSoundIds.clear()
effectHash = 0
}
}
Loading