feat: detect steps and play audio
This commit is contained in:
@@ -12,6 +12,8 @@ cmake_minimum_required(VERSION 3.22.1)
|
||||
# build script scope).
|
||||
project("lockstep-native")
|
||||
|
||||
add_subdirectory(libpasada/pasada-lib)
|
||||
|
||||
# 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.
|
||||
@@ -31,6 +33,8 @@ add_library(${CMAKE_PROJECT_NAME} SHARED
|
||||
PlaybackEngine.cpp
|
||||
mp3file.cpp
|
||||
jni_bridge.cpp
|
||||
jni_stepdetector.cpp
|
||||
StepDetector.cpp
|
||||
)
|
||||
|
||||
find_package (oboe REQUIRED CONFIG)
|
||||
@@ -45,11 +49,14 @@ set_target_properties(mpg123 PROPERTIES IMPORTED_LOCATION
|
||||
${mpg123_DIR}/lib/${ANDROID_ABI}/libmpg123.so)
|
||||
include_directories(${mpg123_DIR}/lib/${ANDROID_ABI}/include)
|
||||
|
||||
target_include_directories(${CMAKE_PROJECT_NAME} PRIVATE libpasada/pasada-lib/include)
|
||||
|
||||
# Specifies libraries CMake should link to your target library. You
|
||||
# can link libraries from various origins, such as libraries defined in this
|
||||
# build script, prebuilt third-party libraries, or Android system libraries.
|
||||
target_link_libraries(${CMAKE_PROJECT_NAME}
|
||||
# List libraries link to the target library
|
||||
pasada
|
||||
oboe::oboe
|
||||
mpg123
|
||||
android
|
||||
|
||||
@@ -37,7 +37,7 @@ public:
|
||||
std::lock_guard<std::mutex> lock(mLock);
|
||||
oboe::AudioStreamBuilder builder;
|
||||
// The builder set methods can be chained for convenience.
|
||||
Result result = builder.setSharingMode(oboe::SharingMode::Exclusive)
|
||||
Result result = builder.setSharingMode(oboe::SharingMode::Shared)
|
||||
->setPerformanceMode(oboe::PerformanceMode::LowLatency)
|
||||
->setChannelCount(kChannelCount)
|
||||
->setSampleRate(kSampleRate)
|
||||
|
||||
@@ -56,3 +56,7 @@ PlaybackEngine::~PlaybackEngine() {
|
||||
delete mPlayer;
|
||||
mPlayer = nullptr;
|
||||
}
|
||||
|
||||
void PlaybackEngine::playBeat() {
|
||||
if(mPlayer) mPlayer->setStartBeat();
|
||||
}
|
||||
|
||||
@@ -5,13 +5,16 @@
|
||||
#ifndef LOCKSTEP_PLAYBACKENGINE_H
|
||||
#define LOCKSTEP_PLAYBACKENGINE_H
|
||||
|
||||
#include "StepListener.h"
|
||||
#include "MixingPlayer.h"
|
||||
#include <string>
|
||||
|
||||
class PlaybackEngine {
|
||||
class PlaybackEngine : public StepListener {
|
||||
public:
|
||||
PlaybackEngine(std::string filesDir, int resid);
|
||||
virtual ~PlaybackEngine();
|
||||
/** Play a beat sound. */
|
||||
virtual void playBeat();
|
||||
private:
|
||||
MixingPlayer *mPlayer;
|
||||
std::string mFilesDir;
|
||||
|
||||
44
app/src/main/cpp/StepDetector.cpp
Normal file
44
app/src/main/cpp/StepDetector.cpp
Normal file
@@ -0,0 +1,44 @@
|
||||
//
|
||||
// Created by david on 03.03.2026.
|
||||
//
|
||||
|
||||
#include "StepDetector.h"
|
||||
|
||||
// TODO: we are hardcoding filter coefficients for 60 Hz
|
||||
// TODO: this is tolerable for 50 Hz
|
||||
|
||||
// TODO: check if we can do with floats instead of doubles
|
||||
// (check how much the [already bad] accuracy of filtering suffers)
|
||||
|
||||
// TODO: in Java, check if delta timestamps effectively match FPS
|
||||
// TODO: FPS constant should be passed as argument to C++ (but we keep an FPS define to validate the coefficients)
|
||||
|
||||
// Butterworth filter: order=5, fc=0.5, fs=60, btype='highpass'
|
||||
static std::vector hpf_taps_b {0.91875845, -4.59379227, 9.18758454, -9.18758454, 4.59379227, -0.91875845};
|
||||
static std::vector hpf_taps_a {1. , -4.83056552, 9.33652742, -9.02545247, 4.36360803, -0.8441171};
|
||||
static size_t upslope_width = 4;
|
||||
const size_t len_refr = (size_t) (FPS / (MAX_BPM / 60));
|
||||
|
||||
StepDetector::StepDetector(StepListener *listener) :
|
||||
listener(listener),
|
||||
f_highpass(hpf_taps_b, hpf_taps_a),
|
||||
f_neg(1, 0, 0, std::vector {-1.0}),
|
||||
f_ssf(upslope_width),
|
||||
f_ssd(len_refr)
|
||||
{}
|
||||
|
||||
#if (FPS != 60)
|
||||
#error "FPS must currently be 60, as highpass taps are pre-computed for that value"
|
||||
#endif
|
||||
|
||||
void StepDetector::filter(std::vector<float> values) {
|
||||
// TODO: later on, we should use a vector projection towards gravity
|
||||
auto s0 = (double) values[1]; // take y-axis value for now
|
||||
auto s1 = f_highpass.filter(s0);
|
||||
auto s2 = f_neg.filter(s1);
|
||||
auto s3 = f_ssf.filter(s2);
|
||||
auto s4 = f_ssd.filter(s3);
|
||||
if(s4 > 0.0 && listener != nullptr) {
|
||||
listener->playBeat();
|
||||
}
|
||||
}
|
||||
26
app/src/main/cpp/StepDetector.h
Normal file
26
app/src/main/cpp/StepDetector.h
Normal file
@@ -0,0 +1,26 @@
|
||||
//
|
||||
// Created by david on 03.03.2026.
|
||||
//
|
||||
|
||||
#ifndef LOCKSTEP_STEPDETECTOR_H
|
||||
#define LOCKSTEP_STEPDETECTOR_H
|
||||
|
||||
#include "StepListener.h"
|
||||
#include "iir_filter.h"
|
||||
#include "ssf_filter.h"
|
||||
#include <vector>
|
||||
|
||||
class StepDetector {
|
||||
protected:
|
||||
StepListener *listener;
|
||||
IirFilter f_highpass;
|
||||
Filt f_neg;
|
||||
SsfFilter f_ssf;
|
||||
SsfStepDetector f_ssd;
|
||||
|
||||
public:
|
||||
StepDetector(StepListener *listener);
|
||||
void filter(std::vector<float> values);
|
||||
};
|
||||
|
||||
#endif //LOCKSTEP_STEPDETECTOR_H
|
||||
14
app/src/main/cpp/StepListener.h
Normal file
14
app/src/main/cpp/StepListener.h
Normal file
@@ -0,0 +1,14 @@
|
||||
//
|
||||
// Created by david on 03.03.2026.
|
||||
//
|
||||
|
||||
#ifndef LOCKSTEP_STEPLISTENER_H
|
||||
#define LOCKSTEP_STEPLISTENER_H
|
||||
|
||||
class StepListener {
|
||||
public:
|
||||
virtual ~StepListener() {}
|
||||
virtual void playBeat() = 0;
|
||||
};
|
||||
|
||||
#endif //LOCKSTEP_STEPLISTENER_H
|
||||
52
app/src/main/cpp/jni_stepdetector.cpp
Normal file
52
app/src/main/cpp/jni_stepdetector.cpp
Normal file
@@ -0,0 +1,52 @@
|
||||
//
|
||||
// Created by david on 03.03.2026.
|
||||
//
|
||||
|
||||
#include <jni.h>
|
||||
#include "StepDetector.h"
|
||||
#include <new>
|
||||
#include <vector>
|
||||
|
||||
extern "C" {
|
||||
|
||||
jint throwIllegalArgumentException(JNIEnv *env, const char *message)
|
||||
{
|
||||
jclass exClass;
|
||||
const char *className = "java/lang/IllegalArgumentException";
|
||||
exClass = env->FindClass(className);
|
||||
if (exClass == nullptr) {
|
||||
return -1;
|
||||
}
|
||||
return env->ThrowNew(exClass, message);
|
||||
}
|
||||
|
||||
JNIEXPORT jlong JNICALL
|
||||
Java_at_lockstep_filter_StepDetector_native_1create(
|
||||
JNIEnv *env,
|
||||
jclass /*unused*/, jlong engineHandle) {
|
||||
auto *listener = reinterpret_cast<StepListener *>(engineHandle);
|
||||
// We use std::nothrow so `new` returns a nullptr if the engine creation fails
|
||||
auto *detector = new(std::nothrow) StepDetector(listener);
|
||||
return reinterpret_cast<jlong>(detector);
|
||||
}
|
||||
|
||||
JNIEXPORT void JNICALL
|
||||
Java_at_lockstep_filter_StepDetector_native_1delete(
|
||||
JNIEnv *env,
|
||||
jclass /*unused*/, jlong handle) {
|
||||
delete reinterpret_cast<StepDetector *>(handle);
|
||||
}
|
||||
|
||||
JNIEXPORT void JNICALL
|
||||
Java_at_lockstep_filter_StepDetector_native_1filter(
|
||||
JNIEnv *env,
|
||||
jclass /*unused*/, jlong handle, jlong timestamp, jfloatArray values) {
|
||||
if(values == nullptr) throwIllegalArgumentException(env, "values == null");
|
||||
float* nativeValues = (float *)env->GetFloatArrayElements(values, 0);
|
||||
jsize length = env->GetArrayLength(values);
|
||||
std::vector<float> vecValues(nativeValues, nativeValues + length);
|
||||
auto *detector = reinterpret_cast<StepDetector *>(handle);
|
||||
detector->filter(vecValues);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user