Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit a3de36f

Browse files
author
chenyouwei
committed
fix:采样率16k、32k处理完成
1 parent 25fea56 commit a3de36f

File tree

12 files changed

+110
-511
lines changed

12 files changed

+110
-511
lines changed

‎app/src/main/java/com/hugh/MainActivity.java‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import com.hugh.audiofun.FmodActivity;
99
import com.hugh.audiofun.R;
10+
import com.hugh.libwebrtc.other.RtcFileActivity;
1011
import com.hugh.webrtcdemo.RtcActivity;
1112
import com.hugh.sound.SoundTouchExActivity;
1213

@@ -44,5 +45,12 @@ public void onClick(View v) {
4445
startActivity(new Intent(MainActivity.this, RtcActivity.class));
4546
}
4647
});
48+
49+
findViewById(R.id.btn_go_test).setOnClickListener(new View.OnClickListener() {
50+
@Override
51+
public void onClick(View v) {
52+
53+
}
54+
});
4755
}
4856
}

‎app/src/main/java/com/hugh/component/audio/AudioTrackManager.java‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public class AudioTrackManager {
2525
/*音频的采样率,44.1kHz可以所有手机*/
2626
private static int SAMPLE_RATE_IN_HZ = 44100;
2727
/*音频的声道数,此处为单声道*/
28-
private static int CHANNEL_CONFIGURATION = AudioFormat.CHANNEL_IN_STEREO;
28+
private static int CHANNEL_CONFIGURATION = AudioFormat.CHANNEL_OUT_MONO;
2929
/*采样格式,数据位宽是16位*/
3030
private static int AUDIO_FORMAT = AudioFormat.ENCODING_PCM_16BIT;
3131
/*音频缓存区大小*/
@@ -55,10 +55,11 @@ public static AudioTrackManager getInstance(){
5555
/**
5656
* 初始化配置
5757
*/
58-
public void initConfig() {
58+
public void initConfig(intsample) {
5959

6060
if (null != mAudioTrack) mAudioTrack.release();
61-
61+
SAMPLE_RATE_IN_HZ =sample;
62+
Log.e("aaa","初始码率---->"+sample);
6263
mBufferSizeInBytes = AudioTrack.getMinBufferSize(SAMPLE_RATE_IN_HZ,CHANNEL_CONFIGURATION,AUDIO_FORMAT);
6364
mAudioTrack = new AudioTrack(STREAM_TYPE,SAMPLE_RATE_IN_HZ,CHANNEL_CONFIGURATION,AUDIO_FORMAT,mBufferSizeInBytes,AudioTrack.MODE_STREAM);
6465

@@ -84,7 +85,7 @@ public synchronized void play(String filePath){
8485
/**
8586
* 停止播放
8687
*/
87-
private void stop(){
88+
public void stop(){
8889
try {
8990
if (mAudioTrack != null){
9091

‎app/src/main/java/com/hugh/webrtcdemo/RtcActivity.java‎

Lines changed: 49 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525

2626
import com.hugh.audiofun.R;
27+
import com.hugh.component.audio.AudioTrackManager;
2728
import com.hugh.libwebrtc.WebRtcAGCUtils;
2829
import com.hugh.libwebrtc.WebRtcNsUtils;
2930
import 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];

‎app/src/main/res/layout/activity_main.xml‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,13 @@
3636
android:layout_marginTop="20dp"
3737
android:text="webRtc操作页面"
3838
android:textSize="20sp" />
39+
40+
<Button
41+
android:id="@+id/btn_go_test"
42+
android:layout_width="wrap_content"
43+
android:layout_height="wrap_content"
44+
android:layout_marginTop="20dp"
45+
android:visibility="gone"
46+
android:text="测试页面"
47+
android:textSize="20sp" />
3948
</LinearLayout>

‎app/src/main/res/layout/module_rtc_activity_main.xml‎

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,12 @@
9999
android:layout_height="wrap_content"
100100
android:text="选择本地手机文件" />
101101

102+
<Button
103+
android:id="@+id/btn_play_origin_file"
104+
android:layout_width="wrap_content"
105+
android:layout_height="wrap_content"
106+
android:text="播放原文件" />
107+
102108
<Button
103109
android:id="@+id/btn_choose_sample"
104110
android:layout_width="wrap_content"
@@ -165,19 +171,19 @@
165171
android:id="@+id/tv_current_processfile"
166172
android:layout_width="wrap_content"
167173
android:layout_height="wrap_content"
168-
android:layout_marginLeft="20dp"
169-
app:layout_constraintStart_toEndOf="@+id/tv_process_content"
170-
app:layout_constraintTop_toTopOf="parent"
174+
android:padding="1dp"
175+
app:layout_constraintStart_toStartOf="parent"
176+
app:layout_constraintTop_toBottomOf="@+id/tv_process_content"
171177
tools:text="ddddd" />
172178

173179
<Button
174180
android:id="@+id/btn_audio_play"
175-
app:layout_constraintTop_toBottomOf="@+id/tv_process_content"
176-
app:layout_constraintStart_toStartOf="parent"
177-
android:layout_marginTop="20dp"
178181
android:layout_width="wrap_content"
179182
android:layout_height="wrap_content"
180-
android:text="AudioTrack 播放当前生成文件"/>
183+
android:layout_marginTop="20dp"
184+
android:text="AudioTrack 播放当前生成文件"
185+
app:layout_constraintStart_toStartOf="parent"
186+
app:layout_constraintTop_toBottomOf="@+id/tv_current_processfile" />
181187

182188
</androidx.constraintlayout.widget.ConstraintLayout>
183189

‎libwebrtc/src/main/AndroidManifest.xml‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
package="com.hugh.libwebrtc" >
33
<application >
44

5-
<activity android:name=".other.AFRtcMainActivity"/>
65
<activity android:name=".other.RtcFileActivity"/>
76
</application>
87
</manifest>

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /