Skip to content

Commit

Permalink
Add send commands
Browse files Browse the repository at this point in the history
  • Loading branch information
simonnorberg committed Aug 6, 2023
1 parent d827fd3 commit 70c0fe8
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 39 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ kortholt.openPatch(R.raw.test, "test.pd")
// Start the audio output stream
kortholt.startStream()

// Use PdBase to send messages to libpd
PdBase.sendBang("play")
// Send messages to libpd
kortholt.sendBang("play")

// Stop the audio output stream
kortholt.stopStream()
Expand All @@ -44,7 +44,7 @@ repositories {
mavenCentral()
}
dependencies {
implementation "net.simno.kortholt:kortholt:2.0.1"
implementation "net.simno.kortholt:kortholt:3.0.0"
}
```

Expand Down
2 changes: 2 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ coroutines = "1.7.3"
kotlin = "1.9.0"
ktlint = "0.50.0"
relinker = "1.4.5"
zip4j = "2.11.5"

[plugins]
android-application = { id = "com.android.application", version.ref = "agp" }
Expand All @@ -36,3 +37,4 @@ compose-material = { module = "androidx.compose.material3:material3" }
compose-lint = { module = "com.slack.lint.compose:compose-lint-checks", version.ref = "compose-lint" }
coroutines-core = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-core", version.ref = "coroutines" }
relinker = { module = "com.getkeepsafe.relinker:relinker", version.ref = "relinker" }
zip4j = { module = "net.lingala.zip4j:zip4j", version.ref = "zip4j" }
10 changes: 3 additions & 7 deletions lib/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,6 @@ android {
path("src/main/cpp/CMakeLists.txt")
}
}
sourceSets {
getByName("main") {
java.srcDirs("src/main/java", "src/main/cpp/libpd/java")
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
Expand Down Expand Up @@ -58,12 +53,13 @@ dependencies {
api(libs.androidx.annotation)
implementation(libs.androidx.core)
implementation(libs.relinker)
implementation(libs.zip4j)
}

val siteUrl = "https://github.com/simonnorberg/kortholt"
val gitUrl = "https://github.com/simonnorberg/kortholt.git"

version = "2.0.1"
version = "3.0.0"
group = "net.simno.kortholt"

afterEvaluate {
Expand All @@ -73,7 +69,7 @@ afterEvaluate {
from(components["release"])
groupId = "net.simno.kortholt"
artifactId = "kortholt"
version = "2.0.1"
version = "3.0.0"
pom {
name.set("kortholt")
url.set(siteUrl)
Expand Down
5 changes: 3 additions & 2 deletions lib/src/main/java/net/simno/kortholt/Kortholt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,11 @@ object Kortholt {
): Boolean

suspend fun closePatch(): Boolean

suspend fun startStream(): Boolean

suspend fun stopStream(): Boolean
fun sendBang(receiver: String)
fun sendFloat(receiver: String, x: Float)
fun sendList(receiver: String, vararg args: Any)

@ExperimentalWaveFile
suspend fun saveWaveFile(
Expand Down
60 changes: 36 additions & 24 deletions lib/src/main/java/net/simno/kortholt/KortholtPlayer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,23 @@ import java.util.concurrent.atomic.AtomicLong
import kotlin.time.Duration
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.withContext
import net.lingala.zip4j.ZipFile
import org.puredata.core.PdBase
import org.puredata.core.PdBaseLoader
import org.puredata.core.utils.IoUtils

internal class KortholtPlayer(
private val context: Context,
private val dispatcher: CoroutineDispatcher
) : Kortholt.Player {

private val pdBase = PdBase()
private val patchHandle = AtomicLong(NOT_SET)
private val kortholtHandle = AtomicLong(NOT_SET)

init {
PdBaseLoader.loaderHandler = object : PdBaseLoader() {
override fun load() {
ReLinker.loadLibrary(context, "pd", VERSION)
ReLinker.loadLibrary(context, "pdnative", VERSION)
ReLinker.loadLibrary(context, "kortholt", VERSION)
}
}
ReLinker.loadLibrary(context, "pd", VERSION)
ReLinker.loadLibrary(context, "pdnative", VERSION)
ReLinker.loadLibrary(context, "kortholt", VERSION)
pdBase.initialize()
}

override suspend fun openPatch(
Expand All @@ -39,27 +36,30 @@ internal class KortholtPlayer(
extractZip: Boolean
) = withContext(dispatcher) {
runCatching {
PdBase.addToSearchPath(context.applicationInfo.nativeLibraryDir)
val dir = context.cacheDir
var patchFile: File? = null
runCatching {
context.resources.openRawResource(patchRes).use { input ->
patchFile = if (extractZip) {
IoUtils.extractZipResource(input, dir, true)
File(dir, patchName)
} else {
IoUtils.extractResource(input, patchName, dir)
}
pdBase.addToSearchPath(context.applicationInfo.nativeLibraryDir)
context.resources.openRawResource(patchRes).use { input ->
val dir = context.cacheDir
val patchFile = File(dir, patchName)
if (extractZip) {
dir.mkdirs()
val zip = File.createTempFile("temp", ".zip")
zip.outputStream().use { output -> input.copyTo(output) }
ZipFile(zip).extractAll(dir.absolutePath)
zip.delete()
} else {
patchFile.outputStream().use { output -> input.copyTo(output) }
}
if (patchFile.exists()) {
patchHandle.set(pdBase.openFile(patchFile.name, patchFile.parentFile?.absolutePath ?: "."))
}
patchHandle.set(PdBase.openPatch(patchFile).toLong())
patchFile.delete()
}
patchFile?.delete()
}.isSuccess
}

override suspend fun closePatch() = withContext(dispatcher) {
runCatching {
patchHandle.getAndSet(NOT_SET).takeIf { it != NOT_SET }?.let { PdBase.closePatch(it.toInt()) }
patchHandle.getAndSet(NOT_SET).takeIf { it != NOT_SET }?.let { pdBase.closeFile(it) }
}.isSuccess
}

Expand All @@ -79,6 +79,18 @@ internal class KortholtPlayer(
}.isSuccess
}

override fun sendBang(receiver: String) {
pdBase.sendBang(receiver)
}

override fun sendFloat(receiver: String, x: Float) {
pdBase.sendFloat(receiver, x)
}

override fun sendList(receiver: String, vararg args: Any) {
pdBase.sendList(receiver, *args)
}

@ExperimentalWaveFile
override suspend fun saveWaveFile(
outputFile: File,
Expand Down Expand Up @@ -125,6 +137,6 @@ internal class KortholtPlayer(

companion object {
private const val NOT_SET = -1L
private const val VERSION = "2.0.1"
private const val VERSION = "3.0.0"
}
}
37 changes: 37 additions & 0 deletions lib/src/main/java/org/puredata/core/PdBase.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package org.puredata.core

internal class PdBase {
external fun initialize()
external fun addToSearchPath(dir: String)
external fun openFile(patch: String, dir: String): Long
external fun closeFile(ptr: Long)

external fun sendBang(receiver: String): Int
external fun sendFloat(receiver: String, x: Float): Int

fun sendList(receiver: String, vararg args: Any) {
if (startMessage(args.size) == 0) {
var success = true
for (arg in args) {
when (arg) {
is Int -> addFloat(arg.toFloat())
is Float -> addFloat(arg)
is Double -> addFloat(arg.toFloat())
is String -> addSymbol(arg)
else -> {
success = false
break
}
}
}
if (success) {
finishList(receiver)
}
}
}

private external fun startMessage(length: Int): Int
private external fun addFloat(x: Float)
private external fun addSymbol(symbol: String)
private external fun finishList(receiver: String): Int
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import net.simno.kortholt.ExperimentalWaveFile
import net.simno.kortholt.kortholt
import org.puredata.core.PdBase

@OptIn(ExperimentalWaveFile::class)
@Composable
Expand Down Expand Up @@ -76,14 +75,14 @@ fun SampleScreen(
label = R.string.left_label,
checked = true,
onCheckedChange = { isChecked ->
PdBase.sendFloat("left", if (isChecked) 1f else 0f)
kortholt.sendFloat("left", if (isChecked) 1f else 0f)
}
)
LabeledCheckbox(
label = R.string.right_label,
checked = true,
onCheckedChange = { isChecked ->
PdBase.sendFloat("right", if (isChecked) 1f else 0f)
kortholt.sendFloat("right", if (isChecked) 1f else 0f)
}
)
Text(
Expand Down

0 comments on commit 70c0fe8

Please sign in to comment.