diff --git a/.idea/misc.xml b/.idea/misc.xml index fc3da58..78b59de 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,5 +1,12 @@ + + + diff --git a/.idea/misc.xml b/.idea/misc.xml index fc3da58..78b59de 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,5 +1,12 @@ + + + diff --git a/app/src/main/cpp/AndroidBridge.cpp b/app/src/main/cpp/AndroidBridge.cpp new file mode 100644 index 0000000..4991080 --- /dev/null +++ b/app/src/main/cpp/AndroidBridge.cpp @@ -0,0 +1,25 @@ +#include +#include +#include "AudioEngine.h" +#include +#include + +static AudioEngine *audioEngine; + +extern "C" { + +JNIEXPORT void JNICALL +Java_com_lukas_music_MainActivity_startAudio(JNIEnv *env, jobject activity) { + audioEngine = new AudioEngine(); +} + +JNIEXPORT void JNICALL +Java_com_lukas_music_MainActivity_muteAudio(JNIEnv *env, jobject activity, float volume) { + audioEngine->stop(); +} + +JNIEXPORT void JNICALL +Java_com_lukas_music_MainActivity_unmuteAudio(JNIEnv *env, jobject activity, float volume) { + audioEngine->start(); +} +} \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml index fc3da58..78b59de 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,5 +1,12 @@ + + + diff --git a/app/src/main/cpp/AndroidBridge.cpp b/app/src/main/cpp/AndroidBridge.cpp new file mode 100644 index 0000000..4991080 --- /dev/null +++ b/app/src/main/cpp/AndroidBridge.cpp @@ -0,0 +1,25 @@ +#include +#include +#include "AudioEngine.h" +#include +#include + +static AudioEngine *audioEngine; + +extern "C" { + +JNIEXPORT void JNICALL +Java_com_lukas_music_MainActivity_startAudio(JNIEnv *env, jobject activity) { + audioEngine = new AudioEngine(); +} + +JNIEXPORT void JNICALL +Java_com_lukas_music_MainActivity_muteAudio(JNIEnv *env, jobject activity, float volume) { + audioEngine->stop(); +} + +JNIEXPORT void JNICALL +Java_com_lukas_music_MainActivity_unmuteAudio(JNIEnv *env, jobject activity, float volume) { + audioEngine->start(); +} +} \ No newline at end of file diff --git a/app/src/main/cpp/CMakeLists.txt b/app/src/main/cpp/CMakeLists.txt index 8d5c88c..92cc33c 100644 --- a/app/src/main/cpp/CMakeLists.txt +++ b/app/src/main/cpp/CMakeLists.txt @@ -1,48 +1,23 @@ -# For more information about using CMake with Android Studio, read the -# documentation: https://d.android.com/studio/projects/add-native-code.html - -# Sets the minimum version of CMake required to build the native library. - cmake_minimum_required(VERSION 3.18.1) -# Declares and names the project. - project("music") -# Creates and names a library, sets it as either STATIC -# or SHARED, and provides the relative paths to its source code. -# You can define multiple libraries, and CMake builds them for you. -# Gradle automatically packages shared libraries with your APK. - -add_library( # Sets the name of the library. +add_library( music - # Sets the library as a shared library. SHARED - # Provides a relative path to your source file(s). - native-lib.cpp) + SineWave.cpp + AndroidBridge.cpp + AudioEngine.cpp +) -# Searches for a specified prebuilt library and stores the path as a -# variable. Because CMake includes system libraries in the search path by -# default, you only need to specify the name of the public NDK library -# you want to add. CMake verifies that the library exists before -# completing its build. - -find_library( # Sets the name of the path variable. +find_library( log-lib - - # Specifies the name of the NDK library that - # you want CMake to locate. log) -# Specifies libraries CMake should link to your target library. You -# can link multiple libraries, such as libraries you define in this -# build script, prebuilt third-party libraries, or system libraries. - -target_link_libraries( # Specifies the target library. +target_link_libraries( music - - # Links the target library to the log library - # included in the NDK. - ${log-lib}) \ No newline at end of file + ${log-lib} + aaudio +) \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml index fc3da58..78b59de 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,5 +1,12 @@ + + + diff --git a/app/src/main/cpp/AndroidBridge.cpp b/app/src/main/cpp/AndroidBridge.cpp new file mode 100644 index 0000000..4991080 --- /dev/null +++ b/app/src/main/cpp/AndroidBridge.cpp @@ -0,0 +1,25 @@ +#include +#include +#include "AudioEngine.h" +#include +#include + +static AudioEngine *audioEngine; + +extern "C" { + +JNIEXPORT void JNICALL +Java_com_lukas_music_MainActivity_startAudio(JNIEnv *env, jobject activity) { + audioEngine = new AudioEngine(); +} + +JNIEXPORT void JNICALL +Java_com_lukas_music_MainActivity_muteAudio(JNIEnv *env, jobject activity, float volume) { + audioEngine->stop(); +} + +JNIEXPORT void JNICALL +Java_com_lukas_music_MainActivity_unmuteAudio(JNIEnv *env, jobject activity, float volume) { + audioEngine->start(); +} +} \ No newline at end of file diff --git a/app/src/main/cpp/CMakeLists.txt b/app/src/main/cpp/CMakeLists.txt index 8d5c88c..92cc33c 100644 --- a/app/src/main/cpp/CMakeLists.txt +++ b/app/src/main/cpp/CMakeLists.txt @@ -1,48 +1,23 @@ -# For more information about using CMake with Android Studio, read the -# documentation: https://d.android.com/studio/projects/add-native-code.html - -# Sets the minimum version of CMake required to build the native library. - cmake_minimum_required(VERSION 3.18.1) -# Declares and names the project. - project("music") -# Creates and names a library, sets it as either STATIC -# or SHARED, and provides the relative paths to its source code. -# You can define multiple libraries, and CMake builds them for you. -# Gradle automatically packages shared libraries with your APK. - -add_library( # Sets the name of the library. +add_library( music - # Sets the library as a shared library. SHARED - # Provides a relative path to your source file(s). - native-lib.cpp) + SineWave.cpp + AndroidBridge.cpp + AudioEngine.cpp +) -# Searches for a specified prebuilt library and stores the path as a -# variable. Because CMake includes system libraries in the search path by -# default, you only need to specify the name of the public NDK library -# you want to add. CMake verifies that the library exists before -# completing its build. - -find_library( # Sets the name of the path variable. +find_library( log-lib - - # Specifies the name of the NDK library that - # you want CMake to locate. log) -# Specifies libraries CMake should link to your target library. You -# can link multiple libraries, such as libraries you define in this -# build script, prebuilt third-party libraries, or system libraries. - -target_link_libraries( # Specifies the target library. +target_link_libraries( music - - # Links the target library to the log library - # included in the NDK. - ${log-lib}) \ No newline at end of file + ${log-lib} + aaudio +) \ No newline at end of file diff --git a/app/src/main/cpp/SineWave.cpp b/app/src/main/cpp/SineWave.cpp new file mode 100644 index 0000000..1aad9c3 --- /dev/null +++ b/app/src/main/cpp/SineWave.cpp @@ -0,0 +1,17 @@ +#include "SineWave.h" +#include +#include + +void SineWave::setSampleRate(uint32_t sampleRate) { + phaseStep = (2 * M_PI * frequency) / (double) sampleRate; +} + +void SineWave::render(float *data, uint32_t frameCount) { + for (uint32_t i = 0; i < frameCount; i++) { + data[i] = (float)(sin(phase) * amplitude); + phase += phaseStep; + if (phase > 2*M_PI) { + phase -= 2*M_PI; + } + } +} \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml index fc3da58..78b59de 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,5 +1,12 @@ + + + diff --git a/app/src/main/cpp/AndroidBridge.cpp b/app/src/main/cpp/AndroidBridge.cpp new file mode 100644 index 0000000..4991080 --- /dev/null +++ b/app/src/main/cpp/AndroidBridge.cpp @@ -0,0 +1,25 @@ +#include +#include +#include "AudioEngine.h" +#include +#include + +static AudioEngine *audioEngine; + +extern "C" { + +JNIEXPORT void JNICALL +Java_com_lukas_music_MainActivity_startAudio(JNIEnv *env, jobject activity) { + audioEngine = new AudioEngine(); +} + +JNIEXPORT void JNICALL +Java_com_lukas_music_MainActivity_muteAudio(JNIEnv *env, jobject activity, float volume) { + audioEngine->stop(); +} + +JNIEXPORT void JNICALL +Java_com_lukas_music_MainActivity_unmuteAudio(JNIEnv *env, jobject activity, float volume) { + audioEngine->start(); +} +} \ No newline at end of file diff --git a/app/src/main/cpp/CMakeLists.txt b/app/src/main/cpp/CMakeLists.txt index 8d5c88c..92cc33c 100644 --- a/app/src/main/cpp/CMakeLists.txt +++ b/app/src/main/cpp/CMakeLists.txt @@ -1,48 +1,23 @@ -# For more information about using CMake with Android Studio, read the -# documentation: https://d.android.com/studio/projects/add-native-code.html - -# Sets the minimum version of CMake required to build the native library. - cmake_minimum_required(VERSION 3.18.1) -# Declares and names the project. - project("music") -# Creates and names a library, sets it as either STATIC -# or SHARED, and provides the relative paths to its source code. -# You can define multiple libraries, and CMake builds them for you. -# Gradle automatically packages shared libraries with your APK. - -add_library( # Sets the name of the library. +add_library( music - # Sets the library as a shared library. SHARED - # Provides a relative path to your source file(s). - native-lib.cpp) + SineWave.cpp + AndroidBridge.cpp + AudioEngine.cpp +) -# Searches for a specified prebuilt library and stores the path as a -# variable. Because CMake includes system libraries in the search path by -# default, you only need to specify the name of the public NDK library -# you want to add. CMake verifies that the library exists before -# completing its build. - -find_library( # Sets the name of the path variable. +find_library( log-lib - - # Specifies the name of the NDK library that - # you want CMake to locate. log) -# Specifies libraries CMake should link to your target library. You -# can link multiple libraries, such as libraries you define in this -# build script, prebuilt third-party libraries, or system libraries. - -target_link_libraries( # Specifies the target library. +target_link_libraries( music - - # Links the target library to the log library - # included in the NDK. - ${log-lib}) \ No newline at end of file + ${log-lib} + aaudio +) \ No newline at end of file diff --git a/app/src/main/cpp/SineWave.cpp b/app/src/main/cpp/SineWave.cpp new file mode 100644 index 0000000..1aad9c3 --- /dev/null +++ b/app/src/main/cpp/SineWave.cpp @@ -0,0 +1,17 @@ +#include "SineWave.h" +#include +#include + +void SineWave::setSampleRate(uint32_t sampleRate) { + phaseStep = (2 * M_PI * frequency) / (double) sampleRate; +} + +void SineWave::render(float *data, uint32_t frameCount) { + for (uint32_t i = 0; i < frameCount; i++) { + data[i] = (float)(sin(phase) * amplitude); + phase += phaseStep; + if (phase > 2*M_PI) { + phase -= 2*M_PI; + } + } +} \ No newline at end of file diff --git a/app/src/main/cpp/SineWave.h b/app/src/main/cpp/SineWave.h new file mode 100644 index 0000000..acdd262 --- /dev/null +++ b/app/src/main/cpp/SineWave.h @@ -0,0 +1,15 @@ +#ifndef MUSIC_SINEWAVE_H +#define MUSIC_SINEWAVE_H + +#include + +class SineWave { +private: + float phaseStep = 0.01, phase = 0; +public: + float amplitude = 0.3, frequency = 440.0f; + void render(float *data, uint32_t frameCount); + void setSampleRate(uint32_t sampleRate); +}; + +#endif //MUSIC_SINEWAVE_H \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml index fc3da58..78b59de 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,5 +1,12 @@ + + + diff --git a/app/src/main/cpp/AndroidBridge.cpp b/app/src/main/cpp/AndroidBridge.cpp new file mode 100644 index 0000000..4991080 --- /dev/null +++ b/app/src/main/cpp/AndroidBridge.cpp @@ -0,0 +1,25 @@ +#include +#include +#include "AudioEngine.h" +#include +#include + +static AudioEngine *audioEngine; + +extern "C" { + +JNIEXPORT void JNICALL +Java_com_lukas_music_MainActivity_startAudio(JNIEnv *env, jobject activity) { + audioEngine = new AudioEngine(); +} + +JNIEXPORT void JNICALL +Java_com_lukas_music_MainActivity_muteAudio(JNIEnv *env, jobject activity, float volume) { + audioEngine->stop(); +} + +JNIEXPORT void JNICALL +Java_com_lukas_music_MainActivity_unmuteAudio(JNIEnv *env, jobject activity, float volume) { + audioEngine->start(); +} +} \ No newline at end of file diff --git a/app/src/main/cpp/CMakeLists.txt b/app/src/main/cpp/CMakeLists.txt index 8d5c88c..92cc33c 100644 --- a/app/src/main/cpp/CMakeLists.txt +++ b/app/src/main/cpp/CMakeLists.txt @@ -1,48 +1,23 @@ -# For more information about using CMake with Android Studio, read the -# documentation: https://d.android.com/studio/projects/add-native-code.html - -# Sets the minimum version of CMake required to build the native library. - cmake_minimum_required(VERSION 3.18.1) -# Declares and names the project. - project("music") -# Creates and names a library, sets it as either STATIC -# or SHARED, and provides the relative paths to its source code. -# You can define multiple libraries, and CMake builds them for you. -# Gradle automatically packages shared libraries with your APK. - -add_library( # Sets the name of the library. +add_library( music - # Sets the library as a shared library. SHARED - # Provides a relative path to your source file(s). - native-lib.cpp) + SineWave.cpp + AndroidBridge.cpp + AudioEngine.cpp +) -# Searches for a specified prebuilt library and stores the path as a -# variable. Because CMake includes system libraries in the search path by -# default, you only need to specify the name of the public NDK library -# you want to add. CMake verifies that the library exists before -# completing its build. - -find_library( # Sets the name of the path variable. +find_library( log-lib - - # Specifies the name of the NDK library that - # you want CMake to locate. log) -# Specifies libraries CMake should link to your target library. You -# can link multiple libraries, such as libraries you define in this -# build script, prebuilt third-party libraries, or system libraries. - -target_link_libraries( # Specifies the target library. +target_link_libraries( music - - # Links the target library to the log library - # included in the NDK. - ${log-lib}) \ No newline at end of file + ${log-lib} + aaudio +) \ No newline at end of file diff --git a/app/src/main/cpp/SineWave.cpp b/app/src/main/cpp/SineWave.cpp new file mode 100644 index 0000000..1aad9c3 --- /dev/null +++ b/app/src/main/cpp/SineWave.cpp @@ -0,0 +1,17 @@ +#include "SineWave.h" +#include +#include + +void SineWave::setSampleRate(uint32_t sampleRate) { + phaseStep = (2 * M_PI * frequency) / (double) sampleRate; +} + +void SineWave::render(float *data, uint32_t frameCount) { + for (uint32_t i = 0; i < frameCount; i++) { + data[i] = (float)(sin(phase) * amplitude); + phase += phaseStep; + if (phase > 2*M_PI) { + phase -= 2*M_PI; + } + } +} \ No newline at end of file diff --git a/app/src/main/cpp/SineWave.h b/app/src/main/cpp/SineWave.h new file mode 100644 index 0000000..acdd262 --- /dev/null +++ b/app/src/main/cpp/SineWave.h @@ -0,0 +1,15 @@ +#ifndef MUSIC_SINEWAVE_H +#define MUSIC_SINEWAVE_H + +#include + +class SineWave { +private: + float phaseStep = 0.01, phase = 0; +public: + float amplitude = 0.3, frequency = 440.0f; + void render(float *data, uint32_t frameCount); + void setSampleRate(uint32_t sampleRate); +}; + +#endif //MUSIC_SINEWAVE_H \ No newline at end of file diff --git a/app/src/main/java/com/lukas/music/MainActivity.kt b/app/src/main/java/com/lukas/music/MainActivity.kt index 7f4c840..83b5861 100644 --- a/app/src/main/java/com/lukas/music/MainActivity.kt +++ b/app/src/main/java/com/lukas/music/MainActivity.kt @@ -2,12 +2,14 @@ import androidx.appcompat.app.AppCompatActivity import android.os.Bundle +import android.view.View import android.widget.TextView import com.lukas.music.databinding.ActivityMainBinding class MainActivity : AppCompatActivity() { private lateinit var binding: ActivityMainBinding + private var isOn = true; override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -15,18 +17,24 @@ binding = ActivityMainBinding.inflate(layoutInflater) setContentView(binding.root) - // Example of a call to a native method - binding.sampleText.text = stringFromJNI() + binding.sampleText.text = "test" + startAudio() } - /** - * A native method that is implemented by the 'music' native library, - * which is packaged with this application. - */ - external fun stringFromJNI(): String + public fun toggleAudio(view: View) { + isOn = !isOn + if (isOn) { + unmuteAudio() + } else { + muteAudio() + } + } + + private external fun startAudio() + private external fun muteAudio() + private external fun unmuteAudio() companion object { - // Used to load the 'music' library on application startup. init { System.loadLibrary("music") } diff --git a/.idea/misc.xml b/.idea/misc.xml index fc3da58..78b59de 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,5 +1,12 @@ + + + diff --git a/app/src/main/cpp/AndroidBridge.cpp b/app/src/main/cpp/AndroidBridge.cpp new file mode 100644 index 0000000..4991080 --- /dev/null +++ b/app/src/main/cpp/AndroidBridge.cpp @@ -0,0 +1,25 @@ +#include +#include +#include "AudioEngine.h" +#include +#include + +static AudioEngine *audioEngine; + +extern "C" { + +JNIEXPORT void JNICALL +Java_com_lukas_music_MainActivity_startAudio(JNIEnv *env, jobject activity) { + audioEngine = new AudioEngine(); +} + +JNIEXPORT void JNICALL +Java_com_lukas_music_MainActivity_muteAudio(JNIEnv *env, jobject activity, float volume) { + audioEngine->stop(); +} + +JNIEXPORT void JNICALL +Java_com_lukas_music_MainActivity_unmuteAudio(JNIEnv *env, jobject activity, float volume) { + audioEngine->start(); +} +} \ No newline at end of file diff --git a/app/src/main/cpp/CMakeLists.txt b/app/src/main/cpp/CMakeLists.txt index 8d5c88c..92cc33c 100644 --- a/app/src/main/cpp/CMakeLists.txt +++ b/app/src/main/cpp/CMakeLists.txt @@ -1,48 +1,23 @@ -# For more information about using CMake with Android Studio, read the -# documentation: https://d.android.com/studio/projects/add-native-code.html - -# Sets the minimum version of CMake required to build the native library. - cmake_minimum_required(VERSION 3.18.1) -# Declares and names the project. - project("music") -# Creates and names a library, sets it as either STATIC -# or SHARED, and provides the relative paths to its source code. -# You can define multiple libraries, and CMake builds them for you. -# Gradle automatically packages shared libraries with your APK. - -add_library( # Sets the name of the library. +add_library( music - # Sets the library as a shared library. SHARED - # Provides a relative path to your source file(s). - native-lib.cpp) + SineWave.cpp + AndroidBridge.cpp + AudioEngine.cpp +) -# Searches for a specified prebuilt library and stores the path as a -# variable. Because CMake includes system libraries in the search path by -# default, you only need to specify the name of the public NDK library -# you want to add. CMake verifies that the library exists before -# completing its build. - -find_library( # Sets the name of the path variable. +find_library( log-lib - - # Specifies the name of the NDK library that - # you want CMake to locate. log) -# Specifies libraries CMake should link to your target library. You -# can link multiple libraries, such as libraries you define in this -# build script, prebuilt third-party libraries, or system libraries. - -target_link_libraries( # Specifies the target library. +target_link_libraries( music - - # Links the target library to the log library - # included in the NDK. - ${log-lib}) \ No newline at end of file + ${log-lib} + aaudio +) \ No newline at end of file diff --git a/app/src/main/cpp/SineWave.cpp b/app/src/main/cpp/SineWave.cpp new file mode 100644 index 0000000..1aad9c3 --- /dev/null +++ b/app/src/main/cpp/SineWave.cpp @@ -0,0 +1,17 @@ +#include "SineWave.h" +#include +#include + +void SineWave::setSampleRate(uint32_t sampleRate) { + phaseStep = (2 * M_PI * frequency) / (double) sampleRate; +} + +void SineWave::render(float *data, uint32_t frameCount) { + for (uint32_t i = 0; i < frameCount; i++) { + data[i] = (float)(sin(phase) * amplitude); + phase += phaseStep; + if (phase > 2*M_PI) { + phase -= 2*M_PI; + } + } +} \ No newline at end of file diff --git a/app/src/main/cpp/SineWave.h b/app/src/main/cpp/SineWave.h new file mode 100644 index 0000000..acdd262 --- /dev/null +++ b/app/src/main/cpp/SineWave.h @@ -0,0 +1,15 @@ +#ifndef MUSIC_SINEWAVE_H +#define MUSIC_SINEWAVE_H + +#include + +class SineWave { +private: + float phaseStep = 0.01, phase = 0; +public: + float amplitude = 0.3, frequency = 440.0f; + void render(float *data, uint32_t frameCount); + void setSampleRate(uint32_t sampleRate); +}; + +#endif //MUSIC_SINEWAVE_H \ No newline at end of file diff --git a/app/src/main/java/com/lukas/music/MainActivity.kt b/app/src/main/java/com/lukas/music/MainActivity.kt index 7f4c840..83b5861 100644 --- a/app/src/main/java/com/lukas/music/MainActivity.kt +++ b/app/src/main/java/com/lukas/music/MainActivity.kt @@ -2,12 +2,14 @@ import androidx.appcompat.app.AppCompatActivity import android.os.Bundle +import android.view.View import android.widget.TextView import com.lukas.music.databinding.ActivityMainBinding class MainActivity : AppCompatActivity() { private lateinit var binding: ActivityMainBinding + private var isOn = true; override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -15,18 +17,24 @@ binding = ActivityMainBinding.inflate(layoutInflater) setContentView(binding.root) - // Example of a call to a native method - binding.sampleText.text = stringFromJNI() + binding.sampleText.text = "test" + startAudio() } - /** - * A native method that is implemented by the 'music' native library, - * which is packaged with this application. - */ - external fun stringFromJNI(): String + public fun toggleAudio(view: View) { + isOn = !isOn + if (isOn) { + unmuteAudio() + } else { + muteAudio() + } + } + + private external fun startAudio() + private external fun muteAudio() + private external fun unmuteAudio() companion object { - // Used to load the 'music' library on application startup. init { System.loadLibrary("music") } diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 0fdf985..b4de30a 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -16,4 +16,16 @@ app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml index fc3da58..78b59de 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,5 +1,12 @@ + + + diff --git a/app/src/main/cpp/AndroidBridge.cpp b/app/src/main/cpp/AndroidBridge.cpp new file mode 100644 index 0000000..4991080 --- /dev/null +++ b/app/src/main/cpp/AndroidBridge.cpp @@ -0,0 +1,25 @@ +#include +#include +#include "AudioEngine.h" +#include +#include + +static AudioEngine *audioEngine; + +extern "C" { + +JNIEXPORT void JNICALL +Java_com_lukas_music_MainActivity_startAudio(JNIEnv *env, jobject activity) { + audioEngine = new AudioEngine(); +} + +JNIEXPORT void JNICALL +Java_com_lukas_music_MainActivity_muteAudio(JNIEnv *env, jobject activity, float volume) { + audioEngine->stop(); +} + +JNIEXPORT void JNICALL +Java_com_lukas_music_MainActivity_unmuteAudio(JNIEnv *env, jobject activity, float volume) { + audioEngine->start(); +} +} \ No newline at end of file diff --git a/app/src/main/cpp/CMakeLists.txt b/app/src/main/cpp/CMakeLists.txt index 8d5c88c..92cc33c 100644 --- a/app/src/main/cpp/CMakeLists.txt +++ b/app/src/main/cpp/CMakeLists.txt @@ -1,48 +1,23 @@ -# For more information about using CMake with Android Studio, read the -# documentation: https://d.android.com/studio/projects/add-native-code.html - -# Sets the minimum version of CMake required to build the native library. - cmake_minimum_required(VERSION 3.18.1) -# Declares and names the project. - project("music") -# Creates and names a library, sets it as either STATIC -# or SHARED, and provides the relative paths to its source code. -# You can define multiple libraries, and CMake builds them for you. -# Gradle automatically packages shared libraries with your APK. - -add_library( # Sets the name of the library. +add_library( music - # Sets the library as a shared library. SHARED - # Provides a relative path to your source file(s). - native-lib.cpp) + SineWave.cpp + AndroidBridge.cpp + AudioEngine.cpp +) -# Searches for a specified prebuilt library and stores the path as a -# variable. Because CMake includes system libraries in the search path by -# default, you only need to specify the name of the public NDK library -# you want to add. CMake verifies that the library exists before -# completing its build. - -find_library( # Sets the name of the path variable. +find_library( log-lib - - # Specifies the name of the NDK library that - # you want CMake to locate. log) -# Specifies libraries CMake should link to your target library. You -# can link multiple libraries, such as libraries you define in this -# build script, prebuilt third-party libraries, or system libraries. - -target_link_libraries( # Specifies the target library. +target_link_libraries( music - - # Links the target library to the log library - # included in the NDK. - ${log-lib}) \ No newline at end of file + ${log-lib} + aaudio +) \ No newline at end of file diff --git a/app/src/main/cpp/SineWave.cpp b/app/src/main/cpp/SineWave.cpp new file mode 100644 index 0000000..1aad9c3 --- /dev/null +++ b/app/src/main/cpp/SineWave.cpp @@ -0,0 +1,17 @@ +#include "SineWave.h" +#include +#include + +void SineWave::setSampleRate(uint32_t sampleRate) { + phaseStep = (2 * M_PI * frequency) / (double) sampleRate; +} + +void SineWave::render(float *data, uint32_t frameCount) { + for (uint32_t i = 0; i < frameCount; i++) { + data[i] = (float)(sin(phase) * amplitude); + phase += phaseStep; + if (phase > 2*M_PI) { + phase -= 2*M_PI; + } + } +} \ No newline at end of file diff --git a/app/src/main/cpp/SineWave.h b/app/src/main/cpp/SineWave.h new file mode 100644 index 0000000..acdd262 --- /dev/null +++ b/app/src/main/cpp/SineWave.h @@ -0,0 +1,15 @@ +#ifndef MUSIC_SINEWAVE_H +#define MUSIC_SINEWAVE_H + +#include + +class SineWave { +private: + float phaseStep = 0.01, phase = 0; +public: + float amplitude = 0.3, frequency = 440.0f; + void render(float *data, uint32_t frameCount); + void setSampleRate(uint32_t sampleRate); +}; + +#endif //MUSIC_SINEWAVE_H \ No newline at end of file diff --git a/app/src/main/java/com/lukas/music/MainActivity.kt b/app/src/main/java/com/lukas/music/MainActivity.kt index 7f4c840..83b5861 100644 --- a/app/src/main/java/com/lukas/music/MainActivity.kt +++ b/app/src/main/java/com/lukas/music/MainActivity.kt @@ -2,12 +2,14 @@ import androidx.appcompat.app.AppCompatActivity import android.os.Bundle +import android.view.View import android.widget.TextView import com.lukas.music.databinding.ActivityMainBinding class MainActivity : AppCompatActivity() { private lateinit var binding: ActivityMainBinding + private var isOn = true; override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -15,18 +17,24 @@ binding = ActivityMainBinding.inflate(layoutInflater) setContentView(binding.root) - // Example of a call to a native method - binding.sampleText.text = stringFromJNI() + binding.sampleText.text = "test" + startAudio() } - /** - * A native method that is implemented by the 'music' native library, - * which is packaged with this application. - */ - external fun stringFromJNI(): String + public fun toggleAudio(view: View) { + isOn = !isOn + if (isOn) { + unmuteAudio() + } else { + muteAudio() + } + } + + private external fun startAudio() + private external fun muteAudio() + private external fun unmuteAudio() companion object { - // Used to load the 'music' library on application startup. init { System.loadLibrary("music") } diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 0fdf985..b4de30a 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -16,4 +16,16 @@ app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> + + \ No newline at end of file diff --git a/build.gradle b/build.gradle index 7b9b67d..bf99e36 100644 --- a/build.gradle +++ b/build.gradle @@ -1,4 +1,3 @@ -// Top-level build file where you can add configuration options common to all sub-projects/modules. plugins { id 'com.android.application' version '7.2.1' apply false id 'com.android.library' version '7.2.1' apply false