Browse Source

Implement SpeechSynthesis.getBufferSizeInMillis in Java.

master
Reece H. Dunn 10 years ago
parent
commit
46c5f88c6f

+ 2
- 14
android/jni/jni/eSpeakService.cpp View File

SYNTH_ABORT = 1 SYNTH_ABORT = 1
}; };


const int DEFAULT_BUFFER_SIZE = 1000;

struct native_data_t { struct native_data_t {
JNIEnv *env; JNIEnv *env;
jobject object; jobject object;
int sampleRate; int sampleRate;
int bufferSizeInMillis;


native_data_t() { native_data_t() {
env = NULL; env = NULL;
object = NULL; object = NULL;
sampleRate = 0; sampleRate = 0;
bufferSizeInMillis = DEFAULT_BUFFER_SIZE;
} }
}; };




JNIEXPORT jboolean JNIEXPORT jboolean
JNICALL Java_com_reecedunn_espeak_SpeechSynthesis_nativeCreate( JNICALL Java_com_reecedunn_espeak_SpeechSynthesis_nativeCreate(
JNIEnv *env, jobject object, jstring path) {
JNIEnv *env, jobject object, jstring path, jint bufferSizeInMillis) {
if (DEBUG) LOGV("%s [env=%p, object=%p]", __FUNCTION__, env, object); if (DEBUG) LOGV("%s [env=%p, object=%p]", __FUNCTION__, env, object);
native_data_t *nat = new native_data_t; native_data_t *nat = new native_data_t;




nat->object = env->NewWeakGlobalRef(object); nat->object = env->NewWeakGlobalRef(object);
if (DEBUG) LOGV("Initializing with path %s", c_path); if (DEBUG) LOGV("Initializing with path %s", c_path);
nat->sampleRate = espeak_Initialize(AUDIO_OUTPUT_SYNCHRONOUS, nat->bufferSizeInMillis, c_path, 0);
nat->sampleRate = espeak_Initialize(AUDIO_OUTPUT_SYNCHRONOUS, bufferSizeInMillis, c_path, 0);


if (c_path) env->ReleaseStringUTFChars(path, c_path); if (c_path) env->ReleaseStringUTFChars(path, c_path);


return (jint)(nat ? nat->sampleRate : 0); return (jint)(nat ? nat->sampleRate : 0);
} }


JNIEXPORT jint
JNICALL Java_com_reecedunn_espeak_SpeechSynthesis_nativeGetBufferSizeInMillis(
JNIEnv *env, jobject object) {
if (DEBUG) LOGV("%s", __FUNCTION__);
const native_data_t *nat = getNativeData(env, object);
return (jint) nat->bufferSizeInMillis;
}

JNIEXPORT jobjectArray JNIEXPORT jobjectArray
JNICALL Java_com_reecedunn_espeak_SpeechSynthesis_nativeGetAvailableVoices( JNICALL Java_com_reecedunn_espeak_SpeechSynthesis_nativeGetAvailableVoices(
JNIEnv *env, jobject object) { JNIEnv *env, jobject object) {

+ 4
- 5
android/src/com/reecedunn/espeak/SpeechSynthesis.java View File



public static final int CHANNEL_COUNT_MONO = 1; public static final int CHANNEL_COUNT_MONO = 1;
public static final int FORMAT_PCM_S16 = 2; public static final int FORMAT_PCM_S16 = 2;
public static final int BUFFER_SIZE_IN_MILLIS = 1000;


static { static {
System.loadLibrary("ttsespeak"); System.loadLibrary("ttsespeak");
} }


public int getBufferSizeInBytes() { public int getBufferSizeInBytes() {
final int bufferSizeInMillis = nativeGetBufferSizeInMillis();
final int bufferSizeInMillis = BUFFER_SIZE_IN_MILLIS;
final int sampleRate = nativeGetSampleRate(); final int sampleRate = nativeGetSampleRate();
return (bufferSizeInMillis * sampleRate) / 1000; return (bufferSizeInMillis * sampleRate) / 1000;
} }
return; return;
} }


if (!nativeCreate(mDatapath)) {
if (!nativeCreate(mDatapath, BUFFER_SIZE_IN_MILLIS)) {
Log.e(TAG, "Failed to initialize speech synthesis library"); Log.e(TAG, "Failed to initialize speech synthesis library");
return; return;
} }


private static native final boolean nativeClassInit(); private static native final boolean nativeClassInit();


private native final boolean nativeCreate(String path);
private native final boolean nativeCreate(String path, int bufferSizeInMillis);


private native final boolean nativeDestroy(); private native final boolean nativeDestroy();




private native final int nativeGetSampleRate(); private native final int nativeGetSampleRate();


private native final int nativeGetBufferSizeInMillis();

private native final String[] nativeGetAvailableVoices(); private native final String[] nativeGetAvailableVoices();


private native final boolean nativeSetVoiceByName(String name); private native final boolean nativeSetVoiceByName(String name);

Loading…
Cancel
Save