speex JNI编码解码音频变成了噪音

时间:2012-03-05 11:57:58

标签: android encoding java-native-interface decode speex

我在Android IOS中对wav文件进行编码/解码。

我解码speex编码文件后,当我播放它时会变成完全噪音,文件中没有语音内容。

这是编码/解码代码。

#include <jni.h>
#include "speex/speex.h"
#include <stdio.h>
#include <android/log.h>
#define FRAME_SIZE 160
#define DEBUG_TAG "NDK_TEST_LOG"
int nbBytes = 0;
/*Holds the state of the encoder*/
void *enc_state;
void *dec_state;
/*Holds bits so they can be read and written to by the Speex routines*/
SpeexBits bits;
int i, tmp, j;
char cbits[200];
short in_short[FRAME_SIZE];
short out_short[FRAME_SIZE];
int frame_size = 0;

int codec_open = 0;

int dec_frame_size;
int enc_frame_size;

SpeexBits ebits, dbits;
void *enc_state1;
void *dec_state1;

void Java_com_argusoft_JNI2Activity_init(JNIEnv * env, jobject jobj) {
    /*Initialization of the structure that holds the bits for speex*/
    speex_bits_init(&bits);
    /*Create a new encoder state in narrowband mode*/
    enc_state = speex_encoder_init(speex_lib_get_mode(SPEEX_MODEID_NB));
    /*Set the quality to 1*/
    speex_encoder_ctl(enc_state, SPEEX_GET_FRAME_SIZE, &frame_size);
    /*set speex controller  */
    __android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG," Encode FRAME SIZE : %d", frame_size);
    tmp = 0;
    speex_encoder_ctl(enc_state, SPEEX_SET_VBR, &tmp);
    tmp = 8;
    speex_encoder_ctl(enc_state, SPEEX_SET_QUALITY, &tmp);
    tmp = 1;
    speex_encoder_ctl(enc_state, SPEEX_SET_COMPLEXITY, &tmp);
}
void Java_com_argusoft_JNI2Activity_init1(JNIEnv * env, jobject jobj) {
    speex_bits_init(&bits);
    dec_state = speex_decoder_init(speex_lib_get_mode(SPEEX_MODEID_NB));
    tmp = 0;
    speex_encoder_ctl(dec_state, SPEEX_SET_VBR, &tmp);
    tmp = 8;
    speex_encoder_ctl(dec_state, SPEEX_SET_QUALITY, &tmp);
    tmp = 1;
    speex_encoder_ctl(dec_state, SPEEX_SET_COMPLEXITY, &tmp);
}
jbyteArray Java_com_argusoft_JNI2Activity_encode1(JNIEnv * env, jobject jobj,jshortArray inputData) {
    jbyteArray ret;
    jshort * inputArrayElements = (*env)->GetShortArrayElements(env, inputData,0);
    speex_bits_reset(&bits);
    jsize len = (*env)->GetArrayLength(env, inputData);
    speex_encode_int(enc_state, inputArrayElements, &bits);
    nbBytes = speex_bits_nbytes(&bits);
    ret = (jbyteArray)((*env)->NewByteArray(env, nbBytes));
    jbyte * arrayElements = (*env)->GetByteArrayElements(env, ret, 0);
    speex_bits_write(&bits, arrayElements, nbBytes);
    (*env)->ReleaseShortArrayElements(env, inputData, inputArrayElements,JNI_ABORT);
    (*env)->ReleaseByteArrayElements(env, ret, arrayElements, 0);
    return ret;
}

jshortArray Java_com_argusoft_JNI2Activity_decode1(JNIEnv * env, jobject jobj,jbyteArray in) {
    jshortArray returnVal;
    speex_bits_reset(&bits);
    jbyte * arrayElements = (*env)->GetByteArrayElements(env, in, 0);
    jsize len = (*env)->GetArrayLength(env, in);
    returnVal = ((*env)->NewShortArray(env, frame_size));
    jshort * outputArrayElements = (*env)->GetShortArrayElements(env,returnVal, 0);
    speex_bits_read_from(&bits, arrayElements, (len));
    (*env)->ReleaseShortArrayElements(env, returnVal, outputArrayElements,JNI_ABORT);
    (*env)->ReleaseByteArrayElements(env, in, arrayElements, 0);

    return returnVal;
}
可以有人帮忙,我解决这个问题吗? 谢谢&amp;问候。

1 个答案:

答案 0 :(得分:0)

可能为时已晚,但你可以在这里查看工作示例: speex support in android