From 456da33f9573ca8d4bafe54baf99d7665ed62ac2 Mon Sep 17 00:00:00 2001 From: Alexandre Lissy Date: Thu, 8 Nov 2018 16:03:59 +0100 Subject: [PATCH] Building native_client on Android $ ndk-build APP_PLATFORM=android-21 APP_BUILD_SCRIPT=$(pwd)/Android.mk NDK_PROJECT_PATH=$(pwd) APP_STL=c++_shared TFDIR=... TARGET_ARCH_ABI=arm64-v8a --- native_client/Android.mk | 14 +++++++++++ native_client/client.cc | 50 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 native_client/Android.mk diff --git a/native_client/Android.mk b/native_client/Android.mk new file mode 100644 index 00000000..d21551fd --- /dev/null +++ b/native_client/Android.mk @@ -0,0 +1,14 @@ +LOCAL_PATH := $(call my-dir) + +include $(CLEAR_VARS) +LOCAL_MODULE := deepspeech-prebuilt +LOCAL_SRC_FILES := $(TFDIR)/bazel-bin/native_client/libdeepspeech.so +include $(PREBUILT_SHARED_LIBRARY) + +include $(CLEAR_VARS) +LOCAL_CPP_EXTENSION := .cc .cxx .cpp +LOCAL_MODULE := deepspeech +LOCAL_SRC_FILES := client.cc +LOCAL_SHARED_LIBRARIES := deepspeech-prebuilt +LOCAL_LDFLAGS := -Wl,--no-as-needed +include $(BUILD_EXECUTABLE) diff --git a/native_client/client.cc b/native_client/client.cc index 5826e875..ca8c9f94 100644 --- a/native_client/client.cc +++ b/native_client/client.cc @@ -6,7 +6,9 @@ #include #include #include +#ifndef __ANDROID__ #include +#endif // __ANDROID__ #include #include @@ -59,6 +61,7 @@ GetAudioBuffer(const char* path) { ds_audio_buffer res = {0}; +#ifndef __ANDROID__ sox_format_t* input = sox_open_read(path, NULL, NULL, NULL); assert(input); @@ -147,6 +150,51 @@ GetAudioBuffer(const char* path) // Close sox handles sox_close(output); sox_close(input); +#endif // __ANDROID__ + +#ifdef __ANDROID__ + // FIXME: Hack and support only 16kHz mono 16-bits PCM + FILE* wave = fopen(path, "r"); + + size_t rv; + + unsigned short audio_format; + fseek(wave, 20, SEEK_SET); rv = fread(&audio_format, 2, 1, wave); + assert(rv == 2); + + unsigned short num_channels; + fseek(wave, 22, SEEK_SET); rv = fread(&num_channels, 2, 1, wave); + assert(rv == 2); + + unsigned int sample_rate; + fseek(wave, 24, SEEK_SET); rv = fread(&sample_rate, 4, 1, wave); + assert(rv == 2); + + unsigned short bits_per_sample; + fseek(wave, 34, SEEK_SET); rv = fread(&bits_per_sample, 2, 1, wave); + assert(rv == 2); + + assert(audio_format == 1); // 1 is PCM + assert(num_channels == 1); // MONO + assert(sample_rate == 16000); // 16000 Hz + assert(bits_per_sample == 16); // 16 bits per sample + + fprintf(stderr, "audio_format=%d\n", audio_format); + fprintf(stderr, "num_channels=%d\n", num_channels); + fprintf(stderr, "sample_rate=%d\n", sample_rate); + fprintf(stderr, "bits_per_sample=%d\n", bits_per_sample); + + fseek(wave, 40, SEEK_SET); rv = fread(&res.buffer_size, 4, 1, wave); + assert(rv == 2); + fprintf(stderr, "res.buffer_size=%ld\n", res.buffer_size); + + fseek(wave, 44, SEEK_SET); + res.buffer = (char*)malloc(sizeof(char) * res.buffer_size); + rv = fread(res.buffer, sizeof(char), res.buffer_size, wave); + assert(rv == res.buffer_size); + + fclose(wave); +#endif // __ANDROID__ #ifdef __APPLE__ res.buffer_size = (size_t)(output->olength * 2); @@ -255,8 +303,10 @@ main(int argc, char **argv) break; } +#ifndef __ANDROID__ // Deinitialise and quit sox_quit(); +#endif // __ANDROID__ DS_DestroyModel(ctx);