ActionScript bit operations encoding audio to a FLV - actionscript-3

I'm creating an FLV via AS3 using BitmapData; the resulting flv has no audio. So I'm trying to add an audio track on top. I've converted a Sound object's bytearray into a WAV bytearray.
I'm adding on FLV headers (specifically audio headers). I'm looking through documentation (pdf here), and this is how it specifies how to write the header:
I'm pretty new to bytes/bits and bitwise operators, so looking at how the video header was created, I'm wondering about a few things:
When it says bit flags, does this mean that all 4 options should be combined into a bitmask? Is that what the "0x08 : AUDIO" is telling me? That it should be in a byte (base-8, right?). So to store these options, would I do something like this?
//Write byte with options: uncompressed, 44khz, 16-bit, stereo
var tag:ByteArray = new ByteArray();
tag.writeByte(0x0321);
This seems wrong, ugh.
Or can I use tag.writeByte(2) to add each option on one at a time? I've also seen tag.writeByte(2 >>> 0xff) and what-not. I'm thoroughly confused. Could anyone help, or suggest a book or write-up where I might be able to figure this out?
In this image when it says UB(4), UB(2), UB(1), UB(1) under type for each field, does the "UB" tell me that they should all be combined? What does the [#] mean in this situation?
I've got my work cut out for me; but any help would be greatly appreciated!
Thanks a ton

There is an open source lib to encode Flv at runtime in as3, did you tried : https://github.com/zeropointnine/leelib/tree/master/src/leelib/util/flvEncoder ?

Related

AS3 how to check if sound object is buffering http sound stream

was wondering how i can check if sound object is buffering a http audio stream in as3? like shoutcast/icecast etc. i searched but nothing came to me that can do this job. so any hint or idea will be appreciated. i found isbuffering but the problem that isbuffering only stop when whole sound is loaded and this will never happen in a streamed audio.
Thanks alot
You can handle the "progress" event or check changing of the "bytesLoaded" property.

Flash CS6 Importing sounds

I'm working on a Flash project in CS6 and need to import a sound to my library but I'm having endless problems with it. I'm using external files in Actionscript 3.0 (e.g. I cannot do a URLRequest as it won't work externally, need to import)
I'm using a sound that should work, some errors go in that it can't be over 160kbps, this is only at 32 in the MP3 file. I converted it to an AIFF, no success, a WAV? no success.
If it's an MP3 I simply get "Couldn't import "
If it's a AIFF or WAV I get "One or more files were not imported because there was a problem reading them"
Adobe suggest you need to files under 160kbps, all are set at 32kbps to be safe.
Another suggestion said you needed Quicktime 7.7.1 (or newer) and iTunes 10.5.5.3 (or newer) for this to work with an MP3 file. I have Quick 7.7.3 and iTunes 11.0 so I really have no idea of the problem.
Can anyone help, this project needs finished quickly.
Thanks.
John.
Could be to do with bitrates.
I was running into issues with some mp3 files that I had converted from wav using Audacity. It was a specific few that wouldn't work, so I went back to Audacity and compared the working-mp3-yielding wavs to the non-working. I noticed that the working wavs had a bitrate of 44100Hz the non-working had 48000Hz. Setting the project rate ( bottom left corner ) to 44100Hz before exporting yielded mp3s that imported into flash with no problem.
In short:
48000 Hz = naughty
44100 Hz = nice
This is a weird problem here's a simple code snippet that load and play an MP3 sound AS3 doesn't support WAV natively :
//sound object
var sound:Sound = new Sound(new URLRequest("music.mp3"))
var soundChannel:SoundChannel = sound.play();
Hope this help :)
This post is kind of old but I thought I would post an answer after running into the same problem. I am also using Flash Pro in CS6. What you want to do is to embed the sound into your FLA so that when your SWF is compiled the sound will go with it. Khaled's answer is great but this issue needs a little more TLC.
What worked for me was to get an application that edits sound clips. I downloaded the open source 'Audacity.' In the bottom left corner of Audacity you can edit the project rate. Take it down to about 1600 or lower. Then save the project and export the sound as an mp3.
Finally, make sure that your sound clip is in the same folder as your FLA. Import into your FLA (this should work now) and export your sound for actionscript by right clicking the sound in the library, going to properties. Change the name of the class to whatever you want (at least get rid of the '.mp3' from the class name).
Then use the following code (which is different from the typical URL loader for sounds):
//sound is a constructor variable under your document class
public class DocumentClass extends MovieClip
{
var yourSound:yourSoundClass;
public function DocumentClass()
{
init();
}
function init():void
{
yourSound = new yourSoundClass();
}
}
Then play your sound by using yourSound.play(); This technique will embed your sound within the SWF so it will go wherever the SWF goes. I hope that helps anyone who runs into this.

Embedded Sounds cut off early

