【问题标题】:How to draw a waveform with min/max algorithm correctly? (like audacity does)如何正确绘制最小/最大算法的波形? (就像大胆一样)
【发布时间】: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


【解决方案1】:

如果有人试图在 java 中使用 Libgdx 实现相同的算法,我找到了解决方案。不要使用 ShapeRenderer,而是使用 Pixmaps 和 bug 消失。如果你只使用 java 不用担心,第一篇文章中的算法可以正常工作,你只需要调整一些东西,因为你可能正在使用 swing 或其他一些图形库,但逻辑是一样的。最终结果:

【讨论】:

    猜你喜欢
    • 2021-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多