Skip to content

Commit

Permalink
add android source code (#552)
Browse files Browse the repository at this point in the history
  • Loading branch information
karlstav committed Sep 29, 2024
1 parent f515f93 commit 93edc8f
Show file tree
Hide file tree
Showing 51 changed files with 1,920 additions and 7 deletions.
21 changes: 20 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,23 @@

cmake_minimum_required(VERSION 3.13.0)
project(cavacore)
add_library(cavacore STATIC cavacore.c)
if (ANDROID)
add_library(cavacore SHARED cavacore.c)
if (NOT DEFINED FFTW_DIR)
message(FATAL_ERROR "FFTW_DIR not set, required by android, see cavandroid/README.md")
endif()
if (NOT EXISTS ${FFTW_DIR})
message(FATAL_ERROR "given FFTW_DIR: ${FFTW_DIR} does not exist")
endif()
if (NOT EXISTS "${FFTW_DIR}/jni/fftw3/api/")
message(FATAL_ERROR "given fftw include dir: ${FFTW_DIR}/jni/fftw3/api/ does not exist")
endif()
if (NOT EXISTS "${FFTW_DIR}/obj/local/${CMAKE_ANDROID_ARCH_ABI}/")
message(FATAL_ERROR "given lib dir: ${FFTW_DIR}/obj/local/${CMAKE_ANDROID_ARCH_ABI}/ does not exist, did you build it? See cavandroid/README.md")
endif()
target_include_directories(cavacore PRIVATE "${FFTW_DIR}/jni/fftw3/api/")
target_link_directories(cavacore PRIVATE "${FFTW_DIR}/obj/local/${CMAKE_ANDROID_ARCH_ABI}/")
target_link_libraries(cavacore fftw3)
else()
add_library(cavacore STATIC cavacore.c)
endif()
82 changes: 76 additions & 6 deletions cavacore.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
#include <math.h>
#include <stdlib.h>
#include <string.h>
#ifdef __ANDROID__
#include <jni.h>
struct cava_plan *plan;
double *cava_in;
double *cava_out;
#endif

struct cava_plan *cava_init(int number_of_bars, unsigned int rate, int channels, int autosens,
double noise_reduction, int low_cut_off, int high_cut_off) {
Expand Down Expand Up @@ -88,6 +94,11 @@ struct cava_plan *cava_init(int number_of_bars, unsigned int rate, int channels,
p->frame_skip = 1;
p->noise_reduction = noise_reduction;

int fftw_flag = FFTW_MEASURE;
#ifdef __ANDROID__
fftw_flag = FFTW_ESTIMATE;
#endif

p->FFTbassbufferSize = treble_buffer_size * 8;
p->FFTmidbufferSize = treble_buffer_size * 4;
p->FFTtreblebufferSize = treble_buffer_size;
Expand Down Expand Up @@ -125,20 +136,20 @@ struct cava_plan *cava_init(int number_of_bars, unsigned int rate, int channels,
p->in_bass_l_raw = fftw_alloc_real(p->FFTbassbufferSize);
p->out_bass_l = fftw_alloc_complex(p->FFTbassbufferSize / 2 + 1);
p->p_bass_l =
fftw_plan_dft_r2c_1d(p->FFTbassbufferSize, p->in_bass_l, p->out_bass_l, FFTW_MEASURE);
fftw_plan_dft_r2c_1d(p->FFTbassbufferSize, p->in_bass_l, p->out_bass_l, fftw_flag);

// MID
p->in_mid_l = fftw_alloc_real(p->FFTmidbufferSize);
p->in_mid_l_raw = fftw_alloc_real(p->FFTmidbufferSize);
p->out_mid_l = fftw_alloc_complex(p->FFTmidbufferSize / 2 + 1);
p->p_mid_l = fftw_plan_dft_r2c_1d(p->FFTmidbufferSize, p->in_mid_l, p->out_mid_l, FFTW_MEASURE);
p->p_mid_l = fftw_plan_dft_r2c_1d(p->FFTmidbufferSize, p->in_mid_l, p->out_mid_l, fftw_flag);

// TREBLE
p->in_treble_l = fftw_alloc_real(p->FFTtreblebufferSize);
p->in_treble_l_raw = fftw_alloc_real(p->FFTtreblebufferSize);
p->out_treble_l = fftw_alloc_complex(p->FFTtreblebufferSize / 2 + 1);
p->p_treble_l =
fftw_plan_dft_r2c_1d(p->FFTtreblebufferSize, p->in_treble_l, p->out_treble_l, FFTW_MEASURE);
fftw_plan_dft_r2c_1d(p->FFTtreblebufferSize, p->in_treble_l, p->out_treble_l, fftw_flag);

memset(p->in_bass_l, 0, sizeof(double) * p->FFTbassbufferSize);
memset(p->in_mid_l, 0, sizeof(double) * p->FFTmidbufferSize);
Expand All @@ -155,22 +166,22 @@ struct cava_plan *cava_init(int number_of_bars, unsigned int rate, int channels,
p->in_bass_r_raw = fftw_alloc_real(p->FFTbassbufferSize);
p->out_bass_r = fftw_alloc_complex(p->FFTbassbufferSize / 2 + 1);
p->p_bass_r =
fftw_plan_dft_r2c_1d(p->FFTbassbufferSize, p->in_bass_r, p->out_bass_r, FFTW_MEASURE);
fftw_plan_dft_r2c_1d(p->FFTbassbufferSize, p->in_bass_r, p->out_bass_r, fftw_flag);

// MID
p->in_mid_r = fftw_alloc_real(p->FFTmidbufferSize);
p->in_mid_r_raw = fftw_alloc_real(p->FFTmidbufferSize);
p->out_mid_r = fftw_alloc_complex(p->FFTmidbufferSize / 2 + 1);
p->p_mid_r =
fftw_plan_dft_r2c_1d(p->FFTmidbufferSize, p->in_mid_r, p->out_mid_r, FFTW_MEASURE);
fftw_plan_dft_r2c_1d(p->FFTmidbufferSize, p->in_mid_r, p->out_mid_r, fftw_flag);

// TREBLE
p->in_treble_r = fftw_alloc_real(p->FFTtreblebufferSize);
p->in_treble_r_raw = fftw_alloc_real(p->FFTtreblebufferSize);
p->out_treble_r = fftw_alloc_complex(p->FFTtreblebufferSize / 2 + 1);

p->p_treble_r = fftw_plan_dft_r2c_1d(p->FFTtreblebufferSize, p->in_treble_r,
p->out_treble_r, FFTW_MEASURE);
p->out_treble_r, fftw_flag);

memset(p->in_bass_r, 0, sizeof(double) * p->FFTbassbufferSize);
memset(p->in_mid_r, 0, sizeof(double) * p->FFTmidbufferSize);
Expand Down Expand Up @@ -563,3 +574,62 @@ void cava_destroy(struct cava_plan *p) {
fftw_destroy_plan(p->p_treble_r);
}
}

#ifdef __ANDROID__
JNIEXPORT jfloatArray JNICALL Java_com_karlstav_cava_MyGLRenderer_InitCava(
JNIEnv *env, jobject thiz, jint number_of_bars_set, jint refresh_rate, jint lower_cut_off,
jint higher_cut_off) {
jfloatArray cuttOffFreq = (*env)->NewFloatArray(env, number_of_bars_set + 1);
float noise_reduction = pow((float)refresh_rate / 130, 0.75);

plan =
cava_init(number_of_bars_set, 44100, 1, 1, noise_reduction, lower_cut_off, higher_cut_off);
cava_in = (double *)malloc(plan->FFTbassbufferSize * sizeof(double));
cava_out = (double *)malloc(plan->number_of_bars * sizeof(double));
(*env)->SetFloatArrayRegion(env, cuttOffFreq, 0, plan->number_of_bars + 1,
plan->cut_off_frequency);
return cuttOffFreq;
}

JNIEXPORT jdoubleArray JNICALL Java_com_karlstav_cava_MyGLRenderer_ExecCava(JNIEnv *env,
jobject thiz,
jdoubleArray cava_input,
jint new_samples) {

jdoubleArray cavaReturn = (*env)->NewDoubleArray(env, plan->number_of_bars);

cava_in = (*env)->GetDoubleArrayElements(env, cava_input, NULL);

cava_execute(cava_in, new_samples, cava_out, plan);
(*env)->SetDoubleArrayRegion(env, cavaReturn, 0, plan->number_of_bars, cava_out);
(*env)->ReleaseDoubleArrayElements(env, cava_input, cava_in, JNI_ABORT);

return cavaReturn;
}

JNIEXPORT int JNICALL Java_com_karlstav_cava_CavaCoreTest_InitCava(JNIEnv *env, jobject thiz,
jint number_of_bars_set) {

plan = cava_init(number_of_bars_set, 44100, 1, 1, 0.7, 50, 10000);
return 1;
}

JNIEXPORT jdoubleArray JNICALL Java_com_karlstav_cava_CavaCoreTest_ExecCava(JNIEnv *env,
jobject thiz,
jdoubleArray cava_input,
jint new_samples) {

jdoubleArray cavaReturn = (*env)->NewDoubleArray(env, plan->number_of_bars);

cava_in = (*env)->GetDoubleArrayElements(env, cava_input, NULL);

cava_execute(cava_in, new_samples, cava_out, plan);
(*env)->SetDoubleArrayRegion(env, cavaReturn, 0, plan->number_of_bars, cava_out);
(*env)->ReleaseDoubleArrayElements(env, cava_input, cava_in, JNI_ABORT);

return cavaReturn;
}
JNIEXPORT void JNICALL Java_com_karlstav_cava_MyGLRenderer_DestroyCava(JNIEnv *env, jobject thiz) {
cava_destroy(plan);
}
#endif
26 changes: 26 additions & 0 deletions cavandroid/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# CAVA on android

## FFTW3

To build cava on android you need to get the fftw3 library.
Luckily someone has made a repository for compiling FFTW3 on Android.
Just make sure ndk-build is in your path before running the build script.


```
git clone https://github.com/Lauszus/fftw3-android
cd fftw3-android
./build.sh
```

By default we will look for the fftw3 lib in a folder called fftw3-android besides the cava dir:

```
./
../
cava/
fftw3-android/
```

Edit the cmake argument in the app gradle file for setting it to some other place.
111 changes: 111 additions & 0 deletions cavandroid/app/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import com.android.build.api.dsl.LintOptions

plugins {
id("com.android.application")
id("org.jetbrains.kotlin.android")
}

android {
signingConfigs {
getByName("debug") {
storeFile = file("../keystore.jks")
storePassword = "cava123"
keyPassword = "cava123"
keyAlias = "key0"
}
create("release") {
storeFile = file("../keystore.jks")
storePassword = "cava123"
keyPassword = "cava123"
keyAlias = "key0"
}
}
namespace = "com.karlstav.cava"
compileSdk = 33

defaultConfig {
applicationId = "com.karlstav.cava"
minSdk = 30
targetSdk = 33
versionCode = 14
versionName = "@string/app_ver"

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables {
useSupportLibrary = true
}
/*
ndk {
abiFilters += listOf("arm64-v8a")
}
*/
externalNativeBuild {

// For ndk-build, instead use the ndkBuild block.
cmake {

// Passes optional arguments to CMake.
arguments("-DFFTW_DIR=${projectDir}/../../../fftw3-android")
}
}
}

buildTypes {
release {
isMinifyEnabled = false
proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro")
isDebuggable = false
signingConfig = signingConfigs.getByName("release")
}
}
// Encapsulates your external native build configurations.
externalNativeBuild {

// Encapsulates your CMake build configurations.
cmake {
// Provides a relative path to your CMake build script.
path = file("../../CMakeLists.txt")
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = "1.8"
}
buildFeatures {
compose = true
}
composeOptions {
kotlinCompilerExtensionVersion = "1.4.3"
}
packaging {
resources {
excludes += "/META-INF/{AL2.0,LGPL2.1}"
}
}

}

dependencies {

implementation("androidx.core:core-ktx:1.9.0")
implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.6.1")
implementation("androidx.activity:activity-compose:1.7.0")
implementation ("androidx.appcompat:appcompat:1.3.1")
implementation("androidx.preference:preference-ktx:1.2.0")

implementation(platform("androidx.compose:compose-bom:2023.03.00"))
implementation("androidx.compose.ui:ui")
implementation("androidx.compose.ui:ui-graphics")
implementation("androidx.compose.ui:ui-tooling-preview")
implementation("androidx.compose.material3:material3")
testImplementation("junit:junit:4.13.2")
androidTestImplementation("androidx.test.ext:junit:1.1.5")
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
androidTestImplementation(platform("androidx.compose:compose-bom:2023.03.00"))
androidTestImplementation("androidx.compose.ui:ui-test-junit4")
debugImplementation("androidx.compose.ui:ui-tooling")
debugImplementation("androidx.compose.ui:ui-test-manifest")
}
21 changes: 21 additions & 0 deletions cavandroid/app/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.karlstav.cava

import androidx.test.ext.junit.runners.AndroidJUnit4

import org.junit.Test
import org.junit.runner.RunWith

import org.junit.Assert.*

/**
* Instrumented test, which will execute on an Android device.
*
* See [testing documentation](http://d.android.com/tools/testing).
*/
@RunWith(AndroidJUnit4::class)
class CavaCoreTest {
external fun InitCava(pInt: Int): Int
external fun ExecCava(pDoubleArray: DoubleArray, pInt: Int): DoubleArray

@Test
fun cavaExec_null_test() {
System.loadLibrary("cavacore")

val numberOfBars = 10
val cavaData = DoubleArray(256)
val barsData = FloatArray(10)


for (i in 0 until 256) {
cavaData[i] = 0.0
}
val rc = InitCava(numberOfBars)

var cavaOut = ExecCava(cavaData, 256);


for (i in 0 until numberOfBars) {
assertEquals(0.0f, barsData[0])
}
}
}
Loading

0 comments on commit 93edc8f

Please sign in to comment.