Skip to content

Commit 526bb7f

Browse files
committed
Fixed audio generation on Android
1 parent 7bf63ba commit 526bb7f

File tree

8 files changed

+36
-35
lines changed

8 files changed

+36
-35
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.7"
2+
lib = "0.1.8"
33

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

midi/build.gradle.kts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ kotlin {
5252
sourceSets {
5353

5454
commonMain.dependencies {
55-
// implementation(projects.soundfont)
55+
implementation(projects.soundfont)
5656
implementation(libs.kotlinx.io)
5757
}
5858

@@ -115,15 +115,15 @@ mavenPublishing {
115115

116116
// Sign with default plugin
117117
signing {
118-
useInMemoryPgpKeys(
119-
System.getenv("SIGNING_KEY"),
120-
System.getenv("SIGNING_KEY_PASSWORD")
121-
)
122-
sign(publishing.publications)
123-
124-
// Temporary workaround, see https://github.com/gradle/gradle/issues/26091#issuecomment-1722947958
125-
tasks.withType<AbstractPublishToMaven>().configureEach {
126-
val signingTasks = tasks.withType<Sign>()
127-
mustRunAfter(signingTasks)
128-
}
118+
// useInMemoryPgpKeys(
119+
// System.getenv("SIGNING_KEY"),
120+
// System.getenv("SIGNING_KEY_PASSWORD")
121+
// )
122+
// sign(publishing.publications)
123+
//
124+
// // Temporary workaround, see https://github.com/gradle/gradle/issues/26091#issuecomment-1722947958
125+
// tasks.withType<AbstractPublishToMaven>().configureEach {
126+
// val signingTasks = tasks.withType<Sign>()
127+
// mustRunAfter(signingTasks)
128+
// }
129129
}

midi/src/commonMain/kotlin/io/github/lemcoder/mikrosoundfont/midi/MidiSequencer.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import io.github.lemcoder.mikrosoundfont.SoundFont
55
class MidiSequencer(
66
private val soundFont: SoundFont,
77
private val sampleRate: Int,
8+
private val channels: Int
89
) {
910
private val sampleBlockSize: Int = 512
1011
private val messages = mutableListOf<MidiMessage>()
@@ -24,16 +25,16 @@ class MidiSequencer(
2425

2526
while (targetTime > currentTime) {
2627
currentTime += (sampleBlockSize * (1000.0 / sampleRate)).toInt()
27-
audioBuffer += soundFont.renderFloat(sampleBlockSize, false)
28+
audioBuffer += soundFont.renderFloat(sampleBlockSize, channels, false)
2829
}
2930
}
3031

3132
return audioBuffer
3233
}
3334

3435
private fun MidiMessage.process() = when (this) {
35-
is MidiVoiceMessage.NoteOff -> soundFont.channels[channel].noteOff(key)
36-
is MidiVoiceMessage.NoteOn -> soundFont.channels[channel].noteOn(key, if (velocity == 0) 0f else 1f)
36+
is MidiVoiceMessage.NoteOff -> soundFont.noteOff(channel, key)
37+
is MidiVoiceMessage.NoteOn -> soundFont.noteOn(channel, key, velocity / 127.0f)
3738
is MidiVoiceMessage.PitchBend -> soundFont.channels[channel].setPitchWheel(pitchBend)
3839
is MidiVoiceMessage.ProgramChange -> soundFont.channels[channel].setPresetNumber(program, channel == 9)
3940
is MidiVoiceMessage.ControlChange -> soundFont.channels[channel].setMidiControl(control, controlValue)

soundfont/build.gradle.kts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -141,15 +141,15 @@ mavenPublishing {
141141

142142
// Sign with default plugin
143143
signing {
144-
useInMemoryPgpKeys(
145-
System.getenv("SIGNING_KEY"),
146-
System.getenv("SIGNING_KEY_PASSWORD")
147-
)
148-
sign(publishing.publications)
149-
150-
// Temporary workaround, see https://github.com/gradle/gradle/issues/26091#issuecomment-1722947958
151-
tasks.withType<AbstractPublishToMaven>().configureEach {
152-
val signingTasks = tasks.withType<Sign>()
153-
mustRunAfter(signingTasks)
154-
}
144+
// useInMemoryPgpKeys(
145+
// System.getenv("SIGNING_KEY"),
146+
// System.getenv("SIGNING_KEY_PASSWORD")
147+
// )
148+
// sign(publishing.publications)
149+
//
150+
// // Temporary workaround, see https://github.com/gradle/gradle/issues/26091#issuecomment-1722947958
151+
// tasks.withType<AbstractPublishToMaven>().configureEach {
152+
// val signingTasks = tasks.withType<Sign>()
153+
// mustRunAfter(signingTasks)
154+
// }
155155
}

soundfont/src/androidMain/cpp/mikro_sound_font_jni.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,10 @@ JNIEXPORT void JNICALL Java_io_github_lemcoder_mikrosoundfont_internal_SoundFont
8888
}
8989
}
9090

91-
JNIEXPORT jfloatArray JNICALL Java_io_github_lemcoder_mikrosoundfont_internal_SoundFontDelegate_renderFloat(JNIEnv *env, jobject obj, jint samples, jboolean isMixing) {
91+
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) {
93-
int item_count = samples * 2;
94-
float *buffer = malloc(item_count * sizeof(float) * 2);
93+
int item_count = samples * channels;
94+
float *buffer = malloc(item_count * sizeof(float) * channels);
9595
tsf_render_float(g_tsf, buffer, samples, 0);
9696

9797
jfloatArray output = (*env)->NewFloatArray(env, item_count);
@@ -100,6 +100,7 @@ JNIEXPORT jfloatArray JNICALL Java_io_github_lemcoder_mikrosoundfont_internal_So
100100

101101
return output;
102102
}
103+
103104
return NULL;
104105
}
105106

soundfont/src/androidMain/kotlin/io/github/lemcoder/mikrosoundfont/internal/SoundFontDelegate.android.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,5 @@ internal class SoundFontDelegate : SoundFont {
4747
external override fun noteOffAll()
4848
external override fun activeVoiceCount(): Int
4949
external override fun setBankPreset(channel: Int, bank: Int, presetNumber: Int)
50-
external override fun renderFloat(samples: Int, isMixing: Boolean): FloatArray
50+
external override fun renderFloat(samples: Int, channels: Int, isMixing: Boolean): FloatArray
5151
}

soundfont/src/commonMain/kotlin/io/github/lemcoder/mikrosoundfont/SoundFont.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ interface SoundFont {
106106
* Render output samples in F32 format
107107
* @param isMixing: if false clear the buffer first, otherwise mix into existing data
108108
*/
109-
fun renderFloat(samples: Int, isMixing: Boolean): FloatArray
109+
fun renderFloat(samples: Int, channels: Int, isMixing: Boolean): FloatArray
110110

111111
/**
112112
* Supported output modes by the render methods

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@ internal class SoundFontDelegate : SoundFont {
9090
SoundFont.OutputMode.TSF_STEREO_UNWEAVED -> TSFOutputMode.TSF_STEREO_UNWEAVED
9191
SoundFont.OutputMode.TSF_MONO -> TSFOutputMode.TSF_MONO
9292
}
93-
tsf_set_output(it.reinterpret(), tsfMode, sampleRate, globalGainDb)
9493
}
9594
}
9695

@@ -148,9 +147,9 @@ internal class SoundFontDelegate : SoundFont {
148147
}
149148
}
150149

151-
override fun renderFloat(samples: Int, isMixing: Boolean): FloatArray {
150+
override fun renderFloat(samples: Int, channels: Int, isMixing: Boolean): FloatArray {
152151
return withSoundFont {
153-
val buffer = FloatArray(samples * 2)
152+
val buffer = FloatArray(samples * channels)
154153
val flagMixing = if (isMixing) 1 else 0
155154
tsf_render_float(it.reinterpret(), buffer.refTo(0), samples, flagMixing)
156155
buffer

0 commit comments

Comments
 (0)