diff --git a/app/src/main/cpp/AudioHost.cpp b/app/src/main/cpp/AudioHost.cpp index dc80bd1..8e98b44 100644 --- a/app/src/main/cpp/AudioHost.cpp +++ b/app/src/main/cpp/AudioHost.cpp @@ -15,6 +15,9 @@ } AudioHost *thiz = static_cast(userData); for (auto const &instrument: *thiz->instruments) { + if (!instrument) { + continue; + } instrument->render(buffer, sampleCount); } for (uint32_t i = 0; i < sampleCount; i++) { diff --git a/app/src/main/cpp/AudioHost.cpp b/app/src/main/cpp/AudioHost.cpp index dc80bd1..8e98b44 100644 --- a/app/src/main/cpp/AudioHost.cpp +++ b/app/src/main/cpp/AudioHost.cpp @@ -15,6 +15,9 @@ } AudioHost *thiz = static_cast(userData); for (auto const &instrument: *thiz->instruments) { + if (!instrument) { + continue; + } instrument->render(buffer, sampleCount); } for (uint32_t i = 0; i < sampleCount; i++) { diff --git a/app/src/main/cpp/JavaFunctions.cpp b/app/src/main/cpp/JavaFunctions.cpp index 61b6e73..5505a35 100644 --- a/app/src/main/cpp/JavaFunctions.cpp +++ b/app/src/main/cpp/JavaFunctions.cpp @@ -8,7 +8,7 @@ static AudioHost *audioHost; -template +template void *listGet(_InputIterator iterator, uint32_t n) { for (uint32_t i = 0; i < n; i++) { iterator++; @@ -16,6 +16,18 @@ return *iterator; } +Instrument *getInstrument(uint32_t id) { + return static_cast(listGet(audioHost->instruments->begin(), id)); +} + +template +void listSet(_InputIterator iterator, uint32_t n, void *value) { + for (uint32_t i = 0; i < n; i++) { + iterator++; + } + *iterator = static_cast(value); +} + extern "C" { JNIEXPORT void JNICALL @@ -34,17 +46,13 @@ JNIEXPORT void JNICALL Java_com_lukas_music_instruments_InternalInstrument_startNote(JNIEnv *env, jobject thiz, jint id, jdouble frequency) { - Instrument *instrument = static_cast(listGet(audioHost->instruments->begin(), - id)); - instrument->startNote(frequency); + getInstrument(id)->startNote(frequency); } JNIEXPORT void JNICALL Java_com_lukas_music_instruments_InternalInstrument_endNote(JNIEnv *env, jobject thiz, jint id) { - Instrument *instrument = static_cast(listGet(audioHost->instruments->begin(), - id)); - instrument->endNote(); + getInstrument(id)->endNote(); } JNIEXPORT void JNICALL Java_com_lukas_music_ui_fragments_PlayFragment_setMasterVolume(JNIEnv *env, jobject thiz, @@ -54,16 +62,18 @@ JNIEXPORT void JNICALL Java_com_lukas_music_instruments_InternalInstrument_setInstrumentWaveform(JNIEnv *env, jobject thiz, jint id, jint waveform) { - Instrument *instrument = static_cast(listGet(audioHost->instruments->begin(), - id)); - instrument->setWaveform(static_cast(waveform)); + getInstrument(id)->setWaveform(static_cast(waveform)); } JNIEXPORT void JNICALL Java_com_lukas_music_instruments_InternalInstrument_setVolume(JNIEnv *env, jobject thiz, jint id, jfloat volume) { - Instrument *instrument = static_cast(listGet(audioHost->instruments->begin(), - id)); - instrument->wave->amplitude = volume; + getInstrument(id)->wave->amplitude = volume; +} + +JNIEXPORT void JNICALL +Java_com_lukas_music_instruments_InternalInstrument_destroy(JNIEnv *env, jobject thiz, jint id) { + listSet(audioHost->instruments->begin(), id, nullptr); + delete getInstrument(id); } } \ No newline at end of file diff --git a/app/src/main/cpp/AudioHost.cpp b/app/src/main/cpp/AudioHost.cpp index dc80bd1..8e98b44 100644 --- a/app/src/main/cpp/AudioHost.cpp +++ b/app/src/main/cpp/AudioHost.cpp @@ -15,6 +15,9 @@ } AudioHost *thiz = static_cast(userData); for (auto const &instrument: *thiz->instruments) { + if (!instrument) { + continue; + } instrument->render(buffer, sampleCount); } for (uint32_t i = 0; i < sampleCount; i++) { diff --git a/app/src/main/cpp/JavaFunctions.cpp b/app/src/main/cpp/JavaFunctions.cpp index 61b6e73..5505a35 100644 --- a/app/src/main/cpp/JavaFunctions.cpp +++ b/app/src/main/cpp/JavaFunctions.cpp @@ -8,7 +8,7 @@ static AudioHost *audioHost; -template +template void *listGet(_InputIterator iterator, uint32_t n) { for (uint32_t i = 0; i < n; i++) { iterator++; @@ -16,6 +16,18 @@ return *iterator; } +Instrument *getInstrument(uint32_t id) { + return static_cast(listGet(audioHost->instruments->begin(), id)); +} + +template +void listSet(_InputIterator iterator, uint32_t n, void *value) { + for (uint32_t i = 0; i < n; i++) { + iterator++; + } + *iterator = static_cast(value); +} + extern "C" { JNIEXPORT void JNICALL @@ -34,17 +46,13 @@ JNIEXPORT void JNICALL Java_com_lukas_music_instruments_InternalInstrument_startNote(JNIEnv *env, jobject thiz, jint id, jdouble frequency) { - Instrument *instrument = static_cast(listGet(audioHost->instruments->begin(), - id)); - instrument->startNote(frequency); + getInstrument(id)->startNote(frequency); } JNIEXPORT void JNICALL Java_com_lukas_music_instruments_InternalInstrument_endNote(JNIEnv *env, jobject thiz, jint id) { - Instrument *instrument = static_cast(listGet(audioHost->instruments->begin(), - id)); - instrument->endNote(); + getInstrument(id)->endNote(); } JNIEXPORT void JNICALL Java_com_lukas_music_ui_fragments_PlayFragment_setMasterVolume(JNIEnv *env, jobject thiz, @@ -54,16 +62,18 @@ JNIEXPORT void JNICALL Java_com_lukas_music_instruments_InternalInstrument_setInstrumentWaveform(JNIEnv *env, jobject thiz, jint id, jint waveform) { - Instrument *instrument = static_cast(listGet(audioHost->instruments->begin(), - id)); - instrument->setWaveform(static_cast(waveform)); + getInstrument(id)->setWaveform(static_cast(waveform)); } JNIEXPORT void JNICALL Java_com_lukas_music_instruments_InternalInstrument_setVolume(JNIEnv *env, jobject thiz, jint id, jfloat volume) { - Instrument *instrument = static_cast(listGet(audioHost->instruments->begin(), - id)); - instrument->wave->amplitude = volume; + getInstrument(id)->wave->amplitude = volume; +} + +JNIEXPORT void JNICALL +Java_com_lukas_music_instruments_InternalInstrument_destroy(JNIEnv *env, jobject thiz, jint id) { + listSet(audioHost->instruments->begin(), id, nullptr); + delete getInstrument(id); } } \ No newline at end of file diff --git a/app/src/main/java/com/lukas/music/instruments/Instrument.kt b/app/src/main/java/com/lukas/music/instruments/Instrument.kt index b8ef1bc..92a896c 100644 --- a/app/src/main/java/com/lukas/music/instruments/Instrument.kt +++ b/app/src/main/java/com/lukas/music/instruments/Instrument.kt @@ -23,6 +23,7 @@ abstract fun startNote(note: Note) abstract fun stop() abstract fun stopNote(note: Note) + abstract fun destroy() companion object { val instruments = mutableListOf() diff --git a/app/src/main/cpp/AudioHost.cpp b/app/src/main/cpp/AudioHost.cpp index dc80bd1..8e98b44 100644 --- a/app/src/main/cpp/AudioHost.cpp +++ b/app/src/main/cpp/AudioHost.cpp @@ -15,6 +15,9 @@ } AudioHost *thiz = static_cast(userData); for (auto const &instrument: *thiz->instruments) { + if (!instrument) { + continue; + } instrument->render(buffer, sampleCount); } for (uint32_t i = 0; i < sampleCount; i++) { diff --git a/app/src/main/cpp/JavaFunctions.cpp b/app/src/main/cpp/JavaFunctions.cpp index 61b6e73..5505a35 100644 --- a/app/src/main/cpp/JavaFunctions.cpp +++ b/app/src/main/cpp/JavaFunctions.cpp @@ -8,7 +8,7 @@ static AudioHost *audioHost; -template +template void *listGet(_InputIterator iterator, uint32_t n) { for (uint32_t i = 0; i < n; i++) { iterator++; @@ -16,6 +16,18 @@ return *iterator; } +Instrument *getInstrument(uint32_t id) { + return static_cast(listGet(audioHost->instruments->begin(), id)); +} + +template +void listSet(_InputIterator iterator, uint32_t n, void *value) { + for (uint32_t i = 0; i < n; i++) { + iterator++; + } + *iterator = static_cast(value); +} + extern "C" { JNIEXPORT void JNICALL @@ -34,17 +46,13 @@ JNIEXPORT void JNICALL Java_com_lukas_music_instruments_InternalInstrument_startNote(JNIEnv *env, jobject thiz, jint id, jdouble frequency) { - Instrument *instrument = static_cast(listGet(audioHost->instruments->begin(), - id)); - instrument->startNote(frequency); + getInstrument(id)->startNote(frequency); } JNIEXPORT void JNICALL Java_com_lukas_music_instruments_InternalInstrument_endNote(JNIEnv *env, jobject thiz, jint id) { - Instrument *instrument = static_cast(listGet(audioHost->instruments->begin(), - id)); - instrument->endNote(); + getInstrument(id)->endNote(); } JNIEXPORT void JNICALL Java_com_lukas_music_ui_fragments_PlayFragment_setMasterVolume(JNIEnv *env, jobject thiz, @@ -54,16 +62,18 @@ JNIEXPORT void JNICALL Java_com_lukas_music_instruments_InternalInstrument_setInstrumentWaveform(JNIEnv *env, jobject thiz, jint id, jint waveform) { - Instrument *instrument = static_cast(listGet(audioHost->instruments->begin(), - id)); - instrument->setWaveform(static_cast(waveform)); + getInstrument(id)->setWaveform(static_cast(waveform)); } JNIEXPORT void JNICALL Java_com_lukas_music_instruments_InternalInstrument_setVolume(JNIEnv *env, jobject thiz, jint id, jfloat volume) { - Instrument *instrument = static_cast(listGet(audioHost->instruments->begin(), - id)); - instrument->wave->amplitude = volume; + getInstrument(id)->wave->amplitude = volume; +} + +JNIEXPORT void JNICALL +Java_com_lukas_music_instruments_InternalInstrument_destroy(JNIEnv *env, jobject thiz, jint id) { + listSet(audioHost->instruments->begin(), id, nullptr); + delete getInstrument(id); } } \ No newline at end of file diff --git a/app/src/main/java/com/lukas/music/instruments/Instrument.kt b/app/src/main/java/com/lukas/music/instruments/Instrument.kt index b8ef1bc..92a896c 100644 --- a/app/src/main/java/com/lukas/music/instruments/Instrument.kt +++ b/app/src/main/java/com/lukas/music/instruments/Instrument.kt @@ -23,6 +23,7 @@ abstract fun startNote(note: Note) abstract fun stop() abstract fun stopNote(note: Note) + abstract fun destroy() companion object { val instruments = mutableListOf() diff --git a/app/src/main/java/com/lukas/music/instruments/InternalInstrument.kt b/app/src/main/java/com/lukas/music/instruments/InternalInstrument.kt index 0bb7281..b4c68a4 100644 --- a/app/src/main/java/com/lukas/music/instruments/InternalInstrument.kt +++ b/app/src/main/java/com/lukas/music/instruments/InternalInstrument.kt @@ -63,9 +63,14 @@ endNote(id) } + fun destroy() { + destroy(id) + } + private external fun createInstrument(): Int private external fun setInstrumentWaveform(id: Int, waveform: Int) private external fun startNote(id: Int, frequency: Double) private external fun endNote(id: Int) private external fun setVolume(id: Int, volume: Float) + private external fun destroy(id: Int) } \ No newline at end of file diff --git a/app/src/main/cpp/AudioHost.cpp b/app/src/main/cpp/AudioHost.cpp index dc80bd1..8e98b44 100644 --- a/app/src/main/cpp/AudioHost.cpp +++ b/app/src/main/cpp/AudioHost.cpp @@ -15,6 +15,9 @@ } AudioHost *thiz = static_cast(userData); for (auto const &instrument: *thiz->instruments) { + if (!instrument) { + continue; + } instrument->render(buffer, sampleCount); } for (uint32_t i = 0; i < sampleCount; i++) { diff --git a/app/src/main/cpp/JavaFunctions.cpp b/app/src/main/cpp/JavaFunctions.cpp index 61b6e73..5505a35 100644 --- a/app/src/main/cpp/JavaFunctions.cpp +++ b/app/src/main/cpp/JavaFunctions.cpp @@ -8,7 +8,7 @@ static AudioHost *audioHost; -template +template void *listGet(_InputIterator iterator, uint32_t n) { for (uint32_t i = 0; i < n; i++) { iterator++; @@ -16,6 +16,18 @@ return *iterator; } +Instrument *getInstrument(uint32_t id) { + return static_cast(listGet(audioHost->instruments->begin(), id)); +} + +template +void listSet(_InputIterator iterator, uint32_t n, void *value) { + for (uint32_t i = 0; i < n; i++) { + iterator++; + } + *iterator = static_cast(value); +} + extern "C" { JNIEXPORT void JNICALL @@ -34,17 +46,13 @@ JNIEXPORT void JNICALL Java_com_lukas_music_instruments_InternalInstrument_startNote(JNIEnv *env, jobject thiz, jint id, jdouble frequency) { - Instrument *instrument = static_cast(listGet(audioHost->instruments->begin(), - id)); - instrument->startNote(frequency); + getInstrument(id)->startNote(frequency); } JNIEXPORT void JNICALL Java_com_lukas_music_instruments_InternalInstrument_endNote(JNIEnv *env, jobject thiz, jint id) { - Instrument *instrument = static_cast(listGet(audioHost->instruments->begin(), - id)); - instrument->endNote(); + getInstrument(id)->endNote(); } JNIEXPORT void JNICALL Java_com_lukas_music_ui_fragments_PlayFragment_setMasterVolume(JNIEnv *env, jobject thiz, @@ -54,16 +62,18 @@ JNIEXPORT void JNICALL Java_com_lukas_music_instruments_InternalInstrument_setInstrumentWaveform(JNIEnv *env, jobject thiz, jint id, jint waveform) { - Instrument *instrument = static_cast(listGet(audioHost->instruments->begin(), - id)); - instrument->setWaveform(static_cast(waveform)); + getInstrument(id)->setWaveform(static_cast(waveform)); } JNIEXPORT void JNICALL Java_com_lukas_music_instruments_InternalInstrument_setVolume(JNIEnv *env, jobject thiz, jint id, jfloat volume) { - Instrument *instrument = static_cast(listGet(audioHost->instruments->begin(), - id)); - instrument->wave->amplitude = volume; + getInstrument(id)->wave->amplitude = volume; +} + +JNIEXPORT void JNICALL +Java_com_lukas_music_instruments_InternalInstrument_destroy(JNIEnv *env, jobject thiz, jint id) { + listSet(audioHost->instruments->begin(), id, nullptr); + delete getInstrument(id); } } \ No newline at end of file diff --git a/app/src/main/java/com/lukas/music/instruments/Instrument.kt b/app/src/main/java/com/lukas/music/instruments/Instrument.kt index b8ef1bc..92a896c 100644 --- a/app/src/main/java/com/lukas/music/instruments/Instrument.kt +++ b/app/src/main/java/com/lukas/music/instruments/Instrument.kt @@ -23,6 +23,7 @@ abstract fun startNote(note: Note) abstract fun stop() abstract fun stopNote(note: Note) + abstract fun destroy() companion object { val instruments = mutableListOf() diff --git a/app/src/main/java/com/lukas/music/instruments/InternalInstrument.kt b/app/src/main/java/com/lukas/music/instruments/InternalInstrument.kt index 0bb7281..b4c68a4 100644 --- a/app/src/main/java/com/lukas/music/instruments/InternalInstrument.kt +++ b/app/src/main/java/com/lukas/music/instruments/InternalInstrument.kt @@ -63,9 +63,14 @@ endNote(id) } + fun destroy() { + destroy(id) + } + private external fun createInstrument(): Int private external fun setInstrumentWaveform(id: Int, waveform: Int) private external fun startNote(id: Int, frequency: Double) private external fun endNote(id: Int) private external fun setVolume(id: Int, volume: Float) + private external fun destroy(id: Int) } \ No newline at end of file diff --git a/app/src/main/java/com/lukas/music/instruments/MonoInstrument.kt b/app/src/main/java/com/lukas/music/instruments/MonoInstrument.kt index 8b1ba50..2b43524 100644 --- a/app/src/main/java/com/lukas/music/instruments/MonoInstrument.kt +++ b/app/src/main/java/com/lukas/music/instruments/MonoInstrument.kt @@ -49,4 +49,8 @@ stop() } } + + override fun destroy() { + internalInstrument.destroy() + } } \ No newline at end of file diff --git a/app/src/main/cpp/AudioHost.cpp b/app/src/main/cpp/AudioHost.cpp index dc80bd1..8e98b44 100644 --- a/app/src/main/cpp/AudioHost.cpp +++ b/app/src/main/cpp/AudioHost.cpp @@ -15,6 +15,9 @@ } AudioHost *thiz = static_cast(userData); for (auto const &instrument: *thiz->instruments) { + if (!instrument) { + continue; + } instrument->render(buffer, sampleCount); } for (uint32_t i = 0; i < sampleCount; i++) { diff --git a/app/src/main/cpp/JavaFunctions.cpp b/app/src/main/cpp/JavaFunctions.cpp index 61b6e73..5505a35 100644 --- a/app/src/main/cpp/JavaFunctions.cpp +++ b/app/src/main/cpp/JavaFunctions.cpp @@ -8,7 +8,7 @@ static AudioHost *audioHost; -template +template void *listGet(_InputIterator iterator, uint32_t n) { for (uint32_t i = 0; i < n; i++) { iterator++; @@ -16,6 +16,18 @@ return *iterator; } +Instrument *getInstrument(uint32_t id) { + return static_cast(listGet(audioHost->instruments->begin(), id)); +} + +template +void listSet(_InputIterator iterator, uint32_t n, void *value) { + for (uint32_t i = 0; i < n; i++) { + iterator++; + } + *iterator = static_cast(value); +} + extern "C" { JNIEXPORT void JNICALL @@ -34,17 +46,13 @@ JNIEXPORT void JNICALL Java_com_lukas_music_instruments_InternalInstrument_startNote(JNIEnv *env, jobject thiz, jint id, jdouble frequency) { - Instrument *instrument = static_cast(listGet(audioHost->instruments->begin(), - id)); - instrument->startNote(frequency); + getInstrument(id)->startNote(frequency); } JNIEXPORT void JNICALL Java_com_lukas_music_instruments_InternalInstrument_endNote(JNIEnv *env, jobject thiz, jint id) { - Instrument *instrument = static_cast(listGet(audioHost->instruments->begin(), - id)); - instrument->endNote(); + getInstrument(id)->endNote(); } JNIEXPORT void JNICALL Java_com_lukas_music_ui_fragments_PlayFragment_setMasterVolume(JNIEnv *env, jobject thiz, @@ -54,16 +62,18 @@ JNIEXPORT void JNICALL Java_com_lukas_music_instruments_InternalInstrument_setInstrumentWaveform(JNIEnv *env, jobject thiz, jint id, jint waveform) { - Instrument *instrument = static_cast(listGet(audioHost->instruments->begin(), - id)); - instrument->setWaveform(static_cast(waveform)); + getInstrument(id)->setWaveform(static_cast(waveform)); } JNIEXPORT void JNICALL Java_com_lukas_music_instruments_InternalInstrument_setVolume(JNIEnv *env, jobject thiz, jint id, jfloat volume) { - Instrument *instrument = static_cast(listGet(audioHost->instruments->begin(), - id)); - instrument->wave->amplitude = volume; + getInstrument(id)->wave->amplitude = volume; +} + +JNIEXPORT void JNICALL +Java_com_lukas_music_instruments_InternalInstrument_destroy(JNIEnv *env, jobject thiz, jint id) { + listSet(audioHost->instruments->begin(), id, nullptr); + delete getInstrument(id); } } \ No newline at end of file diff --git a/app/src/main/java/com/lukas/music/instruments/Instrument.kt b/app/src/main/java/com/lukas/music/instruments/Instrument.kt index b8ef1bc..92a896c 100644 --- a/app/src/main/java/com/lukas/music/instruments/Instrument.kt +++ b/app/src/main/java/com/lukas/music/instruments/Instrument.kt @@ -23,6 +23,7 @@ abstract fun startNote(note: Note) abstract fun stop() abstract fun stopNote(note: Note) + abstract fun destroy() companion object { val instruments = mutableListOf() diff --git a/app/src/main/java/com/lukas/music/instruments/InternalInstrument.kt b/app/src/main/java/com/lukas/music/instruments/InternalInstrument.kt index 0bb7281..b4c68a4 100644 --- a/app/src/main/java/com/lukas/music/instruments/InternalInstrument.kt +++ b/app/src/main/java/com/lukas/music/instruments/InternalInstrument.kt @@ -63,9 +63,14 @@ endNote(id) } + fun destroy() { + destroy(id) + } + private external fun createInstrument(): Int private external fun setInstrumentWaveform(id: Int, waveform: Int) private external fun startNote(id: Int, frequency: Double) private external fun endNote(id: Int) private external fun setVolume(id: Int, volume: Float) + private external fun destroy(id: Int) } \ No newline at end of file diff --git a/app/src/main/java/com/lukas/music/instruments/MonoInstrument.kt b/app/src/main/java/com/lukas/music/instruments/MonoInstrument.kt index 8b1ba50..2b43524 100644 --- a/app/src/main/java/com/lukas/music/instruments/MonoInstrument.kt +++ b/app/src/main/java/com/lukas/music/instruments/MonoInstrument.kt @@ -49,4 +49,8 @@ stop() } } + + override fun destroy() { + internalInstrument.destroy() + } } \ No newline at end of file diff --git a/app/src/main/java/com/lukas/music/instruments/PolyInstrument.kt b/app/src/main/java/com/lukas/music/instruments/PolyInstrument.kt index c6dae83..26e4fbb 100644 --- a/app/src/main/java/com/lukas/music/instruments/PolyInstrument.kt +++ b/app/src/main/java/com/lukas/music/instruments/PolyInstrument.kt @@ -69,4 +69,10 @@ } } } + + override fun destroy() { + for (instrument in internalInstruments) { + instrument.destroy() + } + } } \ No newline at end of file diff --git a/app/src/main/cpp/AudioHost.cpp b/app/src/main/cpp/AudioHost.cpp index dc80bd1..8e98b44 100644 --- a/app/src/main/cpp/AudioHost.cpp +++ b/app/src/main/cpp/AudioHost.cpp @@ -15,6 +15,9 @@ } AudioHost *thiz = static_cast(userData); for (auto const &instrument: *thiz->instruments) { + if (!instrument) { + continue; + } instrument->render(buffer, sampleCount); } for (uint32_t i = 0; i < sampleCount; i++) { diff --git a/app/src/main/cpp/JavaFunctions.cpp b/app/src/main/cpp/JavaFunctions.cpp index 61b6e73..5505a35 100644 --- a/app/src/main/cpp/JavaFunctions.cpp +++ b/app/src/main/cpp/JavaFunctions.cpp @@ -8,7 +8,7 @@ static AudioHost *audioHost; -template +template void *listGet(_InputIterator iterator, uint32_t n) { for (uint32_t i = 0; i < n; i++) { iterator++; @@ -16,6 +16,18 @@ return *iterator; } +Instrument *getInstrument(uint32_t id) { + return static_cast(listGet(audioHost->instruments->begin(), id)); +} + +template +void listSet(_InputIterator iterator, uint32_t n, void *value) { + for (uint32_t i = 0; i < n; i++) { + iterator++; + } + *iterator = static_cast(value); +} + extern "C" { JNIEXPORT void JNICALL @@ -34,17 +46,13 @@ JNIEXPORT void JNICALL Java_com_lukas_music_instruments_InternalInstrument_startNote(JNIEnv *env, jobject thiz, jint id, jdouble frequency) { - Instrument *instrument = static_cast(listGet(audioHost->instruments->begin(), - id)); - instrument->startNote(frequency); + getInstrument(id)->startNote(frequency); } JNIEXPORT void JNICALL Java_com_lukas_music_instruments_InternalInstrument_endNote(JNIEnv *env, jobject thiz, jint id) { - Instrument *instrument = static_cast(listGet(audioHost->instruments->begin(), - id)); - instrument->endNote(); + getInstrument(id)->endNote(); } JNIEXPORT void JNICALL Java_com_lukas_music_ui_fragments_PlayFragment_setMasterVolume(JNIEnv *env, jobject thiz, @@ -54,16 +62,18 @@ JNIEXPORT void JNICALL Java_com_lukas_music_instruments_InternalInstrument_setInstrumentWaveform(JNIEnv *env, jobject thiz, jint id, jint waveform) { - Instrument *instrument = static_cast(listGet(audioHost->instruments->begin(), - id)); - instrument->setWaveform(static_cast(waveform)); + getInstrument(id)->setWaveform(static_cast(waveform)); } JNIEXPORT void JNICALL Java_com_lukas_music_instruments_InternalInstrument_setVolume(JNIEnv *env, jobject thiz, jint id, jfloat volume) { - Instrument *instrument = static_cast(listGet(audioHost->instruments->begin(), - id)); - instrument->wave->amplitude = volume; + getInstrument(id)->wave->amplitude = volume; +} + +JNIEXPORT void JNICALL +Java_com_lukas_music_instruments_InternalInstrument_destroy(JNIEnv *env, jobject thiz, jint id) { + listSet(audioHost->instruments->begin(), id, nullptr); + delete getInstrument(id); } } \ No newline at end of file diff --git a/app/src/main/java/com/lukas/music/instruments/Instrument.kt b/app/src/main/java/com/lukas/music/instruments/Instrument.kt index b8ef1bc..92a896c 100644 --- a/app/src/main/java/com/lukas/music/instruments/Instrument.kt +++ b/app/src/main/java/com/lukas/music/instruments/Instrument.kt @@ -23,6 +23,7 @@ abstract fun startNote(note: Note) abstract fun stop() abstract fun stopNote(note: Note) + abstract fun destroy() companion object { val instruments = mutableListOf() diff --git a/app/src/main/java/com/lukas/music/instruments/InternalInstrument.kt b/app/src/main/java/com/lukas/music/instruments/InternalInstrument.kt index 0bb7281..b4c68a4 100644 --- a/app/src/main/java/com/lukas/music/instruments/InternalInstrument.kt +++ b/app/src/main/java/com/lukas/music/instruments/InternalInstrument.kt @@ -63,9 +63,14 @@ endNote(id) } + fun destroy() { + destroy(id) + } + private external fun createInstrument(): Int private external fun setInstrumentWaveform(id: Int, waveform: Int) private external fun startNote(id: Int, frequency: Double) private external fun endNote(id: Int) private external fun setVolume(id: Int, volume: Float) + private external fun destroy(id: Int) } \ No newline at end of file diff --git a/app/src/main/java/com/lukas/music/instruments/MonoInstrument.kt b/app/src/main/java/com/lukas/music/instruments/MonoInstrument.kt index 8b1ba50..2b43524 100644 --- a/app/src/main/java/com/lukas/music/instruments/MonoInstrument.kt +++ b/app/src/main/java/com/lukas/music/instruments/MonoInstrument.kt @@ -49,4 +49,8 @@ stop() } } + + override fun destroy() { + internalInstrument.destroy() + } } \ No newline at end of file diff --git a/app/src/main/java/com/lukas/music/instruments/PolyInstrument.kt b/app/src/main/java/com/lukas/music/instruments/PolyInstrument.kt index c6dae83..26e4fbb 100644 --- a/app/src/main/java/com/lukas/music/instruments/PolyInstrument.kt +++ b/app/src/main/java/com/lukas/music/instruments/PolyInstrument.kt @@ -69,4 +69,10 @@ } } } + + override fun destroy() { + for (instrument in internalInstruments) { + instrument.destroy() + } + } } \ No newline at end of file diff --git a/app/src/main/java/com/lukas/music/ui/adapters/InstrumentViewHolder.kt b/app/src/main/java/com/lukas/music/ui/adapters/InstrumentViewHolder.kt index b0ec2c5..3e129bc 100644 --- a/app/src/main/java/com/lukas/music/ui/adapters/InstrumentViewHolder.kt +++ b/app/src/main/java/com/lukas/music/ui/adapters/InstrumentViewHolder.kt @@ -60,6 +60,7 @@ val index = Instrument.instruments.indexOf(instrument) Instrument.instruments -= instrument!! adapter.notifyItemRemoved(index) + instrument!!.destroy() } }