Skip to content

Commit

Permalink
v1.5-添加音频可视化接口 扩展so库平台
Browse files Browse the repository at this point in the history
  • Loading branch information
zhaolewei committed Apr 8, 2019
1 parent 7dffc81 commit dac4e4e
Show file tree
Hide file tree
Showing 19 changed files with 880 additions and 8 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
3. 实时获取录音的音量、及录音byte数据
4. 获取wav/mp3录音文件的时长
5. 可配置录音的采样率、位宽 - v1.04更新
5. 录音可视化 - v1.05更新

### 博客
https://www.jianshu.com/p/c0222de2faed
Expand Down Expand Up @@ -94,6 +95,15 @@ https://www.jianshu.com/p/c0222de2faed
}
});
```
* 音频可视化数据监听
```java
recordManager.setRecordFftDataListener(new RecordFftDataListener() {
@Override
public void onFftData(byte[] data) {
audioView.setWaveData(data);
}
});
```
3. 录音控制
* 开始录音
```java
Expand Down
221 changes: 221 additions & 0 deletions app/src/main/java/com/main/zlw/zlwaudiorecorder/AudioView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,221 @@
package com.main.zlw.zlwaudiorecorder;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import java.util.ArrayList;
import java.util.List;

/**
* @author zhaolewei on 2018/8/17.
*/
public class AudioView extends View {

/**
* 频谱数量
*/
private static final int LUMP_COUNT = 128;
private static final int LUMP_WIDTH = 6;
private static final int LUMP_SPACE = 2;
private static final int LUMP_MIN_HEIGHT = LUMP_WIDTH;
private static final int LUMP_MAX_HEIGHT = 200;//TODO: HEIGHT
private static final int LUMP_SIZE = LUMP_WIDTH + LUMP_SPACE;
private static final int LUMP_COLOR = Color.parseColor("#6de8fd");

private static final int WAVE_SAMPLING_INTERVAL = 3;

private static final float SCALE = LUMP_MAX_HEIGHT / LUMP_COUNT;

private ShowStyle upShowStyle = ShowStyle.STYLE_HOLLOW_LUMP;
private ShowStyle downShowStyle = ShowStyle.STYLE_WAVE;

private byte[] waveData;
List<Point> pointList;

private Paint lumpPaint;
Path wavePath = new Path();


public AudioView(Context context) {
super(context);
init();
}

public AudioView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init();
}

public AudioView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}

private void init() {
lumpPaint = new Paint();
lumpPaint.setAntiAlias(true);
lumpPaint.setColor(LUMP_COLOR);

lumpPaint.setStrokeWidth(2);
lumpPaint.setStyle(Paint.Style.STROKE);
}

public void setWaveData(byte[] data) {
this.waveData = readyData(data);
genSamplingPoint(data);
invalidate();
}


public void setStyle(ShowStyle upShowStyle, ShowStyle downShowStyle) {
this.upShowStyle = upShowStyle;
this.downShowStyle = downShowStyle;
}


@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
wavePath.reset();

for (int i = 0; i < LUMP_COUNT; i++) {
if (waveData == null) {
canvas.drawRect((LUMP_WIDTH + LUMP_SPACE) * i,
LUMP_MAX_HEIGHT - LUMP_MIN_HEIGHT,
(LUMP_WIDTH + LUMP_SPACE) * i + LUMP_WIDTH,
LUMP_MAX_HEIGHT,
lumpPaint);
continue;
}

switch (upShowStyle) {
case STYLE_HOLLOW_LUMP:
drawLump(canvas, i, false);
break;
case STYLE_WAVE:
drawWave(canvas, i, false);
break;
default:
break;
}

switch (downShowStyle) {
case STYLE_HOLLOW_LUMP:
drawLump(canvas, i, true);
break;
case STYLE_WAVE:
drawWave(canvas, i, true);
break;
default:
break;
}
}
}

/**
* 预处理数据
*
* @return
*/
private static byte[] readyData(byte[] fft) {
byte[] newData = new byte[LUMP_COUNT];
byte abs;
for (int i = 0; i < LUMP_COUNT; i++) {
abs = (byte) Math.abs(fft[i]);
//描述:Math.abs -128时越界
newData[i] = abs < 0 ? 127 : abs;
}
return newData;
}

/**
* 绘制曲线
*
* @param canvas
* @param i
* @param reversal
*/
private void drawWave(Canvas canvas, int i, boolean reversal) {
if (pointList == null || pointList.size() < 2) {
return;
}
float ratio = SCALE * (reversal ? -1 : 1);
if (i < pointList.size() - 2) {
Point point = pointList.get(i);
Point nextPoint = pointList.get(i + 1);
int midX = (point.x + nextPoint.x) >> 1;
if (i == 0) {
wavePath.moveTo(point.x, LUMP_MAX_HEIGHT - point.y * ratio);
}
wavePath.cubicTo(midX, LUMP_MAX_HEIGHT - point.y * ratio,
midX, LUMP_MAX_HEIGHT - nextPoint.y * ratio,
nextPoint.x, LUMP_MAX_HEIGHT - nextPoint.y * ratio);

canvas.drawPath(wavePath, lumpPaint);
}
}

/**
* 绘制矩形条
*/
private void drawLump(Canvas canvas, int i, boolean reversal) {
int minus = reversal ? -1 : 1;
float top = (LUMP_MAX_HEIGHT - (LUMP_MIN_HEIGHT + waveData[i] * SCALE) * minus);

canvas.drawRect(LUMP_SIZE * i,
top,
LUMP_SIZE * i + LUMP_WIDTH,
LUMP_MAX_HEIGHT,
lumpPaint);
}

