Skip to content

Commit

Permalink
Merge pull request #78 from ZTFtrue/develope
Browse files Browse the repository at this point in the history
optimize animation
  • Loading branch information
ZTFtrue authored Oct 18, 2024
2 parents bcaff94 + 63e5a50 commit f0321f3
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 62 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ android {
applicationId = "com.ztftrue.music"
minSdk = 30
targetSdk = 34
versionCode = 36
versionName = "0.1.36"
versionCode = 37
versionName = "0.1.37"

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables {
Expand Down
1 change: 0 additions & 1 deletion app/src/main/java/com/ztftrue/music/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import android.support.v4.media.MediaMetadataCompat
import android.support.v4.media.session.MediaControllerCompat
import android.support.v4.media.session.MediaSessionCompat
import android.support.v4.media.session.PlaybackStateCompat
import android.util.Log
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.result.ActivityResultLauncher
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import android.os.Bundle
import android.support.v4.media.session.MediaSessionCompat
import android.util.Log
import androidx.media3.common.C
import androidx.media3.common.Format
import androidx.media3.common.audio.AudioProcessor
import androidx.media3.common.util.UnstableApi
import be.tarsos.dsp.io.TarsosDSPAudioFloatConverter
Expand All @@ -15,11 +14,7 @@ import com.ztftrue.music.effects.SoundUtils.getOutputSize
import com.ztftrue.music.play.EVENT_Visualization_Change
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.awaitAll
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext
import java.nio.ByteBuffer
import java.nio.ByteOrder

Expand All @@ -29,7 +24,8 @@ class VisualizationAudioProcessor(private var mediaSession: MediaSessionCompat?)

private var active = false
private var outputAudioFormat: AudioProcessor.AudioFormat? = null

var r = 2
var BYTES_PER_SAMPLE: Int = 2
private var sampleBuffer: FloatArray = FloatArray(0)

private var bufferSize = 2048
Expand All @@ -44,8 +40,8 @@ class VisualizationAudioProcessor(private var mediaSession: MediaSessionCompat?)
outputAudioFormat = AudioProcessor.AudioFormat.NOT_SET
}

fun setMediaSession(mediaSession: MediaSessionCompat) {
this.mediaSession=mediaSession
fun setMediaSession(mediaSession: MediaSessionCompat) {
this.mediaSession = mediaSession
}

fun setActive(active: Boolean) {
Expand All @@ -54,7 +50,6 @@ class VisualizationAudioProcessor(private var mediaSession: MediaSessionCompat?)
}
}

var r = 2

override fun configure(inputAudioFormat: AudioProcessor.AudioFormat): AudioProcessor.AudioFormat {
// TODO need support more encoding
Expand Down Expand Up @@ -84,10 +79,9 @@ class VisualizationAudioProcessor(private var mediaSession: MediaSessionCompat?)
TarsosDSPAudioFloatConverter.getConverter(
tarsosDSPAudioFormat
)
leftMax = 1.0
rightMax = 1.0

BYTES_PER_SAMPLE = getBytePerSample(outputAudioFormat!!.encoding)
readSize = getOutputSize(outputAudioFormat!!, BYTES_PER_SAMPLE)/10
readSize = getOutputSize(outputAudioFormat!!, BYTES_PER_SAMPLE) / 10
pcmToFrequencyDomain =
PCMToFrequencyDomain(
readSize / (outputAudioFormat!!.channelCount * BYTES_PER_SAMPLE),
Expand All @@ -108,12 +102,12 @@ class VisualizationAudioProcessor(private var mediaSession: MediaSessionCompat?)
if (!inputBuffer.hasRemaining()) {
return
}
this.outputBuffer = inputBuffer
if (dataBuffer.remaining() < 1 || dataBuffer.remaining() < inputBuffer.limit()) {
expandBuffer(dataBuffer.capacity() + inputBuffer.limit() * 2);
Log.d("ExpandBuffer", dataBuffer.remaining().toString())
}
dataBuffer.put(inputBuffer)

dataBuffer.put(inputBuffer.array())
}

private fun expandBuffer(newCapacity: Int) {
Expand All @@ -125,28 +119,18 @@ class VisualizationAudioProcessor(private var mediaSession: MediaSessionCompat?)

override fun queueEndOfStream() {
// TODO
dataBuffer.flip()
val processedBuffer = ByteBuffer.allocate(dataBuffer.limit())
processedBuffer.put(dataBuffer)
this.outputBuffer = processedBuffer
dataBuffer.compact()
// val processedBuffer = ByteBuffer.allocate(dataBuffer.limit())
// processedBuffer.put(dataBuffer)
// this.outputBuffer = processedBuffer
// dataBuffer.compact()
inputEnded = true
}

var BYTES_PER_SAMPLE: Int = 2

private var leftMax = 1.0
private var rightMax = 1.0


override fun getOutput(): ByteBuffer {
processData()
// if (outputBuffer.position() == 0) {
// return EMPTY_BUFFER;
// }
val outputBuffer: ByteBuffer = this.outputBuffer
this.outputBuffer = AudioProcessor.EMPTY_BUFFER
outputBuffer.flip()
// this.outputBuffer = AudioProcessor.EMPTY_BUFFER
return outputBuffer
}

Expand Down Expand Up @@ -180,25 +164,9 @@ class VisualizationAudioProcessor(private var mediaSession: MediaSessionCompat?)
bundle.putFloatArray("magnitude", m)
mediaSession?.setExtras(bundle)
}
this.outputBuffer = processedBuffer
}
dataBuffer.compact()
}
} else {
dataBuffer.flip()
if (dataBuffer.hasRemaining()) {
val dataRemaining = dataBuffer.remaining() // 获取 dataBuffer 中剩余的有效数据量
val readSize =
if (dataRemaining > bufferSize) bufferSize else dataRemaining // 确定实际读取量
val processedBuffer: ByteBuffer = ByteBuffer.allocate(readSize)
val oldLimit = dataBuffer.limit() // 记录旧的 limit
dataBuffer.limit(dataBuffer.position() + readSize) // 设置新 limit 来控制读取量
processedBuffer.put(dataBuffer)
dataBuffer.limit(oldLimit)
processedBuffer.order(ByteOrder.nativeOrder())
this.outputBuffer = processedBuffer
}
dataBuffer.compact()
}
}

