From 9b11259467fca3feb9e5e9b4b8414383090180e7 Mon Sep 17 00:00:00 2001 From: ztftrue Date: Fri, 18 Oct 2024 20:29:50 +0800 Subject: [PATCH] fix bugs --- .../effects/VisualizationAudioProcessor.kt | 58 +++++-------------- 1 file changed, 13 insertions(+), 45 deletions(-) diff --git a/app/src/main/java/com/ztftrue/music/effects/VisualizationAudioProcessor.kt b/app/src/main/java/com/ztftrue/music/effects/VisualizationAudioProcessor.kt index 5b19b53..a8cc861 100644 --- a/app/src/main/java/com/ztftrue/music/effects/VisualizationAudioProcessor.kt +++ b/app/src/main/java/com/ztftrue/music/effects/VisualizationAudioProcessor.kt @@ -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 @@ -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 @@ -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 @@ -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) { @@ -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 @@ -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), @@ -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) { @@ -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 } @@ -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() } }