Details about as3 Sound.extract() format? - actionscript-3

Using the sound object .extract() method, it's possible to retrieve the uncompressed sound data as a ByteArray. What is the format of the extracted sound data? Custom internal format, or established format? Endian type?
I would like to be able to load sounddata directly into a byteArray and feed sound playback from this data (bypassing step 1 below).
Here are my experiments so far:
Step 1. Here I load a sound, extract the data content and save it to disc:
private var sound:Sound;
private var byteArray:ByteArray;
// loading a sound...
private function init():void {
sound = new Sound();
sound.addEventListener(Event.COMPLETE, onComplete);
sound.load(new URLRequest('short.mp3'));
}
// saving it to disc...
private function onComplete(e:Event):void {
byteArray = new ByteArray();
sound.extract(byteArray, 100000000);
byteArray.position = 0;
var file:FileReference = new FileReference();
file.save(byteArray, 'short.data');
}
Step 2. Here I load the data from disc and feed the playback of a new sound object with this data:
private var sound:Sound;
private var byteArray:ByteArray;
// loading a sound...
private function init():void {
file = new FileReference();
file.addEventListener(Event.COMPLETE, onLoaded);
file.addEventListener(Event.SELECT, onSelected);
file.browse();
}
private function onSelected(e:Event):void {
file.load();
}
private function onLoaded(e:Event):void {
trace('loaded');
trace(file.data.length);
byteArray = file.data;
sound2 = new Sound();
sound2.addEventListener(SampleDataEvent.SAMPLE_DATA, onSampleData);
sound2.play();
}
private function onSampleData(e:SampleDataEvent):void {
trace(e.position);
for (var i:int = 0; i < 2048; i++) {
var left:Number = byteArray.readFloat();
var right:Number = byteArray.readFloat();
e.data.writeFloat(left * 0.2);
e.data.writeFloat(right * 0.2);
}
}
I would like to be able of accomplishing this second step (loading data from disc and feeding a sound object playback) by using an external converter utility (sox or something).
/ Jonas

It should be standard 32bit floats, one per channel per sample. So I would try -e floating-point for sox, I haven't tried though but I would do it with a certain level of confidence... :) Floats are endian-independent, I believe...

Related

Recording Flash Webcam FMS 4.5 to Mp4 results in terrible quality

