View comments | RSS feed
Packageflash.media
Classpublic final class SoundMixer
InheritanceSoundMixer Inheritance Object

Language Version : ActionScript 3.0
Runtime Versions : AIR 1.0, Flash Player 9

The SoundMixer class contains static properties and methods for global sound control in a Flash Player or AIR application.



Public Properties
 PropertyDefined By
  bufferTime : int
[static] The number of seconds to preload an embedded streaming sound into a buffer before it starts to stream.
SoundMixer
 Inheritedconstructor : Object
A reference to the class object or constructor function for a given object instance.
Object
 Inheritedprototype : Object
[static] A reference to the prototype object of a class or function object.
Object
  soundTransform : SoundTransform
[static] The SoundTransform object that controls global sound properties.
SoundMixer
Public Methods
 MethodDefined By
  
[static] Determines whether any sounds are not accessible due to security restrictions.
SoundMixer
  
computeSpectrum(outputArray:ByteArray, FFTMode:Boolean = false, stretchFactor:int = 0):void
[static] Takes a snapshot of the current sound wave and places it into the specified ByteArray object.
SoundMixer
 Inherited
Indicates whether an object has a specified property defined.
Object
 Inherited
Indicates whether an instance of the Object class is in the prototype chain of the object specified as the parameter.
Object
 Inherited
Indicates whether the specified property exists and is enumerable.
Object
 Inherited
Sets the availability of a dynamic property for loop operations.
Object
  
[static] Stops all sounds currently playing.
SoundMixer
 Inherited
Returns the string representation of the specified object.
Object
 Inherited
Returns the primitive value of the specified object.
Object
Property Detail
bufferTimeproperty
bufferTime:int  [read-write]

Language Version : ActionScript 3.0
Runtime Versions : AIR 1.0, Flash Player 9

The number of seconds to preload an embedded streaming sound into a buffer before it starts to stream. The data in a loaded sound, including its buffer time, cannot be accessed by code in a file that is in a different domain unless you implement a cross-domain policy file. However, in the application sandbox in an AIR application, code can access data in sound files from any source. For more information about security and sound, see the Sound class description.

The SoundMixer.bufferTime property only affects the buffer time for embedded streaming sounds in a SWF and is independent of dynamically created Sound objects (that is, Sound objects created in ActionScript). The value of SoundMixer.bufferTime cannot override or set the default of the buffer time specified in the SoundLoaderContext object that is passed to the Sound.load() method.


Implementation
    public static function get bufferTime():int
    public function set bufferTime(value:int):void

See also

soundTransformproperty 
soundTransform:SoundTransform  [read-write]

Language Version : ActionScript 3.0
Runtime Versions : AIR 1.0, Flash Player 9

The SoundTransform object that controls global sound properties. A SoundTransform object includes properties for setting volume, panning, left speaker assignment, and right speaker assignment. The SoundTransform object used in this property provides final sound settings that are applied to all sounds after any individual sound settings are applied.


Implementation
    public static function get soundTransform():SoundTransform
    public function set soundTransform(value:SoundTransform):void

See also

Method Detail
areSoundsInaccessible()method
public static function areSoundsInaccessible():Boolean

Language Version : ActionScript 3.0
Runtime Versions : AIR 1.0, Flash Player 9

Determines whether any sounds are not accessible due to security restrictions. For example, a sound loaded from a domain other than that of the content calling this method is not accessible if the server for the sound has no cross-domain policy file that grants access to the domain of that domain. The sound can still be loaded and played, but low-level operations, such as getting ID3 metadata for the sound, cannot be performed on inaccessible sounds.

For AIR application content in the application security sandbox, calling this method always returns false. All sounds, including those loaded from other domains, are accessible to content in the application security sandbox.

Returns
Boolean — The string representation of the boolean.

See also

computeSpectrum()method 
public static function computeSpectrum(outputArray:ByteArray, FFTMode:Boolean = false, stretchFactor:int = 0):void

Language Version : ActionScript 3.0
Runtime Versions : AIR 1.0, Flash Player 9

Takes a snapshot of the current sound wave and places it into the specified ByteArray object. The values are formatted as normalized floating-point values, in the range -1.0 to 1.0. The ByteArray object passed to the outputArray parameter is overwritten with the new values. The size of the ByteArray object created is fixed to 512 floating-point values, where the first 256 values represent the left channel, and the second 256 values represent the right channel.

Note: This method is subject to local file security restrictions and restrictions on cross-domain loading. If you are working with local files or sounds loaded from a server in a different domain than the calling content, you might need to address sandbox restrictions through a cross-domain policy file. For more information, see the Sound class description. In addition, this method cannot be used to extract data from RTMP streams, even when it is called by content that reside in the same domain as the RTMP server.

