AS3:提取的样本数据永远循环最后一个块
我正在开发一个虚拟钢琴键盘,可以处理导入的 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;
}
}
必须有一个非常简单的解决方案,我只是没有处理原始数据的经验,所以如果您能帮助我解决任何问题,我将非常感激。
I'm working on a virtual piano keyboard that processes imported mp3 sound samples. I've come to a solution with this question, but as I run this test code below, the sound loops its last data chunk of each sound source forever, and obviously, I want it to stop at the end of the extracted sound data. The same happens with any extracted sample data, I don't even have to mix it. I added a dynamically drawn graphic waveform so you can visually check what happens. This is my first time using the ByteArray class, so I don't know how to solve this issue.
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;
}
}
There must be a very simple solution for this, I just don't have experience with handling raw data, so I'd be really thankful if you could help me out with anything.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,我几天前就想出了一个解决方案,只是没有时间在这里发布。
计算声音的 ByteArray 样本长度的简单方法是
samplesLength = sound.length * 44.1;
。这是因为声音以毫秒为单位显示其长度,即每秒 1000 个立体声样本对,而 Flash 中声音的采样分辨率为 44100 Hz(也是每秒样本对),因此渲染了 44.1 个样本对。短暂的毫秒。
另外,样本是 32 位浮点值,样本对的长度是 64 位(8 字节),所以如果我是正确的,它是
samplesLength = soundExtract.length / 8;
。Okay, I've come up with a solution a few days ago, I just didn't have time to post it here.
An easy way to calculate a sound's ByteArray sample length is
samplesLength = sound.length * 44.1;
.This is because a sound displays its length in milliseconds, which are 1000 stereo sample-pairs per second, and the sampling resolution for sounds in flash is 44100 Hz (also sample-pairs per second), so there are 44.1 sample-pairs rendered over a brief millisecond.
Also, a sample is a 32-bit Float value, and a sample-pair is 64 bits (8 bytes) long, so if I'm correct, it's
samplesLength = soundExtract.length / 8;
.