Merge pull request #36 from Myzel394/fix-lag

Fix lag
This commit is contained in:
Myzel394 2023-10-24 19:54:27 +02:00 committed by GitHub
commit b12aeaeec8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 6 deletions

View File

@ -9,7 +9,7 @@ import java.util.concurrent.Executors
import java.util.concurrent.ScheduledExecutorService
import java.util.concurrent.TimeUnit
abstract class ExtraRecorderInformationService: RecorderService() {
abstract class ExtraRecorderInformationService : RecorderService() {
abstract fun getAmplitudeAmount(): Int
abstract fun getAmplitude(): Int
@ -30,7 +30,11 @@ abstract class ExtraRecorderInformationService: RecorderService() {
// Delete old amplitudes
if (amplitudes.size > getAmplitudeAmount()) {
amplitudes.drop(amplitudes.size - getAmplitudeAmount())
// Should be more efficient than dropping the elements, getting a new list
// clearing old list and adding new elements to it
repeat(amplitudes.size - getAmplitudeAmount()) {
amplitudes.removeAt(0)
}
}
handler.postDelayed(::updateAmplitude, 100)

View File

@ -38,7 +38,7 @@ fun RealtimeAudioVisualizer(
audioRecorder: AudioRecorderModel,
) {
val scope = rememberCoroutineScope()
val amplitudes = audioRecorder.amplitudes!!
val amplitudes = audioRecorder.amplitudes
val primary = MaterialTheme.colorScheme.primary
val primaryMuted = primary.copy(alpha = 0.3f)
@ -63,7 +63,7 @@ fun RealtimeAudioVisualizer(
}
val configuration = LocalConfiguration.current
val screenWidth = with (LocalDensity.current) {configuration.screenWidthDp.dp.toPx()}
val screenWidth = with(LocalDensity.current) { configuration.screenWidthDp.dp.toPx() }
LaunchedEffect(screenWidth) {
// Add 1 to allow the visualizer to overflow the screen
@ -86,9 +86,10 @@ fun RealtimeAudioVisualizer(
val isOverThreshold = offset >= GROW_START_INDEX
val horizontalProgress = (
clamp(horizontalValue, GROW_START, GROW_END)
- GROW_START) / (GROW_END - GROW_START)
- GROW_START) / (GROW_END - GROW_START)
val amplitudePercentage = (amplitude.toFloat() / MAX_AMPLITUDE).coerceAtMost(1f)
val boxHeight = (height * amplitudePercentage * horizontalProgress).coerceAtLeast(15f)
val boxHeight =
(height * amplitudePercentage * horizontalProgress).coerceAtLeast(15f)
drawRoundRect(
color = if (amplitudePercentage > 0.05f && isOverThreshold) primary else primaryMuted,