diff --git a/app/src/main/cpp/CMakeLists.txt b/app/src/main/cpp/CMakeLists.txt index 8588bed..39bdefe 100644 --- a/app/src/main/cpp/CMakeLists.txt +++ b/app/src/main/cpp/CMakeLists.txt @@ -19,6 +19,7 @@ effects/Processable.cpp effects/Effect.cpp effects/LowPass.cpp + effects/Noise.cpp ) find_library( diff --git a/app/src/main/cpp/CMakeLists.txt b/app/src/main/cpp/CMakeLists.txt index 8588bed..39bdefe 100644 --- a/app/src/main/cpp/CMakeLists.txt +++ b/app/src/main/cpp/CMakeLists.txt @@ -19,6 +19,7 @@ effects/Processable.cpp effects/Effect.cpp effects/LowPass.cpp + effects/Noise.cpp ) find_library( diff --git a/app/src/main/cpp/Instrument.cpp b/app/src/main/cpp/Instrument.cpp index 9a9db2b..7310a37 100644 --- a/app/src/main/cpp/Instrument.cpp +++ b/app/src/main/cpp/Instrument.cpp @@ -10,6 +10,7 @@ wave->host = host; envelope->initialize(host); lowPass->host = host; + noise->host = host; } void multiply(float *target, float *modulation, uint32_t size) { @@ -44,7 +45,9 @@ void Instrument::render(float *buffer, uint32_t count) { float *waveform = wave->render(count); processEffect(waveform, count, lowPass); + processEffect(waveform, count, noise); multiply(waveform, envelope->render(count), count); + multiply(waveform, volume, count); add(buffer, waveform, count); } diff --git a/app/src/main/cpp/CMakeLists.txt b/app/src/main/cpp/CMakeLists.txt index 8588bed..39bdefe 100644 --- a/app/src/main/cpp/CMakeLists.txt +++ b/app/src/main/cpp/CMakeLists.txt @@ -19,6 +19,7 @@ effects/Processable.cpp effects/Effect.cpp effects/LowPass.cpp + effects/Noise.cpp ) find_library( diff --git a/app/src/main/cpp/Instrument.cpp b/app/src/main/cpp/Instrument.cpp index 9a9db2b..7310a37 100644 --- a/app/src/main/cpp/Instrument.cpp +++ b/app/src/main/cpp/Instrument.cpp @@ -10,6 +10,7 @@ wave->host = host; envelope->initialize(host); lowPass->host = host; + noise->host = host; } void multiply(float *target, float *modulation, uint32_t size) { @@ -44,7 +45,9 @@ void Instrument::render(float *buffer, uint32_t count) { float *waveform = wave->render(count); processEffect(waveform, count, lowPass); + processEffect(waveform, count, noise); multiply(waveform, envelope->render(count), count); + multiply(waveform, volume, count); add(buffer, waveform, count); } diff --git a/app/src/main/cpp/Instrument.h b/app/src/main/cpp/Instrument.h index 3ab90a8..077bfe0 100644 --- a/app/src/main/cpp/Instrument.h +++ b/app/src/main/cpp/Instrument.h @@ -7,6 +7,7 @@ #include "waveforms/Waveform.h" #include "AudioHost.h" #include "effects/LowPass.h" +#include "effects/Noise.h" class Instrument { private: @@ -17,10 +18,15 @@ Envelope *const envelope = new Envelope(); Waveform *wave; LowPass *lowPass = new LowPass(); + Noise *noise = new Noise(); + float volume = 0; void render(float *buffer, uint32_t count); + void startNote(float frequency); + void endNote(); + void setWaveform(WaveformType waveform); }; diff --git a/app/src/main/cpp/CMakeLists.txt b/app/src/main/cpp/CMakeLists.txt index 8588bed..39bdefe 100644 --- a/app/src/main/cpp/CMakeLists.txt +++ b/app/src/main/cpp/CMakeLists.txt @@ -19,6 +19,7 @@ effects/Processable.cpp effects/Effect.cpp effects/LowPass.cpp + effects/Noise.cpp ) find_library( diff --git a/app/src/main/cpp/Instrument.cpp b/app/src/main/cpp/Instrument.cpp index 9a9db2b..7310a37 100644 --- a/app/src/main/cpp/Instrument.cpp +++ b/app/src/main/cpp/Instrument.cpp @@ -10,6 +10,7 @@ wave->host = host; envelope->initialize(host); lowPass->host = host; + noise->host = host; } void multiply(float *target, float *modulation, uint32_t size) { @@ -44,7 +45,9 @@ void Instrument::render(float *buffer, uint32_t count) { float *waveform = wave->render(count); processEffect(waveform, count, lowPass); + processEffect(waveform, count, noise); multiply(waveform, envelope->render(count), count); + multiply(waveform, volume, count); add(buffer, waveform, count); } diff --git a/app/src/main/cpp/Instrument.h b/app/src/main/cpp/Instrument.h index 3ab90a8..077bfe0 100644 --- a/app/src/main/cpp/Instrument.h +++ b/app/src/main/cpp/Instrument.h @@ -7,6 +7,7 @@ #include "waveforms/Waveform.h" #include "AudioHost.h" #include "effects/LowPass.h" +#include "effects/Noise.h" class Instrument { private: @@ -17,10 +18,15 @@ Envelope *const envelope = new Envelope(); Waveform *wave; LowPass *lowPass = new LowPass(); + Noise *noise = new Noise(); + float volume = 0; void render(float *buffer, uint32_t count); + void startNote(float frequency); + void endNote(); + void setWaveform(WaveformType waveform); }; diff --git a/app/src/main/cpp/JavaFunctions.cpp b/app/src/main/cpp/JavaFunctions.cpp index b39abb1..7ccc60b 100644 --- a/app/src/main/cpp/JavaFunctions.cpp +++ b/app/src/main/cpp/JavaFunctions.cpp @@ -69,7 +69,7 @@ JNIEXPORT void JNICALL Java_com_lukas_music_instruments_InternalInstrument_setVolume(JNIEnv *env, jobject thiz, jint id, jfloat volume) { - getInstrument(id)->wave->amplitude = volume; + getInstrument(id)->volume = volume; } JNIEXPORT void JNICALL @@ -106,6 +106,9 @@ case 0: effect = instrument->lowPass; break; + case 1: + effect = instrument->noise; + break; } effect->influence = influence; effect->parameter1 = parameter1; diff --git a/app/src/main/cpp/CMakeLists.txt b/app/src/main/cpp/CMakeLists.txt index 8588bed..39bdefe 100644 --- a/app/src/main/cpp/CMakeLists.txt +++ b/app/src/main/cpp/CMakeLists.txt @@ -19,6 +19,7 @@ effects/Processable.cpp effects/Effect.cpp effects/LowPass.cpp + effects/Noise.cpp ) find_library( diff --git a/app/src/main/cpp/Instrument.cpp b/app/src/main/cpp/Instrument.cpp index 9a9db2b..7310a37 100644 --- a/app/src/main/cpp/Instrument.cpp +++ b/app/src/main/cpp/Instrument.cpp @@ -10,6 +10,7 @@ wave->host = host; envelope->initialize(host); lowPass->host = host; + noise->host = host; } void multiply(float *target, float *modulation, uint32_t size) { @@ -44,7 +45,9 @@ void Instrument::render(float *buffer, uint32_t count) { float *waveform = wave->render(count); processEffect(waveform, count, lowPass); + processEffect(waveform, count, noise); multiply(waveform, envelope->render(count), count); + multiply(waveform, volume, count); add(buffer, waveform, count); } diff --git a/app/src/main/cpp/Instrument.h b/app/src/main/cpp/Instrument.h index 3ab90a8..077bfe0 100644 --- a/app/src/main/cpp/Instrument.h +++ b/app/src/main/cpp/Instrument.h @@ -7,6 +7,7 @@ #include "waveforms/Waveform.h" #include "AudioHost.h" #include "effects/LowPass.h" +#include "effects/Noise.h" class Instrument { private: @@ -17,10 +18,15 @@ Envelope *const envelope = new Envelope(); Waveform *wave; LowPass *lowPass = new LowPass(); + Noise *noise = new Noise(); + float volume = 0; void render(float *buffer, uint32_t count); + void startNote(float frequency); + void endNote(); + void setWaveform(WaveformType waveform); }; diff --git a/app/src/main/cpp/JavaFunctions.cpp b/app/src/main/cpp/JavaFunctions.cpp index b39abb1..7ccc60b 100644 --- a/app/src/main/cpp/JavaFunctions.cpp +++ b/app/src/main/cpp/JavaFunctions.cpp @@ -69,7 +69,7 @@ JNIEXPORT void JNICALL Java_com_lukas_music_instruments_InternalInstrument_setVolume(JNIEnv *env, jobject thiz, jint id, jfloat volume) { - getInstrument(id)->wave->amplitude = volume; + getInstrument(id)->volume = volume; } JNIEXPORT void JNICALL @@ -106,6 +106,9 @@ case 0: effect = instrument->lowPass; break; + case 1: + effect = instrument->noise; + break; } effect->influence = influence; effect->parameter1 = parameter1; diff --git a/app/src/main/cpp/effects/Noise.cpp b/app/src/main/cpp/effects/Noise.cpp new file mode 100644 index 0000000..b858a97 --- /dev/null +++ b/app/src/main/cpp/effects/Noise.cpp @@ -0,0 +1,19 @@ +#include "Noise.h" +#include +#include + +void Noise::update() { + srand(time(0)); +} + +const static int q = 15; +const static float c1 = (1 << q) - 1; +const static float c2 = ((int) (c1 / 3)) + 1; +const static float c3 = 1.f / c1; + +void Noise::doRender(uint32_t sampleCount) { + for (uint32_t i = 0; i < sampleCount; i++) { + float random = ((float) rand() / (float) (RAND_MAX + 1)); + buffer[i] = (2.f * ((random * c2) + (random * c2) + (random * c2)) - 3.f * (c2 - 1.f)) * c3; + } +} \ No newline at end of file diff --git a/app/src/main/cpp/CMakeLists.txt b/app/src/main/cpp/CMakeLists.txt index 8588bed..39bdefe 100644 --- a/app/src/main/cpp/CMakeLists.txt +++ b/app/src/main/cpp/CMakeLists.txt @@ -19,6 +19,7 @@ effects/Processable.cpp effects/Effect.cpp effects/LowPass.cpp + effects/Noise.cpp ) find_library( diff --git a/app/src/main/cpp/Instrument.cpp b/app/src/main/cpp/Instrument.cpp index 9a9db2b..7310a37 100644 --- a/app/src/main/cpp/Instrument.cpp +++ b/app/src/main/cpp/Instrument.cpp @@ -10,6 +10,7 @@ wave->host = host; envelope->initialize(host); lowPass->host = host; + noise->host = host; } void multiply(float *target, float *modulation, uint32_t size) { @@ -44,7 +45,9 @@ void Instrument::render(float *buffer, uint32_t count) { float *waveform = wave->render(count); processEffect(waveform, count, lowPass); + processEffect(waveform, count, noise); multiply(waveform, envelope->render(count), count); + multiply(waveform, volume, count); add(buffer, waveform, count); } diff --git a/app/src/main/cpp/Instrument.h b/app/src/main/cpp/Instrument.h index 3ab90a8..077bfe0 100644 --- a/app/src/main/cpp/Instrument.h +++ b/app/src/main/cpp/Instrument.h @@ -7,6 +7,7 @@ #include "waveforms/Waveform.h" #include "AudioHost.h" #include "effects/LowPass.h" +#include "effects/Noise.h" class Instrument { private: @@ -17,10 +18,15 @@ Envelope *const envelope = new Envelope(); Waveform *wave; LowPass *lowPass = new LowPass(); + Noise *noise = new Noise(); + float volume = 0; void render(float *buffer, uint32_t count); + void startNote(float frequency); + void endNote(); + void setWaveform(WaveformType waveform); }; diff --git a/app/src/main/cpp/JavaFunctions.cpp b/app/src/main/cpp/JavaFunctions.cpp index b39abb1..7ccc60b 100644 --- a/app/src/main/cpp/JavaFunctions.cpp +++ b/app/src/main/cpp/JavaFunctions.cpp @@ -69,7 +69,7 @@ JNIEXPORT void JNICALL Java_com_lukas_music_instruments_InternalInstrument_setVolume(JNIEnv *env, jobject thiz, jint id, jfloat volume) { - getInstrument(id)->wave->amplitude = volume; + getInstrument(id)->volume = volume; } JNIEXPORT void JNICALL @@ -106,6 +106,9 @@ case 0: effect = instrument->lowPass; break; + case 1: + effect = instrument->noise; + break; } effect->influence = influence; effect->parameter1 = parameter1; diff --git a/app/src/main/cpp/effects/Noise.cpp b/app/src/main/cpp/effects/Noise.cpp new file mode 100644 index 0000000..b858a97 --- /dev/null +++ b/app/src/main/cpp/effects/Noise.cpp @@ -0,0 +1,19 @@ +#include "Noise.h" +#include +#include + +void Noise::update() { + srand(time(0)); +} + +const static int q = 15; +const static float c1 = (1 << q) - 1; +const static float c2 = ((int) (c1 / 3)) + 1; +const static float c3 = 1.f / c1; + +void Noise::doRender(uint32_t sampleCount) { + for (uint32_t i = 0; i < sampleCount; i++) { + float random = ((float) rand() / (float) (RAND_MAX + 1)); + buffer[i] = (2.f * ((random * c2) + (random * c2) + (random * c2)) - 3.f * (c2 - 1.f)) * c3; + } +} \ No newline at end of file diff --git a/app/src/main/cpp/effects/Noise.h b/app/src/main/cpp/effects/Noise.h new file mode 100644 index 0000000..287e4ac --- /dev/null +++ b/app/src/main/cpp/effects/Noise.h @@ -0,0 +1,13 @@ +#ifndef MUSIC_NOISE_H +#define MUSIC_NOISE_H + +#include "Effect.h" + +class Noise : public Effect { + void update(); + + void doRender(uint32_t sampleCount); +}; + + +#endif diff --git a/app/src/main/cpp/CMakeLists.txt b/app/src/main/cpp/CMakeLists.txt index 8588bed..39bdefe 100644 --- a/app/src/main/cpp/CMakeLists.txt +++ b/app/src/main/cpp/CMakeLists.txt @@ -19,6 +19,7 @@ effects/Processable.cpp effects/Effect.cpp effects/LowPass.cpp + effects/Noise.cpp ) find_library( diff --git a/app/src/main/cpp/Instrument.cpp b/app/src/main/cpp/Instrument.cpp index 9a9db2b..7310a37 100644 --- a/app/src/main/cpp/Instrument.cpp +++ b/app/src/main/cpp/Instrument.cpp @@ -10,6 +10,7 @@ wave->host = host; envelope->initialize(host); lowPass->host = host; + noise->host = host; } void multiply(float *target, float *modulation, uint32_t size) { @@ -44,7 +45,9 @@ void Instrument::render(float *buffer, uint32_t count) { float *waveform = wave->render(count); processEffect(waveform, count, lowPass); + processEffect(waveform, count, noise); multiply(waveform, envelope->render(count), count); + multiply(waveform, volume, count); add(buffer, waveform, count); } diff --git a/app/src/main/cpp/Instrument.h b/app/src/main/cpp/Instrument.h index 3ab90a8..077bfe0 100644 --- a/app/src/main/cpp/Instrument.h +++ b/app/src/main/cpp/Instrument.h @@ -7,6 +7,7 @@ #include "waveforms/Waveform.h" #include "AudioHost.h" #include "effects/LowPass.h" +#include "effects/Noise.h" class Instrument { private: @@ -17,10 +18,15 @@ Envelope *const envelope = new Envelope(); Waveform *wave; LowPass *lowPass = new LowPass(); + Noise *noise = new Noise(); + float volume = 0; void render(float *buffer, uint32_t count); + void startNote(float frequency); + void endNote(); + void setWaveform(WaveformType waveform); }; diff --git a/app/src/main/cpp/JavaFunctions.cpp b/app/src/main/cpp/JavaFunctions.cpp index b39abb1..7ccc60b 100644 --- a/app/src/main/cpp/JavaFunctions.cpp +++ b/app/src/main/cpp/JavaFunctions.cpp @@ -69,7 +69,7 @@ JNIEXPORT void JNICALL Java_com_lukas_music_instruments_InternalInstrument_setVolume(JNIEnv *env, jobject thiz, jint id, jfloat volume) { - getInstrument(id)->wave->amplitude = volume; + getInstrument(id)->volume = volume; } JNIEXPORT void JNICALL @@ -106,6 +106,9 @@ case 0: effect = instrument->lowPass; break; + case 1: + effect = instrument->noise; + break; } effect->influence = influence; effect->parameter1 = parameter1; diff --git a/app/src/main/cpp/effects/Noise.cpp b/app/src/main/cpp/effects/Noise.cpp new file mode 100644 index 0000000..b858a97 --- /dev/null +++ b/app/src/main/cpp/effects/Noise.cpp @@ -0,0 +1,19 @@ +#include "Noise.h" +#include +#include + +void Noise::update() { + srand(time(0)); +} + +const static int q = 15; +const static float c1 = (1 << q) - 1; +const static float c2 = ((int) (c1 / 3)) + 1; +const static float c3 = 1.f / c1; + +void Noise::doRender(uint32_t sampleCount) { + for (uint32_t i = 0; i < sampleCount; i++) { + float random = ((float) rand() / (float) (RAND_MAX + 1)); + buffer[i] = (2.f * ((random * c2) + (random * c2) + (random * c2)) - 3.f * (c2 - 1.f)) * c3; + } +} \ No newline at end of file diff --git a/app/src/main/cpp/effects/Noise.h b/app/src/main/cpp/effects/Noise.h new file mode 100644 index 0000000..287e4ac --- /dev/null +++ b/app/src/main/cpp/effects/Noise.h @@ -0,0 +1,13 @@ +#ifndef MUSIC_NOISE_H +#define MUSIC_NOISE_H + +#include "Effect.h" + +class Noise : public Effect { + void update(); + + void doRender(uint32_t sampleCount); +}; + + +#endif diff --git a/app/src/main/cpp/waveforms/Waveform.cpp b/app/src/main/cpp/waveforms/Waveform.cpp index 64e0991..839ca10 100644 --- a/app/src/main/cpp/waveforms/Waveform.cpp +++ b/app/src/main/cpp/waveforms/Waveform.cpp @@ -2,7 +2,4 @@ void Waveform::doRender(uint32_t sampleCount) { renderWaveform(sampleCount); - for (uint32_t i = 0; i < sampleCount; i++) { - buffer[i] *= amplitude; - } } \ No newline at end of file diff --git a/app/src/main/cpp/CMakeLists.txt b/app/src/main/cpp/CMakeLists.txt index 8588bed..39bdefe 100644 --- a/app/src/main/cpp/CMakeLists.txt +++ b/app/src/main/cpp/CMakeLists.txt @@ -19,6 +19,7 @@ effects/Processable.cpp effects/Effect.cpp effects/LowPass.cpp + effects/Noise.cpp ) find_library( diff --git a/app/src/main/cpp/Instrument.cpp b/app/src/main/cpp/Instrument.cpp index 9a9db2b..7310a37 100644 --- a/app/src/main/cpp/Instrument.cpp +++ b/app/src/main/cpp/Instrument.cpp @@ -10,6 +10,7 @@ wave->host = host; envelope->initialize(host); lowPass->host = host; + noise->host = host; } void multiply(float *target, float *modulation, uint32_t size) { @@ -44,7 +45,9 @@ void Instrument::render(float *buffer, uint32_t count) { float *waveform = wave->render(count); processEffect(waveform, count, lowPass); + processEffect(waveform, count, noise); multiply(waveform, envelope->render(count), count); + multiply(waveform, volume, count); add(buffer, waveform, count); } diff --git a/app/src/main/cpp/Instrument.h b/app/src/main/cpp/Instrument.h index 3ab90a8..077bfe0 100644 --- a/app/src/main/cpp/Instrument.h +++ b/app/src/main/cpp/Instrument.h @@ -7,6 +7,7 @@ #include "waveforms/Waveform.h" #include "AudioHost.h" #include "effects/LowPass.h" +#include "effects/Noise.h" class Instrument { private: @@ -17,10 +18,15 @@ Envelope *const envelope = new Envelope(); Waveform *wave; LowPass *lowPass = new LowPass(); + Noise *noise = new Noise(); + float volume = 0; void render(float *buffer, uint32_t count); + void startNote(float frequency); + void endNote(); + void setWaveform(WaveformType waveform); }; diff --git a/app/src/main/cpp/JavaFunctions.cpp b/app/src/main/cpp/JavaFunctions.cpp index b39abb1..7ccc60b 100644 --- a/app/src/main/cpp/JavaFunctions.cpp +++ b/app/src/main/cpp/JavaFunctions.cpp @@ -69,7 +69,7 @@ JNIEXPORT void JNICALL Java_com_lukas_music_instruments_InternalInstrument_setVolume(JNIEnv *env, jobject thiz, jint id, jfloat volume) { - getInstrument(id)->wave->amplitude = volume; + getInstrument(id)->volume = volume; } JNIEXPORT void JNICALL @@ -106,6 +106,9 @@ case 0: effect = instrument->lowPass; break; + case 1: + effect = instrument->noise; + break; } effect->influence = influence; effect->parameter1 = parameter1; diff --git a/app/src/main/cpp/effects/Noise.cpp b/app/src/main/cpp/effects/Noise.cpp new file mode 100644 index 0000000..b858a97 --- /dev/null +++ b/app/src/main/cpp/effects/Noise.cpp @@ -0,0 +1,19 @@ +#include "Noise.h" +#include +#include + +void Noise::update() { + srand(time(0)); +} + +const static int q = 15; +const static float c1 = (1 << q) - 1; +const static float c2 = ((int) (c1 / 3)) + 1; +const static float c3 = 1.f / c1; + +void Noise::doRender(uint32_t sampleCount) { + for (uint32_t i = 0; i < sampleCount; i++) { + float random = ((float) rand() / (float) (RAND_MAX + 1)); + buffer[i] = (2.f * ((random * c2) + (random * c2) + (random * c2)) - 3.f * (c2 - 1.f)) * c3; + } +} \ No newline at end of file diff --git a/app/src/main/cpp/effects/Noise.h b/app/src/main/cpp/effects/Noise.h new file mode 100644 index 0000000..287e4ac --- /dev/null +++ b/app/src/main/cpp/effects/Noise.h @@ -0,0 +1,13 @@ +#ifndef MUSIC_NOISE_H +#define MUSIC_NOISE_H + +#include "Effect.h" + +class Noise : public Effect { + void update(); + + void doRender(uint32_t sampleCount); +}; + + +#endif diff --git a/app/src/main/cpp/waveforms/Waveform.cpp b/app/src/main/cpp/waveforms/Waveform.cpp index 64e0991..839ca10 100644 --- a/app/src/main/cpp/waveforms/Waveform.cpp +++ b/app/src/main/cpp/waveforms/Waveform.cpp @@ -2,7 +2,4 @@ void Waveform::doRender(uint32_t sampleCount) { renderWaveform(sampleCount); - for (uint32_t i = 0; i < sampleCount; i++) { - buffer[i] *= amplitude; - } } \ No newline at end of file diff --git a/app/src/main/cpp/waveforms/Waveform.h b/app/src/main/cpp/waveforms/Waveform.h index c082cda..34e1284 100644 --- a/app/src/main/cpp/waveforms/Waveform.h +++ b/app/src/main/cpp/waveforms/Waveform.h @@ -15,7 +15,6 @@ class Waveform : public Processable { public: - float amplitude = 0.0f; AudioHost *host; void doRender(uint32_t sampleCount); diff --git a/app/src/main/cpp/CMakeLists.txt b/app/src/main/cpp/CMakeLists.txt index 8588bed..39bdefe 100644 --- a/app/src/main/cpp/CMakeLists.txt +++ b/app/src/main/cpp/CMakeLists.txt @@ -19,6 +19,7 @@ effects/Processable.cpp effects/Effect.cpp effects/LowPass.cpp + effects/Noise.cpp ) find_library( diff --git a/app/src/main/cpp/Instrument.cpp b/app/src/main/cpp/Instrument.cpp index 9a9db2b..7310a37 100644 --- a/app/src/main/cpp/Instrument.cpp +++ b/app/src/main/cpp/Instrument.cpp @@ -10,6 +10,7 @@ wave->host = host; envelope->initialize(host); lowPass->host = host; + noise->host = host; } void multiply(float *target, float *modulation, uint32_t size) { @@ -44,7 +45,9 @@ void Instrument::render(float *buffer, uint32_t count) { float *waveform = wave->render(count); processEffect(waveform, count, lowPass); + processEffect(waveform, count, noise); multiply(waveform, envelope->render(count), count); + multiply(waveform, volume, count); add(buffer, waveform, count); } diff --git a/app/src/main/cpp/Instrument.h b/app/src/main/cpp/Instrument.h index 3ab90a8..077bfe0 100644 --- a/app/src/main/cpp/Instrument.h +++ b/app/src/main/cpp/Instrument.h @@ -7,6 +7,7 @@ #include "waveforms/Waveform.h" #include "AudioHost.h" #include "effects/LowPass.h" +#include "effects/Noise.h" class Instrument { private: @@ -17,10 +18,15 @@ Envelope *const envelope = new Envelope(); Waveform *wave; LowPass *lowPass = new LowPass(); + Noise *noise = new Noise(); + float volume = 0; void render(float *buffer, uint32_t count); + void startNote(float frequency); + void endNote(); + void setWaveform(WaveformType waveform); }; diff --git a/app/src/main/cpp/JavaFunctions.cpp b/app/src/main/cpp/JavaFunctions.cpp index b39abb1..7ccc60b 100644 --- a/app/src/main/cpp/JavaFunctions.cpp +++ b/app/src/main/cpp/JavaFunctions.cpp @@ -69,7 +69,7 @@ JNIEXPORT void JNICALL Java_com_lukas_music_instruments_InternalInstrument_setVolume(JNIEnv *env, jobject thiz, jint id, jfloat volume) { - getInstrument(id)->wave->amplitude = volume; + getInstrument(id)->volume = volume; } JNIEXPORT void JNICALL @@ -106,6 +106,9 @@ case 0: effect = instrument->lowPass; break; + case 1: + effect = instrument->noise; + break; } effect->influence = influence; effect->parameter1 = parameter1; diff --git a/app/src/main/cpp/effects/Noise.cpp b/app/src/main/cpp/effects/Noise.cpp new file mode 100644 index 0000000..b858a97 --- /dev/null +++ b/app/src/main/cpp/effects/Noise.cpp @@ -0,0 +1,19 @@ +#include "Noise.h" +#include +#include + +void Noise::update() { + srand(time(0)); +} + +const static int q = 15; +const static float c1 = (1 << q) - 1; +const static float c2 = ((int) (c1 / 3)) + 1; +const static float c3 = 1.f / c1; + +void Noise::doRender(uint32_t sampleCount) { + for (uint32_t i = 0; i < sampleCount; i++) { + float random = ((float) rand() / (float) (RAND_MAX + 1)); + buffer[i] = (2.f * ((random * c2) + (random * c2) + (random * c2)) - 3.f * (c2 - 1.f)) * c3; + } +} \ No newline at end of file diff --git a/app/src/main/cpp/effects/Noise.h b/app/src/main/cpp/effects/Noise.h new file mode 100644 index 0000000..287e4ac --- /dev/null +++ b/app/src/main/cpp/effects/Noise.h @@ -0,0 +1,13 @@ +#ifndef MUSIC_NOISE_H +#define MUSIC_NOISE_H + +#include "Effect.h" + +class Noise : public Effect { + void update(); + + void doRender(uint32_t sampleCount); +}; + + +#endif diff --git a/app/src/main/cpp/waveforms/Waveform.cpp b/app/src/main/cpp/waveforms/Waveform.cpp index 64e0991..839ca10 100644 --- a/app/src/main/cpp/waveforms/Waveform.cpp +++ b/app/src/main/cpp/waveforms/Waveform.cpp @@ -2,7 +2,4 @@ void Waveform::doRender(uint32_t sampleCount) { renderWaveform(sampleCount); - for (uint32_t i = 0; i < sampleCount; i++) { - buffer[i] *= amplitude; - } } \ No newline at end of file diff --git a/app/src/main/cpp/waveforms/Waveform.h b/app/src/main/cpp/waveforms/Waveform.h index c082cda..34e1284 100644 --- a/app/src/main/cpp/waveforms/Waveform.h +++ b/app/src/main/cpp/waveforms/Waveform.h @@ -15,7 +15,6 @@ class Waveform : public Processable { public: - float amplitude = 0.0f; AudioHost *host; void doRender(uint32_t sampleCount); diff --git a/app/src/main/java/com/lukas/music/instruments/effect/EffectType.kt b/app/src/main/java/com/lukas/music/instruments/effect/EffectType.kt index 027a6c2..8af39ea 100644 --- a/app/src/main/java/com/lukas/music/instruments/effect/EffectType.kt +++ b/app/src/main/java/com/lukas/music/instruments/effect/EffectType.kt @@ -22,6 +22,13 @@ "cutoff: ${it.value.format(1)} octaves" } )), + Noise("noise", + arrayOf( + EffectParameterDescription(0f, 1f, 0f) { + "unused" + } + ) + ) ; override fun toString(): String {