Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
w-ensink committed Apr 4, 2023
0 parents commit 022c5db
Show file tree
Hide file tree
Showing 37 changed files with 31,860 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/target

.idea
cmake-build-*
*.wav
*.mp3

Cargo.lock
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "cpp/external/RTNeural"]
path = cpp/external/RTNeural
url = https://github.com/jatinchowdhury18/RTNeural.git
10 changes: 10 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "basic_pitch"
version = "0.0.1"
edition = "2021"


# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]

46 changes: 46 additions & 0 deletions build-android-onnx-libs.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@

# script designed to build onnxruntime for android and copy the relevant files
# to on directory in order to make linking easy

ANDROID_NDK_HOME="$HOME/Library/Android/sdk/ndk/25.2.9519653"
ANDROID_SDK_PATH="$HOME/Library/Android/sdk"
ONNX_TARGET_DIR="$(pwd)/onnxruntime-libs/android"

echo "using onnxruntime dir: $ONNX_REPO_DIR"
echo "using sdk path: $ANDROID_SDK_PATH"
echo "using ndk home: $ANDROID_NDK_HOME"

cd "$ONNX_REPO_DIR" || exit

./build.sh --android \
--android_sdk_path "$ANDROID_SDK_PATH" \
--android_ndk_path "$ANDROID_NDK_HOME" \
--android_abi "arm64-v8a" \
--android_api 33 \
--config=Release \
--parallel

ONNX_BUILD_DIR="$ONNX_REPO_DIR/build/Android/Release"

echo "cd to $(pwd)"
cd "$ONNX_BUILD_DIR" || exit

echo "making target dir '$ONNX_TARGET_DIR' if it doesn't exist"
mkdir -p "$ONNX_TARGET_DIR"

echo "copying relevant onnxruntime libs to '$ONNX_TARGET_DIR'"
cp *.a \
_deps/abseil_cpp-build/absl/hash/libabsl_city.a \
_deps/abseil_cpp-build/absl/hash/libabsl_hash.a \
_deps/abseil_cpp-build/absl/hash/libabsl_low_level_hash.a \
_deps/abseil_cpp-build/absl/base/libabsl_throw_delegate.a \
_deps/abseil_cpp-build/absl/base/libabsl_raw_logging_internal.a \
_deps/abseil_cpp-build/absl/container/libabsl_raw_hash_set.a \
_deps/pytorch_cpuinfo-build/libcpuinfo.a \
_deps/pytorch_cpuinfo-build/deps/clog/libclog.a \
_deps/google_nsync-build/libnsync_cpp.a \
_deps/protobuf-build/libprotobuf-lite.a \
_deps/onnx-build/libonnx.a \
_deps/onnx-build/libonnx_proto.a \
_deps/re2-build/libre2.a \
"$ONNX_TARGET_DIR"
3 changes: 3 additions & 0 deletions build-android.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ANDROID_NDK_HOME=$HOME/Library/Android/sdk/ndk/25.2.9519653 \
CARGO_TARGET_AARCH64_LINUX_ANDROID_LINKER=$HOME/Library/Android/sdk/ndk/25.2.9519653/toolchains/llvm/prebuilt/darwin-x86_64/bin/aarch64-linux-android33-clang++ \
cargo build --target=aarch64-linux-android
189 changes: 189 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
use std::path::Path;
use std::path::PathBuf;

fn cmake_build_dir() -> PathBuf {
PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("cpp")
.join("cmake-build-release")
}

fn add_link_search_path(path: impl AsRef<Path>) {
println!(
"cargo:rustc-link-search={}",
path.as_ref().to_str().unwrap()
);
}

fn add_link_search_paths(paths: &[&str]) {
for path in paths {
add_link_search_path(path);
}
}

fn link_static_libs(libs: &[&str]) {
for lib in libs {
link_static_lib(lib);
}
}

fn link_static_lib(name: &str) {
println!("cargo:rustc-link-lib=static={name}");
}

fn link_neural_pitch() {
let lib_path = cmake_build_dir();
let lib_name = "neural_pitch_detector";
println!("cargo:rustc-link-search={}", lib_path.to_str().unwrap());
println!("cargo:rustc-link-lib=static={lib_name}");
}

fn link_rtneural() {
let lib_path = cmake_build_dir()
.join("external")
.join("RTNeural")
.join("RTNeural");

let lib_name = "RTNeural";
println!("cargo:rustc-link-search={}", lib_path.to_str().unwrap());
println!("cargo:rustc-link-lib=static={lib_name}");
}

fn link_onnx_runtime() {
let lib_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("cpp")
.join("external")
.join("onnxruntime")
.join("lib");
let lib_name = "onnxruntime";
println!("cargo:rustc-link-search={}", lib_path.to_str().unwrap());
println!("cargo:rustc-link-lib=static={lib_name}");
}

