commit 07/10/2025
This commit is contained in:
@@ -1,23 +1,19 @@
|
||||
package audio
|
||||
|
||||
import audio.Bass.BASS_ACTIVE_PLAYING
|
||||
import audio.Bass.BASS_DEVICE_ENABLED
|
||||
import audio.Bass.BASS_DEVICE_INIT
|
||||
import audio.Bass.BASS_POS_BYTE
|
||||
import audio.Bass.BASS_STREAM_DECODE
|
||||
import audio.Bass.BASS_SAMPLE_MONO
|
||||
import audio.Bass.STREAMPROC_PUSH
|
||||
import audio.BassEnc.BASS_ENCODE_PCM
|
||||
import codes.Somecodes.Companion.ValidFile
|
||||
import codes.Somecodes.Companion.ValidString
|
||||
import com.sun.jna.Memory
|
||||
import com.sun.jna.Pointer
|
||||
import commonAudio
|
||||
import contentCache
|
||||
import kotlinx.coroutines.CoroutineName
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.isActive
|
||||
import kotlinx.coroutines.launch
|
||||
import org.tinylog.Logger
|
||||
import java.util.function.BiConsumer
|
||||
@@ -162,7 +158,7 @@ class AudioPlayer (var samplingrate: Int) {
|
||||
}
|
||||
|
||||
|
||||
val job = CoroutineScope(Dispatchers.IO).launch(CoroutineName("WavWriter $target")) {
|
||||
CoroutineScope(Dispatchers.IO).launch(CoroutineName("WavWriter $target")) {
|
||||
bass.BASS_SetDevice(0) // Set to No Sound device for writing
|
||||
val streamhandle = bass.BASS_StreamCreate(samplingrate, 1, BASS_STREAM_DECODE, Pointer(-1), null)
|
||||
if (streamhandle==0){
|
||||
@@ -191,7 +187,7 @@ class AudioPlayer (var samplingrate: Int) {
|
||||
|
||||
var allsuccess = true
|
||||
if (withChime){
|
||||
val chup = commonAudio["chimeup"]
|
||||
val chup = contentCache.getAudioFile("chimeup")
|
||||
if (chup!=null && chup.isValid()){
|
||||
if (pushData(chup.bytes)){
|
||||
println("Chime up pushed")
|
||||
@@ -217,7 +213,7 @@ class AudioPlayer (var samplingrate: Int) {
|
||||
|
||||
}
|
||||
if (withChime){
|
||||
val chdn = commonAudio["chimedown"]
|
||||
val chdn = contentCache.getAudioFile("chimedown")
|
||||
if (chdn!=null && chdn.isValid()){
|
||||
if (pushData(chdn.bytes)){
|
||||
println("Chime down pushed")
|
||||
|
||||
55
src/audio/ContentCache.kt
Normal file
55
src/audio/ContentCache.kt
Normal file
@@ -0,0 +1,55 @@
|
||||
package audio
|
||||
|
||||
/**
|
||||
* Cache for audio content to avoid reloading from disk
|
||||
*/
|
||||
@Suppress("unused")
|
||||
class ContentCache {
|
||||
|
||||
private val map: MutableMap<String, AudioFileInfo> = HashMap()
|
||||
|
||||
/**
|
||||
* Clear the cache, but keep essential sounds : chimeup, chimedown, silence1s, silencehalf
|
||||
*/
|
||||
fun clear(){
|
||||
// dont clear chimeup, chimedown, silence1s, silencehalf
|
||||
val keysToKeep = setOf("chimeup", "chimedown", "silence1s", "silencehalf")
|
||||
map.keys.retainAll(keysToKeep)
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Add an audio file to the cache
|
||||
* @param key The key to identify the audio file
|
||||
* @param audioFile The AudioFileInfo object
|
||||
*/
|
||||
fun addAudioFile(key: String, audioFile: AudioFileInfo) {
|
||||
map[key] = audioFile
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve an audio file from the cache
|
||||
* @param key The key to identify the audio file
|
||||
* @return The AudioFileInfo object, or null if not found
|
||||
*/
|
||||
fun getAudioFile(key: String): AudioFileInfo? {
|
||||
return map[key]
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove an audio file from the cache
|
||||
* @param key The key to identify the audio file
|
||||
*/
|
||||
fun removeAudioFile(key: String) {
|
||||
map.remove(key)
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the cache contains the specified key
|
||||
* @param key The key to check in the cache
|
||||
* @return True if the key exists, false otherwise
|
||||
*/
|
||||
fun haveKey(key: String): Boolean {
|
||||
return map.containsKey(key)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user