commit 07/10/2025

This commit is contained in:
2025-10-08 14:10:50 +07:00
parent 1318bba397
commit efe243e440
8 changed files with 497 additions and 161 deletions

View File

@@ -143,8 +143,7 @@ class AudioPlayer (var samplingrate: Int) {
mem.write(0, data, 0, data.size)
val pushresult = bass.BASS_StreamPutData(streamhandle, mem, data.size)
if (pushresult==-1){
val errcode = bass.BASS_ErrorGetCode()
println("BASS_StreamPutData failed: $errcode")
Logger.error { "BASS_StreamPutData failed: ${bass.BASS_ErrorGetCode()}" }
}
return pushresult != -1
}
@@ -154,32 +153,27 @@ class AudioPlayer (var samplingrate: Int) {
if (withChime){
val chup = contentCache.getAudioFile("chimeup")
if (chup!=null && chup.isValid()){
if (pushData(chup.bytes)){
println("Chime up pushed")
} else {
if (!pushData(chup.bytes)){
all_success = false
println("Chime up failed")
Logger.error { "Failed to push Chime Up" }
}
} else println("Chime Up not valid")
} else Logger.error { "withChime=true, but Chime Up not available" }
}
if (pushData(data)){
println("Data pushed")
} else {
if (!pushData(data)){
all_success = false
println("Data push failed")
Logger.error { "Failed to push Data ByteArray" }
}
if (withChime){
val chdn = contentCache.getAudioFile("chimedown")
if (chdn!=null && chdn.isValid()){
if (pushData(chdn.bytes)){
println("Chime down pushed")
} else {
if (!pushData(chdn.bytes)){
all_success = false
println("Chime down failed")
Logger.error { "Failed to push Chime Down" }
}
} else println("Chime Down not valid")
} else Logger.error { "withChime=true, but Chime Down not available" }
}
val readsize: Long = 1024 * 1024 // read 1 MB at a time
@@ -191,7 +185,6 @@ class AudioPlayer (var samplingrate: Int) {
totalread += read
}
} while (read > 0)
println("Finished reading stream data, total $totalread bytes read")
bassenc.BASS_Encode_Stop(encodehandle)
bass.BASS_StreamFree(streamhandle)
@@ -246,8 +239,7 @@ class AudioPlayer (var samplingrate: Int) {
mem.write(0, data, 0, data.size)
val pushresult = bass.BASS_StreamPutData(streamhandle, mem, data.size)
if (pushresult==-1){
val errcode = bass.BASS_ErrorGetCode()
println("BASS_StreamPutData failed: $errcode")
Logger.error { "BASS_StreamPutData failed: ${bass.BASS_ErrorGetCode()}" }
}
return pushresult != -1
}
@@ -258,39 +250,34 @@ class AudioPlayer (var samplingrate: Int) {
if (withChime){
val chup = contentCache.getAudioFile("chimeup")
if (chup!=null && chup.isValid()){
if (pushData(chup.bytes)){
println("Chime up pushed")
} else {
if (!pushData(chup.bytes)){
allsuccess = false
println("Chime up failed")
Logger.error { "Failed to push Chime Up" }
}
} else println("Chime Up not valid")
} else Logger.error { "withChime=true, but Chime Up not available" }
}
sources.forEach { source ->
if (source.isValid()) {
// write the bytes to the stream
if (pushData(source.bytes)){
println("Source ${source.fileName} pushed")
} else {
if (!pushData(source.bytes)){
allsuccess = false
println("Source ${source.fileName} push failed")
Logger.error { "Source ${source.fileName} push failed" }
}
} else {
allsuccess = false
println("Source ${source.fileName} is not valid")
Logger.error { "Not pushing Source=${source.fileName} because invalid" }
}
}
if (withChime){
val chdn = contentCache.getAudioFile("chimedown")
if (chdn!=null && chdn.isValid()){
if (pushData(chdn.bytes)){
println("Chime down pushed")
} else {
if (!pushData(chdn.bytes)){
allsuccess = false
println("Chime down failed")
Logger.error { "Failed to push Chime Down" }
}
} else println("Chime Down not valid")
} else Logger.error { "withChime=true, but Chime Down not available"}
}
val readsize: Long = 1024 * 1024 // read 1 MB at a time
@@ -302,7 +289,6 @@ class AudioPlayer (var samplingrate: Int) {
totalread += read
}
} while (read > 0)
println("Finished reading stream data, total $totalread bytes read")
// close the encoding handle

104
src/audio/TCPReceiver.kt Normal file
View File

@@ -0,0 +1,104 @@
package audio
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import org.tinylog.Logger
import java.net.ServerSocket
import java.util.function.Consumer
/**
* TCPReceiver is a class that listens for TCP connections on a specified port.
* for receiving PCMFILE from Android SAMI
*/
class TCPReceiver(val portnumber: Int = 5002){
private lateinit var server: ServerSocket
private var isRunning = false
private val dataCallback = mutableMapOf<String, Consumer<ByteArray>>()
private val isfinishd = mutableMapOf<String, Boolean>()
/**
* Start listening for TCP connections on the specified port.
* @return true if successful, false otherwise
*/
fun Start() : Boolean{
try{
server = ServerSocket(portnumber)
isRunning = true
Logger.info { "Server started at port $portnumber" }
CoroutineScope(Dispatchers.IO).launch {
while(isRunning){
try {
val client = server.accept()
val clientAddress = client.inetAddress.hostAddress
CoroutineScope(Dispatchers.IO).launch {
isfinishd[clientAddress] = false
var totalbytes = 0L
try{
val din = client.getInputStream()
Logger.info{ "Start receiving PCMFILE from Android with IP=${clientAddress}" }
do {
val buffer = ByteArray(16384)
val bytesRead = din.read(buffer)
if (bytesRead>0){
val data = ByteArray(bytesRead)
System.arraycopy(buffer, 0, data, 0, bytesRead)
//println("Received $bytesRead bytes from $clientAddress")
totalbytes+=bytesRead
dataCallback[clientAddress].let {
it?.accept(data)
}
}
} while (bytesRead > 0)
} catch (e : Exception){
Logger.error { "Failed receiving data from $clientAddress, Message : ${e.message}" }
}
Logger.info { "Connection from $clientAddress ended, total bytesRead=$totalbytes" }
isfinishd[clientAddress] = true
}
} catch (e: Exception) {
Logger.error { "Failed to accept socket, Message : ${e.message}" }
}
}
}
return true
} catch (e : Exception){
Logger.error { "Failed to Start Server at port $portnumber, Message : ${e.message}" }
return false
}
}
fun RequestDataFrom(ipaddress: String, cb: Consumer<ByteArray>){
dataCallback[ipaddress] = cb
}
fun StopRequestDataFrom(ipaddress: String){
if (isfinishd[ipaddress] != null){
if (isfinishd[ipaddress]==false){
// belum selesai
//println("Waiting for receiving from $ipaddress to finish...")
runBlocking {
while (isfinishd[ipaddress] == false){
kotlinx.coroutines.delay(100)
}
}
}
//println("Removing callback for $ipaddress")
dataCallback.remove(ipaddress)
}
}
/**
* Stop listening for TCP connections and close the server socket.
*/
fun Stop(){
if (isRunning){
isRunning = false
server.close()
}
}
}