本文介绍了以编程方式混合音轨(不播放)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现了一些很棒的演示,演示如何将声音对象混合在一起以进行实时播放.参见下面的工作示例...

I've found some excellent demos of how to mix together sound objects together for live playback. See the working example bellow...

但是可以通过编程方式完成而无需任何回放,这样我就可以输出混合文件了吗?另外,我还将在此过程中添加一些音量变化信息,因此需要像播放缓冲区的工作原理一样以小块的形式添加.

But can it be done programmatically without any playback so I can just output the mixed file? Also I'll be adding some volume change info along the way so it'll need to be added in small chunks like how the play buffer works.

[Embed(source = "audio/track01.mp3")]
private var Track1:Class;
[Embed(source = "audio/track02.mp3")]
private var Track2:Class;
[Embed(source = "audio/track03.mp3")]
private var Track3:Class;
[Embed(source = "audio/track04.mp3")]
private var Track4:Class;[Embed(source = "AudioMixerFilter2.pbj",mimeType = "application/octet-stream")]
private var EmbedShader:Class;

private var shader:Shader = new Shader(new EmbedShader());

private var sound:Vector.<Sound> = new Vector.<Sound>();
private var bytes:Vector.<ByteArray> = new Vector.<ByteArray>();
private var sliders:Vector.<Number> = new Vector.<Number>();

private var sliderVol:int = 1;

private var BUFFER_SIZE:int = 0x800;

public var playback:Sound = new Sound();

public function startAudioMixer(event:FlexEvent):void{


    sound.push(new Track1(), new Track2(), new Track3(), new Track4());
    sliders.push(sliderVol,sliderVol,sliderVol,sliderVol);

    playback.addEventListener(SampleDataEvent.SAMPLE_DATA, onSoundData);
    playback.play();
}

private function onSoundData(event:SampleDataEvent):void {

    for(var i:int = 0; i < sound.length; i++){
        bytes[i] = new ByteArray();
        bytes[i].length = BUFFER_SIZE * 4 * 2;
        sound[i].extract(bytes[i], BUFFER_SIZE);

        var volume:Number = 0;
        bytes[i].position = 0;

        for(var j:int = 0; j < BUFFER_SIZE; j++){
            volume += Math.abs(bytes[i].readFloat());
            volume += Math.abs(bytes[i].readFloat());
        }



        volume = (volume / (BUFFER_SIZE * .5)) * sliderVol; // SLIDER VOL WILL CHANGE

        shader.data['track' + (i + 1)].width    = BUFFER_SIZE / 1024;
        shader.data['track' + (i + 1)].height   = 512;
        shader.data['track' + (i + 1)].input    = bytes[i];
        shader.data['vol'   + (i + 1)].value    = [sliders[i]];

    }

    var shaderJob:ShaderJob = new ShaderJob(shader,event.data,BUFFER_SIZE / 1024,512);
    shaderJob.start(true);
}

推荐答案

最简单的方法就是忘记Pixel Bender的东西.

Easiest way would be to just forget about the Pixel Bender stuff.

加载声音后,使用ENTER_FRAME,该声音使用Sound.extract从每个声音中获取较小的ByteArray,然后通读所有四个提取的ByteArray并进行一些基本数学运算,以得出左侧&的混合"值.正确的信号.将这些值写入最终/混合/输出" ByteArray.对每一帧重复该过程,直到您到达声音的尽头.如果声音的长度不尽相同,则还需要弄清楚如何处理.

Once the Sounds are loaded, use an ENTER_FRAME that uses Sound.extract to get a smallish ByteArray from each Sound, then read through all four extracted ByteArrays doing some basic math to arrive at the 'mixed' values for the left & right signals. Write those values to the "final/mixed/output" ByteArray. Repeat the process each frame until you're at the end of the sounds. If the Sounds aren't all the identical length, you'll need to figure out how to handle that as well.

如果您需要执行混合操作,其中每个音轨的振幅会随时间变化,这将是一个很好的挑战,但是设置起来会花费一些时间.

If you need to perform a mix where the amplitude of each track changes over time, it'd be a good challenge, but would take time to set up.

在进行此操作时,请查看安德烈·米歇尔(Andre Michelle)的Tonfall项目...这是一个复杂但很不错的地方,开始了解AS3中音频的输入/输出.

While you're at it, check out Andre Michelle's Tonfall project... It's a complex but great place to start with understanding the ins/outs of audio in AS3.

这篇关于以编程方式混合音轨(不播放)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 00:57