package com.android;
public class g726Codec {
public native int encode(short[] SpeechData,byte[] BitstreamData);
public native int decode(byte[] SpeechData,short[] BitstreamData);
static{
System.loadLibrary("g726Code");
}
}
采集和播放实例代码+除噪音算法:
Runnable test = new Runnable(){
public void run() {
int samp_rate = 8000 ;
int min = AudioRecord.getMinBufferSize(samp_rate,
AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_16BIT);
Log.e(TAG, "min buffer size:"+min);
//用于采集音频源
AudioRecord record = null;
record = new AudioRecord(
MediaRecorder.AudioSource.MIC,//the recording source
samp_rate, //采样频率,一般为8000hz/s
AudioFormat.CHANNEL_OUT_DEFAULT,
AudioFormat.ENCODING_PCM_16BIT,
min*10);
record.startRecording();
//用于播放音频源
int maxjitter = AudioTrack.getMinBufferSize(samp_rate,
AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_16BIT);
AudioTrack track = new AudioTrack(AudioManager.STREAM_MUSIC,samp_rate,AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT,
maxjitter*10, AudioTrack.MODE_STREAM);
track.play();
int frame_size = 320;//g726_32 : 4:1的压缩比
byte [] audioData = new byte [frame_size/4];
short [] encodeData = new short[frame_size/2];
int num = 0;
//库函数
g726Codec codec = new g726Codec ();
short[] putIn = new short [160];
// int result= 0;
while(running)
{
num = record.read(encodeData, 0, 160);
Log.e(TAG, "num:"+num);
calc1(encodeData,0,160);
if(num == AudioRecord.ERROR_INVALID_OPERATION || num == AudioRecord.ERROR_BAD_VALUE) {
Log.e(TAG, "Bad ");
continue;
}
int iRet = codec.encode(encodeData, audioData);//先用G726进行编码
Log.e(TAG, "encode iRet:"+iRet);
iRet = codec.decode(audioData, encodeData);//然后用g726进行解码
Log.e(TAG, "decode iRet:"+iRet);
track.write(encodeData, 0, 160);
}
record.stop();
record.release();
record = null;
track.stop();
track.release();
track = null;
}
};
/**
* 除噪音算法(此算法应是摘抄自sipdroid)
* @param lin
* @param off
* @param len
*/
void calc1(short[] lin,int off,int len) {
int i,j;
for (i = 0; i < len; i++) {
j = lin[i+off];
lin[i+off] = (short)(j>>2);
}
}
转自: http://www.shouyanwang.org/thread-1742-1-1.html.html