I have successfully setup recording webcam to FLV using FMS 4.5 developer edition, so I wanted to attempt recording to an Mp4 next. I am doing a silent save of the video file because the goal here is to be able to have these videos playable outside of Flash/FMS. I set the program up to save the Mp4 file generated by FMS, but the quality is terrible. I am seeing green distortion when movement is captured, and heavy pixelation. Here is my test application code that saves the video file after 5 seconds of recording. Can anyone please point out where I am going wrong? Any help is greatly appreciated.
package com
{
import flash.display.*;
import flash.net.*;
import flash.utils.Timer;
import flash.events.*;
import flash.filesystem.File;
import flash.media.*;
public class Main extends MovieClip
{
private var nc:NetConnection;
private var ns:NetStream;
private var nsPlayer:NetStream;
private var vid:Video;
private var vidPlayer:Video;
private var cam:Camera;
private var mic:Microphone;
private static const LOCAL_VIDEO:String = "myCamera";
private static const VIDEO_FPS:uint = 30;
private static const SAVE_FOLDER_NAME:String = "Saved_Videos";
private static const PATH_TO_FMS:String = "C:/Program Files/Adobe/Flash Media Server 4.5";
private var timer:Timer = new Timer(5000, 1);
public function Main()
{
addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(evt:Event):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
nc = new NetConnection();
nc.addEventListener(NetStatusEvent.NET_STATUS, onNetStatus);
nc.connect("rtmp://localhost/PublishLive/myCamera");
}
function onNetStatus(evt:NetStatusEvent):void
{
if(evt.info.code == "NetConnection.Connect.Success")
{
publishCamera();
displayPublishingVideo();
timer.addEventListener(TimerEvent.TIMER_COMPLETE, timerCompleted);
timer.start();
}
}
private function timerCompleted(evt:TimerEvent)
{
trace("timer completed");
timer.stop();
ns.close();
ns = null;
var saveFile:File = new File(PATH_TO_FMS + "/applications/PublishLive/streams/" + LOCAL_VIDEO + "/" + LOCAL_VIDEO + ".mp4");
var fileName:String = "Video" + ".mp4";
var dir:File = File.documentsDirectory.resolvePath(SAVE_FOLDER_NAME);
dir.createDirectory();
var fileToSave = dir.resolvePath(fileName);
if(fileToSave.exists)
{
fileToSave.deleteFile();
}
saveFile.copyTo(fileToSave, true);
}
private function publishCamera()
{
var h264Settings:H264VideoStreamSettings = new H264VideoStreamSettings();
h264Settings.setProfileLevel(H264Profile.BASELINE, H264Level.LEVEL_3_1);
cam = Camera.getCamera();
cam.setMode(stage.stageWidth, stage.stageHeight, VIDEO_FPS, true);
cam.setQuality(0, 90);
cam.setKeyFrameInterval(15);
cam.setMotionLevel(100);
mic = Microphone.getMicrophone();
mic.setSilenceLevel(0);
mic.rate = 11;
ns = new NetStream(nc);
ns.videoStreamSettings = h264Settings;
ns.attachCamera(cam);
ns.attachAudio(mic);
ns.publish("mp4:myCamera.mp4", "record");
}
private function displayPublishingVideo():void
{
vid = new Video();
vid.width = stage.stageWidth;
vid.height = stage.stageHeight;
vid.attachCamera(cam);
addChild(vid);
}
}
}
Ok, so I am answering my own question. I found the answer here along with the link to the tool to process: adobe help site
You must convert the files after recording them using a post-processing tool so they can be viewed in other video players.
Edit: VLC can actually play the unprocessed file, so I thought it was a quality issue at first!
You could try to change the quality of the video stream VideoStreamSettings.setQuality(bandwidth:int, quality:int) (link).
You only set one of the bandwidth or quality values and leave the other to 0. I would try to set bandwidth (measured in bytes per second) to 750000 (6 Mbps), which should be plenty for anything less than full HD.
So, in your case, you could try:
h264Settings.setQuality(750000, 0);

actionscript 3.0 - p2p filesharing troubles (max upload file size)

i've designed a p2p apliccation which can transfer files without a server. and everything works fine. i can transfer files between peers. but as it turns out if file size is greater than 16mb (coz the biggest file i could transfer had a size of 15810 kb) it does't transfer to peer. this is the code i'm using:
private function browseFile(farIds:String = ""):void {
fIds = farIds;
file = new FileReference();
file.addEventListener(Event.SELECT, selectHandler);
file.browse();
}
private function selectHandler(event:Event):void {
var btn = getChild("browseFile_" + fIds)
if (btn && btn.alpha) btn.alpha = 0.5;
file = FileReference(event.target);
file.addEventListener(ProgressEvent.PROGRESS, progressHandler);
file.addEventListener(Event.COMPLETE, completeHandler);
file.load();
}
private function progressHandler(event:ProgressEvent):void{
ExternalInterface.call("fileLoadProgress", event.target.name, event.bytesTotal, event.bytesLoaded)
}
private function completeHandler(event:Event):void{
ExternalInterface.call("onFileLoaded")
var fileData:Object = new Object();
fileData.file = event.target.data
fileData.name = event.target.name;
var btn = getChild("browseFile_" + fIds)
if (btn && btn.alpha) btn.alpha = 1;
sendSomeData(fileData, fIds, "receiveFile");
}
public function receiveFile(info:Object, peerID:String):void{
ExternalInterface.call("alert", "receivedFile")
}
private function sendSomeData(data,farIds:String,func:String = "receiveSomeData"):void{
for(var id:String in sendStreams){
sendStreams[id].send(func, data, myPeerID);
}
}
can you tell me how can i allow transferring all files of any sizes?
thanks for your help!
You can split the file in chunks of say 8KB, and send them one by one. However you must check for the order of received chunks and any possible losses on the way.

