└── AudioLatency ├── jni ├── .DS_Store ├── Application.mk ├── pulseusb_jni.h ├── google │ ├── audio_utils │ │ ├── roundup.h │ │ ├── atomic.h │ │ ├── roundup.c │ │ ├── atomic.c │ │ ├── fifo.h │ │ └── fifo.c │ └── roundtrip │ │ ├── sles.h │ │ └── sles.cpp ├── Android.mk ├── opensles_audiolatency.h ├── buffer_lock.h ├── buffer_lock.c ├── opensles.h ├── pulseusb_jni.cpp ├── android_media_opensles_audiolatency_java_api.cpp ├── opensles_audiolatency.cpp └── opensles.c ├── libs ├── x86 │ ├── libpulseusb.so │ └── libaudiolatency_opensles.so ├── mips │ ├── libpulseusb.so │ └── libaudiolatency_opensles.so ├── armeabi │ ├── libpulseusb.so │ └── libaudiolatency_opensles.so ├── mips64 │ ├── libpulseusb.so │ └── libaudiolatency_opensles.so ├── x86_64 │ ├── libpulseusb.so │ └── libaudiolatency_opensles.so ├── arm64-v8a │ ├── libpulseusb.so │ └── libaudiolatency_opensles.so └── armeabi-v7a │ ├── libpulseusb.so │ └── libaudiolatency_opensles.so ├── documentation ├── HeadsetRework.docx ├── AndroidLatencyTestMethodology.docx └── AndroidAudioLatencyTestStepReference.docx ├── res ├── drawable-hdpi │ └── ic_launcher.png ├── drawable-ldpi │ └── ic_launcher.png ├── drawable-mdpi │ └── ic_launcher.png ├── values │ └── strings.xml └── layout │ └── main.xml ├── bin └── AudiolatencytestActivity-debug.apk ├── build.sh ├── project.properties ├── local.properties ├── .classpath ├── proguard-project.txt ├── Android.mk ├── .externalToolBuilders └── JNIbuild.launch ├── AndroidManifest.xml ├── .project ├── proguard.cfg ├── README.md ├── src └── com │ └── intel │ └── android_audio_latency │ ├── audiolatency_openslJNI.java │ ├── PulseUSB.java │ └── AudiolatencytestActivity.java └── build.xml /AudioLatency/jni/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/intel/AudioLatencyTools/master/AudioLatency/jni/.DS_Store -------------------------------------------------------------------------------- /AudioLatency/jni/Application.mk: -------------------------------------------------------------------------------- 1 | APP_ABI := all 2 | NDK_TOOLCHAIN_VERSION := 4.9 3 | #APP_STL := stlport_static 4 | -------------------------------------------------------------------------------- /AudioLatency/libs/x86/libpulseusb.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/intel/AudioLatencyTools/master/AudioLatency/libs/x86/libpulseusb.so -------------------------------------------------------------------------------- /AudioLatency/libs/mips/libpulseusb.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/intel/AudioLatencyTools/master/AudioLatency/libs/mips/libpulseusb.so -------------------------------------------------------------------------------- /AudioLatency/libs/armeabi/libpulseusb.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/intel/AudioLatencyTools/master/AudioLatency/libs/armeabi/libpulseusb.so -------------------------------------------------------------------------------- /AudioLatency/libs/mips64/libpulseusb.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/intel/AudioLatencyTools/master/AudioLatency/libs/mips64/libpulseusb.so -------------------------------------------------------------------------------- /AudioLatency/libs/x86_64/libpulseusb.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/intel/AudioLatencyTools/master/AudioLatency/libs/x86_64/libpulseusb.so -------------------------------------------------------------------------------- /AudioLatency/libs/arm64-v8a/libpulseusb.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/intel/AudioLatencyTools/master/AudioLatency/libs/arm64-v8a/libpulseusb.so -------------------------------------------------------------------------------- /AudioLatency/documentation/HeadsetRework.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/intel/AudioLatencyTools/master/AudioLatency/documentation/HeadsetRework.docx -------------------------------------------------------------------------------- /AudioLatency/libs/armeabi-v7a/libpulseusb.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/intel/AudioLatencyTools/master/AudioLatency/libs/armeabi-v7a/libpulseusb.so -------------------------------------------------------------------------------- /AudioLatency/res/drawable-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/intel/AudioLatencyTools/master/AudioLatency/res/drawable-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /AudioLatency/res/drawable-ldpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/intel/AudioLatencyTools/master/AudioLatency/res/drawable-ldpi/ic_launcher.png -------------------------------------------------------------------------------- /AudioLatency/res/drawable-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/intel/AudioLatencyTools/master/AudioLatency/res/drawable-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /AudioLatency/bin/AudiolatencytestActivity-debug.apk: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/intel/AudioLatencyTools/master/AudioLatency/bin/AudiolatencytestActivity-debug.apk -------------------------------------------------------------------------------- /AudioLatency/libs/mips/libaudiolatency_opensles.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/intel/AudioLatencyTools/master/AudioLatency/libs/mips/libaudiolatency_opensles.so -------------------------------------------------------------------------------- /AudioLatency/libs/x86/libaudiolatency_opensles.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/intel/AudioLatencyTools/master/AudioLatency/libs/x86/libaudiolatency_opensles.so -------------------------------------------------------------------------------- /AudioLatency/libs/armeabi/libaudiolatency_opensles.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/intel/AudioLatencyTools/master/AudioLatency/libs/armeabi/libaudiolatency_opensles.so -------------------------------------------------------------------------------- /AudioLatency/libs/mips64/libaudiolatency_opensles.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/intel/AudioLatencyTools/master/AudioLatency/libs/mips64/libaudiolatency_opensles.so -------------------------------------------------------------------------------- /AudioLatency/libs/x86_64/libaudiolatency_opensles.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/intel/AudioLatencyTools/master/AudioLatency/libs/x86_64/libaudiolatency_opensles.so -------------------------------------------------------------------------------- /AudioLatency/libs/arm64-v8a/libaudiolatency_opensles.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/intel/AudioLatencyTools/master/AudioLatency/libs/arm64-v8a/libaudiolatency_opensles.so -------------------------------------------------------------------------------- /AudioLatency/libs/armeabi-v7a/libaudiolatency_opensles.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/intel/AudioLatencyTools/master/AudioLatency/libs/armeabi-v7a/libaudiolatency_opensles.so -------------------------------------------------------------------------------- /AudioLatency/documentation/AndroidLatencyTestMethodology.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/intel/AudioLatencyTools/master/AudioLatency/documentation/AndroidLatencyTestMethodology.docx -------------------------------------------------------------------------------- /AudioLatency/documentation/AndroidAudioLatencyTestStepReference.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/intel/AudioLatencyTools/master/AudioLatency/documentation/AndroidAudioLatencyTestStepReference.docx -------------------------------------------------------------------------------- /AudioLatency/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | audio IO test 5 | OpenSL audio Latency test 6 | 7 | 8 | -------------------------------------------------------------------------------- /AudioLatency/build.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # This is the build.sh for the ndk build 4 | # ndk-build build out jni libs in libs directory 5 | # ant debug build out debug apk in bin directory 6 | 7 | ndk-build 8 | ant clean 9 | ant debug 10 | 11 | -------------------------------------------------------------------------------- /AudioLatency/project.properties: -------------------------------------------------------------------------------- 1 | # This file is automatically generated by Android Tools. 2 | # Do not modify this file -- YOUR CHANGES WILL BE ERASED! 3 | # 4 | # This file must be checked in Version Control Systems. 5 | # 6 | # To customize properties used by the Ant build system use, 7 | # "ant.properties", and override values to adapt the script to your 8 | # project structure. 9 | 10 | # Project target. 11 | target=android-19 12 | -------------------------------------------------------------------------------- /AudioLatency/local.properties: -------------------------------------------------------------------------------- 1 | # This file is automatically generated by Android Tools. 2 | # Do not modify this file -- YOUR CHANGES WILL BE ERASED! 3 | # 4 | # This file must *NOT* be checked into Version Control Systems, 5 | # as it contains information specific to your local configuration. 6 | 7 | # location of the SDK. This is only used by Ant 8 | # For customization when using a Version Control System, please read the 9 | # header note. 10 | -------------------------------------------------------------------------------- /AudioLatency/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /AudioLatency/jni/pulseusb_jni.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Intel Corporation 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | 18 | #ifndef PULSEUSB_JNI_H 19 | #define PULSEUSB_JNI_H 20 | #include "jni.h" 21 | //#include "JNIHelp.h" 22 | void native_setDtr(int dtr); 23 | #endif 24 | -------------------------------------------------------------------------------- /AudioLatency/proguard-project.txt: -------------------------------------------------------------------------------- 1 | # To enable ProGuard in your project, edit project.properties 2 | # to define the proguard.config property as described in that file. 3 | # 4 | # Add project specific ProGuard rules here. 5 | # By default, the flags in this file are appended to flags specified 6 | # in ${sdk.dir}/tools/proguard/proguard-android.txt 7 | # You can edit the include path and order by changing the ProGuard 8 | # include property in project.properties. 9 | # 10 | # For more details, see 11 | # http://developer.android.com/guide/developing/tools/proguard.html 12 | 13 | # Add any project specific keep options here: 14 | 15 | # If your project uses WebView with JS, uncomment the following 16 | # and specify the fully qualified class name to the JavaScript interface 17 | # class: 18 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 19 | # public *; 20 | #} 21 | -------------------------------------------------------------------------------- /AudioLatency/Android.mk: -------------------------------------------------------------------------------- 1 | # 2 | # Use of this source code is governed by a BSD-style license 3 | # that can be found in the LICENSE file in the root of the source 4 | # tree. An additional intellectual property rights grant can be found 5 | # in the file PATENTS. All contributing project authors may 6 | # be found in the AUTHORS file in the root of the source tree. 7 | 8 | LOCAL_PATH:= $(call my-dir) 9 | 10 | include $(CLEAR_VARS) 11 | 12 | LOCAL_MODULE_TAGS := tests 13 | 14 | LOCAL_SRC_FILES := \ 15 | src/com/intel/android_audio_latency/AudiolatencytestActivity.java \ 16 | src/com/intel/android_audio_latency/audiolatency_openslJNI.java \ 17 | src/com/intel/android_audio_latency/PulseUSB.java 18 | 19 | LOCAL_PACKAGE_NAME := audio-latency 20 | LOCAL_CERTIFICATE := platform 21 | 22 | LOCAL_JNI_SHARED_LIBRARIES := libopensles_audiolatency libpulseusb 23 | LOCAL_LDLIBS := -llog 24 | 25 | include $(BUILD_PACKAGE) 26 | include $(call all-makefiles-under,$(LOCAL_PATH)) 27 | -------------------------------------------------------------------------------- /AudioLatency/.externalToolBuilders/JNIbuild.launch: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /AudioLatency/jni/google/audio_utils/roundup.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2012 The Android Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #ifndef ANDROID_AUDIO_ROUNDUP_H 18 | #define ANDROID_AUDIO_ROUNDUP_H 19 | 20 | #ifdef __cplusplus 21 | extern "C" { 22 | #endif 23 | 24 | // Round up to the next highest power of 2 25 | unsigned roundup(unsigned v); 26 | 27 | #ifdef __cplusplus 28 | } 29 | #endif 30 | 31 | #endif // ANDROID_AUDIO_ROUNDUP_H 32 | -------------------------------------------------------------------------------- /AudioLatency/jni/google/audio_utils/atomic.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 The Android Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #ifndef ANDROID_AUDIO_ATOMIC_H 18 | #define ANDROID_AUDIO_ATOMIC_H 19 | 20 | #include 21 | 22 | #ifdef __cplusplus 23 | extern "C" { 24 | #endif 25 | 26 | int32_t android_atomic_acquire_load(volatile const int32_t* addr); 27 | void android_atomic_release_store(int32_t value, volatile int32_t* addr); 28 | 29 | #ifdef __cplusplus 30 | } 31 | #endif 32 | 33 | #endif // ANDROID_AUDIO_ATOMIC_H 34 | -------------------------------------------------------------------------------- /AudioLatency/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 15 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /AudioLatency/jni/google/audio_utils/roundup.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2012 The Android Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #include "roundup.h" 18 | 19 | unsigned roundup(unsigned v) 20 | { 21 | // __builtin_clz is undefined for zero input 22 | if (v == 0) { 23 | v = 1; 24 | } 25 | int lz = __builtin_clz((int) v); 26 | unsigned rounded = ((unsigned) 0x80000000) >> lz; 27 | // 0x800000001 and higher are actually rounded _down_ to prevent overflow 28 | if (v > rounded && lz > 0) { 29 | rounded <<= 1; 30 | } 31 | return rounded; 32 | } 33 | -------------------------------------------------------------------------------- /AudioLatency/jni/google/audio_utils/atomic.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 The Android Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #include "atomic.h" 18 | 19 | #include 20 | 21 | int32_t android_atomic_acquire_load(volatile const int32_t* addr) 22 | { 23 | volatile atomic_int_least32_t* a = (volatile atomic_int_least32_t*)addr; 24 | return atomic_load_explicit(a, memory_order_acquire); 25 | } 26 | 27 | void android_atomic_release_store(int32_t value, volatile int32_t* addr) 28 | { 29 | volatile atomic_int_least32_t* a = (volatile atomic_int_least32_t*)addr; 30 | atomic_store_explicit(a, value, memory_order_release); 31 | } 32 | -------------------------------------------------------------------------------- /AudioLatency/jni/Android.mk: -------------------------------------------------------------------------------- 1 | LOCAL_PATH := $(call my-dir) 2 | 3 | include $(CLEAR_VARS) 4 | 5 | LOCAL_MODULE := libpulseusb 6 | LOCAL_SRC_FILES := pulseusb_jni.cpp 7 | LOCAL_C_INCLUDES:= \ 8 | $(call include-path-for, wilhelm) \ 9 | $(LOCAL_PATH) \ 10 | 11 | LOCAL_LDLIBS := -llog 12 | 13 | include $(BUILD_SHARED_LIBRARY) 14 | 15 | include $(CLEAR_VARS) 16 | 17 | LOCAL_MODULE := libaudiolatency_opensles 18 | LOCAL_C_INCLUDES:= \ 19 | $(call include-path-for, wilhelm) \ 20 | $(LOCAL_PATH) \ 21 | $(LOCAL_PATH)/google \ 22 | LOCAL_CFLAGS := -O3 -DSTDC_HEADERS 23 | LOCAL_CPPFLAGS :=$(LOCAL_CFLAGS) 24 | LOCAL_LDFLAGS := -Wl,--hash-style=sysv 25 | 26 | ### 27 | 28 | LOCAL_SRC_FILES := opensles_audiolatency.cpp \ 29 | opensles.c \ 30 | android_media_opensles_audiolatency_java_api.cpp \ 31 | buffer_lock.c \ 32 | google/roundtrip/sles.cpp \ 33 | google/audio_utils/atomic.c \ 34 | google/audio_utils/fifo.c \ 35 | google/audio_utils/roundup.c 36 | 37 | LOCAL_SHARED_LIBRARIES := \ 38 | libutils \ 39 | libcutils \ 40 | libpulseusb \ 41 | libstlport 42 | 43 | LOCAL_LDLIBS := -llog \ 44 | -lOpenSLES \ 45 | 46 | 47 | include $(BUILD_SHARED_LIBRARY) 48 | 49 | 50 | -------------------------------------------------------------------------------- /AudioLatency/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | audiotest 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.ui.externaltools.ExternalToolBuilder 10 | full,incremental, 11 | 12 | 13 | LaunchConfigHandle 14 | <project>/.externalToolBuilders/JNIbuild.launch 15 | 16 | 17 | 18 | 19 | com.android.ide.eclipse.adt.ResourceManagerBuilder 20 | 21 | 22 | 23 | 24 | com.android.ide.eclipse.adt.PreCompilerBuilder 25 | 26 | 27 | 28 | 29 | org.eclipse.jdt.core.javabuilder 30 | 31 | 32 | 33 | 34 | com.android.ide.eclipse.adt.ApkBuilder 35 | 36 | 37 | 38 | 39 | 40 | com.android.ide.eclipse.adt.AndroidNature 41 | org.eclipse.jdt.core.javanature 42 | 43 | 44 | -------------------------------------------------------------------------------- /AudioLatency/proguard.cfg: -------------------------------------------------------------------------------- 1 | -optimizationpasses 5 2 | -dontusemixedcaseclassnames 3 | -dontskipnonpubliclibraryclasses 4 | -dontpreverify 5 | -verbose 6 | -optimizations !code/simplification/arithmetic,!field/*,!class/merging/* 7 | 8 | -keep public class * extends android.app.Activity 9 | -keep public class * extends android.app.Application 10 | -keep public class * extends android.app.Service 11 | -keep public class * extends android.content.BroadcastReceiver 12 | -keep public class * extends android.content.ContentProvider 13 | -keep public class * extends android.app.backup.BackupAgentHelper 14 | -keep public class * extends android.preference.Preference 15 | -keep public class com.android.vending.licensing.ILicensingService 16 | 17 | -keepclasseswithmembernames class * { 18 | native ; 19 | } 20 | 21 | -keepclasseswithmembers class * { 22 | public (android.content.Context, android.util.AttributeSet); 23 | } 24 | 25 | -keepclasseswithmembers class * { 26 | public (android.content.Context, android.util.AttributeSet, int); 27 | } 28 | 29 | -keepclassmembers class * extends android.app.Activity { 30 | public void *(android.view.View); 31 | } 32 | 33 | -keepclassmembers enum * { 34 | public static **[] values(); 35 | public static ** valueOf(java.lang.String); 36 | } 37 | 38 | -keep class * implements android.os.Parcelable { 39 | public static final android.os.Parcelable$Creator *; 40 | } 41 | -------------------------------------------------------------------------------- /AudioLatency/jni/opensles_audiolatency.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Intel Corporation 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | 18 | #ifndef OPENSLES_AUDIOLATENCY 19 | #define OPENSLES_AUDIOLATENCY 20 | #ifdef __cplusplus 21 | extern "C" { 22 | #endif 23 | void start_roundtrip(); 24 | void stop_roundtrip(); 25 | void start_roundtrip_google(); 26 | void stop_roundtrip_google(); 27 | void start_input(); 28 | void stop_input(); 29 | void start_output(); 30 | void stop_output(); 31 | void start_continuous_output(); 32 | void stop_continuous_output(); 33 | void start_continuous_input(); 34 | void stop_continuous_input(); 35 | void showCurrentTime(char * message); 36 | void native_setDtr_sles(int value); 37 | void set_audio_parameters(int frames, int rate ); 38 | void start_usb2gpio_calibration(); 39 | void stop_usb2gpio_calibration(); 40 | #ifdef __cplusplus 41 | }; 42 | #endif 43 | #endif 44 | 45 | -------------------------------------------------------------------------------- /AudioLatency/jni/buffer_lock.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Intel Corporation 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | 18 | #ifndef BUFFER_LOCK_OPENSL 19 | #define BUFFER_LOCK_OPENSL 20 | #include 21 | #include 22 | 23 | #include 24 | #define LOG_TAG "OpenSL-AudioLatency" 25 | #define LOGV(fmt, args...) __android_log_print(ANDROID_LOG_VERBOSE, LOG_TAG, fmt, ##args) 26 | #define LOGI(fmt, args...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, fmt, ##args) 27 | #define LOGD(fmt, args...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, fmt, ##args) 28 | #define LOGE(fmt, args...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, fmt, ##args) 29 | 30 | typedef struct { 31 | pthread_mutex_t mutex; 32 | pthread_cond_t cond; 33 | unsigned int symbol; 34 | } bufferLock; 35 | 36 | void* createBufferLock(void); 37 | void waitBufferLock(void *lock); 38 | void signalBufferLock(void *lock); 39 | void destroyBufferLock(void *lock); 40 | 41 | #endif 42 | -------------------------------------------------------------------------------- /AudioLatency/README.md: -------------------------------------------------------------------------------- 1 | # this is the README for android-audiolatencytest 2 | 3 | 5 cases are supported: 4 | 1. roundtrip 5 | 2. cold output 6 | 3. cold input 7 | 4. continuous output 8 | 5. continuous input 9 | 10 | # before start to build, please confirm if you have Android SDK and NDK in your machine. 11 | Build Instruction 12 | This project can be built by only command line with steps as following: 13 | 1. Preparation for the enviroment (ubuntu as example) 14 | 15 | a) download android ndk 16 | https://developer.android.com/ndk/downloads/index.html#Downloads 17 | 18 | b) download android sdk 19 | https://developer.android.com/tools/sdk/tools-notes.html 20 | 21 | c) export ndk and sdk to PATH in ~/.bashrc 22 | export ANDROID_NDK=/home/yijin/tools/Android_tools/android-ndk-r10d 23 | PATH=$ANDROID_NDK:$PATH 24 | export ANDROID_SDK=/home/yijin/tools/Android_tools/android-sdk-linux/tools/ 25 | PATH=$ANDROID_SDK:$PATH 26 | export ANDROID_HOME=/home/yijin/tools/Android_tools/android-sdk-linux/ 27 | PATH=$ANDROID_HOME:$PATH 28 | 29 | d) install android sdk, built tools and platform tools 30 | https://developer.android.com/sdk/installing/adding-packages.html 31 | 32 | e) install ant 33 | sudo apt-get install ant 34 | 35 | 36 | 2. Build project 37 | #./build.sh // ndk-build & ant debug 38 | 39 | Tips for build problems: 40 | 1. Unable to resolve target 'android-**' 41 | a) use command "android list targets" to get your sdk target version 42 | b) use the current target version in file project.properties 43 | 2. Cannot run program "**/${aapt}": error=2, No such file or directory 44 | a) use your build tools path in build.xml : 45 | 46 | 47 | You can also use ndk-build to built the native libs and then use eclipse to build the apk. 48 | -------------------------------------------------------------------------------- /AudioLatency/jni/buffer_lock.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Intel Corporation 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #include "buffer_lock.h" 18 | 19 | //---------------------------------------------------------------------- 20 | // buffer Locks 21 | void* createBufferLock(void) 22 | { 23 | bufferLock *pLock; 24 | pLock = (bufferLock*) malloc(sizeof(bufferLock)); 25 | if (pLock == NULL) 26 | return NULL; 27 | memset(pLock, 0, sizeof(bufferLock)); 28 | 29 | if (pthread_mutex_init(&(pLock->mutex), NULL) != 0) { 30 | pthread_mutex_destroy(&(pLock->mutex)); 31 | free((void*) pLock); 32 | return NULL; 33 | } 34 | if (pthread_cond_init(&(pLock->cond), NULL) != 0) { 35 | pthread_mutex_destroy(&(pLock->mutex)); 36 | free((void*) pLock); 37 | return NULL; 38 | } 39 | pLock->symbol = 0; 40 | 41 | return pLock; 42 | } 43 | 44 | void waitBufferLock(void *lock) 45 | { 46 | bufferLock *pLock = (bufferLock*) lock; 47 | pthread_mutex_lock(&(pLock->mutex)); 48 | while (!pLock->symbol) { 49 | pthread_cond_wait(&(pLock->cond), &(pLock->mutex)); 50 | } 51 | pLock->symbol = 0; 52 | pthread_mutex_unlock(&(pLock->mutex)); 53 | } 54 | 55 | void signalBufferLock(void *lock) 56 | { 57 | bufferLock *pLock = (bufferLock*) lock; 58 | pthread_mutex_lock(&(pLock->mutex)); 59 | pLock->symbol = 1; 60 | pthread_cond_signal(&(pLock->cond)); 61 | pthread_mutex_unlock(&(pLock->mutex)); 62 | } 63 | 64 | void destroyBufferLock(void *lock) 65 | { 66 | bufferLock *pLock = (bufferLock*) lock; 67 | if (pLock == NULL) 68 | return; 69 | signalBufferLock(pLock); 70 | pthread_cond_destroy(&(pLock->cond)); 71 | pthread_mutex_destroy(&(pLock->mutex)); 72 | free(pLock); 73 | } 74 | -------------------------------------------------------------------------------- /AudioLatency/jni/google/roundtrip/sles.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 The Android Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #include 18 | #include 19 | #include 20 | #include 21 | 22 | #ifndef _Included_org_drrickorang_loopback_sles 23 | #define _Included_org_drrickorang_loopback_sles 24 | 25 | //struct audio_utils_fifo; 26 | #define SLES_PRINTF(...) __android_log_print(ANDROID_LOG_INFO, "sles_jni", __VA_ARGS__); 27 | 28 | enum { 29 | SLES_SUCCESS = 0, 30 | SLES_FAIL = 1, 31 | } SLES_STATUS_ENUM; 32 | #ifdef __cplusplus 33 | extern "C" { 34 | #endif 35 | #include 36 | 37 | typedef struct { 38 | SLuint32 rxBufCount; // -r# 39 | SLuint32 txBufCount; // -t# 40 | SLuint32 bufSizeInFrames; // -f# 41 | SLuint32 channels; // -c# 42 | SLuint32 sampleRate; // -s# 43 | SLuint32 exitAfterSeconds; // -e# 44 | SLuint32 freeBufCount; // calculated 45 | SLuint32 bufSizeInBytes; // calculated 46 | int injectImpulse; // -i#i 47 | 48 | // Storage area for the buffer queues 49 | char **rxBuffers; 50 | char **txBuffers; 51 | char **freeBuffers; 52 | 53 | // Buffer indices 54 | SLuint32 rxFront; // oldest recording 55 | SLuint32 rxRear; // next to be recorded 56 | SLuint32 txFront; // oldest playing 57 | SLuint32 txRear; // next to be played 58 | SLuint32 freeFront; // oldest free 59 | SLuint32 freeRear; // next to be freed 60 | 61 | struct audio_utils_fifo fifo; //(*) 62 | struct audio_utils_fifo fifo2; 63 | short *fifo2Buffer; 64 | short *fifoBuffer; 65 | SLAndroidSimpleBufferQueueItf recorderBufferQueue; 66 | SLBufferQueueItf playerBufferQueue; 67 | 68 | pthread_mutex_t mutex;// = PTHREAD_MUTEX_INITIALIZER; 69 | 70 | //other things that belong here 71 | SLObjectItf playerObject; 72 | SLObjectItf recorderObject; 73 | SLObjectItf outputmixObject; 74 | SLObjectItf engineObject; 75 | } sles_data; 76 | 77 | /*enum { 78 | SLES_SUCCESS = 0, 79 | SLES_FAIL = 1, 80 | } SLES_STATUS_ENUM; 81 | */ 82 | int slesInit( sles_data ** ppSles, int samplingRate, int frameCount, int micSource); 83 | //note the double pointer to properly free the memory of the structure 84 | int slesDestroy( sles_data ** ppSles); 85 | 86 | 87 | ///full 88 | int slesFull(sles_data *pSles); 89 | 90 | int slesCreateServer(sles_data *pSles, int samplingRate, int frameCount, int micSource); 91 | int slesProcessNext(sles_data *pSles, double *pSamples, long maxSamples); 92 | int slesDestroyServer(sles_data *pSles); 93 | 94 | #ifdef __cplusplus 95 | } 96 | #endif 97 | #endif //_Included_org_drrickorang_loopback_sles 98 | -------------------------------------------------------------------------------- /AudioLatency/jni/opensles.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Intel Corporation 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | 18 | #ifndef OPENSLES_BASIC 19 | #define OPENSLES_BASIC 20 | 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include "buffer_lock.h" 26 | 27 | #include 28 | #define LOG_TAG "OpenSL-AudioLatency" 29 | #define LOGV(fmt, args...) __android_log_print(ANDROID_LOG_VERBOSE, LOG_TAG, fmt, ##args) 30 | #define LOGI(fmt, args...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, fmt, ##args) 31 | #define LOGD(fmt, args...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, fmt, ##args) 32 | #define LOGE(fmt, args...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, fmt, ##args) 33 | 34 | #ifdef __cplusplus 35 | extern "C" { 36 | #endif 37 | 38 | typedef struct { 39 | // buffers 40 | short *outputBuffer[2]; 41 | short *inputBuffer[2]; 42 | 43 | // buffer indexes 44 | int currentInPositionIndex; 45 | int currentOutPositionIndex; 46 | int currentInBufferIndex; 47 | int currentOutBufferIndex; 48 | 49 | 50 | // locks 51 | void* inBufferLock; 52 | void* outBufferLock; 53 | 54 | // audio parameters 55 | int sampleRate; 56 | int inBufSamples; 57 | int outBufSamples; 58 | int inchannels; 59 | int outchannels; 60 | 61 | // engine interfaces 62 | SLObjectItf engineObject; 63 | SLEngineItf engineEngine; 64 | 65 | // output mix interfaces 66 | SLObjectItf outputMixObject; 67 | SLObjectItf playerObject; 68 | SLAndroidSimpleBufferQueueItf playerBufferQueue; 69 | 70 | // recorder interfaces 71 | SLObjectItf recorderObject; 72 | SLAndroidSimpleBufferQueueItf recorderBufferQueue; 73 | 74 | 75 | } opensles_s; 76 | 77 | //Open both audio input and output device 78 | opensles_s* initAudioInOutput(int sr, int inchannels, int outchannels, int bufferframes); 79 | void destroyAudioInOutput(opensles_s *p); 80 | 81 | //Open audio input device 82 | opensles_s* initAudioInput(int sr, int inchannels, int bufferframes); 83 | void destroyAudioInput(opensles_s *p); 84 | 85 | //Open audio outputput device 86 | opensles_s* initAudioOutput(int sr, int outchannels, int bufferframes); 87 | void destroyAudioOutput(opensles_s *p); 88 | 89 | // read buffer 90 | int readInput(opensles_s *p, short *buffer,int size); 91 | int get_AudioInput_bufferSize(opensles_s *p); 92 | 93 | // write output buffer 94 | int writeOutput(opensles_s *p, short *buffer,int size); 95 | int writeOutputContinuous(opensles_s *p, short *buffer,int size); 96 | int get_AudioOut_bufferSize(const opensles_s *p); 97 | void setOutputStatus(int value); 98 | 99 | #ifdef __cplusplus 100 | }; 101 | #endif 102 | 103 | #endif // #ifndef OPENSL_BASIC 104 | -------------------------------------------------------------------------------- /AudioLatency/jni/pulseusb_jni.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Intel Corporation 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #include 18 | //#include 19 | #include "jni.h" 20 | //#include "JNIHelp.h" 21 | #include "pulseusb_jni.h" 22 | #include 23 | #include 24 | 25 | #define LOG_TAG "PulseUSB_JNI" 26 | 27 | #define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__) 28 | #define LOGW(...) __android_log_print(ANDROID_LOG_WARN, LOG_TAG, __VA_ARGS__) 29 | #define LOGE(...) __android_log_print(ANDROID_LOG_ERRO,LOG_TAG,__VA_ARGS__) 30 | 31 | #ifndef NELEM 32 | #define NELEM(x) ((int) (sizeof(x) / sizeof((x)[0]))) 33 | #endif 34 | 35 | static const char* classname = "com/intel/android_audio_latency/PulseUSB"; 36 | static jobject gUsb = NULL; 37 | static jmethodID gMethodId; 38 | static JNIEnv* gEnv; 39 | static JavaVM* jVm; 40 | static void 41 | native_init(JNIEnv *env, jobject thiz, jobject usb) 42 | { 43 | LOGD("native init\n"); 44 | gUsb = env->NewGlobalRef(usb); 45 | jclass clazz = env->FindClass(classname); 46 | if (clazz == NULL) 47 | LOGD("Class: %s is not found.\n", classname); 48 | 49 | gMethodId = env->GetMethodID(clazz, "setDtr", "(I)V"); 50 | if (gMethodId == NULL) 51 | LOGD("Method: setDtr is not found.\n"); 52 | 53 | env->CallVoidMethod(thiz, gMethodId, 0); 54 | 55 | } 56 | 57 | void 58 | native_setDtr(int dtr) 59 | { 60 | LOGD("native setDtr: %d\n", dtr); 61 | 62 | if (gUsb == NULL) { 63 | LOGW("gUsb is null\n"); 64 | return; 65 | } 66 | 67 | if (gMethodId == NULL) { 68 | LOGW("gMethodId is null\n"); 69 | return; 70 | } 71 | JNIEnv* env; 72 | jVm->AttachCurrentThread(&env, NULL); 73 | if(env == NULL) { 74 | LOGW("env is null\n"); 75 | return; 76 | } 77 | env->CallVoidMethod(gUsb, gMethodId, dtr); 78 | //jVm->DetachCurrentThread(); 79 | } 80 | 81 | 82 | static JNINativeMethod methods[] = { 83 | {"native_init", "(Ljava/lang/Object;)V", (void *)native_init} 84 | }; 85 | 86 | int register_method(JNIEnv* env) { 87 | jclass clazz; 88 | clazz = env->FindClass(classname); 89 | return env->RegisterNatives( 90 | clazz, methods, NELEM(methods)); 91 | } 92 | 93 | jint JNI_OnLoad(JavaVM* vm, void* reserved) { 94 | JNIEnv* env = NULL; 95 | jint result = -1; 96 | 97 | if (!vm) { 98 | LOGD("ERROR: vm is NULL"); 99 | goto fail; 100 | } 101 | jVm = vm; 102 | if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) { 103 | LOGD("ERROR: GetEnv failed\n"); 104 | goto fail; 105 | } 106 | if (register_method(env) < 0) { 107 | LOGD("ERROR: failed to registe pulseusb_jni method\n"); 108 | goto fail; 109 | } 110 | 111 | LOGD("jni onload success"); 112 | result = JNI_VERSION_1_4; 113 | 114 | fail: 115 | return result; 116 | 117 | } 118 | 119 | 120 | -------------------------------------------------------------------------------- /AudioLatency/src/com/intel/android_audio_latency/audiolatency_openslJNI.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Intel Corporation 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.intel.android_audio_latency; 18 | 19 | public class audiolatency_openslJNI { 20 | 21 | static { 22 | try { 23 | java.lang.System.loadLibrary("audiolatency_opensles"); 24 | } catch (UnsatisfiedLinkError e) { 25 | java.lang.System.err.println("native code library failed to load.\n" + e); 26 | java.lang.System.exit(1); 27 | } 28 | } 29 | 30 | public static void start_roundtrip_process() { 31 | start_roundtrip(); 32 | } 33 | 34 | public static void stop_roundtrip_process() { 35 | stop_roundtrip(); 36 | } 37 | 38 | public static void start_roundtrip_google_process() { 39 | start_roundtrip_google(); 40 | } 41 | 42 | public static void stop_roundtrip_google_process() { 43 | stop_roundtrip_google(); 44 | } 45 | 46 | public static void start_input_process() { 47 | start_input(); 48 | } 49 | 50 | public static void stop_input_process() { 51 | stop_input(); 52 | } 53 | 54 | public static void start_output_process() { 55 | start_output(); 56 | } 57 | 58 | public static void stop_output_process() { 59 | stop_output(); 60 | } 61 | 62 | 63 | public static void start_continuous_output_process() { 64 | start_continuous_output(); 65 | } 66 | 67 | public static void stop_continuous_output_process() { 68 | stop_continuous_output(); 69 | } 70 | 71 | public static void start_continuous_input_process() { 72 | start_continuous_input(); 73 | } 74 | 75 | public static void stop_continuous_input_process() { 76 | stop_continuous_input(); 77 | } 78 | 79 | public static void set_audio_parameters_java(int bufferFrames, int sampleRate) { 80 | set_audio_parameters(bufferFrames, sampleRate); 81 | } 82 | 83 | public static void start_usb2gpio_calibration_process() { 84 | start_usb2gpio_calibration(); 85 | } 86 | 87 | public static void stop_usb2gpio_calibration_process() { 88 | stop_usb2gpio_calibration(); 89 | } 90 | 91 | public final static native void start_roundtrip(); 92 | public final static native void stop_roundtrip(); 93 | public final static native void start_roundtrip_google(); 94 | public final static native void stop_roundtrip_google(); 95 | public final static native void start_input(); 96 | public final static native void stop_input(); 97 | public final static native void start_output(); 98 | public final static native void stop_output(); 99 | public final static native void start_continuous_output(); 100 | public final static native void stop_continuous_output(); 101 | public final static native void start_continuous_input(); 102 | public final static native void stop_continuous_input(); 103 | public final static native void set_audio_parameters(int bufferFrames, int sampleRate); 104 | public final static native void start_usb2gpio_calibration(); 105 | public final static native void stop_usb2gpio_calibration(); 106 | 107 | 108 | } 109 | -------------------------------------------------------------------------------- /AudioLatency/jni/google/audio_utils/fifo.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 The Android Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #ifndef ANDROID_AUDIO_FIFO_H 18 | #define ANDROID_AUDIO_FIFO_H 19 | 20 | #include 21 | 22 | // FIXME use atomic_int_least32_t and new atomic operations instead of legacy Android ones 23 | // #include 24 | 25 | #ifdef __cplusplus 26 | extern "C" { 27 | #endif 28 | 29 | // Single writer, single reader non-blocking FIFO. 30 | // Writer and reader must be in same process. 31 | 32 | // No user-serviceable parts within. 33 | struct audio_utils_fifo { 34 | // These fields are const after initialization 35 | size_t mFrameCount; // max number of significant frames to be stored in the FIFO > 0 36 | size_t mFrameCountP2; // roundup(mFrameCount) 37 | size_t mFudgeFactor; // mFrameCountP2 - mFrameCount, the number of "wasted" frames after 38 | // the end of mBuffer. Only the indices are wasted, not any memory. 39 | size_t mFrameSize; // size of each frame in bytes 40 | void *mBuffer; // pointer to caller-allocated buffer of size mFrameCount frames 41 | 42 | volatile int32_t mFront; // frame index of first frame slot available to read, or read index 43 | volatile int32_t mRear; // frame index of next frame slot available to write, or write index 44 | }; 45 | 46 | // Initialize a FIFO object. 47 | // Input parameters: 48 | // fifo Pointer to the FIFO object. 49 | // frameCount Max number of significant frames to be stored in the FIFO > 0. 50 | // If writes and reads always use the same count, and that count is a divisor of 51 | // frameCount, then the writes and reads will never do a partial transfer. 52 | // frameSize Size of each frame in bytes. 53 | // buffer Pointer to a caller-allocated buffer of frameCount frames. 54 | void audio_utils_fifo_init(struct audio_utils_fifo *fifo, size_t frameCount, size_t frameSize, 55 | void *buffer); 56 | 57 | // De-initialize a FIFO object. 58 | // Input parameters: 59 | // fifo Pointer to the FIFO object. 60 | void audio_utils_fifo_deinit(struct audio_utils_fifo *fifo); 61 | 62 | // Write to FIFO. 63 | // Input parameters: 64 | // fifo Pointer to the FIFO object. 65 | // buffer Pointer to source buffer containing 'count' frames of data. 66 | // Returns actual number of frames written <= count. 67 | // The actual transfer count may be zero if the FIFO is full, 68 | // or partial if the FIFO was almost full. 69 | // A negative return value indicates an error. Currently there are no errors defined. 70 | ssize_t audio_utils_fifo_write(struct audio_utils_fifo *fifo, const void *buffer, size_t count); 71 | 72 | // Read from FIFO. 73 | // Input parameters: 74 | // fifo Pointer to the FIFO object. 75 | // buffer Pointer to destination buffer to be filled with up to 'count' frames of data. 76 | // Returns actual number of frames read <= count. 77 | // The actual transfer count may be zero if the FIFO is empty, 78 | // or partial if the FIFO was almost empty. 79 | // A negative return value indicates an error. Currently there are no errors defined. 80 | ssize_t audio_utils_fifo_read(struct audio_utils_fifo *fifo, void *buffer, size_t count); 81 | 82 | #ifdef __cplusplus 83 | } 84 | #endif 85 | 86 | #endif // !ANDROID_AUDIO_FIFO_H 87 | -------------------------------------------------------------------------------- /AudioLatency/build.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 7 | 8 | 9 | 29 | 30 | 31 | 35 | 36 | 37 | 38 | 39 | 40 | 49 | 50 | 51 | 52 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 78 | 79 | 80 | 98 | 99 | 100 | 101 | 102 | -------------------------------------------------------------------------------- /AudioLatency/jni/google/audio_utils/fifo.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 The Android Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | //#define LOG_NDEBUG 0 18 | #define LOG_TAG "audio_utils_fifo" 19 | 20 | #include 21 | #include 22 | #include "fifo.h" 23 | #include "roundup.h" 24 | #include "atomic.h" 25 | //#include 26 | #define ALOG_ASSERT(exp) 27 | 28 | void audio_utils_fifo_init(struct audio_utils_fifo *fifo, size_t frameCount, size_t frameSize, 29 | void *buffer) 30 | { 31 | // We would need a 64-bit roundup to support larger frameCount. 32 | ALOG_ASSERT(fifo != NULL && frameCount > 0 && frameSize > 0 && buffer != NULL); 33 | fifo->mFrameCount = frameCount; 34 | fifo->mFrameCountP2 = roundup(frameCount); 35 | fifo->mFudgeFactor = fifo->mFrameCountP2 - fifo->mFrameCount; 36 | fifo->mFrameSize = frameSize; 37 | fifo->mBuffer = buffer; 38 | fifo->mFront = 0; 39 | fifo->mRear = 0; 40 | } 41 | 42 | void audio_utils_fifo_deinit(struct audio_utils_fifo *fifo __unused) 43 | { 44 | } 45 | 46 | // Return a new index as the sum of an old index (either mFront or mRear) and a specified increment. 47 | static inline int32_t audio_utils_fifo_sum(struct audio_utils_fifo *fifo, int32_t index, 48 | uint32_t increment) 49 | { 50 | if (fifo->mFudgeFactor) { 51 | uint32_t mask = fifo->mFrameCountP2 - 1; 52 | ALOG_ASSERT((index & mask) < fifo->mFrameCount); 53 | ALOG_ASSERT(/*0 <= increment &&*/ increment <= fifo->mFrameCountP2); 54 | if ((index & mask) + increment >= fifo->mFrameCount) { 55 | increment += fifo->mFudgeFactor; 56 | } 57 | index += increment; 58 | ALOG_ASSERT((index & mask) < fifo->mFrameCount); 59 | return index; 60 | } else { 61 | return index + increment; 62 | } 63 | } 64 | 65 | // Return the difference between two indices: rear - front, where 0 <= difference <= mFrameCount. 66 | static inline size_t audio_utils_fifo_diff(struct audio_utils_fifo *fifo, int32_t rear, 67 | int32_t front) 68 | { 69 | int32_t diff = rear - front; 70 | if (fifo->mFudgeFactor) { 71 | uint32_t mask = ~(fifo->mFrameCountP2 - 1); 72 | int32_t genDiff = (rear & mask) - (front & mask); 73 | if (genDiff != 0) { 74 | ALOG_ASSERT(genDiff == (int32_t) fifo->mFrameCountP2); 75 | diff -= fifo->mFudgeFactor; 76 | } 77 | } 78 | // FIFO should not be overfull 79 | ALOG_ASSERT(0 <= diff && diff <= (int32_t) fifo->mFrameCount); 80 | return (size_t) diff; 81 | } 82 | 83 | ssize_t audio_utils_fifo_write(struct audio_utils_fifo *fifo, const void *buffer, size_t count) 84 | { 85 | int32_t front = android_atomic_acquire_load(&fifo->mFront); 86 | int32_t rear = fifo->mRear; 87 | size_t availToWrite = fifo->mFrameCount - audio_utils_fifo_diff(fifo, rear, front); 88 | if (availToWrite > count) { 89 | availToWrite = count; 90 | } 91 | rear &= fifo->mFrameCountP2 - 1; 92 | size_t part1 = fifo->mFrameCount - rear; 93 | if (part1 > availToWrite) { 94 | part1 = availToWrite; 95 | } 96 | if (part1 > 0) { 97 | memcpy((char *) fifo->mBuffer + (rear * fifo->mFrameSize), buffer, 98 | part1 * fifo->mFrameSize); 99 | size_t part2 = availToWrite - part1; 100 | if (part2 > 0) { 101 | memcpy(fifo->mBuffer, (char *) buffer + (part1 * fifo->mFrameSize), 102 | part2 * fifo->mFrameSize); 103 | } 104 | android_atomic_release_store(audio_utils_fifo_sum(fifo, fifo->mRear, availToWrite), 105 | &fifo->mRear); 106 | } 107 | return availToWrite; 108 | } 109 | 110 | ssize_t audio_utils_fifo_read(struct audio_utils_fifo *fifo, void *buffer, size_t count) 111 | { 112 | int32_t rear = android_atomic_acquire_load(&fifo->mRear); 113 | int32_t front = fifo->mFront; 114 | size_t availToRead = audio_utils_fifo_diff(fifo, rear, front); 115 | if (availToRead > count) { 116 | availToRead = count; 117 | } 118 | front &= fifo->mFrameCountP2 - 1; 119 | size_t part1 = fifo->mFrameCount - front; 120 | if (part1 > availToRead) { 121 | part1 = availToRead; 122 | } 123 | if (part1 > 0) { 124 | memcpy(buffer, (char *) fifo->mBuffer + (front * fifo->mFrameSize), 125 | part1 * fifo->mFrameSize); 126 | size_t part2 = availToRead - part1; 127 | if (part2 > 0) { 128 | memcpy((char *) buffer + (part1 * fifo->mFrameSize), fifo->mBuffer, 129 | part2 * fifo->mFrameSize); 130 | } 131 | android_atomic_release_store(audio_utils_fifo_sum(fifo, fifo->mFront, availToRead), 132 | &fifo->mFront); 133 | } 134 | return availToRead; 135 | } 136 | -------------------------------------------------------------------------------- /AudioLatency/res/layout/main.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 9 | 13 | 14 | 19 | 23 |