flash - AS3 sound.extract() 用于波形

标签 flash actionscript-3 audio mp3 flashdevelop

早上好!

我正在尝试为 MP3 创建可视波形。我包含的代码在成功加载 MP3 时调用。我打算只从声音中提取几个重要的样本来创建波形,而不是将整个声音提取到字节数组中。即使在一台好的机器上,提取整首歌曲也会导致 flash 卡住 3-5 秒(或更长时间!)。就我而言,这是不可行的。

不幸的是,我下面的代码无法产生任何数字。如果我提取整首歌曲它会起作用,但仅提取关键点对我没有任何帮助。执行提取是否会使声音对象的其余部分对将来的提取无效?如果是这样,有没有办法解决这个问题,在提取过程中不会长时间卡住闪光?

其余代码中的一些重要变量:

waveFormWidth:波形 Sprite 的静态宽度。
waveFormHeight:波形 Sprite 的静态高度。
歌曲:我将用来创建波形的声音对象。

    public function mapWaveForm(e:Event = null):void
    {

        // Clear the wave form sprite
        waveForm.graphics.clear();
        waveForm.graphics.lineStyle(0, 0x000000, 1);

        // Storage for the wave form bit data
        var byteOutput:ByteArray;
        var leftPeakSize:Number;
        var rightPeakSize:Number;
        var songTotalSamples:int;
        var thisSample:int;

        byteOutput = new ByteArray();

        // How many samples?
        songTotalSamples = (song.length * 44.1);

        // Loop for the wave form width
        for (var peakCount:int = 0; (peakCount < waveFormWidth); peakCount++)
        {

            // Get info at each peak.
            thisSample = Math.floor(songTotalSamples * (peakCount / waveFormWidth));
            song.extract(byteOutput, 1, thisSample);
            byteOutput.position = 0;
            trace(thisSample, byteOutput.readFloat());

            leftPeakSize = byteOutput.readFloat() / 1.27;
            rightPeakSize = byteOutput.readFloat() / 1.27;

            // Turn those peaks into something usable.
            leftPeakSize = leftPeakSize * (waveFormHeight * .5);
            rightPeakSize = rightPeakSize * (waveFormHeight * .5);

            // Make the left channel line
            waveForm.graphics.moveTo(peakCount, (waveFormHeight * .5));
            waveForm.graphics.lineTo(peakCount, (waveFormHeight * .5) - leftPeakSize);

            // Make the right channel line
            waveForm.graphics.moveTo(peakCount, (waveFormHeight * .5));
            waveForm.graphics.lineTo(peakCount, (waveFormHeight * .5) + rightPeakSize);


        }

    }

谢谢你们的帮助!

最佳答案

对于它的值(value),我只是在做完全相同的事情,经过一些研究,我才开始工作。我似乎无法让它与 WAV 文件一起使用,但这与所有类型的 MP3 文件兼容。这就是我最终的结果:

var IMAGE_SIZE:int = new int(600); // Final width of image
var IMAGE_DEPTH:int = new int(5); // How many passes per pixel of image
var RESOLUTION:int = new int(IMAGE_SIZE * IMAGE_DEPTH);
var DRAW_AMPLITUDE:int = new int(150); // pixel height of max volume line

var waveForm:Shape = new Shape();

var mp3File:Sound = new Sound();
mp3File.load(new URLRequest("audio.mp3"));
mp3File.addEventListener(Event.COMPLETE, parseSound);

function parseSound(e:Event):void {
    var soundBytes:ByteArray = new ByteArray();
    for (var i:int=0; i<RESOLUTION; i++) {
        mp3File.extract(soundBytes, 1, Math.floor((mp3File.length*44.1)*(i/RESOLUTION)));
        soundBytes.position = 0;
        while (soundBytes.bytesAvailable > 0) {
            var tmpNum:Number = new Number();
            tmpNum += soundBytes.readFloat();
        }
        drawWave(i, tmpNum);
        soundBytes.clear();
    }
    this.addChild(waveForm);
    trace("--DONE--");
}

function drawWave(i:Number, amp:Number):void {
    var pixX:Number = new Number(Math.floor(i/IMAGE_DEPTH));
    var pixY:Number = new Number((amp*DRAW_AMPLITUDE)/2);
    waveForm.graphics.lineStyle(1, 0x0, (1.2/IMAGE_DEPTH));
    waveForm.graphics.moveTo(pixX, ((DRAW_AMPLITUDE/2)-pixY));
    waveForm.graphics.lineTo(pixX, ((DRAW_AMPLITUDE/2)+pixY));
}

关于flash - AS3 sound.extract() 用于波形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6168056/

相关文章:

matlab - 在Matlab中使用memmapfile读取并表示mp3文件

macos - 如何以编程方式列出当前在 Mac OS 10.6 上使用默认声音输出的进程

Flash builder 4 Debug模式下的跟踪问题

flash - AS3 : Impossible to copy DisplayObjects with content?

flash - java中如何确定包名,为什么 "com"作为root?

actionscript-3 - Flash AS3 FileReference-一次选择并上传多个文件

android - 查看特定文件夹中的音频

iphone - 将 Flash 艺术作品转换为 OpenGL 就绪的矢量格式?

actionscript-3 - 如何在flex中动态设置对象的属性或字段

actionscript-3 - AS3/AIR - 创建纯文本文件?