commit 26/07/2025
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
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
|
||||
@@ -137,32 +138,60 @@ class AudioPlayer (var samplingrate: Int) {
|
||||
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) {
|
||||
val thread = Thread {
|
||||
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@Thread
|
||||
}
|
||||
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@Thread
|
||||
}
|
||||
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@Thread
|
||||
}
|
||||
|
||||
var allsuccess = true
|
||||
sources.forEach { source ->
|
||||
if (source.isValid()) {
|
||||
// write the bytes to the stream
|
||||
val mem = Memory(source.bytes.size.toLong())
|
||||
mem.write(0, source.bytes, 0, source.bytes.size)
|
||||
val pushresult = bass.BASS_StreamPutData(streamhandle, mem, source.bytes.size)
|
||||
if (pushresult == -1) {
|
||||
Logger.error { "Failed to write data from ${source.fileName} to stream: ${bass.BASS_ErrorGetCode()}" }
|
||||
allsuccess = false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// now we wait until the stream is finished
|
||||
while(bassenc.BASS_Encode_IsActive(encodehandle) == BASS_ACTIVE_PLAYING) {
|
||||
Thread.sleep(100) // Sleep for a short time to avoid busy waiting
|
||||
}
|
||||
|
||||
// close the encoding handle
|
||||
bassenc.BASS_Encode_Stop(encodehandle)
|
||||
bass.BASS_StreamFree(streamhandle)
|
||||
callback.accept(false, "BASS_ChannelPlay failed: ${bass.BASS_ErrorGetCode()}")
|
||||
return
|
||||
bass.BASS_ChannelFree(streamhandle)
|
||||
if (allsuccess){
|
||||
callback.accept(true, "WAV file written successfully: $target")
|
||||
} else {
|
||||
callback.accept(false, "Failed to write some data to WAV file: $target")
|
||||
}
|
||||
}
|
||||
thread.name = "WavWriter Thread"
|
||||
thread.isDaemon = true
|
||||
thread.start()
|
||||
|
||||
// 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")
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -126,4 +126,5 @@ public interface BassEnc extends Library {
|
||||
int BASS_Encode_IsActive(int handle);
|
||||
boolean BASS_Encode_SetPaused(int handle, boolean paused);
|
||||
int BASS_Encode_GetChannel(int handle);
|
||||
boolean BASS_Encode_StopEx(int handle, boolean queue);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user