gpt4 book ai didi

actionscript-3 - AS3:提取的sampledata循环永远到最后一 block

转载 作者:行者123 更新时间:2023-12-03 01:01:07 24 4
gpt4 key购买 nike

我正在研究处理导入的mp3声音样本的虚拟钢琴键盘。我已经解决了this问题,但是当我在下面运行此测试代码时,声音将永远循环每个声源的最后一个数据块,显然,我希望它在提取的声音数据的末尾停止。任何提取的样本数据也会发生同样的情况,我什至不必混合使用。我添加了动态绘制的图形波形,以便您可以直观地查看发生了什么。这是我第一次使用ByteArray类,所以我不知道如何解决此问题。

import flash.media.Sound;
import flash.utils.ByteArray;
import flash.geom.Point;
import flash.display.Sprite;
import flash.net.URLRequest;
import flash.events.MouseEvent;

var t0:Sound = new Sound(new URLRequest("5_C.mp3")); //sound imported
var t1:Sound = new Sound(new URLRequest("5_E.mp3")); //other sound imported
//var t0:Sound = new C4(); //sound in library
//var t1:Sound = new E4(); //other sound in library
var t0p:Point = new Point();
var t1p:Point = new Point();
var t0a:ByteArray = new ByteArray();
var t1a:ByteArray = new ByteArray();
var mix:Sound = new Sound();

var spr:Sprite = new Sprite();
addChild(spr);
spr.y = 10;
var lx:Number = 0;
var ly:Number = 0;
spr.graphics.lineStyle(1,0,1,true);

mix.addEventListener(SampleDataEvent.SAMPLE_DATA, onSampleData);

stage.addEventListener(MouseEvent.CLICK, mce);
function mce(e:MouseEvent) {
mix.play();
}

function onSampleData(e:SampleDataEvent):void {

t0a.position = 0;
t1a.position = 0;

t0.extract(t0a, 3072);
t1.extract(t1a, 3072);

t0a.position = 0;
t1a.position = 0;

for(var i:int = 0; i < 3072; i++) {

t0p.x = t0a.readFloat();
t0p.y = t0a.readFloat();
t1p.x = t1a.readFloat();
t1p.y = t1a.readFloat();

var tmp:Point = new Point(Math.max(Math.min(t0p.x + t1p.x, 1), -1), Math.max(Math.min(t0p.y + t1p.y, 1), -1));

e.data.writeFloat(tmp.x); // left
e.data.writeFloat(tmp.y); // right

if (lx > 549) {
lx = 0;
ly += 10;
if (ly > 399) {
spr.graphics.clear();
ly = 0;
spr.graphics.lineStyle(1,0,1,true);
}
spr.graphics.moveTo(lx, (tmp.x + tmp.y) * 4 + ly);
} else {
spr.graphics.lineTo(lx, (tmp.x + tmp.y) * 4 + ly);
}
lx += 0.05;

}
}

对此必须有一个非常简单的解决方案,我只是没有处理原始数据的经验,因此,如果您能为我提供任何帮助,我将非常感谢。

最佳答案

好的,几天前我想出了一个解决方案,只是没有时间在这里发布。

计算声音的ByteArray样本长度的一种简单方法是samplesLength = sound.length * 44.1;

这是因为声音以毫秒为单位显示其长度,即每秒1000个立体声采样对,并且闪光灯中声音的采样分辨率为44100 Hz(也就是每秒的采样对),因此渲染了44.1个采样对。短暂的毫秒。

另外,样本是32位浮点值,样本对的长度是64位(8字节),因此,如果我是对的,则为samplesLength = soundExtract.length / 8;

关于actionscript-3 - AS3:提取的sampledata循环永远到最后一 block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8181177/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com