Skip to content

Commit

Permalink
[BugFix]修复mp3暂停后停止回调及录音问题
Browse files Browse the repository at this point in the history
Signed-off-by: zhaolewei <[email protected]>
  • Loading branch information
zhaolewei committed Jul 1, 2019
1 parent 7bcefbf commit dbb25ce
Showing 1 changed file with 21 additions and 105 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import java.util.List;
import java.util.Locale;

import fftlib.FFT;
import fftlib.FftFactory;

/**
Expand Down Expand Up @@ -121,10 +120,12 @@ public void stop() {
if (state == RecordState.PAUSE) {
makeFile();
state = RecordState.IDLE;
notifyState();
stopMp3Encoded();
} else {
state = RecordState.STOP;
notifyState();
}
notifyState();
}

void pause() {
Expand Down Expand Up @@ -196,20 +197,7 @@ public void run() {

private FftFactory fftFactory = new FftFactory(FftFactory.Level.Original);


private long time;


int i = 0;

private void notifyData(final byte[] data) {
// Logger.w(TAG, "time: %s", System.currentTimeMillis() - time);

time = System.currentTimeMillis();

if (++i % 4 == 0) {
return;
}
if (recordDataListener == null && recordSoundSizeListener == null && recordFftDataListener == null) {
return;
}
Expand All @@ -235,76 +223,7 @@ public void run() {
});
}

/**
* 优化处理FFT数据
*
* @param data pcm byte[]数据
* @return fft
*/
private byte[] makeFftData(byte[] data) {//data.length = 1280
if (data.length < 1024) {
return null;
}
try {
double[] ds = toHardDouble(ByteUtils.toShorts(data));
double[] fft = FFT.fft(ds, 62);
//start
double[] newFft = new double[128];
for (int i = 16; i < 16 + newFft.length; i++) {
if (i < 24) {
newFft[i - 16] = fft[i] * 0.2;
} else if (i < 36) {
newFft[i - 16] = fft[i] * 0.4;
} else if (i < 48) {
newFft[i - 16] = fft[i] * 0.6;
} else {
newFft[i - 16] = fft[i];
}
if (newFft[i - 16] < 10 * 128) {
newFft[i - 16] = newFft[i - 16] * 0.6;
}
}
fft = newFft;
//end
int step = fft.length / 128;
byte[] fftBytes = new byte[128];


int scale = 128;//压缩128基准
double max = getMax(fft);
if (max > 128 * 128) {//高音优化
scale = (int) (max / 128) + 2;
}

for (int i = 0; i < fftBytes.length; i++) {
double tmp = fft[i * step] / scale;
if (tmp > 127) {
fftBytes[i] = 127;
} else if (tmp < -128) {
fftBytes[i] = -127;

} else {
fftBytes[i] = (byte) tmp;
}
}
return fftBytes;
} catch (Exception e) {
Logger.w(TAG, e.getMessage());
}
return null;
}

private double getMax(double[] data) {
double max = 0;
for (int i = 0; i < data.length; i++) {
if (data[i] > max) {
max = data[i];
}
}
return max;
}

public int getDb(byte[] data) {
private int getDb(byte[] data) {
double sum = 0;
double ave;
int length = data.length > 128 ? 128 : data.length;
Expand All @@ -317,15 +236,6 @@ public int getDb(byte[] data) {
return i < 0 ? 27 : i;
}

private double[] toHardDouble(short[] shorts) {
int length = 512;
double[] ds = new double[length];
for (int i = 0; i < length; i++) {
ds[i] = shorts[i];
}
return ds;
}

private void initMp3EncoderThread(int bufferSize) {
try {
mp3EncodeThread = new Mp3EncodeThread(resultFile, bufferSize);
Expand All @@ -348,6 +258,8 @@ private class AudioRecordThread extends Thread {
if (currentConfig.getFormat() == RecordConfig.RecordFormat.MP3) {
if (mp3EncodeThread == null) {
initMp3EncoderThread(bufferSize);
} else {
Logger.e(TAG, "mp3EncodeThread != null, 请检查代码");
}
}
}
Expand Down Expand Up @@ -431,23 +343,27 @@ private void startMp3Recorder() {
if (state != RecordState.PAUSE) {
state = RecordState.IDLE;
notifyState();
if (mp3EncodeThread != null) {
mp3EncodeThread.stopSafe(new Mp3EncodeThread.EncordFinishListener() {
@Override
public void onFinish() {
notifyFinish();
mp3EncodeThread = null;
}
});
} else {
notifyFinish();
}
stopMp3Encoded();
} else {
Logger.d(TAG, "暂停");
}
}
}

private void stopMp3Encoded() {
if (mp3EncodeThread != null) {
mp3EncodeThread.stopSafe(new Mp3EncodeThread.EncordFinishListener() {
@Override
public void onFinish() {
notifyFinish();
mp3EncodeThread = null;
}
});
} else {
Logger.e(TAG, "mp3EncodeThread is null, 代码业务流程有误,请检查!! ");
}
}

private void makeFile() {
switch (currentConfig.getFormat()) {
case MP3:
Expand Down

0 comments on commit dbb25ce

Please sign in to comment.