/**
* 生成波形图的采样数据,减少计算量
*
* @param data
*/
private void genSamplingPoint(byte[] data) {
if (upShowStyle != ShowStyle.STYLE_WAVE && downShowStyle != ShowStyle.STYLE_WAVE) {
return;
}
if (pointList == null) {
pointList = new ArrayList<>();
} else {
pointList.clear();
}
pointList.add(new Point(0, 0));
for (int i = WAVE_SAMPLING_INTERVAL; i < LUMP_COUNT; i += WAVE_SAMPLING_INTERVAL) {
pointList.add(new Point(LUMP_SIZE * i, waveData[i]));
}
pointList.add(new Point(LUMP_SIZE * LUMP_COUNT, 0));
}


/**
* 可视化样式
*/
public enum ShowStyle {
/**
* 空心的矩形小块
*/
STYLE_HOLLOW_LUMP,

/**
* 曲线
*/
STYLE_WAVE,

/**
* 不显示
*/
STYLE_NOTHING
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import com.zlw.main.recorderlib.RecordManager;
import com.zlw.main.recorderlib.recorder.RecordConfig;
import com.zlw.main.recorderlib.recorder.RecordHelper;
import com.zlw.main.recorderlib.recorder.listener.RecordDataListener;
import com.zlw.main.recorderlib.recorder.listener.RecordFftDataListener;
import com.zlw.main.recorderlib.recorder.listener.RecordResultListener;
import com.zlw.main.recorderlib.recorder.listener.RecordSoundSizeListener;
import com.zlw.main.recorderlib.recorder.listener.RecordStateListener;
Expand Down Expand Up @@ -46,6 +46,9 @@ public class MainActivity extends AppCompatActivity {
RadioGroup rgSimpleRate;
@BindView(R.id.tbEncoding)
RadioGroup tbEncoding;
@BindView(R.id.audioView)
AudioView audioView;

private boolean isStart = false;
private boolean isPause = false;
final RecordManager recordManager = RecordManager.getInstance();
Expand All @@ -55,6 +58,7 @@ protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
audioView.setStyle(AudioView.ShowStyle.STYLE_HOLLOW_LUMP, AudioView.ShowStyle.STYLE_HOLLOW_LUMP);
initEvent();
initRecord();
AndPermission.with(this)
Expand Down Expand Up @@ -171,6 +175,13 @@ public void onResult(File result) {
Toast.makeText(MainActivity.this, "录音文件: " + result.getAbsolutePath(), Toast.LENGTH_SHORT).show();
}
});

recordManager.setRecordFftDataListener(new RecordFftDataListener() {
@Override
public void onFftData(byte[] data) {
audioView.setWaveData(data);
}
});
}

@OnClick({R.id.btRecord, R.id.btStop})
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -156,4 +156,9 @@
android:layout_height="wrap_content"
android:text="停止" />

<com.main.zlw.zlwaudiorecorder.AudioView
android:id="@+id/audioView"
android:layout_width="match_parent"
android:layout_height="400dp" />

</LinearLayout>
Binary file modified doc/demo.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added recorderlib/libs/arm64-v8a/libmp3lame.so
Binary file not shown.
Binary file added recorderlib/libs/armeabi-v7a/libmp3lame.so
Binary file not shown.
Binary file modified recorderlib/libs/armeabi/libmp3lame.so
Binary file not shown.
Binary file added recorderlib/libs/mips/libmp3lame.so
Binary file not shown.
Binary file added recorderlib/libs/mips64/libmp3lame.so
Binary file not shown.
Binary file added recorderlib/libs/x86/libmp3lame.so
Binary file not shown.
Binary file added recorderlib/libs/x86_64/libmp3lame.so
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.zlw.main.recorderlib.recorder.RecordHelper;
import com.zlw.main.recorderlib.recorder.RecordService;
import com.zlw.main.recorderlib.recorder.listener.RecordDataListener;
import com.zlw.main.recorderlib.recorder.listener.RecordFftDataListener;
import com.zlw.main.recorderlib.recorder.listener.RecordResultListener;
import com.zlw.main.recorderlib.recorder.listener.RecordSoundSizeListener;
import com.zlw.main.recorderlib.recorder.listener.RecordStateListener;
Expand Down Expand Up @@ -92,19 +93,27 @@ public void setRecordDataListener(RecordDataListener listener) {
}

/**
* 录音音量监听回调
* 录音可视化数据回调,傅里叶转换后的频域数据
*/
public void setRecordSoundSizeListener(RecordSoundSizeListener listener) {
RecordService.setRecordSoundSizeListener(listener);
public void setRecordFftDataListener(RecordFftDataListener recordFftDataListener) {
RecordService.setRecordFftDataListener(recordFftDataListener);
}

/**
* 录音完成回调
* 录音文件转换结束回调
*/
public void setRecordResultListener(RecordResultListener listener) {
RecordService.setRecordResultListener(listener);
}

/**
* 录音音量监听回调
*/
public void setRecordSoundSizeListener(RecordSoundSizeListener listener) {
RecordService.setRecordSoundSizeListener(listener);
}


public boolean changeFormat(RecordConfig.RecordFormat recordFormat) {
return RecordService.changeFormat(recordFormat);
}
Expand Down
Loading

0 comments on commit dac4e4e

Please sign in to comment.