-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ad3721f
commit 30156f8
Showing
41 changed files
with
1,389 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,4 +47,6 @@ build/ | |
obj/ | ||
|
||
gradle.properties | ||
*.hprof | ||
*.hprof | ||
|
||
/lib-oboe |
24 changes: 0 additions & 24 deletions
24
app/src/androidTest/java/com/mercandalli/apps/android/theremin/ExampleInstrumentedTest.kt
This file was deleted.
Oops, something went wrong.
17 changes: 0 additions & 17 deletions
17
app/src/test/java/com/mercandalli/apps/android/theremin/ExampleUnitTest.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
include ':app' | ||
include ':app', ':things' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
cmake_minimum_required(VERSION 3.4.1) | ||
|
||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall -Werror") | ||
|
||
set (OBOE_RELATIVE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/../lib-oboe) | ||
get_filename_component(OBOE_DIR | ||
${OBOE_RELATIVE_PATH} | ||
ABSOLUTE) | ||
|
||
if ((NOT EXISTS ${OBOE_DIR}) OR | ||
(NOT EXISTS ${OBOE_DIR}/CMakeLists.txt)) | ||
execute_process(COMMAND git clone | ||
https://github.com/google/oboe.git | ||
${OBOE_DIR} | ||
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/..) | ||
endif() | ||
|
||
add_subdirectory(${OBOE_DIR} ./oboe) | ||
include_directories(${OBOE_DIR}/include) | ||
|
||
set (DEBUG_UTILS_PATH "../../debug-utils") | ||
set (DEBUG_UTILS_SOURCES ${DEBUG_UTILS_PATH}/trace.cpp) | ||
include_directories(${DEBUG_UTILS_PATH}) | ||
|
||
add_library( native-lib | ||
SHARED | ||
src/main/cpp/AudioManager.cpp | ||
src/main/cpp/AudioManager.h | ||
src/main/cpp/logging_macros.h | ||
src/main/cpp/NativeAudioManagerBridge.cpp | ||
src/main/cpp/WavGenerator.cpp | ||
src/main/cpp/WavGenerator.h) | ||
|
||
find_library( log-lib | ||
log ) | ||
|
||
target_link_libraries( native-lib | ||
android | ||
${log-lib} | ||
oboe) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
apply plugin: 'com.android.application' | ||
apply plugin: 'kotlin-android' | ||
apply plugin: 'kotlin-kapt' | ||
apply plugin: 'kotlin-android-extensions' | ||
|
||
android { | ||
compileSdkVersion rootProject.ext.compileSdkVersion | ||
buildToolsVersion rootProject.ext.buildToolsVersion | ||
|
||
defaultConfig { | ||
applicationId "com.mercandalli.android.apps.theremin" | ||
minSdkVersion rootProject.ext.minSdkVersion | ||
targetSdkVersion rootProject.ext.targetSdkVersion | ||
versionCode 1 | ||
versionName "1.0" | ||
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" | ||
} | ||
buildTypes { | ||
release { | ||
minifyEnabled false | ||
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' | ||
} | ||
} | ||
externalNativeBuild { | ||
cmake { | ||
path "CMakeLists.txt" | ||
} | ||
} | ||
} | ||
|
||
dependencies { | ||
implementation fileTree(dir: 'libs', include: ['*.jar']) | ||
|
||
implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version" | ||
implementation "com.android.support:appcompat-v7:$support_version" | ||
implementation "com.android.support:cardview-v7:$support_version" | ||
implementation "com.android.support:design:$support_version" | ||
implementation 'com.google.android.things:androidthings:0.6.1-devpreview' | ||
|
||
testImplementation 'junit:junit:4.12' | ||
androidTestImplementation 'com.android.support.test:runner:1.0.1' | ||
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1' | ||
} | ||
|
||
task adbLaunchApp(type: Exec) { | ||
group = "adb" | ||
executable = project.android.getAdbExe().toString() | ||
args = ["shell", "am", "start", "-n", "com.mercandalli.android.apps.theremin/.main.MainActivity"] | ||
// adb shell am start -n com.mercandalli.android.apps.theremin/.main.MainActivity | ||
} | ||
|
||
task adbKeyHome(type: Exec) { | ||
group = "adb" | ||
commandLine 'adb', 'shell', 'input keyevent 3' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# Add project specific ProGuard rules here. | ||
# You can control the set of applied configuration files using the | ||
# proguardFiles setting in build.gradle. | ||
# | ||
# For more details, see | ||
# http://developer.android.com/guide/developing/tools/proguard.html | ||
|
||
# If your project uses WebView with JS, uncomment the following | ||
# and specify the fully qualified class name to the JavaScript interface | ||
# class: | ||
#-keepclassmembers class fqcn.of.javascript.interface.for.webview { | ||
# public *; | ||
#} | ||
|
||
# Uncomment this to preserve the line number information for | ||
# debugging stack traces. | ||
#-keepattributes SourceFile,LineNumberTable | ||
|
||
# If you keep the line number information, uncomment this to | ||
# hide the original source file name. | ||
#-renamesourcefileattribute SourceFile |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
package="com.mercandalli.android.apps.theremin"> | ||
|
||
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> | ||
|
||
<application | ||
android:name=".main.MainApplication" | ||
android:theme="@style/AppTheme"> | ||
|
||
<uses-library android:name="com.google.android.things" /> | ||
|
||
<activity android:name=".main.MainActivity"> | ||
|
||
<intent-filter> | ||
<action android:name="android.intent.action.MAIN" /> | ||
|
||
<category android:name="android.intent.category.LAUNCHER" /> | ||
</intent-filter> | ||
|
||
</activity> | ||
|
||
</application> | ||
|
||
</manifest> |
Binary file added
BIN
+662 KB
things/src/main/assets/wav/shape-of-you/dpm_shape_of_you_a_melody_01.wav
Binary file not shown.
Binary file added
BIN
+654 KB
things/src/main/assets/wav/shape-of-you/dpm_shape_of_you_a_melody_02.wav
Binary file not shown.
Binary file added
BIN
+661 KB
things/src/main/assets/wav/shape-of-you/dpm_shape_of_you_a_melody_03.wav
Binary file not shown.
Binary file added
BIN
+664 KB
things/src/main/assets/wav/shape-of-you/dpm_shape_of_you_a_melody_04.wav
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
#include <inttypes.h> | ||
#include <cstring> | ||
|
||
#include "AudioManager.h" | ||
#include "logging_macros.h" | ||
|
||
constexpr int32_t kDefaultChannelCount = 2; | ||
|
||
AudioManager::AudioManager() { | ||
mChannelCount = kDefaultChannelCount; | ||
createPlaybackStream(); | ||
} | ||
|
||
AudioManager::~AudioManager() { | ||
closeOutputStream(); | ||
} | ||
|
||
void AudioManager::createPlaybackStream() { | ||
oboe::AudioStreamBuilder builder; | ||
setupPlaybackStreamParameters(&builder); | ||
oboe::Result result = builder.openStream(&mPlayStream); | ||
if (result == oboe::Result::OK && mPlayStream != nullptr) { | ||
mFramesPerBurst = mPlayStream->getFramesPerBurst(); | ||
int channelCount = mPlayStream->getChannelCount(); | ||
if (channelCount != mChannelCount) { | ||
LOGW("Requested %d channels but received %d", mChannelCount, channelCount); | ||
} | ||
mPlayStream->setBufferSizeInFrames(mFramesPerBurst); | ||
mLatencyTuner = std::unique_ptr<oboe::LatencyTuner>(new oboe::LatencyTuner(*mPlayStream)); | ||
result = mPlayStream->requestStart(); | ||
if (result != oboe::Result::OK) { | ||
LOGE("Error starting stream. %s", oboe::convertToText(result)); | ||
} | ||
} else { | ||
LOGE("Failed to create stream. Error: %s", oboe::convertToText(result)); | ||
} | ||
} | ||
|
||
void AudioManager::setupPlaybackStreamParameters(oboe::AudioStreamBuilder *builder) { | ||
builder->setAudioApi(mAudioApi); | ||
builder->setDeviceId(mPlaybackDeviceId); | ||
builder->setChannelCount(mChannelCount); | ||
builder->setSharingMode(oboe::SharingMode::Exclusive); | ||
builder->setPerformanceMode(oboe::PerformanceMode::LowLatency); | ||
builder->setCallback(this); | ||
} | ||
|
||
void AudioManager::closeOutputStream() { | ||
if (mPlayStream != nullptr) { | ||
oboe::Result result = mPlayStream->requestStop(); | ||
if (result != oboe::Result::OK) { | ||
LOGE("Error stopping output stream. %s", oboe::convertToText(result)); | ||
} | ||
|
||
result = mPlayStream->close(); | ||
if (result != oboe::Result::OK) { | ||
LOGE("Error closing output stream. %s", oboe::convertToText(result)); | ||
} | ||
} | ||
} | ||
|
||
oboe::DataCallbackResult | ||
AudioManager::onAudioReady(oboe::AudioStream *audioStream, void *audioData, int32_t numFrames) { | ||
int32_t bufferSize = audioStream->getBufferSizeInFrames(); | ||
if (mBufferSizeSelection == kBufferSizeAutomatic) { | ||
mLatencyTuner->tune(); | ||
} else if (bufferSize != (mBufferSizeSelection * mFramesPerBurst)) { | ||
audioStream->setBufferSizeInFrames(mBufferSizeSelection * mFramesPerBurst); | ||
} | ||
int32_t channelCount = audioStream->getChannelCount(); | ||
if (audioStream->getFormat() == oboe::AudioFormat::Float) { | ||
for (int i = 0; i < channelCount; ++i) { | ||
wavGenerator->render(static_cast<float *>(audioData) + i, i, channelCount, | ||
numFrames); | ||
} | ||
} else { | ||
for (int i = 0; i < channelCount; ++i) { | ||
wavGenerator->render(static_cast<int16_t *>(audioData) + i, i, channelCount, | ||
numFrames); | ||
} | ||
} | ||
return oboe::DataCallbackResult::Continue; | ||
} | ||
|
||
void AudioManager::onErrorAfterClose(oboe::AudioStream *oboeStream, oboe::Result error) { | ||
if (error == oboe::Result::ErrorDisconnected) restartStream(); | ||
} | ||
|
||
void AudioManager::restartStream() { | ||
LOGI("Restarting stream"); | ||
if (mRestartingLock.try_lock()) { | ||
closeOutputStream(); | ||
createPlaybackStream(); | ||
mRestartingLock.unlock(); | ||
} else { | ||
LOGW("Restart stream operation already in progress - ignoring this request"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
#ifndef SAMPLER_AUDIO_MANAGER_H | ||
#define SAMPLER_AUDIO_MANAGER_H | ||
|
||
#include <thread> | ||
#include <array> | ||
#include <oboe/Oboe.h> | ||
#include <mutex> | ||
#include "WavGenerator.h" | ||
|
||
constexpr int32_t kBufferSizeAutomatic = 0; | ||
|
||
class AudioManager : oboe::AudioStreamCallback { | ||
|
||
public: | ||
AudioManager(); | ||
|
||
~AudioManager(); | ||
|
||
oboe::DataCallbackResult | ||
onAudioReady(oboe::AudioStream *audioStream, void *audioData, int32_t numFrames); | ||
|
||
void onErrorAfterClose(oboe::AudioStream *oboeStream, oboe::Result error); | ||
|
||
inline WavGenerator *getWavGenerator() { | ||
return wavGenerator; | ||
} | ||
|
||
private: | ||
oboe::AudioApi mAudioApi = oboe::AudioApi::Unspecified; | ||
int32_t mPlaybackDeviceId = oboe::kUnspecified; | ||
int32_t mChannelCount; | ||
int32_t mFramesPerBurst; | ||
int32_t mBufferSizeSelection = kBufferSizeAutomatic; | ||
oboe::AudioStream *mPlayStream; | ||
std::unique_ptr<oboe::LatencyTuner> mLatencyTuner; | ||
std::mutex mRestartingLock; | ||
|
||
WavGenerator *wavGenerator = new WavGenerator(); | ||
|
||
void createPlaybackStream(); | ||
|
||
void closeOutputStream(); | ||
|
||
void restartStream(); | ||
|
||
void setupPlaybackStreamParameters(oboe::AudioStreamBuilder *builder); | ||
}; | ||
|
||
#endif // SAMPLER_AUDIO_MANAGER_H |
Oops, something went wrong.