lincollincol / compose-audiowaveform

Audio waveform library for Jetpack Compose
Apache License 2.0
199 stars 12 forks source link

App Crash v1.1.1 - normalize() function returns NaN #14

Closed bam-e22 closed 1 year ago

bam-e22 commented 1 year ago

App crashes when using silent audio source ex) amplitudes = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

java.lang.IllegalStateException: AnimationVector cannot contain a NaN. AnimationVector1D: value = NaN. Animation: TargetBasedAnimation: 1.0 -> NaN,initial velocity: AnimationVector1D: value = 0.0, duration: 500 ms,animationSpec: androidx.compose.animation.core.VectorizedTweenSpec@64fbf4d, playTimeNanos: 0
    at androidx.compose.animation.core.TargetBasedAnimation.getValueFromNanos(Animation.kt:242)
    at androidx.compose.animation.core.SuspendAnimationKt.animate(SuspendAnimation.kt:233)
    at androidx.compose.animation.core.Animatable$runAnimation$2.invokeSuspend(Animatable.kt:305)
    at androidx.compose.animation.core.Animatable$runAnimation$2.invoke(Unknown Source:8)
    at androidx.compose.animation.core.Animatable$runAnimation$2.invoke(Unknown Source:2)
    at androidx.compose.animation.core.MutatorMutex$mutate$2.invokeSuspend(InternalMutatorMutex.kt:119)
    at androidx.compose.animation.core.MutatorMutex$mutate$2.invoke(Unknown Source:8)
    at androidx.compose.animation.core.MutatorMutex$mutate$2.invoke(Unknown Source:4)
    at kotlinx.coroutines.intrinsics.UndispatchedKt.startUndispatchedOrReturn(Undispatched.kt:89)
    at kotlinx.coroutines.CoroutineScopeKt.coroutineScope(CoroutineScope.kt:264)
    at androidx.compose.animation.core.MutatorMutex.mutate(InternalMutatorMutex.kt:112)
    at androidx.compose.animation.core.MutatorMutex.mutate$default(InternalMutatorMutex.kt:109)
    at androidx.compose.animation.core.Animatable.runAnimation(Animatable.kt:295)
    at androidx.compose.animation.core.Animatable.animateTo(Animatable.kt:238)
    at androidx.compose.animation.core.Animatable.animateTo$default(Animatable.kt:225)
    at androidx.compose.animation.core.AnimateAsStateKt$animateValueAsState$3$1.invokeSuspend(AnimateAsState.kt:428)
    at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
    at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
    at androidx.compose.ui.platform.AndroidUiDispatcher.performTrampolineDispatch(AndroidUiDispatcher.android.kt:81)
    at androidx.compose.ui.platform.AndroidUiDispatcher.access$performTrampolineDispatch(AndroidUiDispatcher.android.kt:41)
    at androidx.compose.ui.platform.AndroidUiDispatcher$dispatchCallback$1.run(AndroidUiDispatcher.android.kt:57)
    at android.os.Handler.handleCallback(Handler.java:942)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loopOnce(Looper.java:201)
    at android.os.Looper.loop(Looper.java:288)
    at android.app.ActivityThread.main(ActivityThread.java:7872)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)
thedroiddiv commented 1 year ago

The issue is when minHeight = maxHeight, the diff becomes 0 and div by zero returns NaN, see #L147

thedroiddiv commented 1 year ago

For now I'm just using a workaround normalize(minHeight, maxHeight).map { if (it.isNaN()) 0f else it }

Xing1P commented 1 year ago

I face this issue too

thedroiddiv commented 1 year ago

@Xing1P for now you can try this fix https://github.com/lincollincol/compose-audiowaveform/pull/20/files

thedroiddiv commented 1 year ago

@lincollincol just a friendly reminder. If you're free, please have a look at #20