I have a combined Flash Builder/Flash Pro project. Because of the hassles involving in maintaining sound assets on the timeline, my sounds are all embedded into Class files, like:
[Embed (source="/mp3/Welcome_01_V.mp3", mimeType="audio/mpeg")]
private static const WELCOME_1:Class;
These files are then referenced by the base Classes for the symbols that need them, embedded for Actionscript on Frame 10 (because the second frame label is on Frame 10 to give space for you to read the first one).
What I'm finding is that a few of these Sounds don't play all the way through, but the SoundChannel dispatches the "soundComplete" event, and its final position matches the Sound's length.
All sounds are converted from wav to mp3 at 44Hz / 16 kbps. I faked out the compiler to avoid a reference to Flex by including a dummy SoundAsset that extends Sound.
I don't know what other steps to take to debug this. Is there a way to figure out whether the problem is on the compile side or on the run side?
Updated
More things I have tried:
Looked at the Size report: The nonworking sounds were smaller in
their embedded form than the source mp3
Got rid of my own BitmapAsset and let Flash link in the Flex Framework and do whatever that does (definitely worse)
Dropped the encoding from 44 kHz to 22 kHz (no improvement or worse)
Dropped the bit rate to 8kbps (the lowest dbPowerAmp, the tool I use, supports). This usually helps somewhat, but I still usually use a word or two from the end of the file
Dropped both parameters in the encoding. This helped a few that just dropping the bit rate didn't, but not all files. Plus it sounds tinny.
Thanks!
For Flash audio, I recommend importing the sound assets into a FLA using wav files if you have the high quality source wavs. Otherwise, you can consider converting your mp3 into a wav as well. Then set the FLA export settings to the quality you want and Flash will convert your wavs into its own format at the quality you set with hopefully less issues.
Once you do that, you can export the sound symbol for actionscript in your library and set a class name just like how you would embed it.
One other trick I use is I have one FLA just for sound assets which can be used to store as big waves as I want. And when I export that, it becomes a small SWF file which I can then embed in my main application. That way, I never have Flash reconvert the wavs to the swf every single time I export the swf. Instead it just copies the swf data which is much faster as well.
[Embed(source="Audio/Sfx.swf", symbol="WELCOME_1_WAV")]
private static const WELCOME_1:Class;
If you are having audio cut off issues in Flash Pro, you may want to check your frame rate.
I had an issue with sounds cutting off (in Flash pro CC 2014). My issue turned out to be related to the frame rate being set to 25 rather than the default 24. I have been using 25 to resolve an issue unrelated to anything in this project, so my solution was to change the FPS to 24, which invoked the necessity to move all of the synchronized animations to re-align with the corresponding audio.
Why long(ish) audio tracks get cut off at the end when the frame rate is at 25 regardless of using proper keyframing is a mystery. This solved the symptoms however, so if you are having audio cut off issues in Flash Pro, you may want to check your frame rate.
My symptoms were specifically when an audio clip was particularly long, and deep into the time line.
What worked for me: I opened the audio files in an audio editor and added a few seconds of silence to the end.
Good luck! - J.Hall

Flash a3 - stopping at a cue point

I have an flv vid and set the cue point dynamically
vid.addASCuePoint(1, "cuePoint1");
How ever it does not always stop in the same place. How can I be more exact? I take this is due to buffering and a few other factors?
import fl.video.*;
//etc etc
vid.addEventListener(fl.video.VideoEvent.READY, flvPlayback_ready);
vid.addEventListener(MetadataEvent.CUE_POINT, flvPlayback_cuePoint);
function flvPlayback_ready(evt:fl.video.VideoEvent):void {
vid.addASCuePoint(1, "cuePoint1");
vid.play();
}
function flvPlayback_cuePoint(evt:MetadataEvent):void {
vid.pause();
trace("CUE POINT!!!");
trace("\t", "name:", evt.info.name); // name: cuePoint1
trace("\t", "time:", evt.info.time); // time: 1
trace("\t", "type:", evt.info.type); // type: actionscript
}
Cuepoints can be added to a video in two ways:
1) encoded into the actual video
2) added via actionscript
I don't have any links to the appropriate documentation, but I've dealt with this problem before. The stuff I read says that adding cuepoints via actionscript will not be as accurate as encoding them onto the video. ...and your code indicates that this is how you are adding them...
I wish I had better news for you, but the only way I am aware of to get more accuracy is to add the keypoints during encoding.
It's probably due to the number of keyframes that the flv was encoded with (I think the number is 1 per 30 frames by default when encoding with MediaEncoder).
Try re-encoding the flv and set the Key Frame Distance to something smaller (or even embed the keyframe using Media Encoder rather than adding it via ActionScript).

How to apply sound distortion in actionscript-3?

Let's say the sound input is either an embedded mp3 file or the microphone.
Is there an example of how to make it sound demonic and creepy, or like a radio transmission from the battlefield in actionscript-3 dynamically on runtime.
Reference:
http://www.youtube.com/watch?v=JAY88WH0FcU
As far as I know, you simply can't with the microphone, unless you first send it to a server.
With an audio file (embedded or not), you can distort it by playing with its bytes (ref), but its not at all a trivial task (I'm not aware of any library for "easy" sound processing).