Skip to content

Commit

Permalink
Fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
ShirasawaSama committed Dec 12, 2023
1 parent 9e0d19a commit 1bc2d9b
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 30 deletions.
1 change: 1 addition & 0 deletions audio-sources/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ kotlin {
sourceSets {
named("commonMain") {
dependencies {
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:${extra["eim.dependencies.kotlinx.coroutines"]}")
implementation("be.tarsos.dsp:core:${extra["eim.dependencies.tarsos"]}")
implementation("com.googlecode.soundlibs:mp3spi:1.9.5.4") {
exclude("com.googlecode.soundlibs", "tritonus_share")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class DefaultFileAudioSource(override val file: Path) : FileAudioSource {
override var length = 0L
private set
override val isClosed get() = stream == null
override val isRandomAccessible get() = isWav || isFlac
override val isRandomAccessible get() = stream?.markSupported() == true || isFlac
private var _position = 0L
override var position: Long
get() = _position
Expand All @@ -46,8 +46,11 @@ class DefaultFileAudioSource(override val file: Path) : FileAudioSource {
_position = value.coerceIn(0, length - 1)

if (isFlac) {
flacDecoder?.seek(_position)
flacDecoder?.let { synchronized(it) { it.seek(_position) } }
buffer.empty()
} else if (isWav) {
stream?.reset()
stream?.skip(_position * frameSize)
}
}

Expand All @@ -58,18 +61,11 @@ class DefaultFileAudioSource(override val file: Path) : FileAudioSource {
private var flacDecoder: FLACDecoder? = null
private var pcmData: ByteData? = null
private var tempBuffer: ByteArray? = null
private var isFloatData = false
private lateinit var buffer: RingBuffer

init {
run {
// AudioSourceManager.instance.fileSourcesCache[file]?.get()?.let {
// memoryAudioSource = it
// sampleRate = it.sampleRate
// channels = it.channels
// length = it.length
// return@run
// }

val randomStream = JFlacRandomFileInputStream(file.toFile())
var format = AudioSystem.getAudioFileFormat(randomStream)
if (format is MpegAudioFileFormat) {
Expand All @@ -86,13 +82,17 @@ class DefaultFileAudioSource(override val file: Path) : FileAudioSource {
isFlac = format.type == FlacFileFormatType.FLAC
sampleRate = format.format.sampleRate
channels = format.format.channels
if (channels < 0 || length < 0) throw UnsupportedOperationException("Unsupported file!")
if (channels < 0 || length < 0) throw UnsupportedOperationException("Unsupported format: $format")
frameSize = channels * (bits / 8)
newFormat = AudioFormat(AudioFormat.Encoding.PCM_SIGNED, sampleRate, bits,
channels, frameSize, sampleRate, false)
isFloatData = format.format.encoding == AudioFormat.Encoding.PCM_FLOAT

if (isWav) {
stream = AudioSystem.getAudioInputStream(randomStream).apply { mark(0) }
val s = AudioSystem.getAudioInputStream(randomStream).apply { mark(0) }
stream = if (format.format.encoding != AudioFormat.Encoding.PCM_SIGNED) {
AudioSystem.getAudioInputStream(newFormat, s) ?: throw UnsupportedOperationException("Unsupported format: $format")
} else s
} else if (isFlac) {
stream = randomStream
buffer = RingBuffer()
Expand All @@ -105,12 +105,6 @@ class DefaultFileAudioSource(override val file: Path) : FileAudioSource {
throw UnsupportedOperationException(e)
}
}

// if (readAll) {
// isFirstTimeToClose = true
// memoryAudioSource = AudioSourceManager.instance.createMemorySource(this)
// AudioSourceManager.instance.fileSourcesCache[file] = WeakReference(memoryAudioSource!!)
// }
}
}

Expand All @@ -135,8 +129,11 @@ class DefaultFileAudioSource(override val file: Path) : FileAudioSource {
var thisLen = len2
if (thisLen > buffer.available) thisLen = buffer.available
if (thisLen < frameSize) {
val frame = d.readNextFrame() ?: break
val data = d.decodeFrame(frame, pcmData)
val data = synchronized(d) {
val frame = d.readNextFrame()
if (frame == null) null
else d.decodeFrame(frame, pcmData)
} ?: break
pcmData = data
buffer.resize(data.len * 2)
buffer.put(data.data, 0, data.len)
Expand All @@ -156,7 +153,9 @@ class DefaultFileAudioSource(override val file: Path) : FileAudioSource {
}

override fun nextBlock(buffers: Array<FloatArray>, length: Int, offset: Int): Int {
val sampleCount = length.coerceAtMost(buffers.firstOrNull()?.size ?: 0)
var sampleCount = length.coerceAtMost(buffers.firstOrNull()?.size ?: 0)
if (_position > this.length) return 0
else if (_position + sampleCount > this.length) sampleCount = (this.length - _position).toInt()
// read into temporary byte buffer
var byteBufferSize = sampleCount * frameSize
var lTempBuffer = tempBuffer
Expand Down
13 changes: 7 additions & 6 deletions daw/src/jvmMain/kotlin/com/eimsound/daw/impl/PlayPositionImpl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -79,16 +79,17 @@ class PlayPositionImpl(

private var lastTime = 0
private fun checkTimeInPPQ() {
if (_timeInPPQ !in projectRange) if (isProjectLooping) {
_timeInPPQ = projectRange.first
} else {
isPlaying = false
_timeInPPQ = projectRange.last
}
if (timeToPause > 0) {
if (_timeInPPQ > lastTime && timeToPause > 0) timeToPause -= bufferSize
if (timeToPause <= 0) _isPlaying = false
lastTime = _timeInPPQ
} else if (_timeInPPQ !in projectRange) {
if (isProjectLooping) {
_timeInPPQ = projectRange.first
} else {
isPlaying = false
_timeInPPQ = projectRange.last
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ class AudioClipImpl(
override fun fromJson(json: JsonElement) {
super.fromJson(json)
json as JsonObject
json["file"]?.let { target = AudioSourceManager.createFileAudioSource(Path(it.asString())) }
json["file"]?.let { target = AudioSourceManager.createCachedFileSource(Path(it.asString())) }
json["volumeEnvelope"]?.let { volumeEnvelope.fromJson(it) }
json["bpm"]?.let { bpm = it.asFloat() }
json["timeStretcher"]?.let {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,12 +159,20 @@ object FileSystemBrowser : Panel {
val interactionSource = remember { MutableInteractionSource() }
Box(Modifier.padding(horizontal = 4.dp), content = c)
Box(Modifier.fillMaxSize()
.draggable(draggableState, Orientation.Horizontal, interactionSource = interactionSource)
.draggable(
draggableState, Orientation.Horizontal, interactionSource = interactionSource,
onDragStopped = {
fileBrowserPreviewer.position.isPlaying = true
}
)
.pointerInput(Unit) {
detectTapGestures({
fileBrowserPreviewer.position.isProjectLooping =
!fileBrowserPreviewer.position.isProjectLooping
}) { fileBrowserPreviewer.playPosition = it.x / width[0].toDouble() }
}) {
fileBrowserPreviewer.playPosition = it.x / width[0].toDouble()
fileBrowserPreviewer.position.isPlaying = true
}
}
.pointerHoverIcon(PointerIcon.HorizontalResize)
) {
Expand Down

0 comments on commit 1bc2d9b

Please sign in to comment.