2424
2525
2626import  com .hugh .audiofun .R ;
27+ import  com .hugh .component .audio .AudioTrackManager ;
2728import  com .hugh .libwebrtc .WebRtcAGCUtils ;
2829import  com .hugh .libwebrtc .WebRtcNsUtils ;
2930import  com .hugh .sound .util .ContentUtil ;
@@ -87,6 +88,7 @@ public class RtcActivity extends Activity implements View.OnClickListener {
8788 private  AudioTrack  mAudioTrack ;
8889 private  File  mFile ;
8990 private  File  mProcessFile ;
91+  private  String  mProcessFilePath ;
9092 private  String  AUDIO_FILE_PATH ;
9193 private  String  AUDIO_PROCESS_FILE_PATH ;
9294 private  String  srcPath ;
@@ -101,11 +103,13 @@ public class RtcActivity extends Activity implements View.OnClickListener {
101103 private  TextView  mTvTitle ;
102104 private  Button  mBtnChooseFile ;
103105 private  Button  mBtnChooseSample ;
106+  private  TextView  mTvCurrentProcessFile ;
104107 private  boolean  mIsOpenAgc ;
105108 private  long  nsxId ; //ns降噪id 
106109 private  long  agcId ; //agc增益id 
107110 private  int  num_bands  = 1 ;
108111 private  Button  mPlayBtn ;
112+  private  Button  mPlayOriginBtn ;
109113
110114
111115 @ Override 
@@ -119,6 +123,13 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
119123 mTvTitle  = findViewById (R .id .tv_title );
120124 mBtnChooseSample  = findViewById (R .id .btn_choose_sample );
121125 mPlayBtn  = findViewById (R .id .btn_audio_play );
126+  mTvCurrentProcessFile  = findViewById (R .id .tv_current_processfile );
127+  mPlayOriginBtn  = findViewById (R .id .btn_play_origin_file );
128+  mBtnNsOperate  = findViewById (R .id .ns_audio );
129+  mBtnNsOperate .setOnClickListener (this );
130+  mPlayBtn .setOnClickListener (this );
131+  mBtnChooseFile .setOnClickListener (this );
132+  mPlayOriginBtn .setOnClickListener (this );
122133 mTvTitle .setText ("webrtc测试" );
123134 agc_switch .setOnCheckedChangeListener (new  CompoundButton .OnCheckedChangeListener () {
124135 @ Override 
@@ -133,14 +144,6 @@ public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
133144 }
134145 });
135146
136- 137-  mBtnChooseFile .setOnClickListener (new  View .OnClickListener () {
138-  @ Override 
139-  public  void  onClick (View  v ) {
140-  selectFile ();
141-  }
142-  });
143- 144147 mBtnChooseSample .setOnClickListener (new  View .OnClickListener () {
145148 @ Override 
146149 public  void  onClick (View  v ) {
@@ -154,16 +157,6 @@ public void selectSample(int Sample) {
154157 dialog .show ();
155158 }
156159 });
157- 158-  mPlayBtn .setOnClickListener (new  View .OnClickListener () {
159-  @ Override 
160-  public  void  onClick (View  v ) {
161-  Log .e ("aaa" ,"播放" );
162-  }
163-  });
164- 165-  mBtnNsOperate  = findViewById (R .id .ns_audio );
166-  mBtnNsOperate .setOnClickListener (this );
167160 selectId  = R .id .rb_8k ;
168161 RadioGroup  radioGroup  = findViewById (R .id .rg );
169162 int  checkedRadioButtonId  = radioGroup .getCheckedRadioButtonId ();
@@ -184,6 +177,7 @@ public void onCheckedChanged(RadioGroup group, int checkedId) {
184177 }
185178 }
186179
180+ 187181 protected  void  selectFile () {
188182 Intent  intent  = new  Intent (Intent .ACTION_GET_CONTENT );
189183 intent .addCategory (Intent .CATEGORY_OPENABLE );
@@ -257,6 +251,17 @@ public void onClick(View v) {
257251 }
258252
259253 process ();
254+  } else  if  (v  == mPlayBtn ) {
255+  Log .e ("aaa" ,"当前播放新文件采样率----->" +mSampleRate );
256+  AudioTrackManager .getInstance ().initConfig (mSampleRate );
257+  AudioTrackManager .getInstance ().play (mProcessFilePath );
258+  }else  if (v  == mBtnChooseFile ){
259+  selectFile ();
260+  }else  if (v  == mPlayOriginBtn ){
261+  Log .e ("aaa" ,"当前播放原文件采样率----->" +mSampleRate );
262+  Log .e ("aaa" ,"当前播放原文件 ----->" +AUDIO_FILE_PATH );
263+  AudioTrackManager .getInstance ().initConfig (mSampleRate );
264+  AudioTrackManager .getInstance ().play (AUDIO_FILE_PATH );
260265 }
261266 }
262267
@@ -269,7 +274,7 @@ private void initAudio() {
269274 Log .e ("aaa" , "AUDIO_FILE_PATH=="  + AUDIO_FILE_PATH );
270275
271276 mProcessFile  = new  File (AUDIO_PROCESS_FILE_PATH );
272- 277+ mProcessFilePath  =  AUDIO_PROCESS_FILE_PATH ; 
273278 mFile  = new  File (AUDIO_FILE_PATH );
274279
275280 if  (!mFile .exists () || mFile .length () <= 0 ) {
@@ -328,7 +333,7 @@ private void process() {
328333 //ns初始化 
329334 //fs == 8000 || fs == 16000 || fs == 32000 || fs == 48000 
330335 nsxId  = WebRtcNsUtils .WebRtcNsx_Create ();
331-  int  nsxInit  = WebRtcNsUtils .WebRtcNsx_Init (nsxId , 8000 ); //0代表成功 
336+  int  nsxInit  = WebRtcNsUtils .WebRtcNsx_Init (nsxId , mSampleRate ); //0代表成功 
332337 int  nexSetPolicy  = WebRtcNsUtils .nsxSetPolicy (nsxId , 2 );
333338
334339 //agc初始化 
@@ -338,6 +343,18 @@ private void process() {
338343 int  agcSetConfig  = WebRtcAGCUtils .agcSetConfig (agcId , (short ) 3 , (short ) 20 , true );
339344 Log .e ("aaa" , "nexId--"  + nsxId  + "-----nsxInit----"  + nsxInit  + "---nexSetPolicy---"  + nexSetPolicy );
340345 Log .e ("aaa" , "agcId---->"  + agcId  + "-----agcInit--->"  + agcInit  + "----agcSetConfig--"  + agcSetConfig );
346+  int  sample  = 80 ;
347+  if (mSampleRate  == 8000 ){
348+  sample  =80 ;
349+  }else  if (mSampleRate  == 16000 ){
350+  sample  = 160 ;
351+  }else  if (mSampleRate  == 32000 ){
352+  sample  = 320 ;
353+  }else  if (mSampleRate  == 48000 ){
354+  sample  = 480 ;
355+  }
356+  final  int  finalSample  = sample ;
357+  mProcessFilePath  = AUDIO_PROCESS_FILE_PATH ;
341358 mThreadExecutor .execute (new  Runnable () {
342359 @ Override 
343360 public  void  run () {
@@ -349,6 +366,8 @@ public void run() {
349366 File  outFile  = new  File (AUDIO_PROCESS_FILE_PATH );
350367 out  = new  FileOutputStream (outFile );
351368
369+  Log .e ("aaa" ,"操作文件:" +inFile +"----mSampleRate:" +mSampleRate +"----->fq:" +finalSample );
370+ 352371 byte [] buf ;
353372 buf  = new  byte [320 ];
354373 while  (ins .read (buf ) != -1 ) {
@@ -359,8 +378,10 @@ public void run() {
359378 ByteBuffer .wrap (buf ).order (ByteOrder .LITTLE_ENDIAN ).asShortBuffer ().get (inputData );
360379 WebRtcNsUtils .WebRtcNsx_Process (nsxId , inputData , num_bands , nsProcessData );
361380 if  (mIsOpenAgc ) {
362-  int  ret  = WebRtcAGCUtils .agcProcess (agcId , nsProcessData , num_bands , 80 , outAgcData , 0 , 0 , 0 , false );
363- // Log.e("aaa", "agc--->ret" + ret); 
381+  int  ret  = WebRtcAGCUtils .agcProcess (agcId , nsProcessData , num_bands , finalSample , outAgcData , 0 , 0 , 0 , false );
382+  if (ret  !=0 ){
383+  Log .e ("aaa" ,"agcProcess 出问题" );
384+  }
364385 out .write (shortsToBytes (outAgcData ));
365386 } else  {
366387 out .write (shortsToBytes (nsProcessData ));
@@ -369,6 +390,7 @@ public void run() {
369390 runOnUiThread (new  Runnable () {
370391 @ Override 
371392 public  void  run () {
393+  mTvCurrentProcessFile .setText (mProcessFilePath );
372394 Toast .makeText (getApplicationContext (), "处理完成" , Toast .LENGTH_LONG ).show ();
373395 }
374396 });
@@ -398,6 +420,11 @@ public void run() {
398420
399421 }
400422
423+  @ Override 
424+  protected  void  onDestroy () {
425+  super .onDestroy ();
426+  AudioTrackManager .getInstance ().stop ();
427+  }
401428
402429 private  byte [] shortsToBytes (short [] data ) {
403430 byte [] buffer  = new  byte [data .length  * 2 ];
0 commit comments