Actionscript - load and play another sound file

i am playing a sound file and i want onclick to start playing another file.
You can check the funcyion PlayAnother() in the following example:
private var TheSound:Sound = new Sound();
private var mySoundChannel:SoundChannel = new SoundChannel();
private function PlaySound(e:MouseEvent):void
{
TheSound.load(new URLRequest("../lib/File1.MP3"));
mySoundChannel = TheSound.play();
}
private function PlayAnother(e:MouseEvent):void
{
mySoundChannel.stop();
TheSound.load(new URLRequest("../lib/File2.MP3"));
}
public function Test1():void
{
var Viewer:Shape = new Shape();
Viewer.graphics.lineStyle(0, 0x000000);
Viewer.graphics.beginFill(0x000000);
Viewer.graphics.drawRect(0, 0, 1, 10);
Viewer.graphics.endFill();
Viewer.width = 30;
Viewer.x = 10;
var Viewer1:Shape = new Shape();
Viewer1.graphics.lineStyle(0, 0x000000);
Viewer1.graphics.beginFill(0x000000);
Viewer1.graphics.drawRect(0, 0, 1, 10);
Viewer1.graphics.endFill();
Viewer1.width = 30;
Viewer1.x = 50;
var tileSpot:Sprite = new Sprite();
var tileSpot1:Sprite = new Sprite();
tileSpot.addChild(Viewer)
tileSpot1.addChild(Viewer1)
addChild(tileSpot);
addChild(tileSpot1);
tileSpot.addEventListener(MouseEvent.CLICK, PlaySound);
tileSpot1.addEventListener(MouseEvent.CLICK, PlayAnother);
}
but i'm getting the error (Functions called in incorrect sequence, or earlier call was unsuccessful).
can any one help please.
Flash is complaining because you're loading a new file into a Sound object which already has data. (If you look at the docs for Sound.load() here, it says "Once load() is called on a Sound object, you can't later load a different sound file into that Sound object").
You just need to instantiate a new Sound before loading File2 and run play() again:
private function PlayAnother(e:MouseEvent):void
{
mySoundChannel.stop();
TheSound = new Sound();
TheSound.load(new URLRequest("../lib/File2.MP3"));
mySoundChannel = TheSound.play();
}

Game session Sound Playback / Recording

