Skip to content

Commit

Permalink
Fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
ShirasawaSama committed Dec 11, 2023
1 parent 9c187c1 commit 099d583
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ interface Renderable {
fun onRenderEnd()
}

enum class RenderFormat(val extend: String, val isLossLess: Boolean = true) {
@Suppress("unused")
enum class RenderFormat(val extension: String, val isLossLess: Boolean = true) {
WAV("wav"),
MP3("mp3", false),
FLAC("flac"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,12 +179,17 @@ class AudioClipImpl(
val offset = if (playTime < 0) {
time = 0
position = 0
lastPos = -1
(pos.bufferSize + playTime).coerceAtLeast(0).toInt()
} else 0
if (tr == null || tr.isDefaultParams) {
if (pos.timeInSamples != lastPos + bufferSize) {
position = time
}
target.getSamples(position, offset, bufferSize, buffers)
position += bufferSize
pauseProcessor.processPause(buffers, offset, bufferSize, pos.timeToPause)
lastPos = pos.timeInSamples
return
} else if (pos.timeInSamples != lastPos + bufferSize) {
position = (time * tr.speedRatio).roundToLong()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,11 @@ val ExportDialog = @Composable {
},
renderFormat == it,
modifier = Modifier.fillMaxWidth()
) { Text(it.extend) }
) { Text(it.extension) }
}
}) {
Text(
renderFormat.extend
renderFormat.extension
)
}
}
Expand Down Expand Up @@ -183,7 +183,7 @@ val ExportDialog = @Composable {
}


if (renderFormat.extend == "flac") {
if (renderFormat.extension == "flac") {
Spacer(Modifier.width(10.dp))
Row(
verticalAlignment = Alignment.CenterVertically,
Expand Down Expand Up @@ -231,7 +231,7 @@ val ExportDialog = @Composable {
if (filename.isEmpty()) return@Button
val renderer = EchoInMirror.bus?.let { JvmRenderer(it) }
val curPosition = EchoInMirror.currentPosition
val audioFile = File("./${filename}.${renderFormat.extend}")
val audioFile = File("./${filename}.${renderFormat.extension}")

isRendering = true
renderStartTime = System.currentTimeMillis()
Expand Down Expand Up @@ -273,7 +273,7 @@ val ExportDialog = @Composable {
Row {
if (isRendering && renderProcess < 1f) Text("取消")
else if (isRendering && renderProcess >= 1f) Text("确认")
else Text("导出到 $filename.${renderFormat.extend}")
else Text("导出到 $filename.${renderFormat.extension}")
}
}
}
Expand Down
28 changes: 13 additions & 15 deletions dsp/src/commonMain/kotlin/com/eimsound/dsp/data/AudioThumbnail.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import kotlinx.coroutines.launch
import org.mapdb.DBMaker
import org.mapdb.HTreeMap
import java.io.File
import java.io.IOException
import java.nio.ByteBuffer
import java.nio.ByteOrder
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.Paths
Expand Down Expand Up @@ -82,16 +82,15 @@ class AudioThumbnail private constructor(
}
}

@OptIn(DelicateCoroutinesApi::class)
constructor(data: ByteBuffer): this(data.long, data.get().toInt(), data.long, data.float, data.int, data.int) {
GlobalScope.launch(Dispatchers.IO) {
repeat(channels) {
data.get(minTree[it], 1, size)
data.get(maxTree[it], 1, size)
}
buildTree()
modification++
constructor(data: ByteBuffer): this(
data.order(ByteOrder.LITTLE_ENDIAN).long, data.get().toInt(), data.long, data.float, data.int, data.int
) {
repeat(channels) {
data.get(minTree[it], 1, size)
data.get(maxTree[it], 1, size)
}
buildTree()
modification++
}

fun read() { modification }
Expand Down Expand Up @@ -153,7 +152,7 @@ class AudioThumbnail private constructor(
}

fun toByteArray(): ByteArray {
val data = ByteBuffer.allocate(29 + channels * size * 2)
val data = ByteBuffer.allocate(29 + channels * size * 2).order(ByteOrder.LITTLE_ENDIAN)
.putLong(modifiedTime) // 8
.put(channels.toByte()) // 1
.putLong(lengthInSamples) // 8
Expand All @@ -175,11 +174,10 @@ class AudioThumbnail private constructor(
}

class AudioThumbnailCache(
file: File, private val samplesPerThumbSample: Int = DEFAULT_SAMPLES_PRE_THUMB_SAMPLE
private val file: File, private val samplesPerThumbSample: Int = DEFAULT_SAMPLES_PRE_THUMB_SAMPLE
): AutoCloseable {
@Suppress("UNCHECKED_CAST")
private val db = DBMaker.fileDB(file).fileMmapEnableIfSupported().closeOnJvmShutdown().make()
.hashMap("audioThumbnails").expireMaxSize(200).createOrOpen() as HTreeMap<String, ByteArray>
private val db = DBMaker.memoryDB().make().hashMap("audioThumbnails").expireMaxSize(200).createOrOpen() as HTreeMap<String, ByteArray>

operator fun get(key: String) = db[key]?.let { AudioThumbnail(ByteBuffer.wrap(it)) }
operator fun set(key: String, time: Long = Files.getLastModifiedTime(Paths.get(key)).toMillis(), value: AudioThumbnail) {
Expand Down Expand Up @@ -211,7 +209,7 @@ class AudioThumbnailCache(
)
set(file.toString(), time, value)
return value
} catch (e: IOException) {
} catch (e: Throwable) {
e.printStackTrace()
onComplete?.invoke(e)
return null
Expand Down

0 comments on commit 099d583

Please sign in to comment.