1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
|
/*
* BRLTTY - A background process providing access to the console screen (when in
* text mode) for a blind person using a refreshable braille display.
*
* Copyright (C) 1995-2024 by The BRLTTY Developers.
*
* BRLTTY comes with ABSOLUTELY NO WARRANTY.
*
* This is free software, placed under the terms of the
* GNU Lesser General Public License, as published by the Free Software
* Foundation; either version 2.1 of the License, or (at your option) any
* later version. Please see the file LICENSE-LGPL for details.
*
* Web Page: http://brltty.app/
*
* This software is maintained by Dave Mielke <dave@mielke.cc>.
*/
#include "prologue.h"
#include <string.h>
#include "log.h"
#include "pcm.h"
#include "system_java.h"
struct PcmDeviceStruct {
JNIEnv *env;
jobject device;
};
static jclass pcmDeviceClass = NULL;
static int
findPcmDeviceClass (JNIEnv *env) {
return findJavaClass(env, &pcmDeviceClass, JAVA_OBJ_BRLTTY("PcmDevice"));
}
PcmDevice *
openPcmDevice (int errorLevel, const char *device) {
PcmDevice *pcm = malloc(sizeof(*pcm));
if (pcm) {
memset(pcm, 0, sizeof(*pcm));
pcm->env = getJavaNativeInterface();
pcm->device = NULL;
if (findPcmDeviceClass(pcm->env)) {
static jmethodID constructor = 0;
if (findJavaConstructor(pcm->env, &constructor, pcmDeviceClass,
JAVA_SIG_CONSTRUCTOR())) {
jobject localReference = (*pcm->env)->NewObject(pcm->env, pcmDeviceClass, constructor);
if (!clearJavaException(pcm->env, 1)) {
jobject globalReference = (*pcm->env)->NewGlobalRef(pcm->env, localReference);
(*pcm->env)->DeleteLocalRef(pcm->env, localReference);
localReference = NULL;
if (globalReference) {
pcm->device = globalReference;
return pcm;
} else {
logMallocError();
clearJavaException(pcm->env, 0);
}
}
}
}
free(pcm);
} else {
logMallocError();
}
return NULL;
}
void
closePcmDevice (PcmDevice *pcm) {
if (pcm) {
if (pcm->device) {
if (findPcmDeviceClass(pcm->env)) {
static jmethodID method = 0;
if (findJavaInstanceMethod(pcm->env, &method, pcmDeviceClass, "close",
JAVA_SIG_METHOD(JAVA_SIG_VOID,
))) {
(*pcm->env)->CallVoidMethod(pcm->env, pcm->device, method);
clearJavaException(pcm->env, 1);
}
}
(*pcm->env)->DeleteGlobalRef(pcm->env, pcm->device);
}
free(pcm);
}
}
int
writePcmData (PcmDevice *pcm, const unsigned char *buffer, int count) {
if (findPcmDeviceClass(pcm->env)) {
static jmethodID method = 0;
if (findJavaInstanceMethod(pcm->env, &method, pcmDeviceClass, "write",
JAVA_SIG_METHOD(JAVA_SIG_BOOLEAN,
JAVA_SIG_ARRAY(JAVA_SIG_SHORT) // samples
))) {
jint size = count / 2;
jshortArray jSamples = (*pcm->env)->NewShortArray(pcm->env, size);
if (jSamples) {
typedef union {
const unsigned char *bytes;
const int16_t *actual;
} Samples;
Samples samples = {
.bytes = buffer
};
(*pcm->env)->SetShortArrayRegion(
pcm->env, jSamples, 0, size, samples.actual
);
if (!clearJavaException(pcm->env, 1)) {
jboolean result = (*pcm->env)->CallBooleanMethod(
pcm->env, pcm->device, method, jSamples
);
(*pcm->env)->DeleteLocalRef(pcm->env, jSamples);
jSamples = NULL;
if (!clearJavaException(pcm->env, 1)) {
if (result == JNI_TRUE) {
return 1;
}
}
}
} else {
logMallocError();
clearJavaException(pcm->env, 0);
}
}
}
return 0;
}
int
getPcmBlockSize (PcmDevice *pcm) {
if (findPcmDeviceClass(pcm->env)) {
static jmethodID method = 0;
if (findJavaInstanceMethod(pcm->env, &method, pcmDeviceClass, "getBufferSize",
JAVA_SIG_METHOD(JAVA_SIG_INT,
))) {
jint result = (*pcm->env)->CallIntMethod(pcm->env, pcm->device, method);
if (!clearJavaException(pcm->env, 1)) return result;
}
}
return 0X100;
}
int
getPcmSampleRate (PcmDevice *pcm) {
if (findPcmDeviceClass(pcm->env)) {
static jmethodID method = 0;
if (findJavaInstanceMethod(pcm->env, &method, pcmDeviceClass, "getSampleRate",
JAVA_SIG_METHOD(JAVA_SIG_INT,
))) {
jint result = (*pcm->env)->CallIntMethod(pcm->env, pcm->device, method);
if (!clearJavaException(pcm->env, 1)) return result;
}
}
return 8000;
}
int
setPcmSampleRate (PcmDevice *pcm, int rate) {
if (findPcmDeviceClass(pcm->env)) {
static jmethodID method = 0;
if (findJavaInstanceMethod(pcm->env, &method, pcmDeviceClass, "setSampleRate",
JAVA_SIG_METHOD(JAVA_SIG_VOID,
JAVA_SIG_INT // rate
))) {
(*pcm->env)->CallVoidMethod(pcm->env, pcm->device, method, rate);
clearJavaException(pcm->env, 1);
}
}
return getPcmSampleRate(pcm);
}
int
getPcmChannelCount (PcmDevice *pcm) {
if (findPcmDeviceClass(pcm->env)) {
static jmethodID method = 0;
if (findJavaInstanceMethod(pcm->env, &method, pcmDeviceClass, "getChannelCount",
JAVA_SIG_METHOD(JAVA_SIG_INT,
))) {
jint result = (*pcm->env)->CallIntMethod(pcm->env, pcm->device, method);
if (!clearJavaException(pcm->env, 1)) return result;
}
}
return 1;
}
int
setPcmChannelCount (PcmDevice *pcm, int channels) {
if (findPcmDeviceClass(pcm->env)) {
static jmethodID method = 0;
if (findJavaInstanceMethod(pcm->env, &method, pcmDeviceClass, "setChannelCount",
JAVA_SIG_METHOD(JAVA_SIG_VOID,
JAVA_SIG_INT // count
))) {
(*pcm->env)->CallVoidMethod(pcm->env, pcm->device, method, channels);
clearJavaException(pcm->env, 1);
}
}
return getPcmChannelCount(pcm);
}
PcmAmplitudeFormat
getPcmAmplitudeFormat (PcmDevice *pcm) {
return PCM_FMT_S16N;
}
PcmAmplitudeFormat
setPcmAmplitudeFormat (PcmDevice *pcm, PcmAmplitudeFormat format) {
return getPcmAmplitudeFormat(pcm);
}
void
pushPcmOutput (PcmDevice *pcm) {
if (findPcmDeviceClass(pcm->env)) {
static jmethodID method = 0;
if (findJavaInstanceMethod(pcm->env, &method, pcmDeviceClass, "push",
JAVA_SIG_METHOD(JAVA_SIG_VOID,
))) {
(*pcm->env)->CallVoidMethod(pcm->env, pcm->device, method);
clearJavaException(pcm->env, 1);
}
}
}
void
awaitPcmOutput (PcmDevice *pcm) {
if (findPcmDeviceClass(pcm->env)) {
static jmethodID method = 0;
if (findJavaInstanceMethod(pcm->env, &method, pcmDeviceClass, "await",
JAVA_SIG_METHOD(JAVA_SIG_VOID,
))) {
(*pcm->env)->CallVoidMethod(pcm->env, pcm->device, method);
clearJavaException(pcm->env, 1);
}
}
}
void
cancelPcmOutput (PcmDevice *pcm) {
if (findPcmDeviceClass(pcm->env)) {
static jmethodID method = 0;
if (findJavaInstanceMethod(pcm->env, &method, pcmDeviceClass, "cancel",
JAVA_SIG_METHOD(JAVA_SIG_VOID,
))) {
(*pcm->env)->CallVoidMethod(pcm->env, pcm->device, method);
clearJavaException(pcm->env, 1);
}
}
}
|