fn main() {
println!("cargo:rerun-if-changed=build.rs");

let target_os = std::env::var("CARGO_CFG_TARGET_OS").unwrap();

match target_os.as_str() {
"macos" => {
println!("cargo:warning=compiling for macos");

println!("cargo:rustc-link-lib=c++");
println!("cargo:rustc-link-lib=framework=Foundation");
link_onnx_runtime();
link_rtneural();
link_neural_pitch();
}
"android" => {
println!("cargo:warning=compiling for android");

configure_cmake_for_android();
build_with_cmake_android();

add_link_search_paths(&[
"/Users/wouter/Dev/cpp/onnxruntime/build/Android/Release",
"/Users/wouter/Dev/cpp/onnxruntime/build/Android/Release/_deps/abseil_cpp-build/absl/hash",
"/Users/wouter/Dev/cpp/onnxruntime/build/Android/Release/_deps/abseil_cpp-build/absl/base",
"/Users/wouter/Dev/cpp/onnxruntime/build/Android/Release/_deps/abseil_cpp-build/absl/container",
"/Users/wouter/Dev/cpp/onnxruntime/build/Android/Release/_deps/pytorch_cpuinfo-build",
"/Users/wouter/Dev/cpp/onnxruntime/build/Android/Release/_deps/pytorch_cpuinfo-build/deps/clog",
"/Users/wouter/Dev/cpp/onnxruntime/build/Android/Release/_deps/google_nsync-build",
"/Users/wouter/Dev/cpp/onnxruntime/build/Android/Release/_deps/protobuf-build",
"/Users/wouter/Dev/cpp/onnxruntime/build/Android/Release/_deps/onnx-build",
"/Users/wouter/Dev/cpp/onnxruntime/build/Android/Release/_deps/re2-build",
]);

link_static_libs(&[
"onnxruntime_common",
"onnx_test_data_proto",
"onnx_test_runner_common",
"onnxruntime_flatbuffers",
"onnxruntime_framework",
"onnxruntime_graph",
"onnxruntime_mlas",
"onnxruntime_optimizer",
"onnxruntime_providers",
"onnxruntime_session",
"onnxruntime_test_utils",
"onnxruntime_util",
"absl_city",
"absl_hash",
"absl_low_level_hash",
"absl_throw_delegate",
"absl_raw_logging_internal",
"absl_raw_hash_set",
"cpuinfo",
"clog",
"nsync_cpp",
"protobuf-lite",
"onnx",
"onnx_proto",
"re2",
]);

add_link_search_path(format!(
"{}/android_libs",
std::env::var("OUT_DIR").unwrap()
));
link_static_lib("neural_pitch_detector");
link_static_lib("RTNeural");

let ndk_dir =
std::env::var("ANDROID_NDK_HOME").expect("set `ANDROID_NDK_HOME` env variable");
add_link_search_path(format!("{ndk_dir}/toolchains/llvm/prebuilt/darwin-x86_64/sysroot/usr/lib/aarch64-linux-android"));
println!("cargo:rustc-link-lib=c++_shared")
}
_ => {}
}
}

fn build_with_cmake_android() {
let cmake_output = std::process::Command::new("cmake")
.args([
"--build",
&format!("{}/cmake-build-release", std::env::var("OUT_DIR").unwrap()),
])
.current_dir("cpp")
.spawn()
.unwrap()
.wait_with_output()
.unwrap();

if !cmake_output.status.success() {
let stdout = String::from_utf8(cmake_output.stdout).unwrap();
let stderr = String::from_utf8(cmake_output.stderr).unwrap();
panic!("failed to build with cmake: \n\nstdout: {stdout}\n\nstderr: {stderr}");
}
}

fn configure_cmake_for_android() {
let ndk_dir = std::env::var("ANDROID_NDK_HOME").expect("set `ANDROID_NDK_HOME` env variable");
let out_dir = std::env::var("OUT_DIR").unwrap();

let cmake_output = std::process::Command::new("cmake")
.args([
"-S",
".",
"-B",
&format!("{out_dir}/cmake-build-release"),
&format!("-DCMAKE_ANDROID_NDK='{ndk_dir}'"),
&format!("-DCMAKE_TOOLCHAIN_FILE='{ndk_dir}/build/cmake/android.toolchain.cmake'"),
"-DCMAKE_BUILD_TYPE=Release",
"-DANDROID_ABI=arm64-v8a",
"-DANDROID_PLATFORM=android-33",
"-DCMAKE_EXPORT_COMPILE_COMMANDS=ON",
"-DANDROID_STL=c++_shared",
&format!("-DCMAKE_ARCHIVE_OUTPUT_DIRECTORY='{out_dir}/android_libs'"),
])
.current_dir(concat!(env!("CARGO_MANIFEST_DIR"), "/cpp"))
.spawn()
.unwrap()
.wait_with_output()
.unwrap();

if !cmake_output.status.success() {
let stdout = String::from_utf8(cmake_output.stdout).unwrap();
let stderr = String::from_utf8(cmake_output.stderr).unwrap();
panic!("failed to build configure cmake: \n\nstdout: {stdout}\n\nstderr: {stderr}");
}
}
5 changes: 5 additions & 0 deletions cpp/.clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
BasedOnStyle: LLVM
Standard: Latest
IndentWidth: 4
TabWidth: 4
UseTab: Never
30 changes: 30 additions & 0 deletions cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
cmake_minimum_required(VERSION 3.16)

