導航:首頁 > 源碼編譯 > pcmulaw演算法

pcmulaw演算法

發布時間:2023-06-05 20:31:33

㈠ 求助帖,pcm轉g711

這是很久的代碼了,有點忘記了,給你參考下。

public final static String TAG = "DefaultTalkBackSession";

TalkBackSession session;

CodecLibObj obj;

private BlockingQueue<byte[]> inputQueue = new LinkedBlockingQueue<byte[]>();

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
session = new DefaultTalkBackSession("192.168.78.65",15528);
obj = session.codecAbilityQuery();
if (obj != null) {
session.startLiveListening(new TalkBackSession.OnAudioDataReceiveListener() {
@Override
public void onAudioDataReceived(byte[] data) {
Log.d(TAG, "收到音頻數據:" + Utils.toHex(data));
try {
inputQueue.put(data);
} catch (InterruptedException e) {
}
}
}, obj, 1);
session.startCall(obj, 1);
ulaw_play();
ulaw_recored();
}
} catch (Exception e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
}

protected void onDestroy() {
super.onDestroy();
if (session != null) {
if (obj != null) {
session.stopLiveListening();
session.endCall();
}
((DefaultTalkBackSession) session).stop();
}
ulaw_stop();
}

private void ulaw_stop(){
if(ulaw_decode_thread != null){
ulaw_decode_thread.interrupt();
}
ulawRunning = false;
}

private void ulaw_play(){
ulawRunning = true;
(ulaw_decode_thread = new Thread(ulaw_decode)).start();
}

boolean ulawRunning = false;

Thread ulaw_decode_thread;

/**
* 解碼線程
*/
Runnable ulaw_decode = new Runnable(){
public void run() {

try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}

android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_AUDIO);

int samp_rate = 8000 ;
int maxjitter = AudioTrack.getMinBufferSize(samp_rate,
AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_16BIT);
AudioTrack track = new AudioTrack(AudioManager.STREAM_VOICE_CALL,samp_rate,AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT,
maxjitter, AudioTrack.MODE_STREAM);
track.play();

try {
while(ulawRunning)
{

byte[] dataForDecode = new byte[0];
try {
dataForDecode = inputQueue.take();
} catch (InterruptedException e) {
}
short[] audioData = new short [dataForDecode.length];
//decode
G711.ulaw2linear(dataForDecode, audioData, audioData.length);
Log.d(TAG, "audioData=" + Utils.toHex(audioData)) ;

//play
track.write(audioData, 0, audioData.length);
}

} catch (Exception e) {
e.printStackTrace();
}finally{
track.release();
track = null;
}
}
};

Runnable ulaw_encode = new Runnable(){
public void run() {

android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_AUDIO);

AudioRecord record = getAudioRecord();

int frame_size = 160;
// int frame_rate = 8000/frame_size ;
// long frame_period = 1000 / frame_rate;

short [] audioData = new short [frame_size];
byte[] encodeData = new byte[frame_size];
int num = 0;

try {
while(ulawRunning)
{
num = record.read(audioData, 0, frame_size);
if(num<=0) continue;
calc2(audioData,0,num);

//進行pcmu編碼
G711.linear2ulaw(audioData, 0, encodeData, num);

//send
session.sendAudioData(encodeData);
}

} catch (Exception e) {
e.printStackTrace();
} finally{
record.release();
}
}
};
void calc2(short[] lin,int off,int len) {
int i,j;

for (i = 0; i < len; i++) {
j = lin[i+off];
lin[i+off] = (short)(j>>1);
}
}

private void ulaw_recored(){
new Thread (ulaw_encode).start();
}

private AudioRecord getAudioRecord(){
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_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_16BIT,
min);
record.startRecording();

return record;
}

閱讀全文

與pcmulaw演算法相關的資料

熱點內容
泰國電影恐怖片男孩溺水待在學校 瀏覽:717
免費電影觀看平台 瀏覽:195
javahtml轉換pdf 瀏覽:529
雲伺服器設備價格實惠 瀏覽:974
linuxapachephp7 瀏覽:325
歐美圖片在線小說 瀏覽:823
什麼叫小電影 瀏覽:612
魔鬼交鋒45電影完整版 瀏覽:751
主角被綠得很慘的玄幻小說 瀏覽:309
女子人體解剖實驗電影 瀏覽:524
電影院優選區和普通區的區別 瀏覽:955
紅色世家子弟重生 瀏覽:683
一女多男現代文 瀏覽:819
上海單片機接頭哪裡有 瀏覽:154
午馬的全部電影鬼片 瀏覽:571
zip壓縮文件顯示不是壓縮文件 瀏覽:603
女主叫朝露電影 瀏覽:16
邵氏兄弟那些電影漏點 瀏覽:479
程序員職場江湖的黑話一定要牢記 瀏覽:403
重慶市雲伺服器 瀏覽:378