Skip to content

Commit

Permalink
Provide audio param setter
Browse files Browse the repository at this point in the history
  • Loading branch information
jurihock committed Jun 7, 2024
1 parent 01a6084 commit 1b4eab2
Show file tree
Hide file tree
Showing 10 changed files with 78 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ class AudioPlugin {
const std::optional<float> samplerate,
const std::optional<size_t> blocksize) = 0;

virtual void set(const std::string& param,
const std::string& value) = 0;

virtual void start() = 0;
virtual void stop() = 0;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,21 @@ jna bool voicesmith_plugin_setup(int input, int output, int samplerate, int bloc
}
}

jna bool voicesmith_plugin_set(const char* param, const char* value, jna_pointer* pointer, jna_result* result) {
if (*pointer == 0) {
return result->nok("Invalid plugin pointer!");
}

try {
auto plugin = reinterpret_cast<AudioPlugin*>(*pointer);
plugin->set(param, value);
return result->ok();
}
catch (const std::exception& exception) {
return result->nok(exception);
}
}

jna bool voicesmith_plugin_start(jna_pointer* pointer, jna_result* result) {
if (*pointer == 0) {
return result->ok();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

jna bool voicesmith_plugin_open(const char* name, jna_callback* callback, jna_pointer* pointer, jna_result* result);
jna bool voicesmith_plugin_setup(int input, int output, int samplerate, int blocksize, jna_pointer* pointer, jna_result* result);
jna bool voicesmith_plugin_set(const char* param, const char* value, jna_pointer* pointer, jna_result* result);
jna bool voicesmith_plugin_start(jna_pointer* pointer, jna_result* result);
jna bool voicesmith_plugin_stop(jna_pointer* pointer, jna_result* result);
jna bool voicesmith_plugin_close(jna_pointer* pointer, jna_result* result);
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ void TestAudioPlugin::setup(const std::optional<int> input,
config.blocksize = blocksize;
}

void TestAudioPlugin::set(const std::string& param,
const std::string& value) {
LOG(INFO) << $("TODO: set {0} to {1}", param, value);
}

void TestAudioPlugin::start() {
if (state.pipeline != nullptr) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ class TestAudioPlugin final : public AudioPlugin {
const std::optional<float> samplerate,
const std::optional<size_t> blocksize) override;

void set(const std::string& param,
const std::string& value) override;

void start() override;
void stop() override;

Expand Down
21 changes: 18 additions & 3 deletions voicesmith/src/main/java/de/jurihock/voicesmith/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -71,24 +71,39 @@ class MainActivity : AudioServiceActivity() {
IntParameterScreen(
name = getString(R.string.delay), unit = getString(R.string.milliseconds), value = delay,
min = 0, max = 1000, inc = 50,
onChange = { delay.intValue = it })
onChange = {
setAudioParameter("delay", it)
delay.intValue = it
})
Spacer(modifier = Modifier.height(Dp(UI.PADDING)))
IntParameterScreen(
name = getString(R.string.pitch), unit = getString(R.string.semitones), value = pitch,
min = -12, max = +12, inc = 1,
onChange = { pitch.intValue = it })
onChange = {
setAudioParameter("pitch", it)
pitch.intValue = it
})
Spacer(modifier = Modifier.height(Dp(UI.PADDING)))
IntParameterScreen(
name = getString(R.string.timbre), unit = getString(R.string.semitones), value = timbre,
min = -12, max = +12, inc = 1,
onChange = { timbre.intValue = it })
onChange = {
setAudioParameter("timbre", it)
timbre.intValue = it
})
Spacer(modifier = Modifier.weight(1f))
}
}
}
}
}

override fun onAudioServiceSync() {
setAudioParameter("delay", delay.intValue)
setAudioParameter("pitch", pitch.intValue)
setAudioParameter("timbre", timbre.intValue)
}

