Skip to content
Open
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 @@ -95,7 +95,7 @@ class McUiSoundService : UiSoundService {
override fun startAmbience(theme: UiSoundTheme, volume: Float) {
try {
val current = ambience
if (current != null && !current.isStopped && current.theme == theme && mc.soundManager.isActive(current)) {
if (current != null && !current.isStopped && current.theme == theme && isAlive(current)) {
current.updateTargetVolume(ambienceVolumeFor(theme, volume))
current.cancelFadeOut()
UiSoundDucking.setActive(true)
Expand Down Expand Up @@ -197,7 +197,8 @@ class McUiSoundService : UiSoundService {
try {
val current = ambience ?: return@execute
if (current.isStopped || current.isFadingOut || current !== ambience) return@execute
if (!mc.soundManager.isActive(current)) {
if (!isAlive(current)) {
mc.soundManager.stop(current)
playAmbienceInstance(current.theme, current.targetVolume, fadeIn = !mc.isPaused)
} else if (current.isStalledFadeIn) {
mc.soundManager.stop(current)
Expand All @@ -217,6 +218,18 @@ class McUiSoundService : UiSoundService {
thread.start()
}

// 1.8.9 starts streams on Paulscode's threads, so a fresh stream reports inactive until its preload ends.
// Stopping it then is lost: preload finishes afterwards and starts the channel untracked.
// A zero-volume play is skipped by the engine, so it never starts and gets no grace.
private fun isAlive(instance: FadingLoopSoundInstance): Boolean {
if (mc.soundManager.isActive(instance)) {
instance.seenActive = true
return true
}
return !instance.startedSilent && !instance.seenActive &&
System.currentTimeMillis() - instance.startedAt < START_GRACE_MS
}

private val musicRefreshUntil = AtomicLong(0)

@Volatile
Expand Down Expand Up @@ -280,6 +293,13 @@ class McUiSoundService : UiSoundService {
@Volatile
private var fadeOutStep = 0f

val startedAt = System.currentTimeMillis()

val startedSilent = targetVolume <= 0f

@Volatile
var seenActive = false

val isFadingOut: Boolean
get() = fadingOut

Expand Down Expand Up @@ -364,5 +384,6 @@ class McUiSoundService : UiSoundService {
const val FADE_IN_TICKS = 30f
const val FADE_IN_FLOOR = 0.0001f
const val STALL_NANOS = 400_000_000L
const val START_GRACE_MS = 5000L
}
}