从ByteArray中混合2声音 [英] mixing 2 sounds from ByteArray
问题描述
我有建立一个混频器和保存所有的序列中的一个数组,然后再次发挥它,现在我要救组合作为一个MP3,我一直在寻求什么办法可以挽救搭配,答案是加载听起来的ByteArray(sound.extract)我有acomplish这一点,但我真的不知道该怎么来存储所有的声音在短短的一个ByteArray中,才能将其保存为MP3,我得到这个code只是举例,装载2音频文件,并把它们存储在单独的ByteArray,并发挥各自的声音,没有任何身体知道如何存储2 ByteArray对象中只有一个?
VAR为mySound:声音=新的声音();
VAR sourceSnd:声音=新的声音();
VAR urlReq:的URLRequest =新的URLRequest(Track1.mp3);
sourceSnd.load(urlReq);
sourceSnd.addEventListener(引发Event.COMPLETE,加载);
功能加载(事件:事件):无效
{
mySound.addEventListener(的SampleDataEvent.SAMPLE_DATA,processSound);
//mySound.play();
}
VAR mySound2:声音=新的声音();
VAR sourceSnd2:声音=新的声音();
VAR urlReq2:的URLRequest =新的URLRequest(Track2.mp3);
sourceSnd2.load(urlReq2);
sourceSnd2.addEventListener(引发Event.COMPLETE,loaded2);
功能loaded2(事件:事件):无效
{
mySound2.addEventListener(的SampleDataEvent.SAMPLE_DATA,processSound2);
mySound2.play();
mySound.play();
}
功能processSound(事件:的SampleDataEvent):无效
{
VAR字节:的ByteArray =新的ByteArray();
sourceSnd.extract(字节,8192);
event.data.writeBytes(字节);
}
功能processSound2(事件:的SampleDataEvent):无效
{
VAR字节:的ByteArray =新的ByteArray();
sourceSnd2.extract(字节,8192);
event.data.writeBytes(字节);
}
一直在研究一个类似的系统了一段时间,我会尽我所能,给你一些方向:
您例如code是不是真的混合MP3的 - 它创造2更多的声音回放加载MP3的通过的SampleDataEvent。你需要做的是只创建一个输出声音文件,该文件将保存/播放所产生的混合声音。您可以轻松地保存数据,因为它发生的,随后该文件保存为新的WAV / MP3 /什么具备的,你。
真正/ psuedo- code(读:懒惰):
输出=新的声音();
output.addEventListener(的SampleDataEvent.SAMPLE_DATA,mixAudio);
松1 =新的声音/加载首MP3
song2 =新的声音/加载第二MP3
//将ByteArray的记录的输出和随后的文件创建
recordedBytes =新的ByteArray();
等到可以同时拥有MP3的完全加载,或运行一个输入框来确定当两个声音不再缓冲(Sound.isBuffering)
当MP3的准备:
//数字存储一些值
VAR LEFT1:数字;
VAR RIGHT1:数字;
VAR LEFT2:数字;
VAR RIGHT2:数字;
//为ByteArrays的提取和混合
VAR bytes1:ByteArray的=新的ByteArray();
VAR字节2:ByteArray的=新的ByteArray();
//开始演出
output.play();
功能mixAudio(E:的SampleDataEvent):无效{
//设置ByteArray的位置为0
bytes1.position = 0;
bytes2.position = 0;
//提取
song1.extract(bytes1,8192);
song2.extract(字节2,8192);
// ByteArray的位置重置为0阅读花车
bytes1.position = 0;
bytes2.position = 0;
//通过所有字节/彩车运行
变种B:INT = 0;
而(B< 8192){
LEFT1 = bytes1.readFloat(); //被左的信号
RIGHT1 = bytes1.readFloat(); //得到正确的信号
LEFT2 = bytes2.readFloat();
RIGHT2 = bytes2.readFloat();
// 混合!
VAR newLeft:数=(LEFT1 + LEFT2)* 0.5;
VAR newRight:数=(RIGHT1 + RIGHT2)* 0.5;
//写入新的东西输出声音的
e.data.writeFloat(newLeft);
e.data.writeFloat(newRight);
//写号的记录的字节数组
recordedBytes.writeFloat(newLeft);
recordedBytes.writeFloat(newRight);
b ++;
}
}
是 - 你真的应该上限可能输出-1/1。做到这一点。这是极其未优化!
确定。所以这是比较容易的部分!艰难的部分是真正将最终ByteArray的MP3。在Flash音频存在的PCM / uncom pressed数据,MP3显然是一个COM pressed格式。这个答案已经太长而这一切信息,我已经从一些非常聪明的人收集。
您可以轻松地适应MicRecorder'是一个通用的声音数据记录: http://www.bytearray.org/?p=1858
转换为MP3将是一个婊子:蒂博对ByteArray.org另一个职位 - 搜索LAME MP3
很好的例子/资源: <一href="http://www.anttikupila.com/flash/soundfx-out-of-the-box-audio-filters-with-actionscript-3/">http://www.anttikupila.com/flash/soundfx-out-of-the-box-audio-filters-with-actionscript-3/
查找安德烈·米歇尔的开放源代码对谷歌code'Tonfall项目。
查找凯文·史密斯的博客和实验室 - 他得到了在使用Pixel Bender的这一切疯狂很好的例子
希望这有助于!
PS - 以安德烈的提示,音频缓冲的最佳长度应为3072试试看你的机器上
!I have build a mixer and save all the sequence in an array and then play it again, now I want to save the mix as an MP3, I have looked for any way to save the mix and the answer is to load the sounds as byteArrays (sound.extract) I have acomplish that but I don't really know how to store all the sounds in just one ByteArray in order to save it as MP3, I got this code just for example, loading 2 audio files and store them in separate ByteArrays, and play each sound, does any body know how to store the 2 byteArrays in just one?
var mySound:Sound = new Sound();
var sourceSnd:Sound = new Sound();
var urlReq:URLRequest = new URLRequest("Track1.mp3");
sourceSnd.load(urlReq);
sourceSnd.addEventListener(Event.COMPLETE, loaded);
function loaded(event:Event):void
{
mySound.addEventListener(SampleDataEvent.SAMPLE_DATA, processSound);
//mySound.play();
}
var mySound2:Sound = new Sound();
var sourceSnd2:Sound = new Sound();
var urlReq2:URLRequest = new URLRequest("Track2.mp3");
sourceSnd2.load(urlReq2);
sourceSnd2.addEventListener(Event.COMPLETE, loaded2);
function loaded2(event:Event):void
{
mySound2.addEventListener(SampleDataEvent.SAMPLE_DATA, processSound2);
mySound2.play();
mySound.play();
}
function processSound(event:SampleDataEvent):void
{
var bytes:ByteArray = new ByteArray();
sourceSnd.extract(bytes, 8192);
event.data.writeBytes(bytes);
}
function processSound2(event:SampleDataEvent):void
{
var bytes:ByteArray = new ByteArray();
sourceSnd2.extract(bytes, 8192);
event.data.writeBytes(bytes);
}
been working on a similar system for a while, I'll do my best to give you some direction:
Your example code is not really mixing the MP3's - it's creating 2 more sounds to playback the loaded MP3's via the SampleDataEvent. What you need to do is create just one "output" Sound file that will hold/playback the resulting mixed sound. You can easily save that data as it happens and subsequently save that file as a new WAV/MP3/what-have-you.
real/psuedo-code (read:lazy) :
output = new Sound();
output.addEventListener( SampleDataEvent.SAMPLE_DATA , mixAudio );
song1 = new Sound / load the first mp3
song2 = new Sound / load the second mp3
// a byteArray for "recording" the output and subsequent file creation
recordedBytes = new ByteArray();
either wait until both mp3's are completely loaded, or run an enter-frame to determine when both Sounds are no longer buffering (Sound.isBuffering )
when the mp3's are ready:
// numbers to store some values
var left1:Number;
var right1:Number;
var left2:Number;
var right2:Number;
// bytearrays for extracting and mixing
var bytes1:ByteArray = new ByteArray( );
var bytes2:ByteArray = new ByteArray( );
// start the show
output.play();
function mixAudio( e:SampleDataEvent ):void{
//set bytearray positions to 0
bytes1.position = 0;
bytes2.position = 0;
//extract
song1.extract( bytes1, 8192 );
song2.extract( bytes2, 8192 );
// reset bytearray positions to 0 for reading the floats
bytes1.position = 0;
bytes2.position = 0;
// run through all the bytes/floats
var b:int = 0;
while( b < 8192 ){
left1 = bytes1.readFloat(); // gets "left" signal
right1 = bytes1.readFloat(); // gets "right" signal
left2 = bytes2.readFloat();
right2 = bytes2.readFloat();
// mix!
var newLeft:Number = ( left1 + left2 ) * .5;
var newRight:Number = ( right1 + right2 ) * .5;
// write the new stuff to the output sound's
e.data.writeFloat( newLeft );
e.data.writeFloat( newRight );
// write numbers to the "recording" byteArray
recordedBytes.writeFloat( newLeft );
recordedBytes.writeFloat( newRight );
b++;
}
}
Yes - you should really cap the possible output at -1/1. Do it. This is extremely un-optimized!
Ok. so that's the easy part! The tough part is really converting the final byteArray to MP3. The audio exists within Flash as PCM/uncompressed data, MP3 is obviously a compressed format. This "answer" is already way too long and all this info I've gleaned from several very smart folks.
You can easily adapt 'MicRecorder' to be a generic Sound data recorder: http://www.bytearray.org/?p=1858
converting to MP3 will be a bitch: Thibault has another post on ByteArray.org - search for LAME MP3.
Excellent example/resource: http://www.anttikupila.com/flash/soundfx-out-of-the-box-audio-filters-with-actionscript-3/
Look up Andre Michelle's open source 'Tonfall' project on Google code.
Look up Kevin Goldsmith's blog and labs - he's got great example on utilizing Pixel Bender with all this madness.
hope this helps!
PS - taking a cue from Andre, the optimal length of the audio buffer should be 3072. Give it a try on your machine!
这篇关于从ByteArray中混合2声音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!