
com.simiacryptus.jopenai.audio.LoudnessWindowBuffer.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jo-penai Show documentation
Show all versions of jo-penai Show documentation
A Java client for OpenAI's API
The newest version!
@file:Suppress("unused")
package com.simiacryptus.jopenai.audio
import org.slf4j.LoggerFactory
import java.util.*
abstract class LoudnessWindowBuffer(
private val inputBuffer: Deque,
private val outputBuffer: Deque,
var continueFn: () -> Boolean,
) {
val outputPacketBuffer = ArrayList()
val recentPacketBuffer = ArrayList()
private val packetLookback = 100
// Main function of the AudioPump class
fun run() {
log.debug("Starting LoudnessWindowBuffer processing loop.")
// Loop until the continueFn returns false
while (this.continueFn() || inputBuffer.isNotEmpty()) {
// Poll the input buffer for a byte array
val bytes = inputBuffer.poll()
// If the byte array is null, sleep for 1 millisecond and continue
if (null == bytes) {
log.trace("Input buffer is empty, sleeping for 1 millisecond.")
Thread.sleep(1)
} else {
log.info("Processing byte array of size: ${bytes.size}.")
val packet = AudioPacket(AudioPacket.convertRaw(bytes))
synchronized(outputPacketBuffer) { outputPacketBuffer.add(packet) }
synchronized(recentPacketBuffer) {
recentPacketBuffer.add(packet)
while (recentPacketBuffer.size > packetLookback) recentPacketBuffer.removeAt(0)
}
if (shouldOutput()) {
log.debug("Output condition met, preparing to output.")
// Add the converted raw to wav byte array to the output buffer
val reduced = synchronized(outputPacketBuffer) { outputPacketBuffer.reduce { a, b -> a + b } }
log.info("Reduced packet size: ${reduced.samples.size}.")
outputBuffer.add(AudioPacket.convertRawToWav(AudioPacket.convertFloatsToRaw(reduced.samples)))
synchronized(outputPacketBuffer) { outputPacketBuffer.clear() }
log.debug("Output buffer updated and outputPacketBuffer cleared.")
}
}
}
log.debug("LoudnessWindowBuffer processing loop ended.")
}
abstract fun shouldOutput(): Boolean
companion object {
// Create a Logger instance for the AudioPump class
private val log = LoggerFactory.getLogger(LoudnessWindowBuffer::class.java)
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy