Skip to content

Commit a16f6c6

Browse files
author
chenyouwei
committed
feat:完成agc调试 数据处理还有点问题
1 parent 0e8d539 commit a16f6c6

File tree

5 files changed

+93
-77
lines changed

5 files changed

+93
-77
lines changed

libwebrtc/src/main/cpp/legacy_agc/agc-lib.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ extern "C" {
99
#endif
1010

1111
JNIEXPORT jlong JNICALL
12-
Java_com_hugh_libwebrtc_WebRtcAGCUtils_WebRtcAgc_1Create(JNIEnv *env, jobject obj) {
12+
Java_com_hugh_libwebrtc_WebRtcAGCUtils_WebRtcAgc_1Create(JNIEnv *env, jclass obj) {
1313
return (long) WebRtcAgc_Create();
1414
}
1515

1616

1717
JNIEXPORT jint JNICALL
18-
Java_com_hugh_libwebrtc_WebRtcAGCUtils_agcFree(JNIEnv *env, jobject obj,
18+
Java_com_hugh_libwebrtc_WebRtcAGCUtils_agcFree(JNIEnv *env, jclass obj,
1919
jlong agcInst) {
2020
void *_agcInst = (void *) agcInst;
2121
if (_agcInst == nullptr)
@@ -26,7 +26,7 @@ Java_com_hugh_libwebrtc_WebRtcAGCUtils_agcFree(JNIEnv *env, jobject obj,
2626

2727
JNIEXPORT jint JNICALL
2828
Java_com_hugh_libwebrtc_WebRtcAGCUtils_WebRtcAgc_1Init(JNIEnv *env,
29-
jobject obj, jlong agcInst,
29+
jclass obj, jlong agcInst,
3030
jint minLevel, jint maxLevel,
3131
jint agcMode, jint fs) {
3232
void *_agcInst = (void *) agcInst;
@@ -36,7 +36,7 @@ Java_com_hugh_libwebrtc_WebRtcAGCUtils_WebRtcAgc_1Init(JNIEnv *env,
3636
}
3737

3838
JNIEXPORT jint JNICALL
39-
Java_com_hugh_libwebrtc_WebRtcAGCUtils_agcSetConfig(JNIEnv *env, jobject obj,
39+
Java_com_hugh_libwebrtc_WebRtcAGCUtils_agcSetConfig(JNIEnv *env, jclass obj,
4040
jlong agcInst,
4141
jshort targetLevelDbfs,
4242
jshort compressionGaindB,
@@ -53,7 +53,7 @@ Java_com_hugh_libwebrtc_WebRtcAGCUtils_agcSetConfig(JNIEnv *env, jobject obj,
5353
}
5454

5555
JNIEXPORT jint JNICALL
56-
Java_com_hugh_libwebrtc_WebRtcAGCUtils_agcProcess(JNIEnv *env, jobject obj,
56+
Java_com_hugh_libwebrtc_WebRtcAGCUtils_agcProcess(JNIEnv *env, jclass obj,
5757
jlong agcInst,
5858
jshortArray inNear,
5959
jint num_bands,
@@ -80,7 +80,7 @@ Java_com_hugh_libwebrtc_WebRtcAGCUtils_agcProcess(JNIEnv *env, jobject obj,
8080

8181

8282
JNIEXPORT jint JNICALL
83-
Java_com_hugh_libwebrtc_WebRtcAGCUtils_agcAddFarend(JNIEnv *env, jobject obj,
83+
Java_com_hugh_libwebrtc_WebRtcAGCUtils_agcAddFarend(JNIEnv *env, jclass obj,
8484
jlong agcInst,
8585
jshortArray inFar,
8686
jint samples) {
@@ -95,7 +95,7 @@ Java_com_hugh_libwebrtc_WebRtcAGCUtils_agcAddFarend(JNIEnv *env, jobject obj,
9595

9696

9797
JNIEXPORT jint JNICALL
98-
Java_com_hugh_libwebrtc_WebRtcAGCUtils_agcAddMic(JNIEnv *env, jobject obj,
98+
Java_com_hugh_libwebrtc_WebRtcAGCUtils_agcAddMic(JNIEnv *env, jclass obj,
9999
jlong agcInst,
100100
jshortArray inMic,
101101
jint num_bands, jint samples
@@ -111,7 +111,7 @@ Java_com_hugh_libwebrtc_WebRtcAGCUtils_agcAddMic(JNIEnv *env, jobject obj,
111111

112112

113113
JNIEXPORT jint JNICALL
114-
Java_com_hugh_libwebrtc_WebRtcAGCUtils_agcVirtualMic(JNIEnv *env, jobject obj,
114+
Java_com_hugh_libwebrtc_WebRtcAGCUtils_agcVirtualMic(JNIEnv *env, jclass obj,
115115
jlong agcInst,
116116
jshortArray inMic,
117117
jint num_bands,

libwebrtc/src/main/java/com/hugh/libwebrtc/RtcActivity.java

Lines changed: 57 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
import android.util.Log;
1616
import android.view.View;
1717
import android.widget.Button;
18+
import android.widget.CompoundButton;
1819
import android.widget.RadioGroup;
20+
import android.widget.Switch;
1921
import 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

libwebrtc/src/main/java/com/hugh/libwebrtc/kt/RtcFileActivity.kt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import kotlin.concurrent.thread
2323
class RtcFileActivity : AppCompatActivity() {
2424
private val tag = "MainActivity"
2525
var isStop = false
26+
private val smpleRate = 16000;
2627
override fun onCreate(savedInstanceState: Bundle?) {
2728
super.onCreate(savedInstanceState)
2829
setContentView(R.layout.module_rtc_activity_file)
@@ -42,7 +43,7 @@ class RtcFileActivity : AppCompatActivity() {
4243
getSystemService(Context.AUDIO_SERVICE) as AudioManager
4344
val bufferSize: Int =
4445
AudioTrack.getMinBufferSize(
45-
16000,
46+
smpleRate,
4647
AudioFormat.CHANNEL_OUT_MONO,
4748
AudioFormat.ENCODING_PCM_16BIT
4849
)
@@ -51,7 +52,7 @@ class RtcFileActivity : AppCompatActivity() {
5152
.build()
5253
val audioFormat: AudioFormat = AudioFormat.Builder()
5354
.setEncoding(AudioFormat.ENCODING_PCM_16BIT)
54-
.setSampleRate(16000)
55+
.setSampleRate(smpleRate)
5556
.setChannelMask(AudioFormat.CHANNEL_OUT_MONO)
5657
.build()
5758
val sessionId = audioManager.generateAudioSessionId()
@@ -73,7 +74,7 @@ class RtcFileActivity : AppCompatActivity() {
7374

7475
agcUtils = WebRtcAGCUtils()
7576
agcId = WebRtcAgc_Create()
76-
val agcInitResult = WebRtcAgc_Init(agcId, 0, 255, 3, 16000)
77+
val agcInitResult = WebRtcAgc_Init(agcId, 0, 255, 3, smpleRate)
7778
val agcSetConfigResult = agcSetConfig(agcId, 9, 9, true)
7879
Log.e(
7980
tag,
@@ -97,10 +98,11 @@ class RtcFileActivity : AppCompatActivity() {
9798
.asShortBuffer()
9899
.get(inputData)
99100
WebRtcNsx_Process(nsxId, inputData, 1, outNsData)
100-
agcProcess(
101+
var ret = agcProcess(
101102
agcId, outNsData, 1, 160, outAgcData,
102103
0, 0, 0, false
103104
)
105+
Log.e("aaa", "ret---->$ret");
104106
if (isStop) {
105107
return@run
106108
}

libwebrtc/src/main/res/layout/module_rtc_activity_file.xml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,13 @@
33
xmlns:app="http://schemas.android.com/apk/res-auto"
44
xmlns:tools="http://schemas.android.com/tools"
55
android:layout_width="match_parent"
6-
android:layout_height="match_parent" >
6+
android:layout_height="match_parent">
77

88
<Switch
99
android:id="@+id/enable_ns_agc_switch"
1010
android:layout_width="wrap_content"
1111
android:layout_height="wrap_content"
1212
android:text="开启 NS &amp; AGC?"
13-
app:layout_constraintBottom_toBottomOf="parent"
1413
app:layout_constraintEnd_toEndOf="parent"
1514
app:layout_constraintStart_toStartOf="parent"
1615
app:layout_constraintTop_toTopOf="parent" />

libwebrtc/src/main/res/layout/module_rtc_activity_main.xml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,25 @@
3636
android:layout_height="wrap_content"
3737
android:text="音频降噪处理" />
3838

39+
<LinearLayout
40+
android:id="@+id/layout_agc"
41+
android:layout_width="wrap_content"
42+
android:layout_height="wrap_content"
43+
android:orientation="horizontal">
44+
45+
<TextView
46+
android:layout_width="wrap_content"
47+
android:layout_height="wrap_content"
48+
android:text="是否开启agc增益"
49+
android:textSize="16sp"
50+
android:textStyle="bold" />
51+
52+
<Switch
53+
android:id="@+id/agc_switch"
54+
android:layout_marginLeft="5dp"
55+
android:layout_width="wrap_content"
56+
android:layout_height="wrap_content" />
57+
58+
</LinearLayout>
59+
3960
</LinearLayout>

0 commit comments

Comments
 (0)