Expand Down
31 changes: 17 additions & 14 deletions app/src/main/java/com/ztftrue/music/ui/play/CoverView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ fun CoverView(musicViewModel: MusicViewModel) {
val showOtherMessage = remember { mutableStateOf(false) }
val magnitudes = remember { musicViewModel.musicVisualizationData }
val dropCount = 100 // 字符数量
val drops = remember { MutableList(dropCount) { mutableStateListOf<Drop>() } }
val drops = remember { mutableStateListOf(mutableStateListOf<Drop>()) }
val columnSpacing = 80f // 列之间的间隔
val dropHeight = 80f // 字符的高度间隔
val textSizeSet = 60f
Expand All @@ -78,13 +78,11 @@ fun CoverView(musicViewModel: MusicViewModel) {
LaunchedEffect(
canvasHeight.floatValue,
canvasWidth.floatValue,
initDrops.value , musicVisualizationEnable.value
initDrops.value, musicVisualizationEnable.value
) {
if (!musicVisualizationEnable.value) {
initDrops.value = false
drops.forEach{
it.clear()
}
drops.clear()
return@LaunchedEffect
}
if (initDrops.value) return@LaunchedEffect
Expand All @@ -94,8 +92,9 @@ fun CoverView(musicViewModel: MusicViewModel) {
val columnRowDropsCount = (canvasHeight.floatValue / dropHeight).toInt() + 10
//有多少列
val columnDropsCount = (canvasHeight.floatValue / columnSpacing).toInt()
drops.clear()
repeat(columnDropsCount) { column ->
drops[column].clear()
drops.add(mutableStateListOf())
repeat(columnRowDropsCount) { row ->
drops[column].add(
Drop(
Expand All @@ -121,7 +120,7 @@ fun CoverView(musicViewModel: MusicViewModel) {
val cDrops = ArrayList<Int>()
// 更新每一列的雨滴
columnDrops.forEachIndexed { index3, drop ->
drop.update(3f)
drop.update(1f)
// 检查是否超出屏幕
if (drop.y > canvasHeight.floatValue + dropHeight) {
cDrops.add(index3)
Expand All @@ -135,7 +134,9 @@ fun CoverView(musicViewModel: MusicViewModel) {
columnDrops.add(d)
}
}
delay(20) // 控制更新频率
magnitudes.add(0f)
magnitudes.removeAt(magnitudes.size - 1)
delay(10) // 控制更新频率
}
}
LazyColumn(
Expand Down Expand Up @@ -174,9 +175,11 @@ fun CoverView(musicViewModel: MusicViewModel) {
)
}
} else {
Box(modifier = Modifier
.fillMaxSize()
.background(color = Color.Black)) {
Box(
modifier = Modifier
.fillMaxSize()
.background(color = Color.Black)
) {

}
}
Expand All @@ -191,9 +194,9 @@ fun CoverView(musicViewModel: MusicViewModel) {
) {
canvasHeight.floatValue = size.height
canvasWidth.floatValue = size.width
val barWidth =
size.width / magnitudes.size
val maxBarHeight = size.width / 2
// val barWidth =
// size.width / magnitudes.size
// val maxBarHeight = size.width / 2
// 绘制每列的雨滴
magnitudes.forEachIndexed { index, magnitude ->
drops.forEachIndexed { index2, columnDrops ->
Expand Down

0 comments on commit f0321f3

Please sign in to comment.