Browse Source

Implement SpeechSynthesis.getSampleRate in Java.

master
Reece H. Dunn 9 years ago
parent
commit
66cfe9af2b

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

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


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


return JNI_TRUE; return JNI_TRUE;
} }


JNIEXPORT jboolean
JNIEXPORT jint
JNICALL Java_com_reecedunn_espeak_SpeechSynthesis_nativeCreate( JNICALL Java_com_reecedunn_espeak_SpeechSynthesis_nativeCreate(
JNIEnv *env, jobject object, jstring path, jint bufferSizeInMillis) { 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);


if (nat == NULL) { if (nat == NULL) {
LOGE("%s: out of memory!", __FUNCTION__); LOGE("%s: out of memory!", __FUNCTION__);
return JNI_FALSE;
return 0;
} }


env->SetIntField(object, FIELD_mNativeData, (jint) nat); env->SetIntField(object, FIELD_mNativeData, (jint) nat);


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, bufferSizeInMillis, c_path, 0);
int 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 (nat->sampleRate > 0) ? JNI_TRUE : JNI_FALSE;
return sampleRate;
} }


JNIEXPORT jboolean JNIEXPORT jboolean
return env->NewStringUTF(espeak_Info(NULL)); return env->NewStringUTF(espeak_Info(NULL));
} }


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

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) {

+ 6
- 8
android/src/com/reecedunn/espeak/SpeechSynthesis.java View File



private boolean mInitialized = false; private boolean mInitialized = false;
private static int mVoiceCount = 0; private static int mVoiceCount = 0;
private int mSampleRate = 0;


public SpeechSynthesis(Context context, SynthReadyCallback callback) { public SpeechSynthesis(Context context, SynthReadyCallback callback) {
// First, ensure the data directory exists, otherwise init will crash. // First, ensure the data directory exists, otherwise init will crash.
} }


public int getSampleRate() { public int getSampleRate() {
return nativeGetSampleRate();
return mSampleRate;
} }


public int getChannelCount() { public int getChannelCount() {
} }


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


public List<Voice> getAvailableVoices() { public List<Voice> getAvailableVoices() {
return; return;
} }


if (!nativeCreate(mDatapath, BUFFER_SIZE_IN_MILLIS)) {
mSampleRate = nativeCreate(mDatapath, BUFFER_SIZE_IN_MILLIS);
if (mSampleRate == 0) {
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, int bufferSizeInMillis);
private native final int nativeCreate(String path, int bufferSizeInMillis);


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


private native final static String nativeGetVersion(); private native final static String nativeGetVersion();


private native final int nativeGetSampleRate();

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