Avg. Rating 5.0

Problem

is it possible to extract raw audio or video data from netstream before data is sent to backend media server via RTMP. so I can massage on the raw data? such as convert asao audio to PCM.

Solution

You can try using ActionScript 3.0 programming to extract raw audio

Detailed explanation

To extract raw audio, use the ActionScript 3.0 programming. To access data, call  SoundMixer.computeSpectrum() on the client.

The SoundMixer.computeSpectrum() method lets an application read the raw sound data
for the waveform that is currently being played. If more than one SoundChannel object is
currently playing the SoundMixer.computeSpectrum() method shows the combined sound
data of every SoundChannel object mixed together.
 
The sound data is returned as a ByteArray object containing 512 bytes of data, each of which
contains a floating point value between -1 and 1. These values represent the amplitude of the
points in the sound waveform being played. The values are delivered in two groups of 256,
the first group for the left stereo channel and the second group for the right stereo channel.
 
 
The code will be similar to :
 
        function extract(target:ByteArray,           length:Number,           startPosition:Number = -1 ):Number;
 
 
 
  

Where :

*target:  A ByteArray object in which the extracted sound samples should be placed.

*length: The number of sound samples to extract. A sample contains both the left and right channels - that is, two 32-bit floating point values.

*startPosition:  The sample at which extraction should begin. If you don't specify a value, the first call to extract() starts at the beginning of the sound; subsequent calls without a value for startPosition progress sequentially through the sound.

*extract() returns the number of samples which could be retrieved. This might be less than the length you requested at the very end of a sound.

I am posting an example by Peter deHaan for better understanding

var url:String = "http://www.helpexamples.com/flash/sound/song3.mp3";
var request:URLRequest = new URLRequest(url);
var s:Sound = new Sound();
s.addEventListener(Event.COMPLETE, completeHandler);
s.load(request);
var song:SoundChannel = s.play();
song.addEventListener(Event.SOUND_COMPLETE, soundCompleteHandler);
var ba:ByteArray = new ByteArray();

var gr:Sprite = new Sprite();
gr.x = 20;
gr.y = 200;
addChild(gr);

var time:Timer = new Timer(50);
time.addEventListener(TimerEvent.TIMER, timerHandler);
time.start();

function completeHandler(event:Event):void {
event.target.play();
}
function soundCompleteHandler(event:Event):void {
time.stop();
}
function timerHandler(event:TimerEvent):void {
SoundMixer.computeSpectrum(ba, true);
var i:int;
gr.graphics.clear();
gr.graphics.lineStyle(0, 0xFF0000);
gr.graphics.beginFill(0xFF0000);
gr.graphics.moveTo(0, 0);
var w:uint = 2;
for (i=0; i<512; i+=w) {
var t:Number = ba.readFloat();
var n:Number = (t * 100);
gr.graphics.drawRect(i, 0, w, -n);
}
}

The sound data from a microphone or from RTMP streams do not pass through the global SoundMixer object, 
the 
SoundMixer.computeSpectrum() method will not return data from those sources. Also, If one or more of the 
sounds being played come from sources outside the current content sandbox, security restrictions will cause the 
SoundMixer.computeSpectrum() 
method to throw an error.

+
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License. Permissions beyond the scope of this license, pertaining to the examples of code included within this work are available at Adobe.

Report abuse

Related recipes