Skip to content

Commit 2334927

Browse files
committed
Release v0.2.0
1 parent c4e7aad commit 2334927

File tree

3 files changed

+31
-12
lines changed

3 files changed

+31
-12
lines changed

gradle/libs.versions.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[versions]
2-
lib = "0.1.9"
2+
lib = "0.2.0"
33

44
agp = "8.7.3"
55
kotlin = "2.1.0"

soundfont/src/androidMain/cpp/mikro_sound_font_jni.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ JNIEXPORT void JNICALL Java_io_github_lemcoder_mikrosoundfont_internal_SoundFont
9191
JNIEXPORT jfloatArray JNICALL Java_io_github_lemcoder_mikrosoundfont_internal_SoundFontDelegate_renderFloat(JNIEnv *env, jobject obj, jint samples, jint channels, jboolean isMixing) {
9292
if (g_tsf) {
9393
int item_count = samples * channels;
94-
float *buffer = malloc(item_count * sizeof(float) * channels);
95-
tsf_render_float(g_tsf, buffer, samples, 0);
94+
float *buffer = malloc(item_count * sizeof(float));
95+
tsf_render_float(g_tsf, buffer, samples, isMixing ? 1 : 0);
9696

9797
jfloatArray output = (*env)->NewFloatArray(env, item_count);
9898
(*env)->SetFloatArrayRegion(env, output, 0, item_count, buffer);

soundfont/src/nativeMain/kotlin/io/github/lemcoder/mikrosoundfont/internal/SoundFontDelegate.native.kt

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
package io.github.lemcoder.mikrosoundfont.internal
22

3+
import io.github.lemcoder.mikrosoundfont.Channel
4+
import io.github.lemcoder.mikrosoundfont.SoundFont
35
import kotlinx.cinterop.CPointer
46
import kotlinx.cinterop.ExperimentalForeignApi
5-
import kotlinx.cinterop.refTo
7+
import kotlinx.cinterop.memScoped
8+
import kotlinx.cinterop.readBytes
69
import kotlinx.cinterop.reinterpret
710
import kotlinx.cinterop.toCValues
811
import kotlinx.cinterop.toKString
9-
import io.github.lemcoder.mikrosoundfont.Channel
10-
import io.github.lemcoder.mikrosoundfont.SoundFont
12+
import platform.posix.malloc
1113
import tinySoundFont.TSFOutputMode
1214
import tinySoundFont.tsf_active_voice_count
1315
import tinySoundFont.tsf_bank_get_presetname
@@ -87,9 +89,11 @@ internal class SoundFontDelegate : SoundFont {
8789
return withSoundFont {
8890
val tsfMode = when (outputMode) {
8991
SoundFont.OutputMode.TSF_STEREO_INTERLEAVED -> TSFOutputMode.TSF_STEREO_INTERLEAVED
90-
SoundFont.OutputMode.TSF_STEREO_UNWEAVED -> TSFOutputMode.TSF_STEREO_UNWEAVED
91-
SoundFont.OutputMode.TSF_MONO -> TSFOutputMode.TSF_MONO
92+
SoundFont.OutputMode.TSF_STEREO_UNWEAVED -> TSFOutputMode.TSF_STEREO_UNWEAVED
93+
SoundFont.OutputMode.TSF_MONO -> TSFOutputMode.TSF_MONO
9294
}
95+
96+
tsf_set_output(it.reinterpret(), tsfMode, sampleRate, globalGainDb)
9397
}
9498
}
9599

@@ -149,13 +153,28 @@ internal class SoundFontDelegate : SoundFont {
149153

150154
override fun renderFloat(samples: Int, channels: Int, isMixing: Boolean): FloatArray {
151155
return withSoundFont {
152-
val buffer = FloatArray(samples * channels)
153-
val flagMixing = if (isMixing) 1 else 0
154-
tsf_render_float(it.reinterpret(), buffer.refTo(0), samples, flagMixing)
155-
buffer
156+
memScoped {
157+
val buffer = malloc((samples * channels * Float.SIZE_BYTES).toULong()) ?: return@withSoundFont floatArrayOf()
158+
val flagMixing = if (isMixing) 1 else 0
159+
tsf_render_float(it.reinterpret(), buffer.reinterpret(), samples, flagMixing)
160+
161+
val bytes = buffer.readBytes(samples * channels * Float.SIZE_BYTES)
162+
163+
bytes.toFloatArray()
164+
}
156165
}
157166
}
158167

159168
private fun <T> withSoundFont(block: (soundFont: CPointer<*>) -> T): T =
160169
this.soundFont.let(block) ?: throw IllegalStateException("SoundFont not loaded")
170+
171+
private fun ByteArray.toFloatArray(): FloatArray {
172+
return FloatArray(size / Float.SIZE_BYTES) { index ->
173+
val intBits = (this[index * Float.SIZE_BYTES].toInt() and 0xFF) or
174+
((this[index * Float.SIZE_BYTES + 1].toInt() and 0xFF) shl 8) or
175+
((this[index * Float.SIZE_BYTES + 2].toInt() and 0xFF) shl 16) or
176+
((this[index * Float.SIZE_BYTES + 3].toInt() and 0xFF) shl 24)
177+
Float.fromBits(intBits)
178+
}
179+
}
161180
}

0 commit comments

Comments
 (0)