1 /*
2 * Copyright 2019 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 #include <sys/types.h>
19 #include <sys/stat.h>
20 #include <fcntl.h>
21 #include <unistd.h>
22
23 #include <android/log.h>
24
25 // parselib includes
26 #include <stream/MemInputStream.h>
27 #include <wav/WavStreamReader.h>
28
29 #include <player/OneShotSampleSource.h>
30 #include <player/SimpleMultiPlayer.h>
31
32 static const char* TAG = "DrumPlayerJNI";
33
34 // JNI functions are "C" calling convention
35 #ifdef __cplusplus
36 extern "C" {
37 #endif
38
39 using namespace iolib;
40 using namespace parselib;
41
42 static SimpleMultiPlayer sDTPlayer;
43
44 /**
45 * Native (JNI) implementation of DrumPlayer.setupAudioStreamNative()
46 */
Java_com_plausiblesoftware_drumthumper_DrumPlayer_setupAudioStreamNative(JNIEnv * env,jobject,jint numChannels)47 JNIEXPORT void JNICALL Java_com_plausiblesoftware_drumthumper_DrumPlayer_setupAudioStreamNative(
48 JNIEnv* env, jobject, jint numChannels) {
49 __android_log_print(ANDROID_LOG_INFO, TAG, "%s", "init()");
50 sDTPlayer.setupAudioStream(numChannels);
51 }
52
53 JNIEXPORT void JNICALL
Java_com_plausiblesoftware_drumthumper_DrumPlayer_startAudioStreamNative(JNIEnv * env,jobject thiz)54 Java_com_plausiblesoftware_drumthumper_DrumPlayer_startAudioStreamNative(
55 JNIEnv *env, jobject thiz) {
56 sDTPlayer.startStream();
57 }
58
59 /**
60 * Native (JNI) implementation of DrumPlayer.teardownAudioStreamNative()
61 */
Java_com_plausiblesoftware_drumthumper_DrumPlayer_teardownAudioStreamNative(JNIEnv *,jobject)62 JNIEXPORT void JNICALL Java_com_plausiblesoftware_drumthumper_DrumPlayer_teardownAudioStreamNative(JNIEnv* , jobject) {
63 __android_log_print(ANDROID_LOG_INFO, TAG, "%s", "deinit()");
64
65 // we know in this case that the sample buffers are all 1-channel, 44.1K
66 sDTPlayer.teardownAudioStream();
67 }
68
69 /**
70 * Native (JNI) implementation of DrumPlayer.allocSampleDataNative()
71 */
72 /**
73 * Native (JNI) implementation of DrumPlayer.loadWavAssetNative()
74 */
Java_com_plausiblesoftware_drumthumper_DrumPlayer_loadWavAssetNative(JNIEnv * env,jobject,jbyteArray bytearray,jint index,jfloat pan)75 JNIEXPORT void JNICALL Java_com_plausiblesoftware_drumthumper_DrumPlayer_loadWavAssetNative(
76 JNIEnv* env, jobject, jbyteArray bytearray, jint index, jfloat pan) {
77 int len = env->GetArrayLength (bytearray);
78
79 unsigned char* buf = new unsigned char[len];
80 env->GetByteArrayRegion (bytearray, 0, len, reinterpret_cast<jbyte*>(buf));
81
82 MemInputStream stream(buf, len);
83
84 WavStreamReader reader(&stream);
85 reader.parse();
86
87 reader.getNumChannels();
88
89 SampleBuffer* sampleBuffer = new SampleBuffer();
90 sampleBuffer->loadSampleData(&reader);
91
92 OneShotSampleSource* source = new OneShotSampleSource(sampleBuffer, pan);
93 sDTPlayer.addSampleSource(source, sampleBuffer);
94
95 delete[] buf;
96 }
97
98 /**
99 * Native (JNI) implementation of DrumPlayer.unloadWavAssetsNative()
100 */
Java_com_plausiblesoftware_drumthumper_DrumPlayer_unloadWavAssetsNative(JNIEnv * env,jobject)101 JNIEXPORT void JNICALL Java_com_plausiblesoftware_drumthumper_DrumPlayer_unloadWavAssetsNative(JNIEnv* env, jobject) {
102 sDTPlayer.unloadSampleData();
103 }
104
105 /**
106 * Native (JNI) implementation of DrumPlayer.trigger()
107 */
Java_com_plausiblesoftware_drumthumper_DrumPlayer_trigger(JNIEnv * env,jobject,jint index)108 JNIEXPORT void JNICALL Java_com_plausiblesoftware_drumthumper_DrumPlayer_trigger(JNIEnv* env, jobject, jint index) {
109 sDTPlayer.triggerDown(index);
110 }
111
112 /**
113 * Native (JNI) implementation of DrumPlayer.trigger()
114 */
Java_com_plausiblesoftware_drumthumper_DrumPlayer_stopTrigger(JNIEnv * env,jobject,jint index)115 JNIEXPORT void JNICALL Java_com_plausiblesoftware_drumthumper_DrumPlayer_stopTrigger(JNIEnv* env, jobject, jint index) {
116 sDTPlayer.triggerUp(index);
117 }
118
119 /**
120 * Native (JNI) implementation of DrumPlayer.getOutputReset()
121 */
Java_com_plausiblesoftware_drumthumper_DrumPlayer_getOutputReset(JNIEnv *,jobject)122 JNIEXPORT jboolean JNICALL Java_com_plausiblesoftware_drumthumper_DrumPlayer_getOutputReset(JNIEnv*, jobject) {
123 return sDTPlayer.getOutputReset();
124 }
125
126 /**
127 * Native (JNI) implementation of DrumPlayer.clearOutputReset()
128 */
Java_com_plausiblesoftware_drumthumper_DrumPlayer_clearOutputReset(JNIEnv *,jobject)129 JNIEXPORT void JNICALL Java_com_plausiblesoftware_drumthumper_DrumPlayer_clearOutputReset(JNIEnv*, jobject) {
130 sDTPlayer.clearOutputReset();
131 }
132
133 /**
134 * Native (JNI) implementation of DrumPlayer.restartStream()
135 */
Java_com_plausiblesoftware_drumthumper_DrumPlayer_restartStream(JNIEnv *,jobject)136 JNIEXPORT void JNICALL Java_com_plausiblesoftware_drumthumper_DrumPlayer_restartStream(JNIEnv*, jobject) {
137 sDTPlayer.resetAll();
138 if (sDTPlayer.openStream() && sDTPlayer.startStream()){
139 __android_log_print(ANDROID_LOG_INFO, TAG, "openStream successful");
140 } else {
141 __android_log_print(ANDROID_LOG_ERROR, TAG, "openStream failed");
142 }
143 }
144
Java_com_plausiblesoftware_drumthumper_DrumPlayer_setPan(JNIEnv * env,jobject thiz,jint index,jfloat pan)145 JNIEXPORT void JNICALL Java_com_plausiblesoftware_drumthumper_DrumPlayer_setPan(
146 JNIEnv *env, jobject thiz, jint index, jfloat pan) {
147 sDTPlayer.setPan(index, pan);
148 }
149
Java_com_plausiblesoftware_drumthumper_DrumPlayer_getPan(JNIEnv * env,jobject thiz,jint index)150 JNIEXPORT jfloat JNICALL Java_com_plausiblesoftware_drumthumper_DrumPlayer_getPan(
151 JNIEnv *env, jobject thiz, jint index) {
152 return sDTPlayer.getPan(index);
153 }
154
Java_com_plausiblesoftware_drumthumper_DrumPlayer_setGain(JNIEnv * env,jobject thiz,jint index,jfloat gain)155 JNIEXPORT void JNICALL Java_com_plausiblesoftware_drumthumper_DrumPlayer_setGain(
156 JNIEnv *env, jobject thiz, jint index, jfloat gain) {
157 sDTPlayer.setGain(index, gain);
158 }
159
Java_com_plausiblesoftware_drumthumper_DrumPlayer_getGain(JNIEnv * env,jobject thiz,jint index)160 JNIEXPORT jfloat JNICALL Java_com_plausiblesoftware_drumthumper_DrumPlayer_getGain(
161 JNIEnv *env, jobject thiz, jint index) {
162 return sDTPlayer.getGain(index);
163 }
164
165 #ifdef __cplusplus
166 }
167 #endif
168