【发布时间】:2017-03-17 02:17:06
【问题描述】:
我正在尝试使用 audacity 使用的最小/最大算法从 .mp3 音频流中绘制波形。该算法从“块”样本中计算最小值和最大值,并在两点之间画一条垂直线。我几乎可以正确地以这种方式绘制波形,但有一些错误。这是一张图片: waveform
如您所见,一开始波形绘制正确,但随后“空白”开始出现,就像我避免绘制一些线条一样。我在我的代码中找不到错误并来这里寻求帮助!这是代码:
/**
*
* */
private void createWaveform(){
//Array that will contain all the chunks from the audio
Array<float[]> chunks = new Array<float[]>();
//the length of the audio in samples
int length = audio.getLengthInSamples();
//the amount of samples per pixel. w = screen width
samplesPerPixel = length/(int)w;
//get all samples
float[] samples = audio.getSamples();
//this is strange, but divides the samples in chunks. i can't find other way, better options are welcome
int max = samplesPerPixel;
int n = 0;
for (int i = 0; i < w; i++){
float[] chunk = new float[samplesPerPixel];
int k = 0;
for (int j = n ; j < max; j++){
chunk[k] = samples[j];
k++;
}
chunks.add(chunk);
n = max;
max+=samplesPerPixel;
}
min_max(chunks);
}
/**
* perform the min/max algorithm of all chunks and creates the lines between points
* */
public void min_max(Array<float[]> chunks){
float max, min;
float x = this.getX();
float y = this.getY()+this.h/2;
for (int i = 0; i < chunks.size; i++ ){
max = getMax(chunks.get(i))*(h/2);
min = getMin(chunks.get(i))*(h/2);
Line l = new Line(new Point(x+i,y+max), new Point(x+i,y+min));
lines.add(l);
}
}
/**
*
* */
private void drawLines(ShapeRenderer shapeRenderer){
for (int i = 0 ; i <lines.size; i++) {
Line l = lines.get(i);
Point p1 = l.getPMin();
Point p2 = l.getPMax();
try {
shapeRenderer.line(p1.x, p1.y, p2.x, p2.y);
}catch(Exception e){
System.out.println("error");
}
}
}
【问题讨论】:
-
我正在使用 java 和 libgdx
标签: java algorithm libgdx mp3 waveform