So I'm making a simple flash game. Basically I have a turntable that goes on for 30 seconds with a couple of sample music that adds up together in multiple layers of sound to form a final song.
Now I would like to be able to record and play the sounds at the end of the game.
I've created a SoundFx class that takes mp3 audio and turns it into byteArrays with the hope to mix the audios in to the same Sound channel.
Now I've reached a certain impass since I cannot properly mix the bytearrays. I'm starting to think it's not possible to encode the byte Arrays as you add the channels to the mix.
I'd love to be guided in the right direction. I'm not sure if the best way to proceed from here, even just the playback would be nice. Creating a button log would probably fix the playback and mixing the audio in a second run to go straight to the file. but it sure seems like a long path to achieve this.
Many thanks and apologies for my crappy english in advance
David R.
some code on the matter:
private var srcSound:Sound;
private var sound1:Sound;
private var sound2:Sound;
private var soundChannel:SoundChannel;
private var bytes:ByteArray;
private var incbytes:ByteArray;
private var mixedBytes:ByteArray;
public var pitchShiftFactor:Number;
public var position:Number;
public var AddSound:Boolean = false;
public var incremental:Number;
public var left1:Number;
public var left2:Number;
public var right1:Number;
public var right2:Number;
public var mixedBytes1:Number;
public var mixedBytes2:Number;
public function SoundFx() {
}
public function playFx(srcSound:Sound):void{
this.srcSound = srcSound;
position = 0;
var morphedSound:Sound = new Sound();
morphedSound.addEventListener(SampleDataEvent.SAMPLE_DATA, sampleDataHandler);
soundChannel = morphedSound.play();
}
public function addSound(sound1:Sound , sound2:Sound):void{
this.sound1 = sound1;
this.sound2 = sound2;
sound1.addEventListener(SampleDataEvent.SAMPLE_DATA, addSampleData);
position = 0;
soundChannel = sound1.play();
soundChannel = sound2.play();
AddSound = true;
}
private function addSampleData(event:SampleDataEvent):void{
position = 0;
var incbytes:ByteArray = new ByteArray();
position += sound1.extract(incbytes, 4096, position);
var bytes:ByteArray = new ByteArray();
position += sound2.extract(bytes, 4096, position);
event.data.writeBytes(mixBytes(bytes, incbytes));
}
private function sampleDataHandler(event:SampleDataEvent):void
{
var bytes:ByteArray = new ByteArray();
position += srcSound.extract(bytes, 4096, position);
event.data.writeBytes(editBytes(bytes));
}
private function mixBytes(bytes1:ByteArray , bytes2:ByteArray ):ByteArray{
bytes.position = 0;
incbytes.position = 0;
var returnBytes:ByteArray = new ByteArray();
while(bytes1.bytesAvailable > 0)
{
left1 = bytes1.readFloat();
left2 = bytes2.readFloat();
right1 = bytes1.readFloat();
right2 = bytes2.readFloat();
mixedBytes1 = left1 + left2;
mixedBytes2 = right1 + right1;
mixedBytes.writeFloat(mixedBytes1);
mixedBytes.writeFloat(mixedBytes2);
}
return mixedBytes;
}
private function editBytes(bytes:ByteArray):ByteArray{
//var skipCount:Number = 0;
var returnBytes:ByteArray = new ByteArray();
bytes.position = 0;
while(bytes.bytesAvailable > 0)
{
//skipCount++;
returnBytes.writeFloat(bytes.readFloat());
returnBytes.writeFloat(bytes.readFloat());
}
return returnBytes;
}
}
i think i get your idea,hope i did.
You should try to pass all of your SoundChannel classes (sound layers being added) to a SoundMixer class (there is plenty of info on this in adobe site),like a funnel,and then add a SAMPLEDATA event listener to that soundmixer, and capture the byte array of that sound mixer,it is like getting the byte array of the final mixdown...then encode those bytes to wav or mp3 .
Sorry i can not write that code down for you,i am not very good with the code yet, but im good at mixing sounds.
Here is an example form adobe site itself:
function loaded(event:Event):void
{
outputSnd.addEventListener(SampleDataEvent.SAMPLE_DATA, processSound);
outputSnd.play();
}
Useful link:
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/media/Sound.html
soundmixer class link:
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/media/SoundMixer.html
experimenting with stratus (cirrus) internet radio i used SoundMixer.computeSpectrum() on the server side and passed that data to the client side with NetStream.send() for drawing and playback.
It worked, but was very ram hungry with client and server on one computer (and with several songs loaded and converted to raw audio data :)
so, try computeSpectrum(), it seems perfect for creating an "audio log"

How to parse <media:content> in RSS using AS3?

Am trying to parse the RSS feed from www.ted.com/talks/rss, I can access all normal tags using E4X but I have no idea how one parses the tags!
For example the
This is my code and I can traverse easily but I want to pull the media:content tags.
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
// entry point
var _loader:URLLoader = new URLLoader();
var _request:URLRequest = new URLRequest("http://www.ted.com/talks/rss");
_loader.addEventListener(ProgressEvent.PROGRESS, onProgress);
_loader.addEventListener(IOErrorEvent.IO_ERROR, IOErrorHandler);
_loader.addEventListener(Event.COMPLETE, onLoadComplete);
_loader.load(_request);
}
private function onLoadComplete(e:Event):void
{
var _rssXML:XMLList = new XMLList(e.target.data);
for (var i:int = 0; i < 50; i++ )
{
//trace(_rssXML.channel.item.link[i]);
trace(_rssXML.channel.item[i]);
}
}
Any help would be appreciated.
Sounds like your using MRSS specifications. You want to look into QName to access qualifying namespaces
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/QName.html