diff --git a/app/src/main/cpp/Envelope.h b/app/src/main/cpp/Envelope.h index aeaaf58..e3573c5 100644 --- a/app/src/main/cpp/Envelope.h +++ b/app/src/main/cpp/Envelope.h @@ -21,11 +21,8 @@ float attack = 0.05, delay = 0.2, sustain = 0.75, release = 0.1; void initialize(AudioHost *host); - void startNote(); - void endNote(); - float *render(uint32_t sampleCount); }; diff --git a/app/src/main/cpp/Envelope.h b/app/src/main/cpp/Envelope.h index aeaaf58..e3573c5 100644 --- a/app/src/main/cpp/Envelope.h +++ b/app/src/main/cpp/Envelope.h @@ -21,11 +21,8 @@ float attack = 0.05, delay = 0.2, sustain = 0.75, release = 0.1; void initialize(AudioHost *host); - void startNote(); - void endNote(); - float *render(uint32_t sampleCount); }; diff --git a/app/src/main/cpp/Instrument.cpp b/app/src/main/cpp/Instrument.cpp index 55d7ecd..6dacc78 100644 --- a/app/src/main/cpp/Instrument.cpp +++ b/app/src/main/cpp/Instrument.cpp @@ -23,6 +23,6 @@ envelope->startNote(); } -void Instrument::endNote(float frequency) { +void Instrument::endNote() { envelope->endNote(); } \ No newline at end of file diff --git a/app/src/main/cpp/Envelope.h b/app/src/main/cpp/Envelope.h index aeaaf58..e3573c5 100644 --- a/app/src/main/cpp/Envelope.h +++ b/app/src/main/cpp/Envelope.h @@ -21,11 +21,8 @@ float attack = 0.05, delay = 0.2, sustain = 0.75, release = 0.1; void initialize(AudioHost *host); - void startNote(); - void endNote(); - float *render(uint32_t sampleCount); }; diff --git a/app/src/main/cpp/Instrument.cpp b/app/src/main/cpp/Instrument.cpp index 55d7ecd..6dacc78 100644 --- a/app/src/main/cpp/Instrument.cpp +++ b/app/src/main/cpp/Instrument.cpp @@ -23,6 +23,6 @@ envelope->startNote(); } -void Instrument::endNote(float frequency) { +void Instrument::endNote() { envelope->endNote(); } \ No newline at end of file diff --git a/app/src/main/cpp/Instrument.h b/app/src/main/cpp/Instrument.h index 8fecbd1..8e4b402 100644 --- a/app/src/main/cpp/Instrument.h +++ b/app/src/main/cpp/Instrument.h @@ -18,7 +18,7 @@ void startNote(float frequency); - void endNote(float frequency); + void endNote(); }; #endif diff --git a/app/src/main/cpp/Envelope.h b/app/src/main/cpp/Envelope.h index aeaaf58..e3573c5 100644 --- a/app/src/main/cpp/Envelope.h +++ b/app/src/main/cpp/Envelope.h @@ -21,11 +21,8 @@ float attack = 0.05, delay = 0.2, sustain = 0.75, release = 0.1; void initialize(AudioHost *host); - void startNote(); - void endNote(); - float *render(uint32_t sampleCount); }; diff --git a/app/src/main/cpp/Instrument.cpp b/app/src/main/cpp/Instrument.cpp index 55d7ecd..6dacc78 100644 --- a/app/src/main/cpp/Instrument.cpp +++ b/app/src/main/cpp/Instrument.cpp @@ -23,6 +23,6 @@ envelope->startNote(); } -void Instrument::endNote(float frequency) { +void Instrument::endNote() { envelope->endNote(); } \ No newline at end of file diff --git a/app/src/main/cpp/Instrument.h b/app/src/main/cpp/Instrument.h index 8fecbd1..8e4b402 100644 --- a/app/src/main/cpp/Instrument.h +++ b/app/src/main/cpp/Instrument.h @@ -18,7 +18,7 @@ void startNote(float frequency); - void endNote(float frequency); + void endNote(); }; #endif diff --git a/app/src/main/cpp/JavaFunctions.cpp b/app/src/main/cpp/JavaFunctions.cpp index 4e70c26..94cd9ab 100644 --- a/app/src/main/cpp/JavaFunctions.cpp +++ b/app/src/main/cpp/JavaFunctions.cpp @@ -25,7 +25,7 @@ extern "C" JNIEXPORT jint JNICALL -Java_com_lukas_music_instruments_Instrument_createInstrument(JNIEnv *env, jobject thiz) { +Java_com_lukas_music_instruments_InternalInstrument_createInstrument(JNIEnv *env, jobject thiz) { uint32_t result = audioHost->instruments->size(); Instrument *instrument = new Instrument(audioHost); audioHost->instruments->push_back(instrument); @@ -34,17 +34,8 @@ extern "C" JNIEXPORT void JNICALL -Java_com_lukas_music_instruments_Instrument_setInstrumentFrequency(JNIEnv *env, jobject thiz, - jint id, jdouble frequency) { - Instrument *instrument = static_cast(listGet(audioHost->instruments->begin(), id)); - instrument->wave->setFrequency(frequency); -} - - -extern "C" -JNIEXPORT void JNICALL -Java_com_lukas_music_instruments_Instrument_setInstrumentActive(JNIEnv *env, jobject thiz, - jint id, jboolean active) { +Java_com_lukas_music_instruments_InternalInstrument_setInstrumentActive(JNIEnv *env, jobject thiz, + jint id, jboolean active) { Instrument *instrument = static_cast(listGet(audioHost->instruments->begin(), id)); instrument->wave->amplitude = active ? 0.3 : 0.0; @@ -52,8 +43,8 @@ } extern "C" JNIEXPORT void JNICALL -Java_com_lukas_music_instruments_Instrument_startNote(JNIEnv *env, jobject thiz, - jint id, jdouble frequency) { +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); @@ -61,9 +52,9 @@ extern "C" JNIEXPORT void JNICALL -Java_com_lukas_music_instruments_Instrument_endNote(JNIEnv *env, jobject thiz, - jint id, jdouble frequency) { +Java_com_lukas_music_instruments_InternalInstrument_endNote(JNIEnv *env, jobject thiz, + jint id) { Instrument *instrument = static_cast(listGet(audioHost->instruments->begin(), id)); - instrument->endNote(frequency); + instrument->endNote(); } \ No newline at end of file diff --git a/app/src/main/cpp/Envelope.h b/app/src/main/cpp/Envelope.h index aeaaf58..e3573c5 100644 --- a/app/src/main/cpp/Envelope.h +++ b/app/src/main/cpp/Envelope.h @@ -21,11 +21,8 @@ float attack = 0.05, delay = 0.2, sustain = 0.75, release = 0.1; void initialize(AudioHost *host); - void startNote(); - void endNote(); - float *render(uint32_t sampleCount); }; diff --git a/app/src/main/cpp/Instrument.cpp b/app/src/main/cpp/Instrument.cpp index 55d7ecd..6dacc78 100644 --- a/app/src/main/cpp/Instrument.cpp +++ b/app/src/main/cpp/Instrument.cpp @@ -23,6 +23,6 @@ envelope->startNote(); } -void Instrument::endNote(float frequency) { +void Instrument::endNote() { envelope->endNote(); } \ No newline at end of file diff --git a/app/src/main/cpp/Instrument.h b/app/src/main/cpp/Instrument.h index 8fecbd1..8e4b402 100644 --- a/app/src/main/cpp/Instrument.h +++ b/app/src/main/cpp/Instrument.h @@ -18,7 +18,7 @@ void startNote(float frequency); - void endNote(float frequency); + void endNote(); }; #endif diff --git a/app/src/main/cpp/JavaFunctions.cpp b/app/src/main/cpp/JavaFunctions.cpp index 4e70c26..94cd9ab 100644 --- a/app/src/main/cpp/JavaFunctions.cpp +++ b/app/src/main/cpp/JavaFunctions.cpp @@ -25,7 +25,7 @@ extern "C" JNIEXPORT jint JNICALL -Java_com_lukas_music_instruments_Instrument_createInstrument(JNIEnv *env, jobject thiz) { +Java_com_lukas_music_instruments_InternalInstrument_createInstrument(JNIEnv *env, jobject thiz) { uint32_t result = audioHost->instruments->size(); Instrument *instrument = new Instrument(audioHost); audioHost->instruments->push_back(instrument); @@ -34,17 +34,8 @@ extern "C" JNIEXPORT void JNICALL -Java_com_lukas_music_instruments_Instrument_setInstrumentFrequency(JNIEnv *env, jobject thiz, - jint id, jdouble frequency) { - Instrument *instrument = static_cast(listGet(audioHost->instruments->begin(), id)); - instrument->wave->setFrequency(frequency); -} - - -extern "C" -JNIEXPORT void JNICALL -Java_com_lukas_music_instruments_Instrument_setInstrumentActive(JNIEnv *env, jobject thiz, - jint id, jboolean active) { +Java_com_lukas_music_instruments_InternalInstrument_setInstrumentActive(JNIEnv *env, jobject thiz, + jint id, jboolean active) { Instrument *instrument = static_cast(listGet(audioHost->instruments->begin(), id)); instrument->wave->amplitude = active ? 0.3 : 0.0; @@ -52,8 +43,8 @@ } extern "C" JNIEXPORT void JNICALL -Java_com_lukas_music_instruments_Instrument_startNote(JNIEnv *env, jobject thiz, - jint id, jdouble frequency) { +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); @@ -61,9 +52,9 @@ extern "C" JNIEXPORT void JNICALL -Java_com_lukas_music_instruments_Instrument_endNote(JNIEnv *env, jobject thiz, - jint id, jdouble frequency) { +Java_com_lukas_music_instruments_InternalInstrument_endNote(JNIEnv *env, jobject thiz, + jint id) { Instrument *instrument = static_cast(listGet(audioHost->instruments->begin(), id)); - instrument->endNote(frequency); + instrument->endNote(); } \ 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 a73439d..72db3a1 100644 --- a/app/src/main/java/com/lukas/music/instruments/Instrument.kt +++ b/app/src/main/java/com/lukas/music/instruments/Instrument.kt @@ -2,15 +2,9 @@ import com.lukas.music.databinding.FragmentInstrumentBinding -class Instrument(val name: String, var frequency: Double) { - private val id = createInstrument() +abstract class Instrument(private var name: String) { private var active = false - init { - print("id: $id") - setInstrumentFrequency(id, frequency) - } - fun applyToView(binding: FragmentInstrumentBinding) { binding.instrumentNameText.text = name binding.floatingActionButton2.setOnClickListener { @@ -18,31 +12,18 @@ } binding.activeSwitch.setOnCheckedChangeListener { _, newActive -> active = newActive - setInstrumentActive(id, newActive) + changeActive(newActive) } binding.activeSwitch.isChecked = active } - fun startNote(frequency: Double) { - startNote(id, frequency) - } - - fun endNote(frequency: Double) { - endNote(id, frequency) - } - - private external fun createInstrument(): Int - private external fun setInstrumentFrequency(id: Int, frequency: Double) - private external fun setInstrumentActive(id: Int, isActive: Boolean) - private external fun startNote(id: Int, frequency: Double) - private external fun endNote(id: Int, frequency: Double) + abstract fun startNote(frequency: Double) + abstract fun changeActive(newActive: Boolean) companion object { val instruments = mutableListOf( - Instrument("A", 440.0), - Instrument("C#", 440 * 1.25), - Instrument("E", 440 * 1.5), + MonoInstrument("Bass"), ) } } \ No newline at end of file diff --git a/app/src/main/cpp/Envelope.h b/app/src/main/cpp/Envelope.h index aeaaf58..e3573c5 100644 --- a/app/src/main/cpp/Envelope.h +++ b/app/src/main/cpp/Envelope.h @@ -21,11 +21,8 @@ float attack = 0.05, delay = 0.2, sustain = 0.75, release = 0.1; void initialize(AudioHost *host); - void startNote(); - void endNote(); - float *render(uint32_t sampleCount); }; diff --git a/app/src/main/cpp/Instrument.cpp b/app/src/main/cpp/Instrument.cpp index 55d7ecd..6dacc78 100644 --- a/app/src/main/cpp/Instrument.cpp +++ b/app/src/main/cpp/Instrument.cpp @@ -23,6 +23,6 @@ envelope->startNote(); } -void Instrument::endNote(float frequency) { +void Instrument::endNote() { envelope->endNote(); } \ No newline at end of file diff --git a/app/src/main/cpp/Instrument.h b/app/src/main/cpp/Instrument.h index 8fecbd1..8e4b402 100644 --- a/app/src/main/cpp/Instrument.h +++ b/app/src/main/cpp/Instrument.h @@ -18,7 +18,7 @@ void startNote(float frequency); - void endNote(float frequency); + void endNote(); }; #endif diff --git a/app/src/main/cpp/JavaFunctions.cpp b/app/src/main/cpp/JavaFunctions.cpp index 4e70c26..94cd9ab 100644 --- a/app/src/main/cpp/JavaFunctions.cpp +++ b/app/src/main/cpp/JavaFunctions.cpp @@ -25,7 +25,7 @@ extern "C" JNIEXPORT jint JNICALL -Java_com_lukas_music_instruments_Instrument_createInstrument(JNIEnv *env, jobject thiz) { +Java_com_lukas_music_instruments_InternalInstrument_createInstrument(JNIEnv *env, jobject thiz) { uint32_t result = audioHost->instruments->size(); Instrument *instrument = new Instrument(audioHost); audioHost->instruments->push_back(instrument); @@ -34,17 +34,8 @@ extern "C" JNIEXPORT void JNICALL -Java_com_lukas_music_instruments_Instrument_setInstrumentFrequency(JNIEnv *env, jobject thiz, - jint id, jdouble frequency) { - Instrument *instrument = static_cast(listGet(audioHost->instruments->begin(), id)); - instrument->wave->setFrequency(frequency); -} - - -extern "C" -JNIEXPORT void JNICALL -Java_com_lukas_music_instruments_Instrument_setInstrumentActive(JNIEnv *env, jobject thiz, - jint id, jboolean active) { +Java_com_lukas_music_instruments_InternalInstrument_setInstrumentActive(JNIEnv *env, jobject thiz, + jint id, jboolean active) { Instrument *instrument = static_cast(listGet(audioHost->instruments->begin(), id)); instrument->wave->amplitude = active ? 0.3 : 0.0; @@ -52,8 +43,8 @@ } extern "C" JNIEXPORT void JNICALL -Java_com_lukas_music_instruments_Instrument_startNote(JNIEnv *env, jobject thiz, - jint id, jdouble frequency) { +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); @@ -61,9 +52,9 @@ extern "C" JNIEXPORT void JNICALL -Java_com_lukas_music_instruments_Instrument_endNote(JNIEnv *env, jobject thiz, - jint id, jdouble frequency) { +Java_com_lukas_music_instruments_InternalInstrument_endNote(JNIEnv *env, jobject thiz, + jint id) { Instrument *instrument = static_cast(listGet(audioHost->instruments->begin(), id)); - instrument->endNote(frequency); + instrument->endNote(); } \ 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 a73439d..72db3a1 100644 --- a/app/src/main/java/com/lukas/music/instruments/Instrument.kt +++ b/app/src/main/java/com/lukas/music/instruments/Instrument.kt @@ -2,15 +2,9 @@ import com.lukas.music.databinding.FragmentInstrumentBinding -class Instrument(val name: String, var frequency: Double) { - private val id = createInstrument() +abstract class Instrument(private var name: String) { private var active = false - init { - print("id: $id") - setInstrumentFrequency(id, frequency) - } - fun applyToView(binding: FragmentInstrumentBinding) { binding.instrumentNameText.text = name binding.floatingActionButton2.setOnClickListener { @@ -18,31 +12,18 @@ } binding.activeSwitch.setOnCheckedChangeListener { _, newActive -> active = newActive - setInstrumentActive(id, newActive) + changeActive(newActive) } binding.activeSwitch.isChecked = active } - fun startNote(frequency: Double) { - startNote(id, frequency) - } - - fun endNote(frequency: Double) { - endNote(id, frequency) - } - - private external fun createInstrument(): Int - private external fun setInstrumentFrequency(id: Int, frequency: Double) - private external fun setInstrumentActive(id: Int, isActive: Boolean) - private external fun startNote(id: Int, frequency: Double) - private external fun endNote(id: Int, frequency: Double) + abstract fun startNote(frequency: Double) + abstract fun changeActive(newActive: Boolean) companion object { val instruments = mutableListOf( - Instrument("A", 440.0), - Instrument("C#", 440 * 1.25), - Instrument("E", 440 * 1.5), + MonoInstrument("Bass"), ) } } \ No newline at end of file diff --git a/app/src/main/java/com/lukas/music/instruments/InternalInstrument.kt b/app/src/main/java/com/lukas/music/instruments/InternalInstrument.kt new file mode 100644 index 0000000..1a4d91b --- /dev/null +++ b/app/src/main/java/com/lukas/music/instruments/InternalInstrument.kt @@ -0,0 +1,24 @@ +package com.lukas.music.instruments + +class InternalInstrument { + private val id = createInstrument() + var active: Boolean = false + get() = active + set(value) { + field = value + setInstrumentActive(id, value) + } + + fun startNote(frequency: Double) { + startNote(id, frequency) + } + + fun endNote() { + endNote(id) + } + + private external fun createInstrument(): Int + private external fun setInstrumentActive(id: Int, isActive: Boolean) + private external fun startNote(id: Int, frequency: Double) + private external fun endNote(id: Int) +} \ No newline at end of file diff --git a/app/src/main/cpp/Envelope.h b/app/src/main/cpp/Envelope.h index aeaaf58..e3573c5 100644 --- a/app/src/main/cpp/Envelope.h +++ b/app/src/main/cpp/Envelope.h @@ -21,11 +21,8 @@ float attack = 0.05, delay = 0.2, sustain = 0.75, release = 0.1; void initialize(AudioHost *host); - void startNote(); - void endNote(); - float *render(uint32_t sampleCount); }; diff --git a/app/src/main/cpp/Instrument.cpp b/app/src/main/cpp/Instrument.cpp index 55d7ecd..6dacc78 100644 --- a/app/src/main/cpp/Instrument.cpp +++ b/app/src/main/cpp/Instrument.cpp @@ -23,6 +23,6 @@ envelope->startNote(); } -void Instrument::endNote(float frequency) { +void Instrument::endNote() { envelope->endNote(); } \ No newline at end of file diff --git a/app/src/main/cpp/Instrument.h b/app/src/main/cpp/Instrument.h index 8fecbd1..8e4b402 100644 --- a/app/src/main/cpp/Instrument.h +++ b/app/src/main/cpp/Instrument.h @@ -18,7 +18,7 @@ void startNote(float frequency); - void endNote(float frequency); + void endNote(); }; #endif diff --git a/app/src/main/cpp/JavaFunctions.cpp b/app/src/main/cpp/JavaFunctions.cpp index 4e70c26..94cd9ab 100644 --- a/app/src/main/cpp/JavaFunctions.cpp +++ b/app/src/main/cpp/JavaFunctions.cpp @@ -25,7 +25,7 @@ extern "C" JNIEXPORT jint JNICALL -Java_com_lukas_music_instruments_Instrument_createInstrument(JNIEnv *env, jobject thiz) { +Java_com_lukas_music_instruments_InternalInstrument_createInstrument(JNIEnv *env, jobject thiz) { uint32_t result = audioHost->instruments->size(); Instrument *instrument = new Instrument(audioHost); audioHost->instruments->push_back(instrument); @@ -34,17 +34,8 @@ extern "C" JNIEXPORT void JNICALL -Java_com_lukas_music_instruments_Instrument_setInstrumentFrequency(JNIEnv *env, jobject thiz, - jint id, jdouble frequency) { - Instrument *instrument = static_cast(listGet(audioHost->instruments->begin(), id)); - instrument->wave->setFrequency(frequency); -} - - -extern "C" -JNIEXPORT void JNICALL -Java_com_lukas_music_instruments_Instrument_setInstrumentActive(JNIEnv *env, jobject thiz, - jint id, jboolean active) { +Java_com_lukas_music_instruments_InternalInstrument_setInstrumentActive(JNIEnv *env, jobject thiz, + jint id, jboolean active) { Instrument *instrument = static_cast(listGet(audioHost->instruments->begin(), id)); instrument->wave->amplitude = active ? 0.3 : 0.0; @@ -52,8 +43,8 @@ } extern "C" JNIEXPORT void JNICALL -Java_com_lukas_music_instruments_Instrument_startNote(JNIEnv *env, jobject thiz, - jint id, jdouble frequency) { +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); @@ -61,9 +52,9 @@ extern "C" JNIEXPORT void JNICALL -Java_com_lukas_music_instruments_Instrument_endNote(JNIEnv *env, jobject thiz, - jint id, jdouble frequency) { +Java_com_lukas_music_instruments_InternalInstrument_endNote(JNIEnv *env, jobject thiz, + jint id) { Instrument *instrument = static_cast(listGet(audioHost->instruments->begin(), id)); - instrument->endNote(frequency); + instrument->endNote(); } \ 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 a73439d..72db3a1 100644 --- a/app/src/main/java/com/lukas/music/instruments/Instrument.kt +++ b/app/src/main/java/com/lukas/music/instruments/Instrument.kt @@ -2,15 +2,9 @@ import com.lukas.music.databinding.FragmentInstrumentBinding -class Instrument(val name: String, var frequency: Double) { - private val id = createInstrument() +abstract class Instrument(private var name: String) { private var active = false - init { - print("id: $id") - setInstrumentFrequency(id, frequency) - } - fun applyToView(binding: FragmentInstrumentBinding) { binding.instrumentNameText.text = name binding.floatingActionButton2.setOnClickListener { @@ -18,31 +12,18 @@ } binding.activeSwitch.setOnCheckedChangeListener { _, newActive -> active = newActive - setInstrumentActive(id, newActive) + changeActive(newActive) } binding.activeSwitch.isChecked = active } - fun startNote(frequency: Double) { - startNote(id, frequency) - } - - fun endNote(frequency: Double) { - endNote(id, frequency) - } - - private external fun createInstrument(): Int - private external fun setInstrumentFrequency(id: Int, frequency: Double) - private external fun setInstrumentActive(id: Int, isActive: Boolean) - private external fun startNote(id: Int, frequency: Double) - private external fun endNote(id: Int, frequency: Double) + abstract fun startNote(frequency: Double) + abstract fun changeActive(newActive: Boolean) companion object { val instruments = mutableListOf( - Instrument("A", 440.0), - Instrument("C#", 440 * 1.25), - Instrument("E", 440 * 1.5), + MonoInstrument("Bass"), ) } } \ No newline at end of file diff --git a/app/src/main/java/com/lukas/music/instruments/InternalInstrument.kt b/app/src/main/java/com/lukas/music/instruments/InternalInstrument.kt new file mode 100644 index 0000000..1a4d91b --- /dev/null +++ b/app/src/main/java/com/lukas/music/instruments/InternalInstrument.kt @@ -0,0 +1,24 @@ +package com.lukas.music.instruments + +class InternalInstrument { + private val id = createInstrument() + var active: Boolean = false + get() = active + set(value) { + field = value + setInstrumentActive(id, value) + } + + fun startNote(frequency: Double) { + startNote(id, frequency) + } + + fun endNote() { + endNote(id) + } + + private external fun createInstrument(): Int + private external fun setInstrumentActive(id: Int, isActive: Boolean) + private external fun startNote(id: Int, frequency: Double) + private external fun endNote(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 new file mode 100644 index 0000000..d7460d4 --- /dev/null +++ b/app/src/main/java/com/lukas/music/instruments/MonoInstrument.kt @@ -0,0 +1,14 @@ +package com.lukas.music.instruments + +class MonoInstrument(name: String) : Instrument(name) { + private val internalInstrument = InternalInstrument() + + override fun startNote(frequency: Double) { + println("note $frequency") + internalInstrument.startNote(frequency) + } + + override fun changeActive(newActive: Boolean) { + internalInstrument.active = newActive + } +} \ No newline at end of file diff --git a/app/src/main/cpp/Envelope.h b/app/src/main/cpp/Envelope.h index aeaaf58..e3573c5 100644 --- a/app/src/main/cpp/Envelope.h +++ b/app/src/main/cpp/Envelope.h @@ -21,11 +21,8 @@ float attack = 0.05, delay = 0.2, sustain = 0.75, release = 0.1; void initialize(AudioHost *host); - void startNote(); - void endNote(); - float *render(uint32_t sampleCount); }; diff --git a/app/src/main/cpp/Instrument.cpp b/app/src/main/cpp/Instrument.cpp index 55d7ecd..6dacc78 100644 --- a/app/src/main/cpp/Instrument.cpp +++ b/app/src/main/cpp/Instrument.cpp @@ -23,6 +23,6 @@ envelope->startNote(); } -void Instrument::endNote(float frequency) { +void Instrument::endNote() { envelope->endNote(); } \ No newline at end of file diff --git a/app/src/main/cpp/Instrument.h b/app/src/main/cpp/Instrument.h index 8fecbd1..8e4b402 100644 --- a/app/src/main/cpp/Instrument.h +++ b/app/src/main/cpp/Instrument.h @@ -18,7 +18,7 @@ void startNote(float frequency); - void endNote(float frequency); + void endNote(); }; #endif diff --git a/app/src/main/cpp/JavaFunctions.cpp b/app/src/main/cpp/JavaFunctions.cpp index 4e70c26..94cd9ab 100644 --- a/app/src/main/cpp/JavaFunctions.cpp +++ b/app/src/main/cpp/JavaFunctions.cpp @@ -25,7 +25,7 @@ extern "C" JNIEXPORT jint JNICALL -Java_com_lukas_music_instruments_Instrument_createInstrument(JNIEnv *env, jobject thiz) { +Java_com_lukas_music_instruments_InternalInstrument_createInstrument(JNIEnv *env, jobject thiz) { uint32_t result = audioHost->instruments->size(); Instrument *instrument = new Instrument(audioHost); audioHost->instruments->push_back(instrument); @@ -34,17 +34,8 @@ extern "C" JNIEXPORT void JNICALL -Java_com_lukas_music_instruments_Instrument_setInstrumentFrequency(JNIEnv *env, jobject thiz, - jint id, jdouble frequency) { - Instrument *instrument = static_cast(listGet(audioHost->instruments->begin(), id)); - instrument->wave->setFrequency(frequency); -} - - -extern "C" -JNIEXPORT void JNICALL -Java_com_lukas_music_instruments_Instrument_setInstrumentActive(JNIEnv *env, jobject thiz, - jint id, jboolean active) { +Java_com_lukas_music_instruments_InternalInstrument_setInstrumentActive(JNIEnv *env, jobject thiz, + jint id, jboolean active) { Instrument *instrument = static_cast(listGet(audioHost->instruments->begin(), id)); instrument->wave->amplitude = active ? 0.3 : 0.0; @@ -52,8 +43,8 @@ } extern "C" JNIEXPORT void JNICALL -Java_com_lukas_music_instruments_Instrument_startNote(JNIEnv *env, jobject thiz, - jint id, jdouble frequency) { +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); @@ -61,9 +52,9 @@ extern "C" JNIEXPORT void JNICALL -Java_com_lukas_music_instruments_Instrument_endNote(JNIEnv *env, jobject thiz, - jint id, jdouble frequency) { +Java_com_lukas_music_instruments_InternalInstrument_endNote(JNIEnv *env, jobject thiz, + jint id) { Instrument *instrument = static_cast(listGet(audioHost->instruments->begin(), id)); - instrument->endNote(frequency); + instrument->endNote(); } \ 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 a73439d..72db3a1 100644 --- a/app/src/main/java/com/lukas/music/instruments/Instrument.kt +++ b/app/src/main/java/com/lukas/music/instruments/Instrument.kt @@ -2,15 +2,9 @@ import com.lukas.music.databinding.FragmentInstrumentBinding -class Instrument(val name: String, var frequency: Double) { - private val id = createInstrument() +abstract class Instrument(private var name: String) { private var active = false - init { - print("id: $id") - setInstrumentFrequency(id, frequency) - } - fun applyToView(binding: FragmentInstrumentBinding) { binding.instrumentNameText.text = name binding.floatingActionButton2.setOnClickListener { @@ -18,31 +12,18 @@ } binding.activeSwitch.setOnCheckedChangeListener { _, newActive -> active = newActive - setInstrumentActive(id, newActive) + changeActive(newActive) } binding.activeSwitch.isChecked = active } - fun startNote(frequency: Double) { - startNote(id, frequency) - } - - fun endNote(frequency: Double) { - endNote(id, frequency) - } - - private external fun createInstrument(): Int - private external fun setInstrumentFrequency(id: Int, frequency: Double) - private external fun setInstrumentActive(id: Int, isActive: Boolean) - private external fun startNote(id: Int, frequency: Double) - private external fun endNote(id: Int, frequency: Double) + abstract fun startNote(frequency: Double) + abstract fun changeActive(newActive: Boolean) companion object { val instruments = mutableListOf( - Instrument("A", 440.0), - Instrument("C#", 440 * 1.25), - Instrument("E", 440 * 1.5), + MonoInstrument("Bass"), ) } } \ No newline at end of file diff --git a/app/src/main/java/com/lukas/music/instruments/InternalInstrument.kt b/app/src/main/java/com/lukas/music/instruments/InternalInstrument.kt new file mode 100644 index 0000000..1a4d91b --- /dev/null +++ b/app/src/main/java/com/lukas/music/instruments/InternalInstrument.kt @@ -0,0 +1,24 @@ +package com.lukas.music.instruments + +class InternalInstrument { + private val id = createInstrument() + var active: Boolean = false + get() = active + set(value) { + field = value + setInstrumentActive(id, value) + } + + fun startNote(frequency: Double) { + startNote(id, frequency) + } + + fun endNote() { + endNote(id) + } + + private external fun createInstrument(): Int + private external fun setInstrumentActive(id: Int, isActive: Boolean) + private external fun startNote(id: Int, frequency: Double) + private external fun endNote(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 new file mode 100644 index 0000000..d7460d4 --- /dev/null +++ b/app/src/main/java/com/lukas/music/instruments/MonoInstrument.kt @@ -0,0 +1,14 @@ +package com.lukas.music.instruments + +class MonoInstrument(name: String) : Instrument(name) { + private val internalInstrument = InternalInstrument() + + override fun startNote(frequency: Double) { + println("note $frequency") + internalInstrument.startNote(frequency) + } + + override fun changeActive(newActive: Boolean) { + internalInstrument.active = newActive + } +} \ 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 new file mode 100644 index 0000000..54e6f3e --- /dev/null +++ b/app/src/main/java/com/lukas/music/instruments/PolyInstrument.kt @@ -0,0 +1,3 @@ +package com.lukas.music.instruments + +class PolyInstrument \ No newline at end of file diff --git a/app/src/main/cpp/Envelope.h b/app/src/main/cpp/Envelope.h index aeaaf58..e3573c5 100644 --- a/app/src/main/cpp/Envelope.h +++ b/app/src/main/cpp/Envelope.h @@ -21,11 +21,8 @@ float attack = 0.05, delay = 0.2, sustain = 0.75, release = 0.1; void initialize(AudioHost *host); - void startNote(); - void endNote(); - float *render(uint32_t sampleCount); }; diff --git a/app/src/main/cpp/Instrument.cpp b/app/src/main/cpp/Instrument.cpp index 55d7ecd..6dacc78 100644 --- a/app/src/main/cpp/Instrument.cpp +++ b/app/src/main/cpp/Instrument.cpp @@ -23,6 +23,6 @@ envelope->startNote(); } -void Instrument::endNote(float frequency) { +void Instrument::endNote() { envelope->endNote(); } \ No newline at end of file diff --git a/app/src/main/cpp/Instrument.h b/app/src/main/cpp/Instrument.h index 8fecbd1..8e4b402 100644 --- a/app/src/main/cpp/Instrument.h +++ b/app/src/main/cpp/Instrument.h @@ -18,7 +18,7 @@ void startNote(float frequency); - void endNote(float frequency); + void endNote(); }; #endif diff --git a/app/src/main/cpp/JavaFunctions.cpp b/app/src/main/cpp/JavaFunctions.cpp index 4e70c26..94cd9ab 100644 --- a/app/src/main/cpp/JavaFunctions.cpp +++ b/app/src/main/cpp/JavaFunctions.cpp @@ -25,7 +25,7 @@ extern "C" JNIEXPORT jint JNICALL -Java_com_lukas_music_instruments_Instrument_createInstrument(JNIEnv *env, jobject thiz) { +Java_com_lukas_music_instruments_InternalInstrument_createInstrument(JNIEnv *env, jobject thiz) { uint32_t result = audioHost->instruments->size(); Instrument *instrument = new Instrument(audioHost); audioHost->instruments->push_back(instrument); @@ -34,17 +34,8 @@ extern "C" JNIEXPORT void JNICALL -Java_com_lukas_music_instruments_Instrument_setInstrumentFrequency(JNIEnv *env, jobject thiz, - jint id, jdouble frequency) { - Instrument *instrument = static_cast(listGet(audioHost->instruments->begin(), id)); - instrument->wave->setFrequency(frequency); -} - - -extern "C" -JNIEXPORT void JNICALL -Java_com_lukas_music_instruments_Instrument_setInstrumentActive(JNIEnv *env, jobject thiz, - jint id, jboolean active) { +Java_com_lukas_music_instruments_InternalInstrument_setInstrumentActive(JNIEnv *env, jobject thiz, + jint id, jboolean active) { Instrument *instrument = static_cast(listGet(audioHost->instruments->begin(), id)); instrument->wave->amplitude = active ? 0.3 : 0.0; @@ -52,8 +43,8 @@ } extern "C" JNIEXPORT void JNICALL -Java_com_lukas_music_instruments_Instrument_startNote(JNIEnv *env, jobject thiz, - jint id, jdouble frequency) { +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); @@ -61,9 +52,9 @@ extern "C" JNIEXPORT void JNICALL -Java_com_lukas_music_instruments_Instrument_endNote(JNIEnv *env, jobject thiz, - jint id, jdouble frequency) { +Java_com_lukas_music_instruments_InternalInstrument_endNote(JNIEnv *env, jobject thiz, + jint id) { Instrument *instrument = static_cast(listGet(audioHost->instruments->begin(), id)); - instrument->endNote(frequency); + instrument->endNote(); } \ 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 a73439d..72db3a1 100644 --- a/app/src/main/java/com/lukas/music/instruments/Instrument.kt +++ b/app/src/main/java/com/lukas/music/instruments/Instrument.kt @@ -2,15 +2,9 @@ import com.lukas.music.databinding.FragmentInstrumentBinding -class Instrument(val name: String, var frequency: Double) { - private val id = createInstrument() +abstract class Instrument(private var name: String) { private var active = false - init { - print("id: $id") - setInstrumentFrequency(id, frequency) - } - fun applyToView(binding: FragmentInstrumentBinding) { binding.instrumentNameText.text = name binding.floatingActionButton2.setOnClickListener { @@ -18,31 +12,18 @@ } binding.activeSwitch.setOnCheckedChangeListener { _, newActive -> active = newActive - setInstrumentActive(id, newActive) + changeActive(newActive) } binding.activeSwitch.isChecked = active } - fun startNote(frequency: Double) { - startNote(id, frequency) - } - - fun endNote(frequency: Double) { - endNote(id, frequency) - } - - private external fun createInstrument(): Int - private external fun setInstrumentFrequency(id: Int, frequency: Double) - private external fun setInstrumentActive(id: Int, isActive: Boolean) - private external fun startNote(id: Int, frequency: Double) - private external fun endNote(id: Int, frequency: Double) + abstract fun startNote(frequency: Double) + abstract fun changeActive(newActive: Boolean) companion object { val instruments = mutableListOf( - Instrument("A", 440.0), - Instrument("C#", 440 * 1.25), - Instrument("E", 440 * 1.5), + MonoInstrument("Bass"), ) } } \ No newline at end of file diff --git a/app/src/main/java/com/lukas/music/instruments/InternalInstrument.kt b/app/src/main/java/com/lukas/music/instruments/InternalInstrument.kt new file mode 100644 index 0000000..1a4d91b --- /dev/null +++ b/app/src/main/java/com/lukas/music/instruments/InternalInstrument.kt @@ -0,0 +1,24 @@ +package com.lukas.music.instruments + +class InternalInstrument { + private val id = createInstrument() + var active: Boolean = false + get() = active + set(value) { + field = value + setInstrumentActive(id, value) + } + + fun startNote(frequency: Double) { + startNote(id, frequency) + } + + fun endNote() { + endNote(id) + } + + private external fun createInstrument(): Int + private external fun setInstrumentActive(id: Int, isActive: Boolean) + private external fun startNote(id: Int, frequency: Double) + private external fun endNote(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 new file mode 100644 index 0000000..d7460d4 --- /dev/null +++ b/app/src/main/java/com/lukas/music/instruments/MonoInstrument.kt @@ -0,0 +1,14 @@ +package com.lukas.music.instruments + +class MonoInstrument(name: String) : Instrument(name) { + private val internalInstrument = InternalInstrument() + + override fun startNote(frequency: Double) { + println("note $frequency") + internalInstrument.startNote(frequency) + } + + override fun changeActive(newActive: Boolean) { + internalInstrument.active = newActive + } +} \ 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 new file mode 100644 index 0000000..54e6f3e --- /dev/null +++ b/app/src/main/java/com/lukas/music/instruments/PolyInstrument.kt @@ -0,0 +1,3 @@ +package com.lukas.music.instruments + +class PolyInstrument \ No newline at end of file diff --git a/app/src/main/java/com/lukas/music/instruments/Rhythm.kt b/app/src/main/java/com/lukas/music/instruments/Rhythm.kt index 181ad4a..074d518 100644 --- a/app/src/main/java/com/lukas/music/instruments/Rhythm.kt +++ b/app/src/main/java/com/lukas/music/instruments/Rhythm.kt @@ -1,5 +1,6 @@ package com.lukas.music.instruments +import com.lukas.music.song.Song import java.util.* import kotlin.concurrent.schedule @@ -8,13 +9,7 @@ fun start() { Timer().schedule(0, 500) { on = !on - Instrument.instruments.forEach { - if (on) { - it.startNote(it.frequency) - } else { - it.endNote(it.frequency) - } - } + Song.currentSong.step() } } } \ No newline at end of file diff --git a/app/src/main/cpp/Envelope.h b/app/src/main/cpp/Envelope.h index aeaaf58..e3573c5 100644 --- a/app/src/main/cpp/Envelope.h +++ b/app/src/main/cpp/Envelope.h @@ -21,11 +21,8 @@ float attack = 0.05, delay = 0.2, sustain = 0.75, release = 0.1; void initialize(AudioHost *host); - void startNote(); - void endNote(); - float *render(uint32_t sampleCount); }; diff --git a/app/src/main/cpp/Instrument.cpp b/app/src/main/cpp/Instrument.cpp index 55d7ecd..6dacc78 100644 --- a/app/src/main/cpp/Instrument.cpp +++ b/app/src/main/cpp/Instrument.cpp @@ -23,6 +23,6 @@ envelope->startNote(); } -void Instrument::endNote(float frequency) { +void Instrument::endNote() { envelope->endNote(); } \ No newline at end of file diff --git a/app/src/main/cpp/Instrument.h b/app/src/main/cpp/Instrument.h index 8fecbd1..8e4b402 100644 --- a/app/src/main/cpp/Instrument.h +++ b/app/src/main/cpp/Instrument.h @@ -18,7 +18,7 @@ void startNote(float frequency); - void endNote(float frequency); + void endNote(); }; #endif diff --git a/app/src/main/cpp/JavaFunctions.cpp b/app/src/main/cpp/JavaFunctions.cpp index 4e70c26..94cd9ab 100644 --- a/app/src/main/cpp/JavaFunctions.cpp +++ b/app/src/main/cpp/JavaFunctions.cpp @@ -25,7 +25,7 @@ extern "C" JNIEXPORT jint JNICALL -Java_com_lukas_music_instruments_Instrument_createInstrument(JNIEnv *env, jobject thiz) { +Java_com_lukas_music_instruments_InternalInstrument_createInstrument(JNIEnv *env, jobject thiz) { uint32_t result = audioHost->instruments->size(); Instrument *instrument = new Instrument(audioHost); audioHost->instruments->push_back(instrument); @@ -34,17 +34,8 @@ extern "C" JNIEXPORT void JNICALL -Java_com_lukas_music_instruments_Instrument_setInstrumentFrequency(JNIEnv *env, jobject thiz, - jint id, jdouble frequency) { - Instrument *instrument = static_cast(listGet(audioHost->instruments->begin(), id)); - instrument->wave->setFrequency(frequency); -} - - -extern "C" -JNIEXPORT void JNICALL -Java_com_lukas_music_instruments_Instrument_setInstrumentActive(JNIEnv *env, jobject thiz, - jint id, jboolean active) { +Java_com_lukas_music_instruments_InternalInstrument_setInstrumentActive(JNIEnv *env, jobject thiz, + jint id, jboolean active) { Instrument *instrument = static_cast(listGet(audioHost->instruments->begin(), id)); instrument->wave->amplitude = active ? 0.3 : 0.0; @@ -52,8 +43,8 @@ } extern "C" JNIEXPORT void JNICALL -Java_com_lukas_music_instruments_Instrument_startNote(JNIEnv *env, jobject thiz, - jint id, jdouble frequency) { +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); @@ -61,9 +52,9 @@ extern "C" JNIEXPORT void JNICALL -Java_com_lukas_music_instruments_Instrument_endNote(JNIEnv *env, jobject thiz, - jint id, jdouble frequency) { +Java_com_lukas_music_instruments_InternalInstrument_endNote(JNIEnv *env, jobject thiz, + jint id) { Instrument *instrument = static_cast(listGet(audioHost->instruments->begin(), id)); - instrument->endNote(frequency); + instrument->endNote(); } \ 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 a73439d..72db3a1 100644 --- a/app/src/main/java/com/lukas/music/instruments/Instrument.kt +++ b/app/src/main/java/com/lukas/music/instruments/Instrument.kt @@ -2,15 +2,9 @@ import com.lukas.music.databinding.FragmentInstrumentBinding -class Instrument(val name: String, var frequency: Double) { - private val id = createInstrument() +abstract class Instrument(private var name: String) { private var active = false - init { - print("id: $id") - setInstrumentFrequency(id, frequency) - } - fun applyToView(binding: FragmentInstrumentBinding) { binding.instrumentNameText.text = name binding.floatingActionButton2.setOnClickListener { @@ -18,31 +12,18 @@ } binding.activeSwitch.setOnCheckedChangeListener { _, newActive -> active = newActive - setInstrumentActive(id, newActive) + changeActive(newActive) } binding.activeSwitch.isChecked = active } - fun startNote(frequency: Double) { - startNote(id, frequency) - } - - fun endNote(frequency: Double) { - endNote(id, frequency) - } - - private external fun createInstrument(): Int - private external fun setInstrumentFrequency(id: Int, frequency: Double) - private external fun setInstrumentActive(id: Int, isActive: Boolean) - private external fun startNote(id: Int, frequency: Double) - private external fun endNote(id: Int, frequency: Double) + abstract fun startNote(frequency: Double) + abstract fun changeActive(newActive: Boolean) companion object { val instruments = mutableListOf( - Instrument("A", 440.0), - Instrument("C#", 440 * 1.25), - Instrument("E", 440 * 1.5), + MonoInstrument("Bass"), ) } } \ No newline at end of file diff --git a/app/src/main/java/com/lukas/music/instruments/InternalInstrument.kt b/app/src/main/java/com/lukas/music/instruments/InternalInstrument.kt new file mode 100644 index 0000000..1a4d91b --- /dev/null +++ b/app/src/main/java/com/lukas/music/instruments/InternalInstrument.kt @@ -0,0 +1,24 @@ +package com.lukas.music.instruments + +class InternalInstrument { + private val id = createInstrument() + var active: Boolean = false + get() = active + set(value) { + field = value + setInstrumentActive(id, value) + } + + fun startNote(frequency: Double) { + startNote(id, frequency) + } + + fun endNote() { + endNote(id) + } + + private external fun createInstrument(): Int + private external fun setInstrumentActive(id: Int, isActive: Boolean) + private external fun startNote(id: Int, frequency: Double) + private external fun endNote(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 new file mode 100644 index 0000000..d7460d4 --- /dev/null +++ b/app/src/main/java/com/lukas/music/instruments/MonoInstrument.kt @@ -0,0 +1,14 @@ +package com.lukas.music.instruments + +class MonoInstrument(name: String) : Instrument(name) { + private val internalInstrument = InternalInstrument() + + override fun startNote(frequency: Double) { + println("note $frequency") + internalInstrument.startNote(frequency) + } + + override fun changeActive(newActive: Boolean) { + internalInstrument.active = newActive + } +} \ 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 new file mode 100644 index 0000000..54e6f3e --- /dev/null +++ b/app/src/main/java/com/lukas/music/instruments/PolyInstrument.kt @@ -0,0 +1,3 @@ +package com.lukas.music.instruments + +class PolyInstrument \ No newline at end of file diff --git a/app/src/main/java/com/lukas/music/instruments/Rhythm.kt b/app/src/main/java/com/lukas/music/instruments/Rhythm.kt index 181ad4a..074d518 100644 --- a/app/src/main/java/com/lukas/music/instruments/Rhythm.kt +++ b/app/src/main/java/com/lukas/music/instruments/Rhythm.kt @@ -1,5 +1,6 @@ package com.lukas.music.instruments +import com.lukas.music.song.Song import java.util.* import kotlin.concurrent.schedule @@ -8,13 +9,7 @@ fun start() { Timer().schedule(0, 500) { on = !on - Instrument.instruments.forEach { - if (on) { - it.startNote(it.frequency) - } else { - it.endNote(it.frequency) - } - } + Song.currentSong.step() } } } \ No newline at end of file diff --git a/app/src/main/java/com/lukas/music/song/Chord.kt b/app/src/main/java/com/lukas/music/song/Chord.kt new file mode 100644 index 0000000..0b224db --- /dev/null +++ b/app/src/main/java/com/lukas/music/song/Chord.kt @@ -0,0 +1,3 @@ +package com.lukas.music.song + +class Chord(val root: Double) \ No newline at end of file diff --git a/app/src/main/cpp/Envelope.h b/app/src/main/cpp/Envelope.h index aeaaf58..e3573c5 100644 --- a/app/src/main/cpp/Envelope.h +++ b/app/src/main/cpp/Envelope.h @@ -21,11 +21,8 @@ float attack = 0.05, delay = 0.2, sustain = 0.75, release = 0.1; void initialize(AudioHost *host); - void startNote(); - void endNote(); - float *render(uint32_t sampleCount); }; diff --git a/app/src/main/cpp/Instrument.cpp b/app/src/main/cpp/Instrument.cpp index 55d7ecd..6dacc78 100644 --- a/app/src/main/cpp/Instrument.cpp +++ b/app/src/main/cpp/Instrument.cpp @@ -23,6 +23,6 @@ envelope->startNote(); } -void Instrument::endNote(float frequency) { +void Instrument::endNote() { envelope->endNote(); } \ No newline at end of file diff --git a/app/src/main/cpp/Instrument.h b/app/src/main/cpp/Instrument.h index 8fecbd1..8e4b402 100644 --- a/app/src/main/cpp/Instrument.h +++ b/app/src/main/cpp/Instrument.h @@ -18,7 +18,7 @@ void startNote(float frequency); - void endNote(float frequency); + void endNote(); }; #endif diff --git a/app/src/main/cpp/JavaFunctions.cpp b/app/src/main/cpp/JavaFunctions.cpp index 4e70c26..94cd9ab 100644 --- a/app/src/main/cpp/JavaFunctions.cpp +++ b/app/src/main/cpp/JavaFunctions.cpp @@ -25,7 +25,7 @@ extern "C" JNIEXPORT jint JNICALL -Java_com_lukas_music_instruments_Instrument_createInstrument(JNIEnv *env, jobject thiz) { +Java_com_lukas_music_instruments_InternalInstrument_createInstrument(JNIEnv *env, jobject thiz) { uint32_t result = audioHost->instruments->size(); Instrument *instrument = new Instrument(audioHost); audioHost->instruments->push_back(instrument); @@ -34,17 +34,8 @@ extern "C" JNIEXPORT void JNICALL -Java_com_lukas_music_instruments_Instrument_setInstrumentFrequency(JNIEnv *env, jobject thiz, - jint id, jdouble frequency) { - Instrument *instrument = static_cast(listGet(audioHost->instruments->begin(), id)); - instrument->wave->setFrequency(frequency); -} - - -extern "C" -JNIEXPORT void JNICALL -Java_com_lukas_music_instruments_Instrument_setInstrumentActive(JNIEnv *env, jobject thiz, - jint id, jboolean active) { +Java_com_lukas_music_instruments_InternalInstrument_setInstrumentActive(JNIEnv *env, jobject thiz, + jint id, jboolean active) { Instrument *instrument = static_cast(listGet(audioHost->instruments->begin(), id)); instrument->wave->amplitude = active ? 0.3 : 0.0; @@ -52,8 +43,8 @@ } extern "C" JNIEXPORT void JNICALL -Java_com_lukas_music_instruments_Instrument_startNote(JNIEnv *env, jobject thiz, - jint id, jdouble frequency) { +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); @@ -61,9 +52,9 @@ extern "C" JNIEXPORT void JNICALL -Java_com_lukas_music_instruments_Instrument_endNote(JNIEnv *env, jobject thiz, - jint id, jdouble frequency) { +Java_com_lukas_music_instruments_InternalInstrument_endNote(JNIEnv *env, jobject thiz, + jint id) { Instrument *instrument = static_cast(listGet(audioHost->instruments->begin(), id)); - instrument->endNote(frequency); + instrument->endNote(); } \ 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 a73439d..72db3a1 100644 --- a/app/src/main/java/com/lukas/music/instruments/Instrument.kt +++ b/app/src/main/java/com/lukas/music/instruments/Instrument.kt @@ -2,15 +2,9 @@ import com.lukas.music.databinding.FragmentInstrumentBinding -class Instrument(val name: String, var frequency: Double) { - private val id = createInstrument() +abstract class Instrument(private var name: String) { private var active = false - init { - print("id: $id") - setInstrumentFrequency(id, frequency) - } - fun applyToView(binding: FragmentInstrumentBinding) { binding.instrumentNameText.text = name binding.floatingActionButton2.setOnClickListener { @@ -18,31 +12,18 @@ } binding.activeSwitch.setOnCheckedChangeListener { _, newActive -> active = newActive - setInstrumentActive(id, newActive) + changeActive(newActive) } binding.activeSwitch.isChecked = active } - fun startNote(frequency: Double) { - startNote(id, frequency) - } - - fun endNote(frequency: Double) { - endNote(id, frequency) - } - - private external fun createInstrument(): Int - private external fun setInstrumentFrequency(id: Int, frequency: Double) - private external fun setInstrumentActive(id: Int, isActive: Boolean) - private external fun startNote(id: Int, frequency: Double) - private external fun endNote(id: Int, frequency: Double) + abstract fun startNote(frequency: Double) + abstract fun changeActive(newActive: Boolean) companion object { val instruments = mutableListOf( - Instrument("A", 440.0), - Instrument("C#", 440 * 1.25), - Instrument("E", 440 * 1.5), + MonoInstrument("Bass"), ) } } \ No newline at end of file diff --git a/app/src/main/java/com/lukas/music/instruments/InternalInstrument.kt b/app/src/main/java/com/lukas/music/instruments/InternalInstrument.kt new file mode 100644 index 0000000..1a4d91b --- /dev/null +++ b/app/src/main/java/com/lukas/music/instruments/InternalInstrument.kt @@ -0,0 +1,24 @@ +package com.lukas.music.instruments + +class InternalInstrument { + private val id = createInstrument() + var active: Boolean = false + get() = active + set(value) { + field = value + setInstrumentActive(id, value) + } + + fun startNote(frequency: Double) { + startNote(id, frequency) + } + + fun endNote() { + endNote(id) + } + + private external fun createInstrument(): Int + private external fun setInstrumentActive(id: Int, isActive: Boolean) + private external fun startNote(id: Int, frequency: Double) + private external fun endNote(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 new file mode 100644 index 0000000..d7460d4 --- /dev/null +++ b/app/src/main/java/com/lukas/music/instruments/MonoInstrument.kt @@ -0,0 +1,14 @@ +package com.lukas.music.instruments + +class MonoInstrument(name: String) : Instrument(name) { + private val internalInstrument = InternalInstrument() + + override fun startNote(frequency: Double) { + println("note $frequency") + internalInstrument.startNote(frequency) + } + + override fun changeActive(newActive: Boolean) { + internalInstrument.active = newActive + } +} \ 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 new file mode 100644 index 0000000..54e6f3e --- /dev/null +++ b/app/src/main/java/com/lukas/music/instruments/PolyInstrument.kt @@ -0,0 +1,3 @@ +package com.lukas.music.instruments + +class PolyInstrument \ No newline at end of file diff --git a/app/src/main/java/com/lukas/music/instruments/Rhythm.kt b/app/src/main/java/com/lukas/music/instruments/Rhythm.kt index 181ad4a..074d518 100644 --- a/app/src/main/java/com/lukas/music/instruments/Rhythm.kt +++ b/app/src/main/java/com/lukas/music/instruments/Rhythm.kt @@ -1,5 +1,6 @@ package com.lukas.music.instruments +import com.lukas.music.song.Song import java.util.* import kotlin.concurrent.schedule @@ -8,13 +9,7 @@ fun start() { Timer().schedule(0, 500) { on = !on - Instrument.instruments.forEach { - if (on) { - it.startNote(it.frequency) - } else { - it.endNote(it.frequency) - } - } + Song.currentSong.step() } } } \ No newline at end of file diff --git a/app/src/main/java/com/lukas/music/song/Chord.kt b/app/src/main/java/com/lukas/music/song/Chord.kt new file mode 100644 index 0000000..0b224db --- /dev/null +++ b/app/src/main/java/com/lukas/music/song/Chord.kt @@ -0,0 +1,3 @@ +package com.lukas.music.song + +class Chord(val root: Double) \ No newline at end of file diff --git a/app/src/main/java/com/lukas/music/song/ChordProgression.kt b/app/src/main/java/com/lukas/music/song/ChordProgression.kt new file mode 100644 index 0000000..24b5adf --- /dev/null +++ b/app/src/main/java/com/lukas/music/song/ChordProgression.kt @@ -0,0 +1,12 @@ +package com.lukas.music.song + +class ChordProgression(private val chords: List) { + private var index = 0 + + fun step(): Chord { + val result = chords[index] + index++ + index %= chords.size + return result + } +} \ No newline at end of file diff --git a/app/src/main/cpp/Envelope.h b/app/src/main/cpp/Envelope.h index aeaaf58..e3573c5 100644 --- a/app/src/main/cpp/Envelope.h +++ b/app/src/main/cpp/Envelope.h @@ -21,11 +21,8 @@ float attack = 0.05, delay = 0.2, sustain = 0.75, release = 0.1; void initialize(AudioHost *host); - void startNote(); - void endNote(); - float *render(uint32_t sampleCount); }; diff --git a/app/src/main/cpp/Instrument.cpp b/app/src/main/cpp/Instrument.cpp index 55d7ecd..6dacc78 100644 --- a/app/src/main/cpp/Instrument.cpp +++ b/app/src/main/cpp/Instrument.cpp @@ -23,6 +23,6 @@ envelope->startNote(); } -void Instrument::endNote(float frequency) { +void Instrument::endNote() { envelope->endNote(); } \ No newline at end of file diff --git a/app/src/main/cpp/Instrument.h b/app/src/main/cpp/Instrument.h index 8fecbd1..8e4b402 100644 --- a/app/src/main/cpp/Instrument.h +++ b/app/src/main/cpp/Instrument.h @@ -18,7 +18,7 @@ void startNote(float frequency); - void endNote(float frequency); + void endNote(); }; #endif diff --git a/app/src/main/cpp/JavaFunctions.cpp b/app/src/main/cpp/JavaFunctions.cpp index 4e70c26..94cd9ab 100644 --- a/app/src/main/cpp/JavaFunctions.cpp +++ b/app/src/main/cpp/JavaFunctions.cpp @@ -25,7 +25,7 @@ extern "C" JNIEXPORT jint JNICALL -Java_com_lukas_music_instruments_Instrument_createInstrument(JNIEnv *env, jobject thiz) { +Java_com_lukas_music_instruments_InternalInstrument_createInstrument(JNIEnv *env, jobject thiz) { uint32_t result = audioHost->instruments->size(); Instrument *instrument = new Instrument(audioHost); audioHost->instruments->push_back(instrument); @@ -34,17 +34,8 @@ extern "C" JNIEXPORT void JNICALL -Java_com_lukas_music_instruments_Instrument_setInstrumentFrequency(JNIEnv *env, jobject thiz, - jint id, jdouble frequency) { - Instrument *instrument = static_cast(listGet(audioHost->instruments->begin(), id)); - instrument->wave->setFrequency(frequency); -} - - -extern "C" -JNIEXPORT void JNICALL -Java_com_lukas_music_instruments_Instrument_setInstrumentActive(JNIEnv *env, jobject thiz, - jint id, jboolean active) { +Java_com_lukas_music_instruments_InternalInstrument_setInstrumentActive(JNIEnv *env, jobject thiz, + jint id, jboolean active) { Instrument *instrument = static_cast(listGet(audioHost->instruments->begin(), id)); instrument->wave->amplitude = active ? 0.3 : 0.0; @@ -52,8 +43,8 @@ } extern "C" JNIEXPORT void JNICALL -Java_com_lukas_music_instruments_Instrument_startNote(JNIEnv *env, jobject thiz, - jint id, jdouble frequency) { +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); @@ -61,9 +52,9 @@ extern "C" JNIEXPORT void JNICALL -Java_com_lukas_music_instruments_Instrument_endNote(JNIEnv *env, jobject thiz, - jint id, jdouble frequency) { +Java_com_lukas_music_instruments_InternalInstrument_endNote(JNIEnv *env, jobject thiz, + jint id) { Instrument *instrument = static_cast(listGet(audioHost->instruments->begin(), id)); - instrument->endNote(frequency); + instrument->endNote(); } \ 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 a73439d..72db3a1 100644 --- a/app/src/main/java/com/lukas/music/instruments/Instrument.kt +++ b/app/src/main/java/com/lukas/music/instruments/Instrument.kt @@ -2,15 +2,9 @@ import com.lukas.music.databinding.FragmentInstrumentBinding -class Instrument(val name: String, var frequency: Double) { - private val id = createInstrument() +abstract class Instrument(private var name: String) { private var active = false - init { - print("id: $id") - setInstrumentFrequency(id, frequency) - } - fun applyToView(binding: FragmentInstrumentBinding) { binding.instrumentNameText.text = name binding.floatingActionButton2.setOnClickListener { @@ -18,31 +12,18 @@ } binding.activeSwitch.setOnCheckedChangeListener { _, newActive -> active = newActive - setInstrumentActive(id, newActive) + changeActive(newActive) } binding.activeSwitch.isChecked = active } - fun startNote(frequency: Double) { - startNote(id, frequency) - } - - fun endNote(frequency: Double) { - endNote(id, frequency) - } - - private external fun createInstrument(): Int - private external fun setInstrumentFrequency(id: Int, frequency: Double) - private external fun setInstrumentActive(id: Int, isActive: Boolean) - private external fun startNote(id: Int, frequency: Double) - private external fun endNote(id: Int, frequency: Double) + abstract fun startNote(frequency: Double) + abstract fun changeActive(newActive: Boolean) companion object { val instruments = mutableListOf( - Instrument("A", 440.0), - Instrument("C#", 440 * 1.25), - Instrument("E", 440 * 1.5), + MonoInstrument("Bass"), ) } } \ No newline at end of file diff --git a/app/src/main/java/com/lukas/music/instruments/InternalInstrument.kt b/app/src/main/java/com/lukas/music/instruments/InternalInstrument.kt new file mode 100644 index 0000000..1a4d91b --- /dev/null +++ b/app/src/main/java/com/lukas/music/instruments/InternalInstrument.kt @@ -0,0 +1,24 @@ +package com.lukas.music.instruments + +class InternalInstrument { + private val id = createInstrument() + var active: Boolean = false + get() = active + set(value) { + field = value + setInstrumentActive(id, value) + } + + fun startNote(frequency: Double) { + startNote(id, frequency) + } + + fun endNote() { + endNote(id) + } + + private external fun createInstrument(): Int + private external fun setInstrumentActive(id: Int, isActive: Boolean) + private external fun startNote(id: Int, frequency: Double) + private external fun endNote(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 new file mode 100644 index 0000000..d7460d4 --- /dev/null +++ b/app/src/main/java/com/lukas/music/instruments/MonoInstrument.kt @@ -0,0 +1,14 @@ +package com.lukas.music.instruments + +class MonoInstrument(name: String) : Instrument(name) { + private val internalInstrument = InternalInstrument() + + override fun startNote(frequency: Double) { + println("note $frequency") + internalInstrument.startNote(frequency) + } + + override fun changeActive(newActive: Boolean) { + internalInstrument.active = newActive + } +} \ 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 new file mode 100644 index 0000000..54e6f3e --- /dev/null +++ b/app/src/main/java/com/lukas/music/instruments/PolyInstrument.kt @@ -0,0 +1,3 @@ +package com.lukas.music.instruments + +class PolyInstrument \ No newline at end of file diff --git a/app/src/main/java/com/lukas/music/instruments/Rhythm.kt b/app/src/main/java/com/lukas/music/instruments/Rhythm.kt index 181ad4a..074d518 100644 --- a/app/src/main/java/com/lukas/music/instruments/Rhythm.kt +++ b/app/src/main/java/com/lukas/music/instruments/Rhythm.kt @@ -1,5 +1,6 @@ package com.lukas.music.instruments +import com.lukas.music.song.Song import java.util.* import kotlin.concurrent.schedule @@ -8,13 +9,7 @@ fun start() { Timer().schedule(0, 500) { on = !on - Instrument.instruments.forEach { - if (on) { - it.startNote(it.frequency) - } else { - it.endNote(it.frequency) - } - } + Song.currentSong.step() } } } \ No newline at end of file diff --git a/app/src/main/java/com/lukas/music/song/Chord.kt b/app/src/main/java/com/lukas/music/song/Chord.kt new file mode 100644 index 0000000..0b224db --- /dev/null +++ b/app/src/main/java/com/lukas/music/song/Chord.kt @@ -0,0 +1,3 @@ +package com.lukas.music.song + +class Chord(val root: Double) \ No newline at end of file diff --git a/app/src/main/java/com/lukas/music/song/ChordProgression.kt b/app/src/main/java/com/lukas/music/song/ChordProgression.kt new file mode 100644 index 0000000..24b5adf --- /dev/null +++ b/app/src/main/java/com/lukas/music/song/ChordProgression.kt @@ -0,0 +1,12 @@ +package com.lukas.music.song + +class ChordProgression(private val chords: List) { + private var index = 0 + + fun step(): Chord { + val result = chords[index] + index++ + index %= chords.size + return result + } +} \ No newline at end of file diff --git a/app/src/main/java/com/lukas/music/song/Note.kt b/app/src/main/java/com/lukas/music/song/Note.kt new file mode 100644 index 0000000..e81286f --- /dev/null +++ b/app/src/main/java/com/lukas/music/song/Note.kt @@ -0,0 +1,3 @@ +package com.lukas.music.song + +class Note \ No newline at end of file diff --git a/app/src/main/cpp/Envelope.h b/app/src/main/cpp/Envelope.h index aeaaf58..e3573c5 100644 --- a/app/src/main/cpp/Envelope.h +++ b/app/src/main/cpp/Envelope.h @@ -21,11 +21,8 @@ float attack = 0.05, delay = 0.2, sustain = 0.75, release = 0.1; void initialize(AudioHost *host); - void startNote(); - void endNote(); - float *render(uint32_t sampleCount); }; diff --git a/app/src/main/cpp/Instrument.cpp b/app/src/main/cpp/Instrument.cpp index 55d7ecd..6dacc78 100644 --- a/app/src/main/cpp/Instrument.cpp +++ b/app/src/main/cpp/Instrument.cpp @@ -23,6 +23,6 @@ envelope->startNote(); } -void Instrument::endNote(float frequency) { +void Instrument::endNote() { envelope->endNote(); } \ No newline at end of file diff --git a/app/src/main/cpp/Instrument.h b/app/src/main/cpp/Instrument.h index 8fecbd1..8e4b402 100644 --- a/app/src/main/cpp/Instrument.h +++ b/app/src/main/cpp/Instrument.h @@ -18,7 +18,7 @@ void startNote(float frequency); - void endNote(float frequency); + void endNote(); }; #endif diff --git a/app/src/main/cpp/JavaFunctions.cpp b/app/src/main/cpp/JavaFunctions.cpp index 4e70c26..94cd9ab 100644 --- a/app/src/main/cpp/JavaFunctions.cpp +++ b/app/src/main/cpp/JavaFunctions.cpp @@ -25,7 +25,7 @@ extern "C" JNIEXPORT jint JNICALL -Java_com_lukas_music_instruments_Instrument_createInstrument(JNIEnv *env, jobject thiz) { +Java_com_lukas_music_instruments_InternalInstrument_createInstrument(JNIEnv *env, jobject thiz) { uint32_t result = audioHost->instruments->size(); Instrument *instrument = new Instrument(audioHost); audioHost->instruments->push_back(instrument); @@ -34,17 +34,8 @@ extern "C" JNIEXPORT void JNICALL -Java_com_lukas_music_instruments_Instrument_setInstrumentFrequency(JNIEnv *env, jobject thiz, - jint id, jdouble frequency) { - Instrument *instrument = static_cast(listGet(audioHost->instruments->begin(), id)); - instrument->wave->setFrequency(frequency); -} - - -extern "C" -JNIEXPORT void JNICALL -Java_com_lukas_music_instruments_Instrument_setInstrumentActive(JNIEnv *env, jobject thiz, - jint id, jboolean active) { +Java_com_lukas_music_instruments_InternalInstrument_setInstrumentActive(JNIEnv *env, jobject thiz, + jint id, jboolean active) { Instrument *instrument = static_cast(listGet(audioHost->instruments->begin(), id)); instrument->wave->amplitude = active ? 0.3 : 0.0; @@ -52,8 +43,8 @@ } extern "C" JNIEXPORT void JNICALL -Java_com_lukas_music_instruments_Instrument_startNote(JNIEnv *env, jobject thiz, - jint id, jdouble frequency) { +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); @@ -61,9 +52,9 @@ extern "C" JNIEXPORT void JNICALL -Java_com_lukas_music_instruments_Instrument_endNote(JNIEnv *env, jobject thiz, - jint id, jdouble frequency) { +Java_com_lukas_music_instruments_InternalInstrument_endNote(JNIEnv *env, jobject thiz, + jint id) { Instrument *instrument = static_cast(listGet(audioHost->instruments->begin(), id)); - instrument->endNote(frequency); + instrument->endNote(); } \ 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 a73439d..72db3a1 100644 --- a/app/src/main/java/com/lukas/music/instruments/Instrument.kt +++ b/app/src/main/java/com/lukas/music/instruments/Instrument.kt @@ -2,15 +2,9 @@ import com.lukas.music.databinding.FragmentInstrumentBinding -class Instrument(val name: String, var frequency: Double) { - private val id = createInstrument() +abstract class Instrument(private var name: String) { private var active = false - init { - print("id: $id") - setInstrumentFrequency(id, frequency) - } - fun applyToView(binding: FragmentInstrumentBinding) { binding.instrumentNameText.text = name binding.floatingActionButton2.setOnClickListener { @@ -18,31 +12,18 @@ } binding.activeSwitch.setOnCheckedChangeListener { _, newActive -> active = newActive - setInstrumentActive(id, newActive) + changeActive(newActive) } binding.activeSwitch.isChecked = active } - fun startNote(frequency: Double) { - startNote(id, frequency) - } - - fun endNote(frequency: Double) { - endNote(id, frequency) - } - - private external fun createInstrument(): Int - private external fun setInstrumentFrequency(id: Int, frequency: Double) - private external fun setInstrumentActive(id: Int, isActive: Boolean) - private external fun startNote(id: Int, frequency: Double) - private external fun endNote(id: Int, frequency: Double) + abstract fun startNote(frequency: Double) + abstract fun changeActive(newActive: Boolean) companion object { val instruments = mutableListOf( - Instrument("A", 440.0), - Instrument("C#", 440 * 1.25), - Instrument("E", 440 * 1.5), + MonoInstrument("Bass"), ) } } \ No newline at end of file diff --git a/app/src/main/java/com/lukas/music/instruments/InternalInstrument.kt b/app/src/main/java/com/lukas/music/instruments/InternalInstrument.kt new file mode 100644 index 0000000..1a4d91b --- /dev/null +++ b/app/src/main/java/com/lukas/music/instruments/InternalInstrument.kt @@ -0,0 +1,24 @@ +package com.lukas.music.instruments + +class InternalInstrument { + private val id = createInstrument() + var active: Boolean = false + get() = active + set(value) { + field = value + setInstrumentActive(id, value) + } + + fun startNote(frequency: Double) { + startNote(id, frequency) + } + + fun endNote() { + endNote(id) + } + + private external fun createInstrument(): Int + private external fun setInstrumentActive(id: Int, isActive: Boolean) + private external fun startNote(id: Int, frequency: Double) + private external fun endNote(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 new file mode 100644 index 0000000..d7460d4 --- /dev/null +++ b/app/src/main/java/com/lukas/music/instruments/MonoInstrument.kt @@ -0,0 +1,14 @@ +package com.lukas.music.instruments + +class MonoInstrument(name: String) : Instrument(name) { + private val internalInstrument = InternalInstrument() + + override fun startNote(frequency: Double) { + println("note $frequency") + internalInstrument.startNote(frequency) + } + + override fun changeActive(newActive: Boolean) { + internalInstrument.active = newActive + } +} \ 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 new file mode 100644 index 0000000..54e6f3e --- /dev/null +++ b/app/src/main/java/com/lukas/music/instruments/PolyInstrument.kt @@ -0,0 +1,3 @@ +package com.lukas.music.instruments + +class PolyInstrument \ No newline at end of file diff --git a/app/src/main/java/com/lukas/music/instruments/Rhythm.kt b/app/src/main/java/com/lukas/music/instruments/Rhythm.kt index 181ad4a..074d518 100644 --- a/app/src/main/java/com/lukas/music/instruments/Rhythm.kt +++ b/app/src/main/java/com/lukas/music/instruments/Rhythm.kt @@ -1,5 +1,6 @@ package com.lukas.music.instruments +import com.lukas.music.song.Song import java.util.* import kotlin.concurrent.schedule @@ -8,13 +9,7 @@ fun start() { Timer().schedule(0, 500) { on = !on - Instrument.instruments.forEach { - if (on) { - it.startNote(it.frequency) - } else { - it.endNote(it.frequency) - } - } + Song.currentSong.step() } } } \ No newline at end of file diff --git a/app/src/main/java/com/lukas/music/song/Chord.kt b/app/src/main/java/com/lukas/music/song/Chord.kt new file mode 100644 index 0000000..0b224db --- /dev/null +++ b/app/src/main/java/com/lukas/music/song/Chord.kt @@ -0,0 +1,3 @@ +package com.lukas.music.song + +class Chord(val root: Double) \ No newline at end of file diff --git a/app/src/main/java/com/lukas/music/song/ChordProgression.kt b/app/src/main/java/com/lukas/music/song/ChordProgression.kt new file mode 100644 index 0000000..24b5adf --- /dev/null +++ b/app/src/main/java/com/lukas/music/song/ChordProgression.kt @@ -0,0 +1,12 @@ +package com.lukas.music.song + +class ChordProgression(private val chords: List) { + private var index = 0 + + fun step(): Chord { + val result = chords[index] + index++ + index %= chords.size + return result + } +} \ No newline at end of file diff --git a/app/src/main/java/com/lukas/music/song/Note.kt b/app/src/main/java/com/lukas/music/song/Note.kt new file mode 100644 index 0000000..e81286f --- /dev/null +++ b/app/src/main/java/com/lukas/music/song/Note.kt @@ -0,0 +1,3 @@ +package com.lukas.music.song + +class Note \ No newline at end of file diff --git a/app/src/main/java/com/lukas/music/song/Song.kt b/app/src/main/java/com/lukas/music/song/Song.kt new file mode 100644 index 0000000..a7c440a --- /dev/null +++ b/app/src/main/java/com/lukas/music/song/Song.kt @@ -0,0 +1,24 @@ +package com.lukas.music.song + +import com.lukas.music.instruments.Instrument + +class Song( + private val chordProgression: ChordProgression +) { + fun step() { + val chord = chordProgression.step() + for (instrument in Instrument.instruments) { + instrument.startNote(chord.root) + } + } + + companion object { + var currentSong = Song( + ChordProgression( + listOf( + Chord(130.82), Chord(164.82), Chord(174.62) + ) + ) + ) + } +} \ No newline at end of file diff --git a/app/src/main/cpp/Envelope.h b/app/src/main/cpp/Envelope.h index aeaaf58..e3573c5 100644 --- a/app/src/main/cpp/Envelope.h +++ b/app/src/main/cpp/Envelope.h @@ -21,11 +21,8 @@ float attack = 0.05, delay = 0.2, sustain = 0.75, release = 0.1; void initialize(AudioHost *host); - void startNote(); - void endNote(); - float *render(uint32_t sampleCount); }; diff --git a/app/src/main/cpp/Instrument.cpp b/app/src/main/cpp/Instrument.cpp index 55d7ecd..6dacc78 100644 --- a/app/src/main/cpp/Instrument.cpp +++ b/app/src/main/cpp/Instrument.cpp @@ -23,6 +23,6 @@ envelope->startNote(); } -void Instrument::endNote(float frequency) { +void Instrument::endNote() { envelope->endNote(); } \ No newline at end of file diff --git a/app/src/main/cpp/Instrument.h b/app/src/main/cpp/Instrument.h index 8fecbd1..8e4b402 100644 --- a/app/src/main/cpp/Instrument.h +++ b/app/src/main/cpp/Instrument.h @@ -18,7 +18,7 @@ void startNote(float frequency); - void endNote(float frequency); + void endNote(); }; #endif diff --git a/app/src/main/cpp/JavaFunctions.cpp b/app/src/main/cpp/JavaFunctions.cpp index 4e70c26..94cd9ab 100644 --- a/app/src/main/cpp/JavaFunctions.cpp +++ b/app/src/main/cpp/JavaFunctions.cpp @@ -25,7 +25,7 @@ extern "C" JNIEXPORT jint JNICALL -Java_com_lukas_music_instruments_Instrument_createInstrument(JNIEnv *env, jobject thiz) { +Java_com_lukas_music_instruments_InternalInstrument_createInstrument(JNIEnv *env, jobject thiz) { uint32_t result = audioHost->instruments->size(); Instrument *instrument = new Instrument(audioHost); audioHost->instruments->push_back(instrument); @@ -34,17 +34,8 @@ extern "C" JNIEXPORT void JNICALL -Java_com_lukas_music_instruments_Instrument_setInstrumentFrequency(JNIEnv *env, jobject thiz, - jint id, jdouble frequency) { - Instrument *instrument = static_cast(listGet(audioHost->instruments->begin(), id)); - instrument->wave->setFrequency(frequency); -} - - -extern "C" -JNIEXPORT void JNICALL -Java_com_lukas_music_instruments_Instrument_setInstrumentActive(JNIEnv *env, jobject thiz, - jint id, jboolean active) { +Java_com_lukas_music_instruments_InternalInstrument_setInstrumentActive(JNIEnv *env, jobject thiz, + jint id, jboolean active) { Instrument *instrument = static_cast(listGet(audioHost->instruments->begin(), id)); instrument->wave->amplitude = active ? 0.3 : 0.0; @@ -52,8 +43,8 @@ } extern "C" JNIEXPORT void JNICALL -Java_com_lukas_music_instruments_Instrument_startNote(JNIEnv *env, jobject thiz, - jint id, jdouble frequency) { +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); @@ -61,9 +52,9 @@ extern "C" JNIEXPORT void JNICALL -Java_com_lukas_music_instruments_Instrument_endNote(JNIEnv *env, jobject thiz, - jint id, jdouble frequency) { +Java_com_lukas_music_instruments_InternalInstrument_endNote(JNIEnv *env, jobject thiz, + jint id) { Instrument *instrument = static_cast(listGet(audioHost->instruments->begin(), id)); - instrument->endNote(frequency); + instrument->endNote(); } \ 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 a73439d..72db3a1 100644 --- a/app/src/main/java/com/lukas/music/instruments/Instrument.kt +++ b/app/src/main/java/com/lukas/music/instruments/Instrument.kt @@ -2,15 +2,9 @@ import com.lukas.music.databinding.FragmentInstrumentBinding -class Instrument(val name: String, var frequency: Double) { - private val id = createInstrument() +abstract class Instrument(private var name: String) { private var active = false - init { - print("id: $id") - setInstrumentFrequency(id, frequency) - } - fun applyToView(binding: FragmentInstrumentBinding) { binding.instrumentNameText.text = name binding.floatingActionButton2.setOnClickListener { @@ -18,31 +12,18 @@ } binding.activeSwitch.setOnCheckedChangeListener { _, newActive -> active = newActive - setInstrumentActive(id, newActive) + changeActive(newActive) } binding.activeSwitch.isChecked = active } - fun startNote(frequency: Double) { - startNote(id, frequency) - } - - fun endNote(frequency: Double) { - endNote(id, frequency) - } - - private external fun createInstrument(): Int - private external fun setInstrumentFrequency(id: Int, frequency: Double) - private external fun setInstrumentActive(id: Int, isActive: Boolean) - private external fun startNote(id: Int, frequency: Double) - private external fun endNote(id: Int, frequency: Double) + abstract fun startNote(frequency: Double) + abstract fun changeActive(newActive: Boolean) companion object { val instruments = mutableListOf( - Instrument("A", 440.0), - Instrument("C#", 440 * 1.25), - Instrument("E", 440 * 1.5), + MonoInstrument("Bass"), ) } } \ No newline at end of file diff --git a/app/src/main/java/com/lukas/music/instruments/InternalInstrument.kt b/app/src/main/java/com/lukas/music/instruments/InternalInstrument.kt new file mode 100644 index 0000000..1a4d91b --- /dev/null +++ b/app/src/main/java/com/lukas/music/instruments/InternalInstrument.kt @@ -0,0 +1,24 @@ +package com.lukas.music.instruments + +class InternalInstrument { + private val id = createInstrument() + var active: Boolean = false + get() = active + set(value) { + field = value + setInstrumentActive(id, value) + } + + fun startNote(frequency: Double) { + startNote(id, frequency) + } + + fun endNote() { + endNote(id) + } + + private external fun createInstrument(): Int + private external fun setInstrumentActive(id: Int, isActive: Boolean) + private external fun startNote(id: Int, frequency: Double) + private external fun endNote(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 new file mode 100644 index 0000000..d7460d4 --- /dev/null +++ b/app/src/main/java/com/lukas/music/instruments/MonoInstrument.kt @@ -0,0 +1,14 @@ +package com.lukas.music.instruments + +class MonoInstrument(name: String) : Instrument(name) { + private val internalInstrument = InternalInstrument() + + override fun startNote(frequency: Double) { + println("note $frequency") + internalInstrument.startNote(frequency) + } + + override fun changeActive(newActive: Boolean) { + internalInstrument.active = newActive + } +} \ 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 new file mode 100644 index 0000000..54e6f3e --- /dev/null +++ b/app/src/main/java/com/lukas/music/instruments/PolyInstrument.kt @@ -0,0 +1,3 @@ +package com.lukas.music.instruments + +class PolyInstrument \ No newline at end of file diff --git a/app/src/main/java/com/lukas/music/instruments/Rhythm.kt b/app/src/main/java/com/lukas/music/instruments/Rhythm.kt index 181ad4a..074d518 100644 --- a/app/src/main/java/com/lukas/music/instruments/Rhythm.kt +++ b/app/src/main/java/com/lukas/music/instruments/Rhythm.kt @@ -1,5 +1,6 @@ package com.lukas.music.instruments +import com.lukas.music.song.Song import java.util.* import kotlin.concurrent.schedule @@ -8,13 +9,7 @@ fun start() { Timer().schedule(0, 500) { on = !on - Instrument.instruments.forEach { - if (on) { - it.startNote(it.frequency) - } else { - it.endNote(it.frequency) - } - } + Song.currentSong.step() } } } \ No newline at end of file diff --git a/app/src/main/java/com/lukas/music/song/Chord.kt b/app/src/main/java/com/lukas/music/song/Chord.kt new file mode 100644 index 0000000..0b224db --- /dev/null +++ b/app/src/main/java/com/lukas/music/song/Chord.kt @@ -0,0 +1,3 @@ +package com.lukas.music.song + +class Chord(val root: Double) \ No newline at end of file diff --git a/app/src/main/java/com/lukas/music/song/ChordProgression.kt b/app/src/main/java/com/lukas/music/song/ChordProgression.kt new file mode 100644 index 0000000..24b5adf --- /dev/null +++ b/app/src/main/java/com/lukas/music/song/ChordProgression.kt @@ -0,0 +1,12 @@ +package com.lukas.music.song + +class ChordProgression(private val chords: List) { + private var index = 0 + + fun step(): Chord { + val result = chords[index] + index++ + index %= chords.size + return result + } +} \ No newline at end of file diff --git a/app/src/main/java/com/lukas/music/song/Note.kt b/app/src/main/java/com/lukas/music/song/Note.kt new file mode 100644 index 0000000..e81286f --- /dev/null +++ b/app/src/main/java/com/lukas/music/song/Note.kt @@ -0,0 +1,3 @@ +package com.lukas.music.song + +class Note \ No newline at end of file diff --git a/app/src/main/java/com/lukas/music/song/Song.kt b/app/src/main/java/com/lukas/music/song/Song.kt new file mode 100644 index 0000000..a7c440a --- /dev/null +++ b/app/src/main/java/com/lukas/music/song/Song.kt @@ -0,0 +1,24 @@ +package com.lukas.music.song + +import com.lukas.music.instruments.Instrument + +class Song( + private val chordProgression: ChordProgression +) { + fun step() { + val chord = chordProgression.step() + for (instrument in Instrument.instruments) { + instrument.startNote(chord.root) + } + } + + companion object { + var currentSong = Song( + ChordProgression( + listOf( + Chord(130.82), Chord(164.82), Chord(174.62) + ) + ) + ) + } +} \ No newline at end of file diff --git a/app/src/main/java/com/lukas/music/ui/InstrumentAdapter.kt b/app/src/main/java/com/lukas/music/ui/InstrumentAdapter.kt index f772787..79cc4ce 100644 --- a/app/src/main/java/com/lukas/music/ui/InstrumentAdapter.kt +++ b/app/src/main/java/com/lukas/music/ui/InstrumentAdapter.kt @@ -19,8 +19,8 @@ } override fun onBindViewHolder(holder: InstrumentViewHolder, position: Int) { - val instrument = Instrument.instruments[position] - instrument.applyToView(holder.binding) + val internalInstrument = Instrument.instruments[position] + internalInstrument.applyToView(holder.binding) } override fun getItemCount(): Int { diff --git a/app/src/main/cpp/Envelope.h b/app/src/main/cpp/Envelope.h index aeaaf58..e3573c5 100644 --- a/app/src/main/cpp/Envelope.h +++ b/app/src/main/cpp/Envelope.h @@ -21,11 +21,8 @@ float attack = 0.05, delay = 0.2, sustain = 0.75, release = 0.1; void initialize(AudioHost *host); - void startNote(); - void endNote(); - float *render(uint32_t sampleCount); }; diff --git a/app/src/main/cpp/Instrument.cpp b/app/src/main/cpp/Instrument.cpp index 55d7ecd..6dacc78 100644 --- a/app/src/main/cpp/Instrument.cpp +++ b/app/src/main/cpp/Instrument.cpp @@ -23,6 +23,6 @@ envelope->startNote(); } -void Instrument::endNote(float frequency) { +void Instrument::endNote() { envelope->endNote(); } \ No newline at end of file diff --git a/app/src/main/cpp/Instrument.h b/app/src/main/cpp/Instrument.h index 8fecbd1..8e4b402 100644 --- a/app/src/main/cpp/Instrument.h +++ b/app/src/main/cpp/Instrument.h @@ -18,7 +18,7 @@ void startNote(float frequency); - void endNote(float frequency); + void endNote(); }; #endif diff --git a/app/src/main/cpp/JavaFunctions.cpp b/app/src/main/cpp/JavaFunctions.cpp index 4e70c26..94cd9ab 100644 --- a/app/src/main/cpp/JavaFunctions.cpp +++ b/app/src/main/cpp/JavaFunctions.cpp @@ -25,7 +25,7 @@ extern "C" JNIEXPORT jint JNICALL -Java_com_lukas_music_instruments_Instrument_createInstrument(JNIEnv *env, jobject thiz) { +Java_com_lukas_music_instruments_InternalInstrument_createInstrument(JNIEnv *env, jobject thiz) { uint32_t result = audioHost->instruments->size(); Instrument *instrument = new Instrument(audioHost); audioHost->instruments->push_back(instrument); @@ -34,17 +34,8 @@ extern "C" JNIEXPORT void JNICALL -Java_com_lukas_music_instruments_Instrument_setInstrumentFrequency(JNIEnv *env, jobject thiz, - jint id, jdouble frequency) { - Instrument *instrument = static_cast(listGet(audioHost->instruments->begin(), id)); - instrument->wave->setFrequency(frequency); -} - - -extern "C" -JNIEXPORT void JNICALL -Java_com_lukas_music_instruments_Instrument_setInstrumentActive(JNIEnv *env, jobject thiz, - jint id, jboolean active) { +Java_com_lukas_music_instruments_InternalInstrument_setInstrumentActive(JNIEnv *env, jobject thiz, + jint id, jboolean active) { Instrument *instrument = static_cast(listGet(audioHost->instruments->begin(), id)); instrument->wave->amplitude = active ? 0.3 : 0.0; @@ -52,8 +43,8 @@ } extern "C" JNIEXPORT void JNICALL -Java_com_lukas_music_instruments_Instrument_startNote(JNIEnv *env, jobject thiz, - jint id, jdouble frequency) { +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); @@ -61,9 +52,9 @@ extern "C" JNIEXPORT void JNICALL -Java_com_lukas_music_instruments_Instrument_endNote(JNIEnv *env, jobject thiz, - jint id, jdouble frequency) { +Java_com_lukas_music_instruments_InternalInstrument_endNote(JNIEnv *env, jobject thiz, + jint id) { Instrument *instrument = static_cast(listGet(audioHost->instruments->begin(), id)); - instrument->endNote(frequency); + instrument->endNote(); } \ 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 a73439d..72db3a1 100644 --- a/app/src/main/java/com/lukas/music/instruments/Instrument.kt +++ b/app/src/main/java/com/lukas/music/instruments/Instrument.kt @@ -2,15 +2,9 @@ import com.lukas.music.databinding.FragmentInstrumentBinding -class Instrument(val name: String, var frequency: Double) { - private val id = createInstrument() +abstract class Instrument(private var name: String) { private var active = false - init { - print("id: $id") - setInstrumentFrequency(id, frequency) - } - fun applyToView(binding: FragmentInstrumentBinding) { binding.instrumentNameText.text = name binding.floatingActionButton2.setOnClickListener { @@ -18,31 +12,18 @@ } binding.activeSwitch.setOnCheckedChangeListener { _, newActive -> active = newActive - setInstrumentActive(id, newActive) + changeActive(newActive) } binding.activeSwitch.isChecked = active } - fun startNote(frequency: Double) { - startNote(id, frequency) - } - - fun endNote(frequency: Double) { - endNote(id, frequency) - } - - private external fun createInstrument(): Int - private external fun setInstrumentFrequency(id: Int, frequency: Double) - private external fun setInstrumentActive(id: Int, isActive: Boolean) - private external fun startNote(id: Int, frequency: Double) - private external fun endNote(id: Int, frequency: Double) + abstract fun startNote(frequency: Double) + abstract fun changeActive(newActive: Boolean) companion object { val instruments = mutableListOf( - Instrument("A", 440.0), - Instrument("C#", 440 * 1.25), - Instrument("E", 440 * 1.5), + MonoInstrument("Bass"), ) } } \ No newline at end of file diff --git a/app/src/main/java/com/lukas/music/instruments/InternalInstrument.kt b/app/src/main/java/com/lukas/music/instruments/InternalInstrument.kt new file mode 100644 index 0000000..1a4d91b --- /dev/null +++ b/app/src/main/java/com/lukas/music/instruments/InternalInstrument.kt @@ -0,0 +1,24 @@ +package com.lukas.music.instruments + +class InternalInstrument { + private val id = createInstrument() + var active: Boolean = false + get() = active + set(value) { + field = value + setInstrumentActive(id, value) + } + + fun startNote(frequency: Double) { + startNote(id, frequency) + } + + fun endNote() { + endNote(id) + } + + private external fun createInstrument(): Int + private external fun setInstrumentActive(id: Int, isActive: Boolean) + private external fun startNote(id: Int, frequency: Double) + private external fun endNote(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 new file mode 100644 index 0000000..d7460d4 --- /dev/null +++ b/app/src/main/java/com/lukas/music/instruments/MonoInstrument.kt @@ -0,0 +1,14 @@ +package com.lukas.music.instruments + +class MonoInstrument(name: String) : Instrument(name) { + private val internalInstrument = InternalInstrument() + + override fun startNote(frequency: Double) { + println("note $frequency") + internalInstrument.startNote(frequency) + } + + override fun changeActive(newActive: Boolean) { + internalInstrument.active = newActive + } +} \ 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 new file mode 100644 index 0000000..54e6f3e --- /dev/null +++ b/app/src/main/java/com/lukas/music/instruments/PolyInstrument.kt @@ -0,0 +1,3 @@ +package com.lukas.music.instruments + +class PolyInstrument \ No newline at end of file diff --git a/app/src/main/java/com/lukas/music/instruments/Rhythm.kt b/app/src/main/java/com/lukas/music/instruments/Rhythm.kt index 181ad4a..074d518 100644 --- a/app/src/main/java/com/lukas/music/instruments/Rhythm.kt +++ b/app/src/main/java/com/lukas/music/instruments/Rhythm.kt @@ -1,5 +1,6 @@ package com.lukas.music.instruments +import com.lukas.music.song.Song import java.util.* import kotlin.concurrent.schedule @@ -8,13 +9,7 @@ fun start() { Timer().schedule(0, 500) { on = !on - Instrument.instruments.forEach { - if (on) { - it.startNote(it.frequency) - } else { - it.endNote(it.frequency) - } - } + Song.currentSong.step() } } } \ No newline at end of file diff --git a/app/src/main/java/com/lukas/music/song/Chord.kt b/app/src/main/java/com/lukas/music/song/Chord.kt new file mode 100644 index 0000000..0b224db --- /dev/null +++ b/app/src/main/java/com/lukas/music/song/Chord.kt @@ -0,0 +1,3 @@ +package com.lukas.music.song + +class Chord(val root: Double) \ No newline at end of file diff --git a/app/src/main/java/com/lukas/music/song/ChordProgression.kt b/app/src/main/java/com/lukas/music/song/ChordProgression.kt new file mode 100644 index 0000000..24b5adf --- /dev/null +++ b/app/src/main/java/com/lukas/music/song/ChordProgression.kt @@ -0,0 +1,12 @@ +package com.lukas.music.song + +class ChordProgression(private val chords: List) { + private var index = 0 + + fun step(): Chord { + val result = chords[index] + index++ + index %= chords.size + return result + } +} \ No newline at end of file diff --git a/app/src/main/java/com/lukas/music/song/Note.kt b/app/src/main/java/com/lukas/music/song/Note.kt new file mode 100644 index 0000000..e81286f --- /dev/null +++ b/app/src/main/java/com/lukas/music/song/Note.kt @@ -0,0 +1,3 @@ +package com.lukas.music.song + +class Note \ No newline at end of file diff --git a/app/src/main/java/com/lukas/music/song/Song.kt b/app/src/main/java/com/lukas/music/song/Song.kt new file mode 100644 index 0000000..a7c440a --- /dev/null +++ b/app/src/main/java/com/lukas/music/song/Song.kt @@ -0,0 +1,24 @@ +package com.lukas.music.song + +import com.lukas.music.instruments.Instrument + +class Song( + private val chordProgression: ChordProgression +) { + fun step() { + val chord = chordProgression.step() + for (instrument in Instrument.instruments) { + instrument.startNote(chord.root) + } + } + + companion object { + var currentSong = Song( + ChordProgression( + listOf( + Chord(130.82), Chord(164.82), Chord(174.62) + ) + ) + ) + } +} \ No newline at end of file diff --git a/app/src/main/java/com/lukas/music/ui/InstrumentAdapter.kt b/app/src/main/java/com/lukas/music/ui/InstrumentAdapter.kt index f772787..79cc4ce 100644 --- a/app/src/main/java/com/lukas/music/ui/InstrumentAdapter.kt +++ b/app/src/main/java/com/lukas/music/ui/InstrumentAdapter.kt @@ -19,8 +19,8 @@ } override fun onBindViewHolder(holder: InstrumentViewHolder, position: Int) { - val instrument = Instrument.instruments[position] - instrument.applyToView(holder.binding) + val internalInstrument = Instrument.instruments[position] + internalInstrument.applyToView(holder.binding) } override fun getItemCount(): Int { diff --git a/app/src/main/java/com/lukas/music/ui/fragments/InstrumentFragment.kt b/app/src/main/java/com/lukas/music/ui/fragments/InstrumentFragment.kt index d79b8e2..9065093 100644 --- a/app/src/main/java/com/lukas/music/ui/fragments/InstrumentFragment.kt +++ b/app/src/main/java/com/lukas/music/ui/fragments/InstrumentFragment.kt @@ -7,9 +7,8 @@ import androidx.fragment.app.Fragment import com.lukas.music.R import com.lukas.music.databinding.FragmentInstrumentBinding -import com.lukas.music.instruments.Instrument -class InstrumentFragment() : Fragment(R.layout.fragment_instrument) { +class InstrumentFragment : Fragment(R.layout.fragment_instrument) { private lateinit var binding: FragmentInstrumentBinding override fun onCreateView(