1 /* 2 * Copyright (C) 2016 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 #include <android/log.h> 17 18 #include "com_android_ndkaudio_AudioPlayer.h" 19 20 #include "AudioPlayer.h" 21 #include "WaveTableGenerator.h" 22 #include "WaveTableOscillator.h" 23 #include "SystemParams.h" 24 25 static const char* TAG = "_com_android_ndkaudio_AudioPlayer_"; 26 27 using namespace ndkaudio; 28 29 static int numChannels = 2; 30 static int waveTableSize = 0; 31 static float * waveTable = 0; 32 33 static WaveTableOscillator* waveTableSource; 34 static AudioPlayer* nativePlayer; 35 36 static SLresult lastSLResult = 0; 37 38 extern "C" { 39 40 JNIEXPORT void JNICALL Java_com_android_ndkaudio_AudioPlayer_Create(JNIEnv*, jobject) { 41 __android_log_print(ANDROID_LOG_INFO, TAG, "AudioPlayer_Create() ..."); 42 43 if (nativePlayer == 0) { 44 waveTableSize = SystemParams::getNumBufferFrames(); 45 waveTable = WaveTableGenerator::genSinWave(waveTableSize, 1.0f); 46 waveTableSource = new WaveTableOscillator(numChannels, waveTable, waveTableSize); 47 48 nativePlayer = new AudioPlayer(); 49 nativePlayer->Open(numChannels, waveTableSource); 50 } 51 } 52 53 JNIEXPORT void JNICALL Java_com_android_ndkaudio_AudioPlayer_Destroy(JNIEnv*, jobject) { 54 __android_log_print(ANDROID_LOG_INFO, TAG, "AudioPlayer_Destroy() ..."); 55 nativePlayer->Close(); 56 } 57 58 JNIEXPORT void JNICALL Java_com_android_ndkaudio_AudioPlayer_RealizePlayer(JNIEnv*, jobject) { 59 __android_log_print(ANDROID_LOG_INFO, TAG, "AudioPlayer_RealizePlayer() ..."); 60 nativePlayer->RealizePlayer(); 61 } 62 63 JNIEXPORT void JNICALL Java_com_android_ndkaudio_AudioPlayer_RealizeRoutingProxy(JNIEnv*, jobject) { 64 __android_log_print(ANDROID_LOG_INFO, TAG, "AudioPlayer_RealizeRoutingProxy() ..."); 65 nativePlayer->RealizeRoutingProxy(); 66 } 67 68 JNIEXPORT void JNICALL Java_com_android_ndkaudio_AudioPlayer_Start(JNIEnv*, jobject) { 69 __android_log_print(ANDROID_LOG_INFO, TAG, "AudioPlayer_Start() ..."); 70 nativePlayer->Start(); 71 } 72 73 JNIEXPORT void JNICALL Java_com_android_ndkaudio_AudioPlayer_Stop(JNIEnv*, jobject) { 74 __android_log_print(ANDROID_LOG_INFO, TAG, "AudioPlayer_Stop() ..."); 75 nativePlayer->Stop(); 76 } 77 78 JNIEXPORT jobject JNICALL Java_com_android_ndkaudio_AudioPlayer_GetRoutingInterface(JNIEnv*, jobject) { 79 __android_log_print(ANDROID_LOG_INFO, TAG, "AudioPlayer_GetRoutingInterface() ..."); 80 81 SLAndroidConfigurationItf configItf = nativePlayer->getConfigItf(); 82 __android_log_print(ANDROID_LOG_INFO, TAG, " configItf:%p", configItf); 83 jobject routingObj = 0; 84 lastSLResult = (*configItf)->AcquireJavaProxy(configItf, SL_ANDROID_JAVA_PROXY_ROUTING, &routingObj); 85 __android_log_print(ANDROID_LOG_INFO, TAG, " routingObj:%p", routingObj); 86 return routingObj; 87 } 88 89 JNIEXPORT void JNICALL Java_com_android_ndkaudio_AudioPlayer_ReleaseRoutingInterface(JNIEnv*, jobject, jobject /*proxyObj*/) { 90 __android_log_print(ANDROID_LOG_INFO, TAG, "AudioPlayer_ReleaseRoutingInterface() ..."); 91 92 SLAndroidConfigurationItf configItf = nativePlayer->getConfigItf(); 93 lastSLResult = (*configItf)->ReleaseJavaProxy(configItf, SL_ANDROID_JAVA_PROXY_ROUTING/*, proxyObj*/); 94 } 95 96 JNIEXPORT jlong JNICALL Java_com_android_ndkaudio_AudioPlayer_GetLastSLResult(JNIEnv*, jobject) { 97 return lastSLResult; 98 } 99 100 JNIEXPORT void JNICALL Java_com_android_ndkaudio_AudioPlayer_ClearLastSLResult(JNIEnv*, jobject) { 101 lastSLResult = 0; 102 } 103 104 } // extern "C" 105