project(neural_pitch_detector VERSION 0.0.1)

enable_language(CXX C)

set(CMAKE_CXX_STANDARD 17)

add_subdirectory(external/RTNeural)

add_library(onnx_runtime STATIC IMPORTED)
set_property(TARGET onnx_runtime PROPERTY IMPORTED_LOCATION
"${CMAKE_CURRENT_LIST_DIR}/external/onnxruntime/lib/libonnxruntime.a")


add_library(neural_pitch_detector STATIC
source/lib.cpp
source/features.h
source/features.cpp
source/constants.h
source/pitch_cnn.h
source/pitch_cnn.cpp
source/pitch_detector.h
source/pitch_detector.cpp
source/notes.h
source/notes.cpp)

target_include_directories(neural_pitch_detector PUBLIC "${CMAKE_CURRENT_LIST_DIR}/external/onnxruntime/include")
target_link_libraries(neural_pitch_detector PUBLIC RTNeural)
target_compile_features(neural_pitch_detector PRIVATE cxx_std_17)
15 changes: 15 additions & 0 deletions cpp/compile-android.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

# building onnxruntime from the repo dir (checkout stable version tag)
# ./build.sh --android --android_sdk_path "~/Library/Android/sdk" --android_ndk_path "~/Library/Android/sdk/ndk/25.2.9519653" --android_abi "arm64-v8a" --android_api 33

cmake -S . -B build-android-release \
-DCMAKE_ANDROID_NDK="/Users/wouter/Library/Android/sdk/ndk/25.2.9519653" \
-DCMAKE_TOOLCHAIN_FILE="/Users/wouter/Library/Android/sdk/ndk/25.2.9519653/build/cmake/android.toolchain.cmake" \
-DCMAKE_BUILD_TYPE=Release \
-DANDROID_ABI=arm64-v8a \
-DANDROID_PLATFORM=android-33 \
-DCMAKE_EXPORT_COMPILE_COMMANDS=ON \
-DANDROID_STL=c++_shared \
-DCMAKE_ARCHIVE_OUTPUT_DIRECTORY="$(pwd)/android_libs"

cmake --build build-android-release --target neural_pitch_detector
1 change: 1 addition & 0 deletions cpp/external/RTNeural
Submodule RTNeural added at c5ba9c
41 changes: 41 additions & 0 deletions cpp/external/onnxruntime/include/coreml_provider_factory.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
#pragma once

#include "onnxruntime_c_api.h"

// COREMLFlags are bool options we want to set for CoreML EP
// This enum is defined as bit flags, and cannot have negative value
// To generate an uint32_t coreml_flags for using with OrtSessionOptionsAppendExecutionProvider_CoreML below,
// uint32_t coreml_flags = 0;
// coreml_flags |= COREML_FLAG_USE_CPU_ONLY;
enum COREMLFlags {
COREML_FLAG_USE_NONE = 0x000,

// Using CPU only in CoreML EP, this may decrease the perf but will provide
// reference output value without precision loss, which is useful for validation
COREML_FLAG_USE_CPU_ONLY = 0x001,

// Enable CoreML EP on subgraph
COREML_FLAG_ENABLE_ON_SUBGRAPH = 0x002,

// By default CoreML Execution provider will be enabled for all compatible Apple devices
// Enable this option will only enable CoreML EP for Apple devices with ANE (Apple Neural Engine)
// Please note, enable this option does not guarantee the entire model to be executed using ANE only
COREML_FLAG_ONLY_ENABLE_DEVICE_WITH_ANE = 0x004,

// Keep COREML_FLAG_MAX at the end of the enum definition
// And assign the last COREMLFlag to it
COREML_FLAG_LAST = COREML_FLAG_ONLY_ENABLE_DEVICE_WITH_ANE,
};

#ifdef __cplusplus
extern "C" {
#endif

ORT_EXPORT ORT_API_STATUS(OrtSessionOptionsAppendExecutionProvider_CoreML,
_In_ OrtSessionOptions* options, uint32_t coreml_flags);

#ifdef __cplusplus
}
#endif
19 changes: 19 additions & 0 deletions cpp/external/onnxruntime/include/cpu_provider_factory.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

#include "onnxruntime_c_api.h"

#ifdef __cplusplus
extern "C" {
#endif

/**
* \param use_arena zero: false. non-zero: true.
*/
ORT_EXPORT
ORT_API_STATUS(OrtSessionOptionsAppendExecutionProvider_CPU, _In_ OrtSessionOptions* options, int use_arena)
ORT_ALL_ARGS_NONNULL;

#ifdef __cplusplus
}
#endif
Loading

0 comments on commit 022c5db

Please sign in to comment.