06
Dec
07

Sound Mixer Source

In Chapter 11 we took an extensive look at sound in AS3. We've created a sound mixer demo to showcase some of the techniques covered in the book.


 

LAS3 Sound Mixer

This movie requires Flash Player 9. Please update your player.

Download: Sound Mixer  Download Sound Mixer (860.5 KB, 2,746 hits)

Demo Features:
- Controlling Individual Sound Volume
- Controlling Individual Sound Panning
- Controlling Sound Volume for Entire swf
- Sound Visualization

Print This Post Print This Post


21 Responses to “Sound Mixer Source”


  1. 1 Eric Oct 5th, 2008 at 8:58 pm

    Hey there! We got to exchange email before on dynamic shadows once. Anyway, I was wondering if you knew how to delay playing a sound using TweenLite. I want to start playing the sound 1 second after I click a movieClip. Any suggestions?
    I have the following:

    var snd:Sound = new Sound(new URLRequest("sound.mp3"));
    TweenLite.to(snd, .45, {play:1, delay:2});

  2. 2 Zevan Oct 6th, 2008 at 8:18 pm

    Hey Eric,

    TweenLite has a function called delayedCall() that's specifically for this:

    import gs.TweenLite;
    
    var snd:Sound = new Sound(new URLRequest("sound.mp3"));
    
    TweenLite.delayedCall(1, onPlaySound);
    function onPlaySound():void{
    	snd.play();
    }
    

    Delayed call takes two parameters, delay and the callback function. TweenLite's delayedCall() is nearly identical to setTimeout() from the flash.utils package. The main difference is that setTimeout() requires that its delay argument be in milliseconds:

    var snd:Sound = new Sound(new URLRequest("sound.mp3"));
    
    setTimeout(onPlaySound, 1000);
    
    function onPlaySound():void{
    	snd.play();
    }
    

    Hope that helps.

  3. 3 Andy Jan 8th, 2009 at 1:02 am

    Hi -
    I was wondering if anyone has every tried to extend the viz class horizontally.
    How can I make my spectrum wider to fit a wider application.

    I seem to be lost in mathworld in the audio chapter.
    Is there any other way to sync the timer/frame rate with
    computeSpectrum? Every time I look at the math it just seems
    I'm stuck w/ 256.

    I just want it to be wider w/out throwing errors and memory leaks.

    tx,
    Andy

  4. 4 Rich Jan 11th, 2009 at 9:56 pm

    @Andy, there are a few ways to adjust the visualization.

    First, in the loop that uses 256, you can change the x-coordinate for each sample. There are 256 samples, so you can either set the x coordinate of your asset to the loop iterator (like i) to get a width of 256, or you can use a multiple to increase that width. For instance, setting the x coordinate to i * 2 will space the horizontal distance out to every 2 pixels.

    In the example code for this post, the following change in Spectrum.as shows this technique:

    for (var i:int = 0; i < 256; i  ) {
        graphics.lineStyle(1, 0x00CC00, 1);
        var val:Number = _bytes.readFloat() * _amplify;
        graphics.moveTo(i * 2, val);
        graphics.lineTo(i * 2, -val);
    }
    for (i = 0; i < 256; i  ) {
        val = _bytes.readFloat() * _amplify;
        graphics.moveTo(i * 2, 70   val);
        graphics.lineTo(i * 2, 70 - val);
    }
    

    As another example, the spectrum would be half as wide if you substituted .5 for 2 in the above.

    An easier way, however, might be to scale the display object that contains the visualization. The code below is a PARTIAL copy-and-paste of the constructor of Mixer.as. The new line is marked by a comment, and appears right after _spectrum is created:

    public function Mixer(){
    
        _masterAudio = new MasterAudio(this);
    
        _spectrum = new Spectrum();
        _spectrum.scaleX = .5;  //insert this line
        LCDdisplay.addChild(_spectrum);
        _spectrum.mask = LCDdisplay.screenMask;
    

    Again, using .5 would reduce by half instead of double, etc.

    In all cases, this particular example uses a mask to show the sound visualizer. The mask is assigned in the last line of the above PARTIAL segment from the constructor of Mixer.as. If you want to change the size of the visualizer, you'll need to change the size of the mask (and, of course, the background art). The mask is the green movie clip screenMask inside LCDdisplay.

  5. 5 Keith Feb 4th, 2009 at 8:31 pm

    Hello,
    I know some As 2 and I´m trying to migrate to As 3,0 working with sound.

    While trying to playing sound a second time pushing a button(loading from an Url) I get the Error#2037:Functions called in an incorrect sequence,or earlier call was unsucessful. None of these statements are true!
    When I try your example "SoundPlayBasic" from your book( which I think is excellent by the way)
    this doesn´t occur. I can´t figure that out. How come? there´s nothing wrong with my script
    (as far as I understand) since it loads and plays once, but not twice.

    regards
    Keith

  6. 6 guillaume Feb 10th, 2009 at 6:36 am

    hi,

    i'm trying to extend your sample with 3 mp3 files of 20 seconds.
    i've some problems with sounds synchronisation : The third file doesn't start with the other.
    Do you know a way to solve this problem ?

    Thanks for your posts, which are very high qulity posts !

    Guillaume from France

  7. 7 Zevan Feb 27th, 2009 at 1:04 am

    Guillaume, I haven't had any problems like the one you're describing. It could be you're version of the flash player but it could also be something else. Do you have some sample code or a link to your file you could post?

  8. 8 Matt Apr 13th, 2009 at 9:47 am

    Hello,

    I`m currently building a mixer for my site similar to the LAS3, but with a slight change - the sounds for each track are selected by the user from a list component and loaded at runtime. This can be done while the mixer is playing or stopped, and of course, it adds a lot of flexibility for making music.

    Everything is more or less working fine, but I am having problems with the short segment of silence added to the mp3s at the end. Of course, when mp3s are brought into the Library and compiled with the .swf, this silence is somehow removed by Flash. However, in my situation this silence is included and causes my loops to have a slight skip at the end! Is there any way of removing this silence (i.e. using a different mp3 encoder like LAME) or does it have to be done through actionscript, TweenLite, etc...? I noticed that the LAS3`s mp3s are loaded at runtime and yet they don`t seem to have this problem. I am truly at a loss!

    BTW - Chapter 11 was absolutely awesome at giving me a head start on all the sound processing!

    Thanks in advance for any help!

    Matt from Japan

  9. 9 Rich Apr 13th, 2009 at 9:22 pm

    @Matt, thanks for the comment about Chapter 11. I'm glad it helped. Unfortunately, what you're describing is part of the MP3 process. One of the most straightforward approaches to this problem is to edit the MP3 files. The thing is, you need to edit the MP3 files themselves, not edit the source and reencode. If you do the latter, the silence you mentioned will just be reinserted.

    Here are a couple of apps that I found via a Google search to do this. i've used neither personally, but they're worth a try:

    Mac: http://deepniner.net/mp3trimmer/
    Windows: http://www.mptrim.com/

  10. 10 Matt Apr 20th, 2009 at 10:36 am

    Rich,

    Thank you very much for the reply! I tried using MPTrim to clean the silence up, and it helped a little, but there is still a gap. I also tried starting the loop a bit late - sound.play(80, 1) - but the results were usually inaccurate and unpredictable. I even tried using a timer object to re-trigger the loops every 7,111 milliseconds (7,111 ms is the length of my sounds) and incorporating the above start delay. Again, the results were unreliable!

    When I import a wav file into the library and then export it as an mp3 file embedded in the .swf file, it still has a short gap. When I built a previous mixer in Flash MX (actionscript 1) there was no gap and all sounds looped perfectly! Of course, in this later case, all sounds were embedded inside movie clips on the stage and scripting was done on the timeline, so maybe that made a difference.

    As I noticed previously, all the sounds in the LAS3 mixer are mp3s that are loaded at runtime, and yet there are no gaps. How did you guys accomplish this?

  11. 11 freewareguy May 25th, 2009 at 11:03 am

    @Matt,

    Recently a partner and I finished making a simple flash game (http://dollsonfire.com/games/memery) and I was having the same problem you do or did. The mp3 loops I was making, would not loop perfectly. And We spent a long time trying t adjust the in-out points for each loop in the actionscript, but nothing.. the problem remained.

    The final and best solution was to bring the raw audio loops in flash (uncompressed WAV) and let Flash do the encoding. It worked wonders and our loops are now accurate.
    I realize this may not be the solution in your case, but as a reference, know that I used LAME to encode my raw wave files from audacity, and it was cutting up the audio and adding silences to the encoded files.

    Good luck. I am also trying to create a simple sound mixer that pulls a list of sounds for each 'track' from an external xml file, but so far this is the closest thing I have found ;)

  12. 12 NauJFracT Jul 24th, 2009 at 12:19 am

    If i want to export the result in a mp3 file... what can i do? thanks!

  13. 13 Rich Jul 24th, 2009 at 1:13 am

    @NauJFracT, I'm afraid this is not a simple matter. I haven't done this myself, but you'll need to use Sound.extract() and ByteArray, and I'm not sure how you'd end up as MP3. I believe Sound.extract() pulls uncompressed audio. You'd need to invoke an MP3 library like LAME or something similar. I don't know of any AS3-only ways to do it but, at the moment, that might only be ignorance on my part. If you used LAME you might be able to do it with Alchemy, but I haven't done it.

  14. 14 NauJFracT Jul 24th, 2009 at 2:55 am

    Rich! Although my english is very bad... i want to give you thanks! i will read something about LAME and flash... because i don't want to user FMS for that...

  15. 15 NauJFracT Jul 24th, 2009 at 2:57 am

    Another question... if i want to save the sound like a "SharedObject" and after play the same session... what can i do? a lot of thanks!

  16. 16 Nima Jan 27th, 2010 at 11:24 pm

    Hi Rich,

    Thank you for the wonderful job you have done.
    I am working on a remix software that users can drag and drop some sounds on timelines(like drum,...) and sing on the top of it through mic. I want to save the whole result as mp3 file or wave.. any idea if it is possible and how? ( i mean how to save multiple sound channels mic in one mp3 or wave file?)

    many thanks in advance.

    Nima

  17. 17 Nima Jan 27th, 2010 at 11:33 pm

    btw I am using flex air2

  18. 18 Lasse Feb 9th, 2010 at 6:04 am

    Hi,

    I was just wondering if you could direct me in the code where the action of pressing the start button takes place. What I am interested in is how to start more than 1 sound simultaneously and then control each sound individually.

    Best regards

    /Lasse

  19. 19 Rich Feb 24th, 2010 at 10:03 pm

    @NauJFracT, I'm afraid you can't save a sound as a shared object.
    @Nima, as far as I know, it's not possible to save the microphone input at present.

  20. 20 Rich Feb 24th, 2010 at 10:08 pm

    @Lasse, the actual starting of the sound is a multi-step process to make sure the sounds are buffered and ready to go before playing. The listener is added to the play button in the constructor of the Mixer class. the onPlay() method of the same class is called when the button is clicked. The onLoop() method is executed every enter frame until all three sounds are buffered. When the sounds are ready, the loopSound method is called on each sound.

  21. 21 Geeem Apr 30th, 2010 at 7:11 pm

    Rich

    I also wanted to thank you for this brilliant mixer. Really inspiring.
    But like others, I'm unable to find a reliable way of creating mp3 loops that don't glitch. Tried everything I know of, so was hoping you might be able to shed some light, as you have clearly discovered something that's eluded all of us!

    Many thanks

    Geeem

Leave a Reply




Speaking

Archives