Commit 06/08/2025

This commit is contained in:
2025-08-06 12:31:12 +07:00
parent 158f3ea4c2
commit 662608550b
6 changed files with 67 additions and 23 deletions

View File

@@ -2,6 +2,7 @@
<profile version="1.0">
<option name="myName" value="Project Default" />
<inspection_tool class="AiaStyle" enabled="false" level="TYPO" enabled_by_default="false" />
<inspection_tool class="AssignedValueIsNeverRead" enabled="false" level="WARNING" enabled_by_default="false" />
<inspection_tool class="ClassName" enabled="false" level="WEAK WARNING" enabled_by_default="false" />
<inspection_tool class="DuplicatedCode" enabled="false" level="WEAK WARNING" enabled_by_default="false" />
<inspection_tool class="FunctionName" enabled="false" level="WEAK WARNING" enabled_by_default="false" />

View File

@@ -0,0 +1,13 @@
<component name="libraryTable">
<library name="jetbrains.kotlinx.coroutines.core" type="repository">
<properties maven-id="org.jetbrains.kotlinx:kotlinx-coroutines-core:1.10.2" />
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlinx/kotlinx-coroutines-core/1.10.2/kotlinx-coroutines-core-1.10.2.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlinx/kotlinx-coroutines-core-jvm/1.10.2/kotlinx-coroutines-core-jvm-1.10.2.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/annotations/23.0.0/annotations-23.0.0.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib/2.1.0/kotlin-stdlib-2.1.0.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</component>

View File

@@ -16,5 +16,6 @@
<orderEntry type="library" name="net.java.dev.jna" level="project" />
<orderEntry type="library" name="java.websocket.Java.WebSocket" level="project" />
<orderEntry type="library" name="io.javalin.bundle" level="project" />
<orderEntry type="library" name="jetbrains.kotlinx.coroutines.core" level="project" />
</component>
</module>

View File

@@ -123,7 +123,7 @@ fun main() {
if (ValidFile(filename)){
try{
val player= AudioFilePlayer(audioID, filename)
player.Play { cb -> afp = null}
player.Play { _ -> afp = null}
afp = player
WsReply(cmd.command,"success")

View File

@@ -1,5 +1,9 @@
package audio
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import java.util.function.Consumer
/**
@@ -31,10 +35,10 @@ class AudioFilePlayer(deviceID: Int, val filename: String?, device_samplingrate:
fun Play(finished: Consumer<Any> ) : Boolean{
if (bass.BASS_ChannelPlay(filehandle, false)){
val thread = Thread{
CoroutineScope(Dispatchers.Default).launch {
isPlaying = true
while(true){
Thread.sleep(1000)
delay(1000)
if (bass.BASS_ChannelIsActive(filehandle)!= Bass.BASS_ACTIVE_PLAYING){
// finished playing
@@ -46,9 +50,25 @@ class AudioFilePlayer(deviceID: Int, val filename: String?, device_samplingrate:
filehandle = 0
finished.accept(true)
}
thread.name = "AudioFilePlayer $filename"
thread.isDaemon = true
thread.start()
// Revisi 06/08/2025 Ganti thread dengan Coroutine
// val thread = Thread{
// isPlaying = true
// while(true){
// Thread.sleep(1000)
//
// if (bass.BASS_ChannelIsActive(filehandle)!= Bass.BASS_ACTIVE_PLAYING){
// // finished playing
// break
// }
// }
// isPlaying = false
// bass.BASS_StreamFree(filehandle)
// filehandle = 0
// finished.accept(true)
// }
// thread.name = "AudioFilePlayer $filename"
// thread.isDaemon = true
// thread.start()
return true
}
return false

View File

@@ -2,6 +2,10 @@ package zello
import com.fasterxml.jackson.module.kotlin.contains
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import org.java_websocket.client.WebSocketClient
import org.java_websocket.handshake.ServerHandshake
import org.slf4j.Logger
@@ -70,7 +74,7 @@ class ZelloClient(val address : URI, val username: String, val password: String)
// audio stream
// format {type(8) = 0x01, stream_id(32), packet_id(32), data[]}
val str_id = bytes.getInt(1)
val packet_id = bytes.getInt(5)
//val packet_id = bytes.getInt(5)
val data = ByteArray(receivedbytes - 9)
bytes.get(9,data )
val job = streamJob[str_id]
@@ -113,13 +117,12 @@ class ZelloClient(val address : URI, val username: String, val password: String)
}
}
@Suppress("UNCHECKED_CAST")
override fun onMessage(message: String?) {
logger.info("Message received: $message")
val jsnode = mapper.readTree(message)
if (jsnode["seq"] != null) {
val seq = jsnode.get("seq").asInt()
when(seq){
when(val seq = jsnode.get("seq").asInt()){
1 ->{
//logon reply
val lgreply = mapper.treeToValue(jsnode, LogonReply::class.java)
@@ -168,7 +171,7 @@ class ZelloClient(val address : URI, val username: String, val password: String)
"on_stream_start" ->{
val streamstart = mapper.treeToValue(jsnode, Event_OnStreamStart::class.java)
logger.info("Stream started on channel ${streamstart.channel} from ${streamstart.from} for ${streamstart.For}")
streamJob.put(streamstart.stream_id, ZelloAudioJob.fromEventOnStreamStart(streamstart))
streamJob[streamstart.stream_id] = ZelloAudioJob.fromEventOnStreamStart(streamstart)
event.onStartStreaming(streamstart.from, streamstart.For, streamstart.channel)
}
"on_stream_stop" -> {
@@ -185,7 +188,7 @@ class ZelloClient(val address : URI, val username: String, val password: String)
"on_image" ->{
val image = mapper.treeToValue(jsnode, Event_OnImage::class.java)
logger.info("Image received, image_id=${image.message_id}, from=${image.from}, For=${image.For}, channel=${image.channel}")
imageJob.put(image.message_id, ZelloImageJob(image.channel,image.message_id,image.height,image.width,image.source,image.from,image.For))
imageJob[image.message_id] = ZelloImageJob(image.channel,image.message_id,image.height,image.width,image.source,image.from,image.For)
}
"on_text_message" ->{
val textmessage = mapper.treeToValue(jsnode, Event_OnTextMessage::class.java)
@@ -208,17 +211,23 @@ class ZelloClient(val address : URI, val username: String, val password: String)
logger.info("Closed from ${if (remote) "Remote side" else "Local side"}, Code=$code, Reason=$reason")
event.onDisconnected()
// try reconnecting after 10 seconds
val thread = Thread {
try {
Thread.sleep(10000)
connect()
} catch (e: InterruptedException) {
logger.error("Reconnection interrupted: ${e.message}")
}
CoroutineScope(Dispatchers.Default).launch {
delay(10000)
connect()
}
thread.name= "ZelloClient-ReconnectThread"
thread.isDaemon = true
thread.start()
//Revisi 06/08/2025 : Change to Coroutines
// val thread = Thread {
// try {
// Thread.sleep(10000)
// connect()
// } catch (e: InterruptedException) {
// logger.error("Reconnection interrupted: ${e.message}")
// }
// }
// thread.name= "ZelloClient-ReconnectThread"
// thread.isDaemon = true
// thread.start()
}
override fun onError(ex: Exception?) {
@@ -254,7 +263,7 @@ class ZelloClient(val address : URI, val username: String, val password: String)
For)
client?.send(mapper.writeValueAsString(x))
// put here, because require a response in onMessage
commandJob.put(seqID, callback)
commandJob[seqID] = callback
}
/**