override fun onAudioServiceStarted() {
state.value = true
game.on()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ open class AudioPlugin(val name: String) : AutoCloseable, JnaCallback {
private val ref = JnaPointerByReference()
private val res = JnaResultByReference()

private var callback: ((exception: AudioPluginException) -> Unit)? = null
private var error: ((exception: AudioPluginException) -> Unit)? = null
private var state: Boolean = false

val isStarted: Boolean
Expand All @@ -34,6 +34,14 @@ open class AudioPlugin(val name: String) : AutoCloseable, JnaCallback {
}.onFailure { throw it }
}

fun set(param: String, value: String) {
res.result { res ->
jna.voicesmith_plugin_set(
param, value,
ref, res)
}.onFailure { throw it }
}

fun start() {
res.result { res ->
jna.voicesmith_plugin_start(ref, res)
Expand Down Expand Up @@ -69,13 +77,13 @@ open class AudioPlugin(val name: String) : AutoCloseable, JnaCallback {
}.also {
state = false
}
callback?.invoke(AudioPluginException(event, data))
error?.invoke(AudioPluginException(event, data))
}
}
}

fun onError(callback: (exception: AudioPluginException) -> Unit) {
this.callback = callback
error = callback
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ open class AudioPluginFactory {

external fun voicesmith_plugin_open(name: String, callback: JnaCallback, pointer: JnaPointerByReference, result: JnaResultByReference) : Boolean
external fun voicesmith_plugin_setup(input: Int, output: Int, samplerate: Int, blocksize: Int, pointer: JnaPointerByReference, result: JnaResultByReference) : Boolean
external fun voicesmith_plugin_set(param: String, value: String, pointer: JnaPointerByReference, result: JnaResultByReference) : Boolean
external fun voicesmith_plugin_start(pointer: JnaPointerByReference, result: JnaResultByReference) : Boolean
external fun voicesmith_plugin_stop(pointer: JnaPointerByReference, result: JnaResultByReference) : Boolean
external fun voicesmith_plugin_close(pointer: JnaPointerByReference, result: JnaResultByReference) : Boolean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,25 @@ import de.jurihock.voicesmith.plug.TestAudioPlugin

class AudioService : Service() {

private var callback: ((exception: Throwable) -> Unit)? = null
private var error: ((exception: Throwable) -> Unit)? = null
private var plugin: AudioPlugin? = null

private val features by lazy { AudioFeatures(this) }

val isStarted: Boolean
get() = plugin?.isStarted ?: false

fun set(param: String, value: String) {
Log.i("Changing audio plugin parameter ${param} to ${value}")
try {
plugin?.set(param, value)
} catch (exception: Throwable) {
Log.e(exception)
}
}

fun start() {
Log.i("Starting audio plugin")
plugin?.onError { onPluginError(it) }
try {
plugin?.start()
} catch (exception: Throwable) {
Expand All @@ -41,6 +49,7 @@ class AudioService : Service() {
Log.i("Creating audio service")
try {
plugin = TestAudioPlugin()
plugin?.onError { onPluginError(it) }
plugin?.setup(0, 0, features.samplerate, features.blocksize)
} catch (exception: Throwable) {
Log.e(exception)
Expand All @@ -61,14 +70,14 @@ class AudioService : Service() {
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int = startAudioService()

fun onServiceError(callback: (exception: Throwable) -> Unit) {
this.callback = callback
error = callback
}

private fun onPluginError(exception: Throwable) {
try {
plugin?.stop()
} finally {
callback?.invoke(exception)
error?.invoke(exception)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ abstract class AudioServiceActivity : ComponentActivity(), ServiceConnection {
}
}

protected abstract fun onAudioServiceSync()
protected abstract fun onAudioServiceStarted()
protected abstract fun onAudioServiceStopped()
protected abstract fun onAudioServiceFailed()
Expand Down Expand Up @@ -114,11 +115,12 @@ abstract class AudioServiceActivity : ComponentActivity(), ServiceConnection {

service = binder.service

service?.onServiceError {
Toast.makeText(this, it.message, Toast.LENGTH_LONG).show()
service?.onServiceError { exception ->
Toast.makeText(this, exception.message, Toast.LENGTH_LONG).show()
onAudioServiceFailed()
}

onAudioServiceSync()
onStartStopAudioService()
}

Expand All @@ -130,4 +132,8 @@ abstract class AudioServiceActivity : ComponentActivity(), ServiceConnection {
onAudioServiceStopped()
}

fun <T> setAudioParameter(param: String, value: T) {
service?.set(param, value.toString())
}

}

0 comments on commit 1b4eab2

Please sign in to comment.