<html>
<head>
<style>
.hmmessage P
{
margin:0px;
padding:0px
}
body.hmmessage
{
FONT-SIZE: 10pt;
FONT-FAMILY:Tahoma
}
</style>
</head>
<body class='hmmessage'><span style="font-family: Courier New,Courier,Monospace;">Dear all:<br><br>I need the help desparately.<br><br>The code is attached below.<br><br>If you guys don't mind take a look at the code below and see how to fit speex's AEC into it.<br><br>Help me look at the #defines, and give me some suggestions on the AEC parameters, I totally have no idea about them.<br><br>Feel free to do anything with the code, if it is by any chance valuable.<br><br>Any ideas or suggestions or sharing is highly appreciated.<br><br>I believe someone has already made AEC to work?!<br><br>But beware, this will be used in my Voip program, so it should be async by nature.<br><br>/**<br> * com_peterhi_Speex.h<br> */<br>#include <jni.h><br>#ifndef _Included_com_peterhi_Speex<br>#define _Included_com_peterhi_Speex<br>#ifdef __cplusplus<br>extern "C" {<br>#endif<br><br><br>JNIEXPORT jint JNICALL Java_com_peterhi_Speex_encoder__(JNIEnv *, jclass);<br>JNIEXPORT jint J
NICALL Java_com_peterhi_Speex_decoder__(JNIEnv *, jclass);<br>JNIEXPORT void JNICALL Java_com_peterhi_Speex_encoder__I(JNIEnv *, jclass, jint);<br>JNIEXPORT void JNICALL Java_com_peterhi_Speex_decoder__I(JNIEnv *, jclass, jint);<br>JNIEXPORT jint JNICALL Java_com_peterhi_Speex_encode(JNIEnv *, jclass, jint, jbyteArray, jbyteArray);<br>JNIEXPORT void JNICALL Java_com_peterhi_Speex_decode(JNIEnv *, jclass, jint, jbyteArray, jint, jbyteArray);<br><br>#ifdef __cplusplus<br>}<br>#endif<br>#endif<br><br><br><br><br><br><br>/************************************************************************************************************/<br><br><br><br><br><br><br>/**<br> * com_peterhi_Speex.c<br> */<br>#include <speex/speex.h><br>#include <speex/speex_preprocess.h><br>#include <speex/speex_resampler.h><br>#include "com_peterhi_Speex.h"<br><br>#define TYPE_ENCODE 1<br>#define TYPE_DECODE 2<br>#define DSP_FRAME_SIZE 160<br>#define ENC_FRAME_SIZE 320<br>#define
FRAME_RATE 8000<br><br>int yes = 1;<br>int quality = 10;<br>float rate = 8000;<br><br>typedef struct {<br> int type;<br> SpeexBits bits;<br> void* pSt;<br> SpeexPreprocessState* pPre;<br> <span style="color: rgb(255, 0, 0);">// Perhaps the echo state here?</span><br>} Codec;<br><br>/**<br> * Creates a new encoder state<br> */<br>JNIEXPORT jint JNICALL Java_com_peterhi_Speex_encoder__(JNIEnv* env, jclass c)<br>{<br> Codec* pCodec = malloc(sizeof(Codec));<br><br> speex_bits_init(&pCodec->bits);<br> pCodec->type = TYPE_ENCODE;<br> pCodec->pSt = speex_encoder_init(speex_lib_get_mode(SPEEX_MODEID_NB));<br> pCodec->pPre = speex_preprocess_state_init(DSP_FRAME_SIZE, FRAME_RATE);<br><br> speex_encoder_ctl(pCodec->pSt, SPEEX_SET_QUALITY, &quality);<br>&nbs
p; speex_preprocess_ctl(pCodec->pPre, SPEEX_PREPROCESS_SET_AGC, &yes);<br> speex_preprocess_ctl(pCodec->pPre, SPEEX_PREPROCESS_SET_AGC_LEVEL, &rate);<br><br> speex_preprocess_ctl(pCodec->pPre, SPEEX_PREPROCESS_SET_DEREVERB, &yes);<br> speex_preprocess_ctl(pCodec->pPre, SPEEX_PREPROCESS_SET_DEREVERB_DECAY, &yes);<br> speex_preprocess_ctl(pCodec->pPre, SPEEX_PREPROCESS_SET_DEREVERB_LEVEL, &yes);<br> speex_preprocess_ctl(pCodec->pPre, SPEEX_PREPROCESS_SET_DENOISE, &yes);<br> <br> <span style="color: rgb(255, 0, 0);">// Any idea how to setup AEC here?</span><br><br> return (jint )pCodec;<br>}<br><br>/**<br> * Creates a new decoder state<br> */<br>JNIEXPORT jint JNICALL Java_com_peterhi_Speex_decoder__(JNIEnv* env, jclass c)<br>{<br> Codec* pCodec = malloc(s
izeof(Codec));<br><br> speex_bits_init(&pCodec->bits);<br> pCodec->type = TYPE_DECODE;<br> pCodec->pSt = speex_decoder_init(speex_lib_get_mode(SPEEX_MODEID_NB));<br><br> return (jint )pCodec;<br>}<br><br>/**<br> * Destroys a new encoder state<br> */<br>JNIEXPORT void JNICALL Java_com_peterhi_Speex_encoder__I(JNIEnv* env, jclass c, jint p)<br>{<br> Codec* pCodec = (Codec* )p;<br><br> if (pCodec->type != TYPE_ENCODE)<br> {<br> return;<br> }<br><br> speex_preprocess_state_destroy(pCodec->pPre);<br> speex_encoder_destroy(pCodec->pSt);<br> speex_bits_destroy(&pCodec->bits);<br><br> free(pCodec);<br>}<br><br>/**<br> * Destroys a new decoder state<br> */<br>JNIEXPORT void JNICALL Java_
com_peterhi_Speex_decoder__I(JNIEnv* env, jclass c, jint p)<br>{<br> Codec* pCodec = (Codec* )p;<br><br> if (pCodec->type != TYPE_DECODE)<br> {<br> return;<br> }<br><br> speex_bits_destroy(&pCodec->bits);<br> speex_decoder_destroy(pCodec->pSt);<br><br> free(pCodec);<br>}<br><br>/**<br> * Encode a byte array.<br> */<br>JNIEXPORT jint JNICALL Java_com_peterhi_Speex_encode(JNIEnv* env, jclass c, jint p, jbyteArray buf1, jbyteArray buf2)<br>{<br> jbyte* bytes1 = (*env)->GetByteArrayElements(env, buf1, JNI_FALSE);<br> jbyte* bytes2 = (*env)->GetByteArrayElements(env, buf2, JNI_FALSE);<br> int len1 = (*env)->GetArrayLength(env, buf1);<br> int len2 = (*env)->GetArrayLength(env, buf2);<br><br> jint r
et = 0;<br><br> Codec* pCodec = (Codec* )p;<br><br> if (pCodec->type != TYPE_ENCODE)<br> {<br> return ret;<br> }<br><br> <span style="color: rgb(255, 0, 0);">// How to use AEC to cancel?</span><br> <br> speex_preprocess_run(pCodec->pPre, (spx_int16_t* )bytes1);<br> speex_bits_reset(&pCodec->bits);<br> speex_encode_int(pCodec->pSt, (spx_int16_t* )bytes1, &pCodec->bits);<br> ret = speex_bits_write(&pCodec->bits, bytes2, len2);<br><br> (*env)->SetByteArrayRegion(env, buf1, 0, len1, bytes1);<br> (*env)->SetByteArrayRegion(env, buf2, 0, len2, bytes2);<br> (*env)->ReleaseByteArrayElements(env, buf1, bytes1, 0);<br> (*env)->ReleaseByteArrayElements(env, buf
2, bytes2, 0);<br><br> return ret;<br>}<br><br>/**<br> * Decode a byte array.<br> */<br>JNIEXPORT void JNICALL Java_com_peterhi_Speex_decode(JNIEnv* env, jclass c, jint p, jbyteArray buf1, jint len, jbyteArray buf2)<br>{<br> jbyte* bytes1 = (*env)->GetByteArrayElements(env, buf1, JNI_FALSE);<br> jbyte* bytes2 = (*env)->GetByteArrayElements(env, buf2, JNI_FALSE);<br> int len1 = (*env)->GetArrayLength(env, buf1);<br> int len2 = (*env)->GetArrayLength(env, buf2);<br><br> Codec* pCodec = (Codec* )p;<br><br> if (pCodec->type != TYPE_DECODE)<br> {<br> return;<br> }<br><br> speex_bits_read_from(&pCodec->bits, bytes1, len);<br> speex_decode_int(pCodec->pSt, &pCodec->bits, (spx_int16_t* )bytes2);<br><br>&nb
sp; (*env)->SetByteArrayRegion(env, buf1, 0, len1, bytes1);<br> (*env)->SetByteArrayRegion(env, buf2, 0, len2, bytes2);<br> (*env)->ReleaseByteArrayElements(env, buf1, bytes1, 0);<br> (*env)->ReleaseByteArrayElements(env, buf2, bytes2, 0);<br>}<br><br><br><br><br><br><br>/**************************************************************************************/<br><br><br><br><br><br><br>/*<br> * Speex.java<br> *<br> * 21 May 2008<br> *<br> * copyright : (C) 2008 by Yun Tao Hai and Beijing Teng Yun Software Inc.<br> * email : hytparadisee@gmail.com<br> *<br> * This program is free software; you can redistribute it and/or modify<br> * it under the terms of the GNU General Public License as published by<br> * the Free Software Foundation; either version 2 of the License, or<br> * (at your option
) any later version.<br> *<br> * This program is distributed in the hope that it will be useful,<br> * but WITHOUT ANY WARRANTY; without even the implied warranty of<br> * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the<br> * GNU General Public License for more details.<br> *<br> * You should have received a copy of the GNU General Public License<br> * along with this program; if not, write to the Free Software Foundation,<br> * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA<br> *<br> */<br>package com.peterhi;<br><br>public class Speex {<br> static {<br> System.loadLibrary("speexjni");<br> }<br> <br> public static native int encoder();<br> public static native int decoder();<br> public static native void encoder(int peer);<br> &n
bsp; public static native void decoder(int peer);<br> public static native int encode(int peer, byte[] in, byte[] out);<br> public static native void decode(int peer, byte[] in, int len, byte[] out);<br>}</span><br><br /><hr />Discover the new Windows Vista <a href='http://search.msn.com/results.aspx?q=windows+vista&mkt=en-US&form=QBRE' target='_new'>Learn more!</a></body>
</html>