first commit 26/07/2025

This commit is contained in:
2025-07-26 10:14:46 +07:00
parent f6ae8e14c0
commit a32a1d3300
4 changed files with 192 additions and 7 deletions

View File

@@ -5,16 +5,27 @@ 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 org.tinylog.Logger
import java.util.function.BiConsumer
@Suppress("unused")
class AudioPlayer {
var bass: Bass = Bass.Instance
class AudioPlayer (var samplingrate: Int) {
val bass: Bass = Bass.Instance
val bassenc : BassEnc = BassEnc.Instance
var initedDevice = -1
init {
Logger.info("Audio version ${Integer.toHexString(bass.BASS_GetVersion())}" as Any)
if (samplingrate<1) samplingrate = 44100 // Default sampling rate
Logger.info {"Bass version ${Integer.toHexString(bass.BASS_GetVersion())}"}
Logger.info { "BassEnc version ${Integer.toHexString(bassenc.BASS_Encode_GetVersion())}" }
InitAudio(0) // Audio 0 is No Sound, use for reading and writing wav silently
}
@@ -110,5 +121,49 @@ class AudioPlayer {
return result
}
/**
* Writes the audio data from the sources to a WAV file.
* @param sources List of AudioFileInfo objects containing the audio data to write.
* @param target The target file name for the WAV file.
* @param callback A BiConsumer that accepts a Boolean indicating success or failure and a String message.
*/
fun WavWriter(sources: List<AudioFileInfo>, target: String, callback: BiConsumer<Boolean, String>) {
if (sources.isEmpty() || !ValidFile(target)) {
callback.accept(false, " Invalid sources")
return
}
if (!ValidString(target)) {
callback.accept(false, " Invalid target file name")
return
}
bass.BASS_SetDevice(0) // Set to No Sound device for writing
val streamhandle = bass.BASS_StreamCreate(samplingrate, 1, BASS_STREAM_DECODE, STREAMPROC_PUSH, null)
if (streamhandle==0){
callback.accept(false, "Failed to create stream: ${bass.BASS_ErrorGetCode()}")
return
}
val encodehandle = bassenc.BASS_Encode_Start(streamhandle, target, BASS_ENCODE_PCM, null, null)
if (encodehandle==0){
bass.BASS_StreamFree(streamhandle)
callback.accept(false, "Failed to start encoding: ${bass.BASS_ErrorGetCode()}")
return
}
val playresult = bass.BASS_ChannelPlay(streamhandle,false)
if (!playresult) {
bassenc.BASS_Encode_Stop(encodehandle)
bass.BASS_StreamFree(streamhandle)
callback.accept(false, "BASS_ChannelPlay failed: ${bass.BASS_ErrorGetCode()}")
return
}
// TODO write each source to the stream
// close the encoding handle
bassenc.BASS_Encode_Stop(encodehandle)
bass.BASS_ChannelFree(streamhandle)
callback.accept(true, "WAV file written successfully: $target")
}
}