加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 07:28:33 所属栏目:百科 来源:网络整理
导读:早上好! 我正在尝试为MP3创建视觉波形.我所包含的代码是在成功加载MP3时调用的.我打算从声音中提取一些重要的样本来创建波形,而不是将整个声音提取到一个bytearray中.即使在一台好机器上,提取整首歌曲也会导致闪光灯冻结3-5秒(或更长时间!).就我的目的而言
早上好!

我正在尝试为MP3创建视觉波形.我所包含的代码是在成功加载MP3时调用的.我打算从声音中提取一些重要的样本来创建波形,而不是将整个声音提取到一个bytearray中.即使在一台好机器上,提取整首歌曲也会导致闪光灯冻结3-5秒(或更长时间!).就我的目的而言,这是不可行的.

不幸的是,我在下面得到的代码没有产生任何数字.如果我提取整个歌曲它的功能,但提取关键点并没有给我什么.执行提取是否会使声音对象的其余部分对将来的提取无效?如果是这样,有什么方法可以在提取过程中长时间不冻结闪存?

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

waveFormWidth:波形精灵的静态宽度.
waveFormHeight:波形精灵的静态高度.
song:我将用于创建波形的声音对象.

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) + rightPeakSize);


        }

    }

谢谢你的帮助!

解决方法

对于它的价值,我只是在研究完全相同的东西,我只是在经过一些研究后才开始工作.我似乎无法使用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,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));
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读