Skip to content

Commit

Permalink
fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
ZTFtrue committed Oct 18, 2024
1 parent 5e2f349 commit 9b11259
Showing 1 changed file with 13 additions and 45 deletions.
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

0 comments on commit 9b11259

Please sign in to comment.