1515import android .util .Log ;
1616import android .view .View ;
1717import android .widget .Button ;
18+ import android .widget .CompoundButton ;
1819import android .widget .RadioGroup ;
20+ import android .widget .Switch ;
1921import android .widget .Toast ;
2022
2123
@@ -45,9 +47,6 @@ public class RtcActivity extends Activity implements View.OnClickListener {
4547 private static final String AUDIO_FILE_AST_8K = "record/recorded_audio.pcm" ;
4648 private static final String AUDIO_FILE_AST_16k = "record/recorded_audio_16k.pcm" ;
4749 private static final String AUDIO_FILE_AST_32k = "record/recorded_audio_32k.pcm" ;
48- private static final String AUDIO_FILE_AST_44k = "record/recorded_audio_44k.pcm" ;
49- // private static final String AUDIO_FILE_AST_32k = "record/test_32k.pcm";
50- private static final String AUDIO_FILE_AST_32k_TEST = "record/recorded_audio_fun.pcm" ;
5150
5251 /**
5352 * 原始音频文件路径
@@ -58,12 +57,7 @@ public class RtcActivity extends Activity implements View.OnClickListener {
5857 "/recorded_audio_16k.pcm" ;
5958 private static final String AUDIO_FILE_PATH_32K = Environment .getExternalStorageDirectory ().getPath () +
6059 "/recorded_audio_32k.pcm" ;
61- private static final String AUDIO_FILE_PATH_44K = Environment .getExternalStorageDirectory ().getPath () +
62- "/recorded_audio_44k.pcm" ;
63- // private static final String AUDIO_FILE_PATH_32K = Environment.getExternalStorageDirectory().getPath() +
64- // "/test_32k.pcm";
65- private static final String AUDIO_FILE_PATH_32K_TEST = Environment .getExternalStorageDirectory ().getPath () +
66- "/recorded_audio_fun.pcm" ;
60+ 6761 /**
6862 * 处理过的音频文件路径
6963 */
@@ -73,13 +67,6 @@ public class RtcActivity extends Activity implements View.OnClickListener {
7367 "/recorded_audio_process_16k.pcm" ;
7468 private static final String AUDIO_PROCESS_FILE_PATH_32k = Environment .getExternalStorageDirectory ().getPath () +
7569 "/recorded_audio_process_32k.pcm" ;
76- // private static final String AUDIO_PROCESS_FILE_PATH_32k = Environment.getExternalStorageDirectory().getPath
77- // () +
78- // "/test_process_32k.pcm";
79- private static final String AUDIO_PROCESS_FILE_PATH_44k = Environment .getExternalStorageDirectory ().getPath () +
80- "/recorded_audio_process_44k.pcm" ;
81- private static final String AUDIO_PROCESS_FILE_PATH_32k_TEST = Environment .getExternalStorageDirectory ().getPath () +
82- "/recorded_audio_process_fun.pcm" ;
8370
8471
8572 private Button mBtnNsOperate ;
@@ -97,11 +84,30 @@ public class RtcActivity extends Activity implements View.OnClickListener {
9784 private ExecutorService mThreadExecutor ;
9885 private int selectId = -1 ;
9986 private boolean isPlaying ;
87+ private Switch agc_switch ;
88+ private boolean mIsOpenAgc ;
89+ private long nsxId ; //ns降噪id
90+ private long agcId ; //agc增益id
91+ private int num_bands = 1 ;
92+ 10093
10194 @ Override
10295 protected void onCreate (@ Nullable Bundle savedInstanceState ) {
10396 super .onCreate (savedInstanceState );
10497 setContentView (R .layout .module_rtc_activity_main );
98+ agc_switch = findViewById (R .id .agc_switch );
99+ agc_switch .setOnCheckedChangeListener (new CompoundButton .OnCheckedChangeListener () {
100+ @ Override
101+ public void onCheckedChanged (CompoundButton buttonView , boolean isChecked ) {
102+ mIsOpenAgc = isChecked ;
103+ if (isChecked ) {
104+ Toast .makeText (RtcActivity .this , "开启agc增益" , Toast .LENGTH_LONG ).show ();
105+ } else {
106+ Toast .makeText (RtcActivity .this , "开启agc增益" , Toast .LENGTH_LONG ).show ();
107+ }
108+ Log .e ("aaa" , "mIsOpenAgc------>" + mIsOpenAgc );
109+ }
110+ });
105111 mBtnNsOperate = findViewById (R .id .ns_audio );
106112 mBtnNsOperate .setOnClickListener (this );
107113 selectId = R .id .rb_8k ;
@@ -123,10 +129,9 @@ public void onCheckedChanged(RadioGroup group, int checkedId) {
123129 } else {
124130 initAudioFile ();
125131 }
126- 127- // startActivity(new Intent(RtcActivity.this,AFRtcMainActivity.class));
128132 }
129133
134+ 130135 private void initAudioFile () {
131136 mSampleRate = SAMPLERATE_8K ;
132137 AUDIO_FILE_PATH = AUDIO_FILE_PATH_8k ;
@@ -181,16 +186,16 @@ private void initAudio() {
181186 if (TextUtils .isEmpty (srcPath ) || TextUtils .isEmpty (AUDIO_FILE_PATH ) || TextUtils .isEmpty (AUDIO_PROCESS_FILE_PATH )) {
182187 return ;
183188 }
184- Log .e ("sws " , "srcPath==" + srcPath );
185- Log .e ("sws " , "AUDIO_PROCESS_FILE_PATH==" + AUDIO_PROCESS_FILE_PATH );
186- Log .e ("sws " , "AUDIO_FILE_PATH==" + AUDIO_FILE_PATH );
189+ Log .e ("aaa " , "srcPath==" + srcPath );
190+ Log .e ("aaa " , "AUDIO_PROCESS_FILE_PATH==" + AUDIO_PROCESS_FILE_PATH );
191+ Log .e ("aaa " , "AUDIO_FILE_PATH==" + AUDIO_FILE_PATH );
187192
188193 mProcessFile = new File (AUDIO_PROCESS_FILE_PATH );
189194
190195 mFile = new File (AUDIO_FILE_PATH );
191196
192197 if (!mFile .exists () || mFile .length () <= 0 ) {
193- Log .e ("sws " , " init file-----------" );
198+ Log .e ("aaa " , " init file-----------" );
194199 mThreadExecutor .execute (new Runnable () {
195200 @ Override
196201 public void run () {
@@ -206,14 +211,14 @@ public void run() {
206211 inputStream .close ();
207212 fileOutputStream .close ();
208213 isInitialized = true ;
209- Log .e ("sws " , " init file end-----------" );
214+ Log .e ("aaa " , " init file end-----------" );
210215 } catch (IOException e ) {
211216 e .printStackTrace ();
212217 }
213218 }
214219 });
215220 } else {
216- Log .e ("sws " , "-----------" );
221+ Log .e ("aaa " , "-----------" );
217222 isInitialized = true ;
218223 }
219224 }
@@ -236,18 +241,28 @@ private void stopPlay() {
236241 }
237242 }
238243
244+ 239245 private void process () {
240246 if (isProcessing ) {
241247 return ;
242248 }
243249 isProcessing = true ;
250+ //ns初始化
251+ //fs == 8000 || fs == 16000 || fs == 32000 || fs == 48000
252+ nsxId = WebRtcNsUtils .WebRtcNsx_Create ();
253+ int nsxInit = WebRtcNsUtils .WebRtcNsx_Init (nsxId , 8000 ); //0代表成功
254+ int nexSetPolicy = WebRtcNsUtils .nsxSetPolicy (nsxId , 2 );
255+ 256+ //agc初始化
257+ agcId = WebRtcAGCUtils .WebRtcAgc_Create ();
258+ //agcMode 0,1,2,3
259+ int agcInit = WebRtcAGCUtils .WebRtcAgc_Init (agcId , 0 , 255 , 3 , mSampleRate );
260+ int agcSetConfig = WebRtcAGCUtils .agcSetConfig (agcId , (short )3 , (short ) 20 , true );
261+ Log .e ("aaa" , "nexId--" + nsxId + "-----nsxInit----" + nsxInit + "---nexSetPolicy---" + nexSetPolicy );
262+ Log .e ("aaa" , "agcId---->" + agcId + "-----agcInit--->" + agcInit + "----agcSetConfig--" + agcSetConfig );
244263 mThreadExecutor .execute (new Runnable () {
245264 @ Override
246265 public void run () {
247- // WebRtcUtils.webRtcAgcInit(0, 255, mSampleRate);
248- // WebRtcNsUtils.webRtcNsInit(mSampleRate);
249- 250- Log .e ("sws" , "====mSampleRate=" + mSampleRate + ": process32KData=" + process32KData );
251266 FileInputStream ins = null ;
252267 FileOutputStream out = null ;
253268 try {
@@ -257,42 +272,21 @@ public void run() {
257272 out = new FileOutputStream (outFile );
258273
259274 byte [] buf ;
260- if (process32KData ) {
261- //TODO
262- /*
263- * 测试发现,32k采样率,数据buf越少,增益后可能有滋滋的声音
264- *
265- */
266- buf = new byte [640 * 40 ];
267- } else {
268- buf = new byte [320 ];
269- }
275+ buf = new byte [320 ];
270276 while (ins .read (buf ) != -1 ) {
271- short [] shortData = new short [buf .length >> 1 ];
272- 273- short [] processData = new short [buf .length >> 1 ];
274- 275- ByteBuffer .wrap (buf ).order (ByteOrder .LITTLE_ENDIAN ).asShortBuffer ().get (shortData );
276- 277- if (process32KData ) {
278- // short[] nsProcessData =shortData;
279- // short[] nsProcessData = WebRtcUtils.webRtcNsProcess32k(shortData.length, shortData);
280- // WebRtcUtils.webRtcAgcProcess32k(nsProcessData, processData, nsProcessData.length);
281- // out.write(shortsToBytes(processData));
277+ short [] inputData = new short [buf .length >> 1 ];
278+ short [] nsProcessData = new short [buf .length >> 1 ];
279+ short [] outAgcData = new short [buf .length >> 1 ];
280+ 281+ ByteBuffer .wrap (buf ).order (ByteOrder .LITTLE_ENDIAN ).asShortBuffer ().get (inputData );
282+ WebRtcNsUtils .WebRtcNsx_Process (nsxId , inputData , num_bands , nsProcessData );
283+ if (mIsOpenAgc ) {
284+ int ret = WebRtcAGCUtils .agcProcess (agcId , nsProcessData , num_bands , 80 , outAgcData , 0 , 0 , 0 , false );
285+ Log .e ("aaa" , "agc--->ret" + ret );
286+ out .write (shortsToBytes (outAgcData ));
282287 } else {
283- short [] nsProcessData = new short [160 ];
284- if (selectId == R .id .rb_16k ) {
285- // nsProcessData = WebRtcNsUtils.webRtcNsProcess(mSampleRate, shortData.length, shortData);
286- // WebRtcUtils.webRtcAgcProcess(nsProcessData, processData, shortData.length);
287- // out.write(shortsToBytes(nsProcessData));
288- } else if (selectId == R .id .rb_8k ) {
289- Log .e ("aaa" , "shortData.length---->" + shortData .length );
290- // WebRtcNsUtils.WebRtcNsx_Process(WebRtcNs, shortData,1, nsProcessData);
291- // WebRtcUtils.webRtcAgcProcess(nsProcessData, processData, nsProcessData.length);
292- out .write (shortsToBytes (nsProcessData ));
293- }
288+ out .write (shortsToBytes (nsProcessData ));
294289 }
295- 296290 }
297291 runOnUiThread (new Runnable () {
298292 @ Override
@@ -304,7 +298,8 @@ public void run() {
304298 e .printStackTrace ();
305299 } finally {
306300 isProcessing = false ;
307- // WebRtcNsUtils.webRtcNsFree();
301+ WebRtcNsUtils .WebRtcNsx_Free (nsxId );
302+ WebRtcAGCUtils .agcFree (agcId );
308303 if (out != null ) {
309304 try {
310305 out .close ();
@@ -320,7 +315,6 @@ public void run() {
320315 }
321316 }
322317 }
323- Log .e ("sws" , "ns end======" );
324318 }
325319 });
326320
0 commit comments