This method is supported over RTMP in Flash Player 9.0.115.0 and later and in Adobe AIR. You can control access to streams on Flash Media Server in a server-side script. For more information, see the Client.audioSampleAccess and Client.videoSampleAccess properties in Server-Side ActionScript Language Reference for Adobe Flash Media Server.

Parameters

outputArray:ByteArray — A ByteArray object that holds the values associated with the sound. If any sounds are not available due to security restrictions (areSoundsInaccessible == true), the outputArray object is left unchanged. If all sounds are stopped, the outputArray object is filled with zeros.
 
FFTMode:Boolean (default = false) — A Boolean value indicating whether a Fourier transformation is performed on the sound data first. Setting this parameter to true causes the method to return a frequency spectrum instead of the raw sound wave. In the frequency spectrum, low frequencies are represented on the left and high frequencies are on the right.
 
stretchFactor:int (default = 0) — The resolution of the sound samples. If you set the stretchFactor value to 0, data is sampled at 44.1 KHz; with a value of 1, data is sampled at 22.05 KHz; with a value of 2, data is sampled 11.025 KHz; and so on.

See also


Example

In the following example, the computeSpectrum() method is used to produce a graphic representation of the sound wave data.

In the constructor, a sound file is loaded and set to play. (There is no error handling in this example and it is assumed that the sound file is in the same directory as the SWF file.) The example listens for the Event.ENTER_FRAME event while the sound plays, repeatedly triggering the onEnterFrame() method to draw a graph of the sound data values. When the sound finishes playing the onPlaybackComplete() method stops the drawing process by removing the listener for the Event.ENTER_FRAME event.

