Change AudioCodecDefines to simple object

This commit is contained in:
Fabian Wiesel
2024-02-25 21:05:30 +01:00
parent b8025b2300
commit 029e82941e
3 changed files with 14 additions and 20 deletions

View File

@@ -19,16 +19,10 @@ package de.rochefort.childmonitor
import android.media.AudioFormat
import de.rochefort.childmonitor.audio.G711UCodec
class AudioCodecDefines private constructor() {
init {
throw IllegalStateException("Do not instantiate!")
}
companion object {
object AudioCodecDefines {
const val FREQUENCY = 8000
const val ENCODING = AudioFormat.ENCODING_PCM_16BIT
val CODEC = G711UCodec()
const val CHANNEL_CONFIGURATION_IN = AudioFormat.CHANNEL_IN_MONO
const val CHANNEL_CONFIGURATION_OUT = AudioFormat.CHANNEL_OUT_MONO
}
}

View File

@@ -37,9 +37,9 @@ import java.io.IOException
import java.net.Socket
class ListenService : Service() {
private val frequency: Int = AudioCodecDefines.Companion.FREQUENCY
private val channelConfiguration: Int = AudioCodecDefines.Companion.CHANNEL_CONFIGURATION_OUT
private val audioEncoding: Int = AudioCodecDefines.Companion.ENCODING
private val frequency: Int = AudioCodecDefines.FREQUENCY
private val channelConfiguration: Int = AudioCodecDefines.CHANNEL_CONFIGURATION_OUT
private val audioEncoding: Int = AudioCodecDefines.ENCODING
private val bufferSize = AudioTrack.getMinBufferSize(frequency, channelConfiguration, audioEncoding)
private val byteBufferSize = bufferSize * 2
private val binder: IBinder = ListenBinder()
@@ -175,7 +175,7 @@ class ListenService : Service() {
val decodedBuffer = ShortArray(byteBufferSize * 2)
while (socket.isConnected && read != -1 && !Thread.currentThread().isInterrupted) {
read = inputStream.read(readBuffer)
val decoded: Int = AudioCodecDefines.Companion.CODEC.decode(decodedBuffer, readBuffer, read, 0)
val decoded: Int = AudioCodecDefines.CODEC.decode(decodedBuffer, readBuffer, read, 0)
if (decoded > 0) {
audioTrack.write(decodedBuffer, 0, decoded)
val decodedBytes = ShortArray(decoded)

View File

@@ -59,9 +59,9 @@ class MonitorService : Service() {
val statusText = ma.findViewById<TextView>(R.id.textStatus)
statusText.setText(R.string.streaming)
}
val frequency: Int = AudioCodecDefines.Companion.FREQUENCY
val channelConfiguration: Int = AudioCodecDefines.Companion.CHANNEL_CONFIGURATION_IN
val audioEncoding: Int = AudioCodecDefines.Companion.ENCODING
val frequency: Int = AudioCodecDefines.FREQUENCY
val channelConfiguration: Int = AudioCodecDefines.CHANNEL_CONFIGURATION_IN
val audioEncoding: Int = AudioCodecDefines.ENCODING
val bufferSize = AudioRecord.getMinBufferSize(frequency, channelConfiguration, audioEncoding)
val audioRecord: AudioRecord = try {
AudioRecord(
@@ -85,7 +85,7 @@ class MonitorService : Service() {
Log.d(TAG, "Socket send buffer size: " + socket.sendBufferSize)
while (socket.isConnected && (this.currentSocket != null) && !Thread.currentThread().isInterrupted) {
val read = audioRecord.read(pcmBuffer, 0, bufferSize)
val encoded: Int = AudioCodecDefines.Companion.CODEC.encode(pcmBuffer, read, ulawBuffer, 0)
val encoded: Int = AudioCodecDefines.CODEC.encode(pcmBuffer, read, ulawBuffer, 0)
out.write(ulawBuffer, 0, encoded)
}
} catch (e: Exception) {