diff --git a/app/src/main/cpp/CMakeLists.txt b/app/src/main/cpp/CMakeLists.txt index e7286cb..b94b511 100644 --- a/app/src/main/cpp/CMakeLists.txt +++ b/app/src/main/cpp/CMakeLists.txt @@ -10,6 +10,8 @@ waveforms/Sine.cpp waveforms/Sawtooth.cpp waveforms/Waveform.cpp + waveforms/Square.cpp + waveforms/Triangle.cpp JavaFunctions.cpp AudioHost.cpp Instrument.cpp diff --git a/app/src/main/cpp/CMakeLists.txt b/app/src/main/cpp/CMakeLists.txt index e7286cb..b94b511 100644 --- a/app/src/main/cpp/CMakeLists.txt +++ b/app/src/main/cpp/CMakeLists.txt @@ -10,6 +10,8 @@ waveforms/Sine.cpp waveforms/Sawtooth.cpp waveforms/Waveform.cpp + waveforms/Square.cpp + waveforms/Triangle.cpp JavaFunctions.cpp AudioHost.cpp Instrument.cpp diff --git a/app/src/main/cpp/Instrument.cpp b/app/src/main/cpp/Instrument.cpp index 3d9396d..d7263ed 100644 --- a/app/src/main/cpp/Instrument.cpp +++ b/app/src/main/cpp/Instrument.cpp @@ -1,6 +1,8 @@ #include "Instrument.h" #include "waveforms/Sawtooth.h" #include "waveforms/Sine.h" +#include "waveforms/Square.h" +#include "waveforms/Triangle.h" Instrument::Instrument(AudioHost *host) { this->host = host; @@ -46,6 +48,11 @@ case SAWTOOTH: wave = new Sawtooth(); break; + case SQUARE: + wave = new Square(); + break; + case TRIANGLE: + wave = new Triangle(); } wave->host = host; delete old; diff --git a/app/src/main/cpp/CMakeLists.txt b/app/src/main/cpp/CMakeLists.txt index e7286cb..b94b511 100644 --- a/app/src/main/cpp/CMakeLists.txt +++ b/app/src/main/cpp/CMakeLists.txt @@ -10,6 +10,8 @@ waveforms/Sine.cpp waveforms/Sawtooth.cpp waveforms/Waveform.cpp + waveforms/Square.cpp + waveforms/Triangle.cpp JavaFunctions.cpp AudioHost.cpp Instrument.cpp diff --git a/app/src/main/cpp/Instrument.cpp b/app/src/main/cpp/Instrument.cpp index 3d9396d..d7263ed 100644 --- a/app/src/main/cpp/Instrument.cpp +++ b/app/src/main/cpp/Instrument.cpp @@ -1,6 +1,8 @@ #include "Instrument.h" #include "waveforms/Sawtooth.h" #include "waveforms/Sine.h" +#include "waveforms/Square.h" +#include "waveforms/Triangle.h" Instrument::Instrument(AudioHost *host) { this->host = host; @@ -46,6 +48,11 @@ case SAWTOOTH: wave = new Sawtooth(); break; + case SQUARE: + wave = new Square(); + break; + case TRIANGLE: + wave = new Triangle(); } wave->host = host; delete old; diff --git a/app/src/main/cpp/waveforms/Square.cpp b/app/src/main/cpp/waveforms/Square.cpp new file mode 100644 index 0000000..d136bca --- /dev/null +++ b/app/src/main/cpp/waveforms/Square.cpp @@ -0,0 +1,17 @@ +#include +#include "Square.h" + +void Square::setFrequency(float frequency) { + period = 1 / frequency; + step = 1 / (double) host->sampleRate; +} + +void Square::renderWaveform(uint32_t frameCount) { + for (uint32_t i = 0; i < frameCount; i++) { + buffer[i] = position > period / 2 ? 1 : -1; + position += step; + if (position > period) { + position = 0; + } + } +} \ No newline at end of file diff --git a/app/src/main/cpp/CMakeLists.txt b/app/src/main/cpp/CMakeLists.txt index e7286cb..b94b511 100644 --- a/app/src/main/cpp/CMakeLists.txt +++ b/app/src/main/cpp/CMakeLists.txt @@ -10,6 +10,8 @@ waveforms/Sine.cpp waveforms/Sawtooth.cpp waveforms/Waveform.cpp + waveforms/Square.cpp + waveforms/Triangle.cpp JavaFunctions.cpp AudioHost.cpp Instrument.cpp diff --git a/app/src/main/cpp/Instrument.cpp b/app/src/main/cpp/Instrument.cpp index 3d9396d..d7263ed 100644 --- a/app/src/main/cpp/Instrument.cpp +++ b/app/src/main/cpp/Instrument.cpp @@ -1,6 +1,8 @@ #include "Instrument.h" #include "waveforms/Sawtooth.h" #include "waveforms/Sine.h" +#include "waveforms/Square.h" +#include "waveforms/Triangle.h" Instrument::Instrument(AudioHost *host) { this->host = host; @@ -46,6 +48,11 @@ case SAWTOOTH: wave = new Sawtooth(); break; + case SQUARE: + wave = new Square(); + break; + case TRIANGLE: + wave = new Triangle(); } wave->host = host; delete old; diff --git a/app/src/main/cpp/waveforms/Square.cpp b/app/src/main/cpp/waveforms/Square.cpp new file mode 100644 index 0000000..d136bca --- /dev/null +++ b/app/src/main/cpp/waveforms/Square.cpp @@ -0,0 +1,17 @@ +#include +#include "Square.h" + +void Square::setFrequency(float frequency) { + period = 1 / frequency; + step = 1 / (double) host->sampleRate; +} + +void Square::renderWaveform(uint32_t frameCount) { + for (uint32_t i = 0; i < frameCount; i++) { + buffer[i] = position > period / 2 ? 1 : -1; + position += step; + if (position > period) { + position = 0; + } + } +} \ No newline at end of file diff --git a/app/src/main/cpp/waveforms/Square.h b/app/src/main/cpp/waveforms/Square.h new file mode 100644 index 0000000..abcc39a --- /dev/null +++ b/app/src/main/cpp/waveforms/Square.h @@ -0,0 +1,15 @@ +#ifndef MUSIC_SQUARE_H +#define MUSIC_SQUARE_H + +#include "Waveform.h" + +class Square : public Waveform { +private: + float position, period, step; +public: + void renderWaveform(uint32_t frameCount); + + void setFrequency(float freq); +}; + +#endif diff --git a/app/src/main/cpp/CMakeLists.txt b/app/src/main/cpp/CMakeLists.txt index e7286cb..b94b511 100644 --- a/app/src/main/cpp/CMakeLists.txt +++ b/app/src/main/cpp/CMakeLists.txt @@ -10,6 +10,8 @@ waveforms/Sine.cpp waveforms/Sawtooth.cpp waveforms/Waveform.cpp + waveforms/Square.cpp + waveforms/Triangle.cpp JavaFunctions.cpp AudioHost.cpp Instrument.cpp diff --git a/app/src/main/cpp/Instrument.cpp b/app/src/main/cpp/Instrument.cpp index 3d9396d..d7263ed 100644 --- a/app/src/main/cpp/Instrument.cpp +++ b/app/src/main/cpp/Instrument.cpp @@ -1,6 +1,8 @@ #include "Instrument.h" #include "waveforms/Sawtooth.h" #include "waveforms/Sine.h" +#include "waveforms/Square.h" +#include "waveforms/Triangle.h" Instrument::Instrument(AudioHost *host) { this->host = host; @@ -46,6 +48,11 @@ case SAWTOOTH: wave = new Sawtooth(); break; + case SQUARE: + wave = new Square(); + break; + case TRIANGLE: + wave = new Triangle(); } wave->host = host; delete old; diff --git a/app/src/main/cpp/waveforms/Square.cpp b/app/src/main/cpp/waveforms/Square.cpp new file mode 100644 index 0000000..d136bca --- /dev/null +++ b/app/src/main/cpp/waveforms/Square.cpp @@ -0,0 +1,17 @@ +#include +#include "Square.h" + +void Square::setFrequency(float frequency) { + period = 1 / frequency; + step = 1 / (double) host->sampleRate; +} + +void Square::renderWaveform(uint32_t frameCount) { + for (uint32_t i = 0; i < frameCount; i++) { + buffer[i] = position > period / 2 ? 1 : -1; + position += step; + if (position > period) { + position = 0; + } + } +} \ No newline at end of file diff --git a/app/src/main/cpp/waveforms/Square.h b/app/src/main/cpp/waveforms/Square.h new file mode 100644 index 0000000..abcc39a --- /dev/null +++ b/app/src/main/cpp/waveforms/Square.h @@ -0,0 +1,15 @@ +#ifndef MUSIC_SQUARE_H +#define MUSIC_SQUARE_H + +#include "Waveform.h" + +class Square : public Waveform { +private: + float position, period, step; +public: + void renderWaveform(uint32_t frameCount); + + void setFrequency(float freq); +}; + +#endif diff --git a/app/src/main/cpp/waveforms/Triangle.cpp b/app/src/main/cpp/waveforms/Triangle.cpp new file mode 100644 index 0000000..30e15d9 --- /dev/null +++ b/app/src/main/cpp/waveforms/Triangle.cpp @@ -0,0 +1,15 @@ +#include "Triangle.h" + +void Triangle::setFrequency(float frequency) { + step = 4 * frequency / (double) host->sampleRate; +} + +void Triangle::renderWaveform(uint32_t frameCount) { + for (uint32_t i = 0; i < frameCount; i++) { + buffer[i] = value; + value += step; + if (value > 1 || value < -1) { + step *= -1; + } + } +} \ No newline at end of file diff --git a/app/src/main/cpp/CMakeLists.txt b/app/src/main/cpp/CMakeLists.txt index e7286cb..b94b511 100644 --- a/app/src/main/cpp/CMakeLists.txt +++ b/app/src/main/cpp/CMakeLists.txt @@ -10,6 +10,8 @@ waveforms/Sine.cpp waveforms/Sawtooth.cpp waveforms/Waveform.cpp + waveforms/Square.cpp + waveforms/Triangle.cpp JavaFunctions.cpp AudioHost.cpp Instrument.cpp diff --git a/app/src/main/cpp/Instrument.cpp b/app/src/main/cpp/Instrument.cpp index 3d9396d..d7263ed 100644 --- a/app/src/main/cpp/Instrument.cpp +++ b/app/src/main/cpp/Instrument.cpp @@ -1,6 +1,8 @@ #include "Instrument.h" #include "waveforms/Sawtooth.h" #include "waveforms/Sine.h" +#include "waveforms/Square.h" +#include "waveforms/Triangle.h" Instrument::Instrument(AudioHost *host) { this->host = host; @@ -46,6 +48,11 @@ case SAWTOOTH: wave = new Sawtooth(); break; + case SQUARE: + wave = new Square(); + break; + case TRIANGLE: + wave = new Triangle(); } wave->host = host; delete old; diff --git a/app/src/main/cpp/waveforms/Square.cpp b/app/src/main/cpp/waveforms/Square.cpp new file mode 100644 index 0000000..d136bca --- /dev/null +++ b/app/src/main/cpp/waveforms/Square.cpp @@ -0,0 +1,17 @@ +#include +#include "Square.h" + +void Square::setFrequency(float frequency) { + period = 1 / frequency; + step = 1 / (double) host->sampleRate; +} + +void Square::renderWaveform(uint32_t frameCount) { + for (uint32_t i = 0; i < frameCount; i++) { + buffer[i] = position > period / 2 ? 1 : -1; + position += step; + if (position > period) { + position = 0; + } + } +} \ No newline at end of file diff --git a/app/src/main/cpp/waveforms/Square.h b/app/src/main/cpp/waveforms/Square.h new file mode 100644 index 0000000..abcc39a --- /dev/null +++ b/app/src/main/cpp/waveforms/Square.h @@ -0,0 +1,15 @@ +#ifndef MUSIC_SQUARE_H +#define MUSIC_SQUARE_H + +#include "Waveform.h" + +class Square : public Waveform { +private: + float position, period, step; +public: + void renderWaveform(uint32_t frameCount); + + void setFrequency(float freq); +}; + +#endif diff --git a/app/src/main/cpp/waveforms/Triangle.cpp b/app/src/main/cpp/waveforms/Triangle.cpp new file mode 100644 index 0000000..30e15d9 --- /dev/null +++ b/app/src/main/cpp/waveforms/Triangle.cpp @@ -0,0 +1,15 @@ +#include "Triangle.h" + +void Triangle::setFrequency(float frequency) { + step = 4 * frequency / (double) host->sampleRate; +} + +void Triangle::renderWaveform(uint32_t frameCount) { + for (uint32_t i = 0; i < frameCount; i++) { + buffer[i] = value; + value += step; + if (value > 1 || value < -1) { + step *= -1; + } + } +} \ No newline at end of file diff --git a/app/src/main/cpp/waveforms/Triangle.h b/app/src/main/cpp/waveforms/Triangle.h new file mode 100644 index 0000000..788dc2d --- /dev/null +++ b/app/src/main/cpp/waveforms/Triangle.h @@ -0,0 +1,17 @@ +#ifndef MUSIC_TRIANGLE_H +#define MUSIC_TRIANGLE_H + + +#include "Waveform.h" + +class Triangle : public Waveform { +private: + float value = 0, step = 0; +public: + void renderWaveform(uint32_t frameCount); + + void setFrequency(float freq); +}; + + +#endif diff --git a/app/src/main/cpp/CMakeLists.txt b/app/src/main/cpp/CMakeLists.txt index e7286cb..b94b511 100644 --- a/app/src/main/cpp/CMakeLists.txt +++ b/app/src/main/cpp/CMakeLists.txt @@ -10,6 +10,8 @@ waveforms/Sine.cpp waveforms/Sawtooth.cpp waveforms/Waveform.cpp + waveforms/Square.cpp + waveforms/Triangle.cpp JavaFunctions.cpp AudioHost.cpp Instrument.cpp diff --git a/app/src/main/cpp/Instrument.cpp b/app/src/main/cpp/Instrument.cpp index 3d9396d..d7263ed 100644 --- a/app/src/main/cpp/Instrument.cpp +++ b/app/src/main/cpp/Instrument.cpp @@ -1,6 +1,8 @@ #include "Instrument.h" #include "waveforms/Sawtooth.h" #include "waveforms/Sine.h" +#include "waveforms/Square.h" +#include "waveforms/Triangle.h" Instrument::Instrument(AudioHost *host) { this->host = host; @@ -46,6 +48,11 @@ case SAWTOOTH: wave = new Sawtooth(); break; + case SQUARE: + wave = new Square(); + break; + case TRIANGLE: + wave = new Triangle(); } wave->host = host; delete old; diff --git a/app/src/main/cpp/waveforms/Square.cpp b/app/src/main/cpp/waveforms/Square.cpp new file mode 100644 index 0000000..d136bca --- /dev/null +++ b/app/src/main/cpp/waveforms/Square.cpp @@ -0,0 +1,17 @@ +#include +#include "Square.h" + +void Square::setFrequency(float frequency) { + period = 1 / frequency; + step = 1 / (double) host->sampleRate; +} + +void Square::renderWaveform(uint32_t frameCount) { + for (uint32_t i = 0; i < frameCount; i++) { + buffer[i] = position > period / 2 ? 1 : -1; + position += step; + if (position > period) { + position = 0; + } + } +} \ No newline at end of file diff --git a/app/src/main/cpp/waveforms/Square.h b/app/src/main/cpp/waveforms/Square.h new file mode 100644 index 0000000..abcc39a --- /dev/null +++ b/app/src/main/cpp/waveforms/Square.h @@ -0,0 +1,15 @@ +#ifndef MUSIC_SQUARE_H +#define MUSIC_SQUARE_H + +#include "Waveform.h" + +class Square : public Waveform { +private: + float position, period, step; +public: + void renderWaveform(uint32_t frameCount); + + void setFrequency(float freq); +}; + +#endif diff --git a/app/src/main/cpp/waveforms/Triangle.cpp b/app/src/main/cpp/waveforms/Triangle.cpp new file mode 100644 index 0000000..30e15d9 --- /dev/null +++ b/app/src/main/cpp/waveforms/Triangle.cpp @@ -0,0 +1,15 @@ +#include "Triangle.h" + +void Triangle::setFrequency(float frequency) { + step = 4 * frequency / (double) host->sampleRate; +} + +void Triangle::renderWaveform(uint32_t frameCount) { + for (uint32_t i = 0; i < frameCount; i++) { + buffer[i] = value; + value += step; + if (value > 1 || value < -1) { + step *= -1; + } + } +} \ No newline at end of file diff --git a/app/src/main/cpp/waveforms/Triangle.h b/app/src/main/cpp/waveforms/Triangle.h new file mode 100644 index 0000000..788dc2d --- /dev/null +++ b/app/src/main/cpp/waveforms/Triangle.h @@ -0,0 +1,17 @@ +#ifndef MUSIC_TRIANGLE_H +#define MUSIC_TRIANGLE_H + + +#include "Waveform.h" + +class Triangle : public Waveform { +private: + float value = 0, step = 0; +public: + void renderWaveform(uint32_t frameCount); + + void setFrequency(float freq); +}; + + +#endif diff --git a/app/src/main/cpp/waveforms/Waveform.h b/app/src/main/cpp/waveforms/Waveform.h index a70ed39..c082cda 100644 --- a/app/src/main/cpp/waveforms/Waveform.h +++ b/app/src/main/cpp/waveforms/Waveform.h @@ -6,6 +6,8 @@ enum WaveformType { SINE = 0, SAWTOOTH = 1, + SQUARE = 2, + TRIANGLE = 3, }; #include "../effects/Processable.h" diff --git a/app/src/main/cpp/CMakeLists.txt b/app/src/main/cpp/CMakeLists.txt index e7286cb..b94b511 100644 --- a/app/src/main/cpp/CMakeLists.txt +++ b/app/src/main/cpp/CMakeLists.txt @@ -10,6 +10,8 @@ waveforms/Sine.cpp waveforms/Sawtooth.cpp waveforms/Waveform.cpp + waveforms/Square.cpp + waveforms/Triangle.cpp JavaFunctions.cpp AudioHost.cpp Instrument.cpp diff --git a/app/src/main/cpp/Instrument.cpp b/app/src/main/cpp/Instrument.cpp index 3d9396d..d7263ed 100644 --- a/app/src/main/cpp/Instrument.cpp +++ b/app/src/main/cpp/Instrument.cpp @@ -1,6 +1,8 @@ #include "Instrument.h" #include "waveforms/Sawtooth.h" #include "waveforms/Sine.h" +#include "waveforms/Square.h" +#include "waveforms/Triangle.h" Instrument::Instrument(AudioHost *host) { this->host = host; @@ -46,6 +48,11 @@ case SAWTOOTH: wave = new Sawtooth(); break; + case SQUARE: + wave = new Square(); + break; + case TRIANGLE: + wave = new Triangle(); } wave->host = host; delete old; diff --git a/app/src/main/cpp/waveforms/Square.cpp b/app/src/main/cpp/waveforms/Square.cpp new file mode 100644 index 0000000..d136bca --- /dev/null +++ b/app/src/main/cpp/waveforms/Square.cpp @@ -0,0 +1,17 @@ +#include +#include "Square.h" + +void Square::setFrequency(float frequency) { + period = 1 / frequency; + step = 1 / (double) host->sampleRate; +} + +void Square::renderWaveform(uint32_t frameCount) { + for (uint32_t i = 0; i < frameCount; i++) { + buffer[i] = position > period / 2 ? 1 : -1; + position += step; + if (position > period) { + position = 0; + } + } +} \ No newline at end of file diff --git a/app/src/main/cpp/waveforms/Square.h b/app/src/main/cpp/waveforms/Square.h new file mode 100644 index 0000000..abcc39a --- /dev/null +++ b/app/src/main/cpp/waveforms/Square.h @@ -0,0 +1,15 @@ +#ifndef MUSIC_SQUARE_H +#define MUSIC_SQUARE_H + +#include "Waveform.h" + +class Square : public Waveform { +private: + float position, period, step; +public: + void renderWaveform(uint32_t frameCount); + + void setFrequency(float freq); +}; + +#endif diff --git a/app/src/main/cpp/waveforms/Triangle.cpp b/app/src/main/cpp/waveforms/Triangle.cpp new file mode 100644 index 0000000..30e15d9 --- /dev/null +++ b/app/src/main/cpp/waveforms/Triangle.cpp @@ -0,0 +1,15 @@ +#include "Triangle.h" + +void Triangle::setFrequency(float frequency) { + step = 4 * frequency / (double) host->sampleRate; +} + +void Triangle::renderWaveform(uint32_t frameCount) { + for (uint32_t i = 0; i < frameCount; i++) { + buffer[i] = value; + value += step; + if (value > 1 || value < -1) { + step *= -1; + } + } +} \ No newline at end of file diff --git a/app/src/main/cpp/waveforms/Triangle.h b/app/src/main/cpp/waveforms/Triangle.h new file mode 100644 index 0000000..788dc2d --- /dev/null +++ b/app/src/main/cpp/waveforms/Triangle.h @@ -0,0 +1,17 @@ +#ifndef MUSIC_TRIANGLE_H +#define MUSIC_TRIANGLE_H + + +#include "Waveform.h" + +class Triangle : public Waveform { +private: + float value = 0, step = 0; +public: + void renderWaveform(uint32_t frameCount); + + void setFrequency(float freq); +}; + + +#endif diff --git a/app/src/main/cpp/waveforms/Waveform.h b/app/src/main/cpp/waveforms/Waveform.h index a70ed39..c082cda 100644 --- a/app/src/main/cpp/waveforms/Waveform.h +++ b/app/src/main/cpp/waveforms/Waveform.h @@ -6,6 +6,8 @@ enum WaveformType { SINE = 0, SAWTOOTH = 1, + SQUARE = 2, + TRIANGLE = 3, }; #include "../effects/Processable.h" diff --git a/app/src/main/java/com/lukas/music/instruments/Waveform.kt b/app/src/main/java/com/lukas/music/instruments/Waveform.kt index 3a3de19..e35f7b8 100644 --- a/app/src/main/java/com/lukas/music/instruments/Waveform.kt +++ b/app/src/main/java/com/lukas/music/instruments/Waveform.kt @@ -13,6 +13,8 @@ enum class Waveform(val id: Int, private val identifier: String) { SINE(0, "sine"), SAWTOOTH(1, "sawtooth"), + SQUARE(2, "square"), + TRIANGLE(3, "triangle"), ; override fun toString(): String {