1 /*
2  * Copyright 2020 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 <jni.h>
18 
19 #include "AudioSink.h"
20 
21 //TODO - Probably wrap the JNI handling in a class with a pointer held in the Java Object
22 // so as to support multiple instances... maybe.
23 
24 // JNI Stuff
25 static float* sAudioBuffer;
26 
27 extern "C" {
28 JNIEXPORT void JNICALL
Java_org_hyphonate_megaaudio_recorder_NativeAudioSink_initN(JNIEnv * env,jobject thiz,jlong native_sink_ptr,jint num_frames,jint num_chans)29 Java_org_hyphonate_megaaudio_recorder_NativeAudioSink_initN(JNIEnv * env , jobject thiz,
30         jlong native_sink_ptr , jint num_frames, jint num_chans ) {
31     sAudioBuffer = new float[num_frames * num_chans];
32 
33     // this is in the wrong place, or rather we need an init() method of AudioSink to call.
34     AudioSink* sink = (AudioSink*)native_sink_ptr;
35     sink->init(num_frames, num_chans);
36 }
37 
38 JNIEXPORT void JNICALL
Java_org_hyphonate_megaaudio_recorder_NativeAudioSink_startN(JNIEnv * env,jobject thiz,jlong native_sink_ptr)39 Java_org_hyphonate_megaaudio_recorder_NativeAudioSink_startN(JNIEnv *env, jobject thiz, jlong native_sink_ptr) {
40     AudioSink* sink = (AudioSink*)native_sink_ptr;
41     sink->start();
42 }
43 
44 JNIEXPORT void JNICALL
Java_org_hyphonate_megaaudio_recorder_NativeAudioSink_stopN(JNIEnv * env,jobject thiz,jlong native_sink_ptr)45 Java_org_hyphonate_megaaudio_recorder_NativeAudioSink_stopN(JNIEnv *env, jobject thiz, jlong native_sink_ptr) {
46     AudioSink* sink = (AudioSink*)native_sink_ptr;
47     sink->stop();
48 }
49 
50 JNIEXPORT void JNICALL
Java_org_hyphonate_megaaudio_recorder_NativeAudioSink_pushN(JNIEnv * env,jobject thiz,jlong native_sink_ptr,jfloatArray audio_data,jint num_frames,jint num_chans)51 Java_org_hyphonate_megaaudio_recorder_NativeAudioSink_pushN(
52         JNIEnv *env, jobject thiz, jlong native_sink_ptr,
53         jfloatArray audio_data, jint num_frames, jint num_chans) {
54         AudioSink * audioSink = (AudioSink*)native_sink_ptr;
55 
56     // convert to float[]
57     float* nativeAudioData = env->GetFloatArrayElements(audio_data, 0);
58 
59     audioSink->push(nativeAudioData, num_frames, num_chans);
60 }
61 
62 } // extern "C"
63