In the onEnterFrame() method, the computeSpectrum() method stores the raw sound in the bytes byte array object. The data is sampled at 44.1 KHz. The byte array containing 512 bytes of data, each of which contains a floating-point value between -1 and 1. The first 256 values represent the left channel, and the second 256 values represent the right channel. The first for loop, reads the first 256 data values (the left stereo channel) and draws a line from each point to the next using the Graphics.lineTo() method. (The vector graphic display of the sound wave is written directly on to the class's sprite object.) The sound bytes are read as 32-bit floating-point number from the byte stream and multiplied by the plot height to allow for the vertical range of the graph. The width is set to twice the width of the channel length. The second for loop reads the next set of 256 values (the right stereo channel), and plots the lines in reverse order. The g.lineTo(CHANNEL_LENGTH * 2, PLOT_HEIGHT); and g.lineTo(0, PLOT_HEIGHT); methods draw the baseline for the waves. The resulting waveform plot produces a mirror-image effect.

package {
    import flash.display.Sprite;
    import flash.display.Graphics;
    import flash.events.Event;
    import flash.media.Sound;
    import flash.media.SoundChannel;
    import flash.media.SoundMixer;
    import flash.net.URLRequest;
    import flash.utils.ByteArray;
    import flash.text.TextField;

    public class SoundMixer_computeSpectrumExample extends Sprite {

        public function SoundMixer_computeSpectrumExample() {
            var snd:Sound = new Sound();
            var req:URLRequest = new URLRequest("Song1.mp3");
            snd.load(req);
            
            var channel:SoundChannel;
            channel = snd.play();
            addEventListener(Event.ENTER_FRAME, onEnterFrame);
            channel.addEventListener(Event.SOUND_COMPLETE, onPlaybackComplete);
        }

        private function onEnterFrame(event:Event):void {
            var bytes:ByteArray = new ByteArray();
            const PLOT_HEIGHT:int = 200;
            const CHANNEL_LENGTH:int = 256;

            SoundMixer.computeSpectrum(bytes, false, 0);
            
            var g:Graphics = this.graphics;
            
            g.clear();
       
            g.lineStyle(0, 0x6600CC);
            g.beginFill(0x6600CC);
            g.moveTo(0, PLOT_HEIGHT);
            
            var n:Number = 0;
            
            for (var i:int = 0; i < CHANNEL_LENGTH; i++) {
                n = (bytes.readFloat() * PLOT_HEIGHT);
                g.lineTo(i * 2, PLOT_HEIGHT - n);
            }

            g.lineTo(CHANNEL_LENGTH * 2, PLOT_HEIGHT);
            g.endFill();
 
            g.lineStyle(0, 0xCC0066);
            g.beginFill(0xCC0066, 0.5);
            g.moveTo(CHANNEL_LENGTH * 2, PLOT_HEIGHT);
            
            for (i = CHANNEL_LENGTH; i > 0; i--) {
                n = (bytes.readFloat() * PLOT_HEIGHT);
                g.lineTo(i * 2, PLOT_HEIGHT - n);
            }
  
            g.lineTo(0, PLOT_HEIGHT);
            g.endFill();
        }
        
        private function onPlaybackComplete(event:Event):void {
            removeEventListener(Event.ENTER_FRAME, onEnterFrame);
        }
    }
}

stopAll()method 
public static function stopAll():void

Language Version : ActionScript 3.0
Runtime Versions : AIR 1.0, Flash Player 9

Stops all sounds currently playing.

This method does not stop the playhead. Sounds set to stream will resume playing as the playhead moves over the frames in which they are located.

When using this property, consider the following security model:

However, in Adobe AIR, content in the application security sandbox (content installed with the AIR application) are not restricted by these security limitations.

For more information, see the following:


Example

In the following example, the stopAll() method is used to mute two sounds that are playing at the same time.

In the constructor, two different sound files are loaded and set to play. The first sound is loaded locally and is assigned to a sound channel. (It is assumed that the file is in the same directory as the SWF file.) The second file is loaded and streamed from the Adobe site. In order to use the SoundMixer.stopAll() method, all sound must be accessible. (A SoundLoaderContext object can be used to check for the cross-domain policy file.) Each sound also has an event listener that is invoked if an IO error occurred while loading the sound file. A muteButton text field is also created. It listens for a mouse click, which will invoke the muteButtonClickHandler() method.

In the muteButtonClickHandler() method, if the text field content is "MUTE," the areSoundsInaccessible() method checks if the sound mixer has access to the files. If the files are accessible, the stopAll() method stops the sounds. By clicking on the text field again, the first sound begins playing and the text field's content changes to "MUTE" again. This time, the stopAll() method mutes the one sound that is running. Note that sound channel stop() method can also be used to stop a specific sound assigned to the channel. (To use the channel functionally, the sound needs to be reassigned to the channel each time the play() method is invoked.)

package {
    import flash.display.Sprite;
    import flash.net.URLRequest;
    import flash.media.Sound;
    import flash.media.SoundLoaderContext;
    import flash.media.SoundChannel;
    import flash.media.SoundMixer;
    import flash.text.TextField;
    import flash.text.TextFieldAutoSize;
    import flash.events.MouseEvent;
    import flash.events.IOErrorEvent;

    public class SoundMixer_stopAllExample extends Sprite  {
        private var firstSound:Sound = new Sound();
        private var secondSound:Sound = new Sound();
        private var muteButton:TextField = new TextField();
        private var channel1:SoundChannel = new SoundChannel();
        
        public function SoundMixer_stopAllExample() {
            firstSound.load(new URLRequest("mySound.mp3"));
            secondSound.load(new URLRequest("http://av.adobe.com/podcast/csbu_dev_podcast_epi_2.mp3"));

            firstSound.addEventListener(IOErrorEvent.IO_ERROR, firstSoundErrorHandler);
            secondSound.addEventListener(IOErrorEvent.IO_ERROR, secondSoundErrorHandler);
            
            channel1 = firstSound.play();
            secondSound.play();
            
            muteButton.autoSize = TextFieldAutoSize.LEFT;
            muteButton.border = true;
            muteButton.background = true;
            muteButton.text = "MUTE";
        
            muteButton.addEventListener(MouseEvent.CLICK, muteButtonClickHandler);         
        
            this.addChild(muteButton);
        }

        private function muteButtonClickHandler(event:MouseEvent):void {

            if(muteButton.text == "MUTE") {        
  
                if(SoundMixer.areSoundsInaccessible() == false) {
                    SoundMixer.stopAll();
                    muteButton.text = "click to play only one of sound.";
                }
                else {
                    muteButton.text = "The sounds are not accessible.";
                }
            }
           else {
                firstSound.play();        
                muteButton.text = "MUTE";
           }
        } 

        private function firstSoundErrorHandler(errorEvent:IOErrorEvent):void {
            trace(errorEvent.text);
        }

        private function secondSoundErrorHandler(errorEvent:IOErrorEvent):void {
            trace(errorEvent.text);
        }
    }
}




 

Comments


ohad1 said on May 3, 2007 at 4:34 AM :
The Compute spectrum function is inncorect.
A signal's spectrum is needed to be shown in the frequncy domain.
The compute spectrum function displays the signal on the time domain(similar to osciloscope).
In order to show the signal in the frequncy domain, a DFT transformation is needed to be applied.
I've genereted such code:

//Init
var s:Sound = new Sound();
var sound_channel:SoundChannel;
var ba:ByteArray = new ByteArray();
var array:Array;
s.load(new URLRequest("song.mp3"));
sound_channel = s.play(0,1000);
this.addEventListener(Event.ENTER_FRAME, spectrum);
var a:Number = 0;
var N:Number = 32; //Signal's length in time
var K:Number = 16; //Signal's length in frequency
var X_r:Array = new Array(); //real part
var X_i:Array = new Array(); // imaginary part
function spectrum(event:Event)
{

// Clear the array
for(var k=0; k < 16; k++)
X_r[k]=X_i[k]=0;
a = 0;
graphics.clear();
SoundMixer.computeSpectrum(ba,true,0);
// DFT
for(var n=0; n < N; n++)
{
a=ba.readFloat();

for(k=0; k < 16; k++)
{
var ang:Number=2*Math.PI*n*k/N;
X_r[k]+=a*Math.cos(2*Math.PI*n*k/N);
X_i[k]+=a*Math.sin(2*Math.PI*n*k/N);

}

}
// show DFT
for(k=0; k < 16; k++)
{graphics.lineStyle(0,0);
var amp=Math.log(1+Math.sqrt(Math.pow(X_r[k],2)+Math.pow(X_i[k],2)));
graphics.beginFill(0x0000FF|(amp*100<<9));
graphics.drawRect(k*8,0,7,-10*(amp));
}



}
pisikopatikalistor said on Jun 22, 2007 at 12:52 AM :
ohad1, is this code working? I want to use compute spectrum, but I don't know how is it working exactly?
minigunbang said on Jun 29, 2007 at 12:51 AM :
pisikopatikalistor, this code is working exactly,
but the _y property is smaller than 0,
so you couldn't see the spectrum

change the code

graphics.drawRect(k*8,0,7,-10*(amp));

====> graphics.drawRect(k*8 + 50 ,50,7,-10*(amp));
pisikopatikalistor said on Jul 20, 2007 at 8:07 AM :
thanks a lot, minigunbang
pisikopatikalistor said on Jul 23, 2007 at 10:31 AM :
I have a problem with computing spectrum
if there is another swf in other IE windows or firefox tabs an error occuring about security like that:
----------------------------------------------------------------------------------------------
SecurityError: Error #2121: Security sandbox violation: SoundMixer.computeSpectrum: http://www.mydomain.com/spectrumEx.swf cannot access http://www.anotherdomain.com/aswfwhichcontainssound.swf. This may be worked around by calling Security.allowDomain.
at flash.media::SoundMixer$/computeSpectrum()
at spectrumEx_fla::MainTimeline/spectrum()
--------------------------------------------------------------------------------------

how can I solve this problem? do U have any idea?
pisikopatikalistor said on Jul 26, 2007 at 1:50 AM :
I updated my player to 9.0.47 from 9.0.45
and everthing is ok
David Stiller said on Aug 8, 2007 at 5:44 AM :
The general description for this class states that SoundMixer "controls embedded streaming sounds in a SWF; it does not control dynamically created Sound objects" -- but in my experience, SoundMixer does indeed act like a truly global sound control. Volume, panning, the stopAll() method ... each one affects all sounds, dynamic or not, in my tests. In timeline audio, this affects both Stream and Event sounds.
ZoD- said on Sep 7, 2007 at 9:55 AM :
Hey pisikopatikalistor,
the Error #2121 happens when you try to call the computeSpectrum function when another flash file with sound is loaded from a different domain.
For example, if you have youtube open when trying to view a flash file that uses computeSpectrum, it will fail because youtube did not grant your domain access to get its sound info. So instead of just returning the byteArray for the sounds that it can access, Adobe (for some silly reason) decided to have computeSpectrum leave the byte array untouched! I know...STUPID! Why not just return the byte array for the sounds that it CAN access?!

It appears the only "solution" is to put your computeSpectrum in a try/catch block (or use areSoundsInaccessable method before calling computeSpectrum ) and simply notify the user that a flash file in a different window is interfering with your sound visualizations!

Hopefully adobe will update the implementation of computeSpectrum. :(
podlium said on Oct 30, 2007 at 5:14 PM :
Hi,

it seems that computeSpectrum only gets frequencies from 0 to 11025?
i wasn't able to get >11025 frequencies with readFloat().
Am i right or is there another way?
FreDeVos said on Nov 5, 2007 at 1:38 AM :
Hi,

it seems that computeSpectrum only gets frequencies from 0 to 11025?
i wasn't able to get >11025 frequencies with readFloat().
Am i right or is there another way?

That's because of the sampling frequency of 22050 Hz (depending on stretchFactor in computeSpectrum(), in your case = 1).

Read this:
http://en.wikipedia.org/wiki/Nyquist_frequency

In particular:
"For example, audio CDs have a sampling frequency of 44100 Hz. The Nyquist frequency is therefore 22050 Hz, which is an upper bound on the highest frequency the data can unambiguously represent."
podlium said on Nov 6, 2007 at 3:59 AM :
yes, but stretchFactor in my case is set to 0, and still i get 256 float values for each channel that respond only to frequencies 0-11050 Hz.

i posted this on the adobe forum
http://www.adobe.com/cfusion/webforums/forum/messageview.cfm?forumid=15&catid=288&threadid=1311004&enterthread=y

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/media/SoundMixer.html