- Download Trillbit.zip, which contains library and headers.
- Go inside the Trillbit's mobile app folder. (Please pull the latest changes from master branch)
cd ~/TrillApp.
- Put any updated/new C++ code in
gen-libs/src/main/cpp/folder. - Build and run the app to verify its working correctly.
- Copy headers:
cp distribution/demodulation/include/bpsk/* ~/Downloads/Trillbit/include
- Copy shared libraries:
cd distribution/demodulation/lib/bpsk/for folder in *; do cp $folder/libtrill.so ~/Downloads/Trillbit/lib/$folder/; done
- Zip the Trillbit folder.
Thursday, August 10, 2017
How to update C++ changes in Custom library?
Integration of an Android App with Custom library
- Android App should have "Include C++ Support" checked.
- Unzip
Trillbit.zipinside<App_Name>folder. - Open
app/CMakeLists.txt. Add the following lines to include "Trill" library and headers:
set(trillbit_DIR ../trillbit)
add_library(lib_itpp SHARED IMPORTED)
set_target_properties(lib_itpp PROPERTIES IMPORTED_LOCATION
${trillbit_DIR}/lib/${ANDROID_ABI}/libitpp.so)
add_library(lib_trill SHARED IMPORTED)
set_target_properties(lib_trill PROPERTIES IMPORTED_LOCATION
${trillbit_DIR}/lib/${ANDROID_ABI}/libtrill.so)
add_library( <your_cpp_filename>
SHARED
src/main/cpp/<your_cpp_filename>.cpp )
target_include_directories(<your_cpp_filename> PRIVATE ${trillbit_DIR}/include)
target_link_libraries(<your_cpp_filename>
android
lib_itpp
lib_trill)
4. Open app/build.gradle and add following line after buildTypes:sourceSets {
main {
// let gradle pack the shared library into apk
jniLibs.srcDirs = ['../trillbit/lib']
}
}
5. Open src/main/cpp/<your_cpp_filename>.cpp for JNI implementation. Here is a sample code to show how to find trigger in recorded audio data using Trill library.#include <jni.h>
#include <string>
#include <trigger.h>
extern "C"
jboolean
Java_com_trillbit_trillapp_MainActivity_isTriggerFound(
JNIEnv* env,
jobject, jshortArray data_arr, jint data_arr_len) {
//normalize raw recorded audio data and put into a string
std::string data_str;
for(jint i = 0; i < data_arr_len; i++)
{
char dataInterim[30];
sprintf(dataInterim,"%f, ", data_arr[i]/pow(2,10)); data_str.append(dataInterim);
}
Trigger tg;
return tg.isFound(data_str);
}
5. Open
src/main/java/MainActivity.java. Following sample code shows how Trillbit's library is accessible to Java code through JNI.static {
System.loadLibrary("<your_cpp_filename>");
}
@Override
protected void onCreate(Bundle savedInstanceState) {
....
....
short[] buffer = new short[BUFFER_SIZE];
recorder.read(buffer, 0, BUFFER_SIZE);
....
if (isTriggerFound(buffer, buffer.length)) {
}
.....
}
// Put this definition at the end of the file
public native boolean isTriggerFound(short[] data, int data_len);
Testing of C++ code via Python
- Install boost in your machine as instructed over here -> https://trillbit.slack.com/files/rajanya/F3CEJ0U9E/Interaction_of_C___and_Python_via_Boost_Python
- Clone C++ repository -> https://bitbucket.org/trillbit/bpskdemodulation_c
cd ~/Download/bpskdemodulation- Pull the latest(if any) C++ code changes.
- Open Eclipse CDT -> Open this project -> Build Project. [This generates 'bpsk.so' in Debug folder]
sudo cp /Debug/bpsk.so /Library/Python/2.7/site-packages/- Create
test.pywith following code for demodulation. ( Ensurehellotrill.wavfile is in the same folder as the python file )
import numpy as np
from scipy.io.wavfile import read
from scipy import signal
import bpsk
def convertArrToStr(array):
array_list = array.tolist();
array_str = ",".join(str(x) for x in array_list)
return array_str
mod_signal = read("hellotrill.wav")
data_signal = np.array(mod_signal[1], dtype=float)
data_signal_str = convertArrToStr(data_signal)
data_output = np.fromstring(bpsk.finddata(data_signal_str), dtype=float, sep=',')
data_output_str = convertArrToStr(data_output)
data = np.fromstring(bpsk.demod(data_output_str), dtype=float, sep=',')
print data
FFTW and IT++ with Eclipse And Command Line
Installation of FFTW:
- Download the latest FFTW package from http://www.fftw.org/.
- Unzip and install FFTW:
cd ~/Downloads/fftw-3.3.5./configuremake && make installVerify thatlibfftw3.aandlibfftw3.laare present in the path/usr/local/lib
- Download the latest IT++ package from http://itpp.sourceforge.net/4.3.1/installation.html.
- Unzip and go inside the folder :
cd ~/Downloads/itpp-4.3.1 - Create a build directory :
mkdir build && cd build/ - Open cmake GUI. If cmake not present , visit https://cmake.org/download/
- In cmake, add the following entries:
- Where is the source code:
~/Downloads/itpp-4.3.1 - Where to build the binaries:
~/Downloads/itpp-4.3.1/build
- Where is the source code:
- Click
Configure -> Use default native compilers -> Done - Click
Generate - Return back to terminal. Run
make && make installto build & install. [Locate the shared library at/usr/local/lib/]
Linking IT++ in Eclipse:
- Install the latest Eclipse CDT from https://eclipse.org/cdt/.
- Add the shared library of itpp in the path:
Project -> Properties -> C/C++ General -> Paths and Symbols -> Libraries -> Add -> enter 'itpp' -> OK
- Write (or get it from BitBucket) C++ code which uses IT++ functions. Then
- Build Project
- Run As -> Local C/C++ Application
- If the following error "Launch failed. Binary not found." is encountered, then fix it by:
Project -> Properties -> C/C++ Build -> Settings -> Binary Parsers -> check Mach-O 64 Parser
Linking IT++ in Command Line:
PATH=$PATH:/usr/local/include/:/usr/local/lib/g++ -o <output> <filename>.cc -litpp./<output>
Friday, June 2, 2017
Interaction of C++ and Python via Boost.Python
- Download latest boost package from http://www.boost.org/.
- Unzip and go inside the folder:
cd ~/Downloads/boost_1_62_0 - Run
./bootstrap.sh - Then run
./b2. This will build all the shared libraries in~/Downloads/boost_1_62_0/stage/lib.(we are interested in boost_python library as of now) - Copy headers and libraries to standard path:
sudo cp -r ~/Downloads/boost_1_62_0/boost /usr/local/include/sudo cp ~/Downloads/boost_1_62_0/stage/lib/libboost_python.* /usr/local/lib/
- Append Python wrapper code in C++ so that existing C++ code is not changed at all. [ Caution: Nomenclature of file name and module should be same!!! ]
char const* greet()
{
return "hello, world";
}
#include <boost/python.hpp>
BOOST_PYTHON_MODULE(<module>)
{
using namespace boost::python;
def("greet", greet);
}
g++ -c <filename>.cpp -o <filename>.o -fPIC && g++ -c <other_filename>.cpp -o <other_filename>.o- For following error "/usr/local/include/boost/python/detail/wrap_python.hpp:50:11: fatal error: 'pyconfig.h' file not found", add
-I/usr/include/python2.7/while compiling. g++ -shared <filename>.o <other_filename>.o -lboost_python -lpython2.7 -o <filename>.so( If <filename>.cpp depends on <other_filename>.cpp, put its compiled object as well. Include -litpp if the c++ code depends on IT++ library)- Put the shared library in the standard python module path:
cp <filename>.so /Library/Python/2.7/site-packages/ - Open .py file and call the C++ function: [ module and C++ filename are same ]
import <module>
module.greet();
- For passing an argument from Python to C++, simply do the following:
In C++:
char const* greet(string name)
{
if(name != null) {
return "hello " + name;
} else {
return "hello, world";
}
}
#include <boost/python.hpp>
BOOST_PYTHON_MODULE(<module>)
{
using namespace boost::python;
def("greet", greet);
}
In Python:
import <module>
module.greet("Rajanya");
IT++ and FFTW integration with Android NDK
IT++ and FFTW libraries need to be compiled specific to Application Binary Interface (ABI) for Android NDK.
These are the following ABIs:
Further details can be found over here-> https://developer.android.com/ndk/guides/abis.html
IMPORTANT: Compile FFTW before IT++ as IT++ incorporates FFTW's archived library inside its own shared library.
FFTW compilation for NDK:
IT++ compilation with FFTW for NDK:
How to make IT++ functions available to the C++ code in Android?
These are the following ABIs:
- X86
- X86_64
- armeabi
- mips
- mips64
Further details can be found over here-> https://developer.android.com/ndk/guides/abis.html
IMPORTANT: Compile FFTW before IT++ as IT++ incorporates FFTW's archived library inside its own shared library.
FFTW compilation for NDK:
- Download the latest package from http://www.fftw.org/
- Unzip and go inside the folder:
cd ~/Downloads/fftw-3.3.5 - Set the compilation parameters specific to an ABI. The example is for 'armeabi'.
export NDK_ROOT="/Users/rajanya/Library/Android/sdk/ndk-bundle"export PATH="$NDK_ROOT/toolchains/arm-linux-androideabi-4.9/prebuilt/darwin-x86_64/bin/:$PATH"export SYS_ROOT="$NDK_ROOT/platforms/android-21/arch-arm/"export CC="arm-linux-androideabi-gcc --sysroot=$SYS_ROOT"export LD="arm-linux-androideabi-ld"export AR="arm-linux-androideabi-ar"export RANLIB="arm-linux-androideabi-ranlib"export STRIP="arm-linux-androideabi-strip"export DEST_DIR="$NDK_ROOT/toolchains/arm-linux-androideabi-4.9/prebuilt/darwin-x86_64/user"
- Generate Makefile:
./configure --host=arm-eabi --build=i386-apple-darwin10.8.0 --prefix=$DEST_DIR LIBS="-lc -lgcc" --disable-fortran make && make install
IT++ compilation with FFTW for NDK:
- Download the latest package from http://itpp.sourceforge.net/4.3.1/installation.html
- Unzip and go inside the folder:
cd ~/Downloads/itpp-4.3.1 mkdir build && cd build/- Download https://github.com/taka-no-me/android-cmake to obtain cmake toolchain file.
- Open cmake GUI. If cmake not present , visit https://cmake.org/download/
- In cmake, add the following entries:
- Where is the source code:
~/Downloads/itpp-4.3.1 - Where to build the binaries:
~/Downloads/itpp-4.3.1/build
- Where is the source code:
- Then click '+Add Entry' button to add the following key-value pair:
ANDROID_NDK -> PATH -> ~/Library/Android/sdk/ndk-bundleANDROID_NATIVE_API_LEVEL -> STRING -> 17ANDROID_TOOLCHAIN_NAME -> STRING -> arm-linux-androideabi-4.9
- Click
Configure -> Specify toolchain file for cross-compiling -> ~/Downloads/android-cmake-master/android.toolchain.cmake - Click
Generate. - Return back to terminal. Run
make && make installto build & install the shared library at/usr/local/lib/
How to make IT++ functions available to the C++ code in Android?
- Copy /usr/local/lib/libitpp.so to <app_path>/app/libs/
- Enter the following lines in the CMake file:
- set(distribution_DIR ${CMAKE_SOURCE_DIR}/../../../libs)
- add_library(lib_itpp SHARED IMPORTED)
- set_target_properties(lib_itpp PROPERTIES IMPORTED_LOCATION
${distribution_DIR}/lib/${ANDROID_ABI}/libitpp.so) - target_link_libraries(native-libandroidlib_itpp)
- #include<itpp/itbase.h> in your native-lib.cpp file.
Subscribe to:
Posts (Atom)