Skip to content

Commit

Permalink
Sound issues fixes.
Browse files Browse the repository at this point in the history
+ Fix when music player block is broken and it's sound doesn't fade out.
+ Fix fade in.
  • Loading branch information
EngineMachiner committed Oct 4, 2023
1 parent 8e8e4b2 commit b49375a
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
2 changes: 2 additions & 0 deletions src/main/kotlin/com/enginemachiner/honkytones/Entity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ interface CanBeMuted {

fun isMuted(entity: Entity): Boolean {

if ( entity.isRemoved ) return false

if ( entity is MusicPlayerEntity ) {

val musicPlayer = world()!!.getBlockEntity( entity.blockPos )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import MarkErrorInputStream
import com.enginemachiner.honkytones.*
import com.enginemachiner.honkytones.CanBeMuted.Companion.isMuted
import com.enginemachiner.honkytones.blocks.musicplayer.MusicPlayer
import com.enginemachiner.honkytones.blocks.musicplayer.MusicPlayerBlockEntity
import com.squareup.okhttp.OkHttpClient
import com.squareup.okhttp.Request
import net.fabricmc.api.EnvType
Expand Down Expand Up @@ -103,9 +104,13 @@ class ExternalSound( private val musicPlayer: MusicPlayer ) : FadingSound("audio

private var nbtVolume = 1f; private var audioStream: AudioStream? = null

private var blockEntity: MusicPlayerBlockEntity? = null

private fun init() {

entity = musicPlayer.blockEntity!!.entity
blockEntity = musicPlayer.blockEntity

entity = blockEntity!!.entity

try { audioStream = getAudioStream() } catch (e: Exception) {

Expand Down Expand Up @@ -145,7 +150,7 @@ class ExternalSound( private val musicPlayer: MusicPlayer ) : FadingSound("audio

}

override fun fadeOut() { if ( !musicPlayer.blockEntity!!.repeatOnPlay ) super.fadeOut() else stop() }
override fun fadeOut() { if ( !blockEntity!!.repeatOnPlay ) super.fadeOut() else stop() }

override fun tick() {

Expand Down
17 changes: 9 additions & 8 deletions src/main/kotlin/com/enginemachiner/honkytones/sound/Sound.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ open class FadingSound( val path: String ) : MovingSoundInstance(
var entity: Entity? = null; var maxVolume = 1f
private var fadeIn = false; private var fadeOut = false
var shouldNetwork = true; var canReplay = true
private var timesStopped = 0; private var lastVolume: Float? = null
private var timesStopped = 0; private var volumeRate = maxVolume

private var isPlaying = false; protected var pos: Vec3d = Vec3d.ZERO

Expand All @@ -45,15 +45,13 @@ open class FadingSound( val path: String ) : MovingSoundInstance(

private fun getRate(): Float {

if ( lastVolume == null ) lastVolume = volume

return 0.125f * lastVolume!! / ( timesStopped + 1 )
return 0.125f * volumeRate / ( timesStopped + 1 )

}

private fun fadeInTick() {

if ( !fadeIn ) return
if ( !fadeIn || fadeOut ) return

val nextVolume = volume + getRate()
if ( nextVolume < maxVolume ) volume = nextVolume else fadeIn = false
Expand Down Expand Up @@ -91,17 +89,20 @@ open class FadingSound( val path: String ) : MovingSoundInstance(

getManager().stop(this); volume = maxVolume

if ( !canReplay ) setDone(); lastVolume = null
if ( !canReplay ) setDone()

}

fun fadeIn() { fadeIn = true; volume = 0f }
open fun fadeIn() { fadeIn = true; volume = 0f }

open fun fadeOut() {

fadeOut = true; if ( !canNetwork() || !shouldNetwork ) return
fadeOut = true; volumeRate = volume

if ( !canNetwork() || !shouldNetwork ) return

fadeOutOnClients()

}

fun setPitch(f: Float) { pitch = f }
Expand Down

0 comments on commit b49375a

Please sign in to comment.