Blastanova

May 26, 2011

Embeds are broken in Flash 10.0 with Flex SDK 4.5

Filed under: Uncategorized — admin @ 7:07 pm

I had a big problem today with backwards compatibility and the Flex 4.5 SDK today.

I know….GASP!

But, next let me say that it didn’t have anything to do with the Flex framework, it was an Actionscript 3 project. THAT surprised me.

So what was it? Well, the crux of the issue is that if you use the embed directive to compile in a visual element in your project, said project will completely fail in Flash 10.0.

Here’s what I mean:

Create a new AS3 project. Don’t make it do anything – just embed a graphic.

package
{
import flash.display.Sprite;

public class TestEmbed extends Sprite
{
[Embed(source="myimg.jpg")]
public var myGraphic:Class;
public function TestEmbed(){}
}
}

No – this code doesn’t actually do anything. It just creates an empty flash project with your graphic compiled in. And it works just fine if you test it on your machine.

Now, take that SWF somewhere that you haven’t upgraded to Flash 10.1 yet.

Guess what? The constructor doesn’t even run – instead you get this error:

VerifyError: Error #1053: Illegal override of z in mx.core.BitmapAsset.

I tried the same with library graphics from a SWF – this time I get a problem with mx.core.SpriteAsset.

What is actually happening here?

Well, when you use the embed directive, your source file is actually cast to a BitmapAsset for use in your project. Likewise, your SWF symbol is cast to a SpriteAsset.

These are the trouble classes. Its worth noting that if your project just calls new BitmapAsset(), you’ll get the same problem.

A SoundAsset isn’t a visual element. Lo and behold, instantiating one of those works just fine.

Eventually I narrowed the problem down to playerglobal.swc. Swapping out the 10.0 playerglobal with the 10.2 playerglobal produces the same error.

Lets add this up. It’s a problem with display assets, and the actual error says there’s an illegal override of Z. I’m guessing that the faux 3D that Adobe added in 10.0 where you can set the Z had an API change from the 10.0 player to the 10.2 player.

When you load up your 10.2 SWF in a 10.0 player, that Z getter/setter is so drastically different, Flash just can’t continue.

So – there it is. If you use the embed directive and compile in the 4.5 SDK, your SWF won’t function in anything less than Flash 10.1

April 11, 2011

Cocoon P2P

Filed under: Uncategorized — admin @ 9:52 am

This is just a short post – mainly for myself to remind me of the opensource awesomeness that was just released. It’s called Cocoon P2P.

What this project appears to be is a peer to peer helper application for Flash powered devices to communicate in the same network. So basically, if I have a Android phone, a laptop, and an Android tablet all running Flash on my wi-fi network, they can talk to each other – without a server!

It sounds fantastic, and available at http://code.google.com/p/cocoon-p2p/. The source code doesn’t look that hefty either, so yay for easy to understanding!

January 15, 2011

Use Flash Text Engine in MX Components in ANT

Filed under: Uncategorized — admin @ 3:04 pm

The title of this post is what I googled endlessly for like 2 hours trying to figure out how to do this.

Hopefully you’ll come here when you google this and get the answer you need. For you fine folks, I’ll save you some time. Add this to your compiler options:

<theme dir="${FLEX_HOME}/frameworks/projects/spark" append="true>
     <include name="MXFTEText.css" /> 
</theme>

For the rest of you – maybe you’d like to know what this is or get a little more background.

“Use Flash Text Engine in MX Components” is a setting you can check in the Flash Builder compiler options for a project. Old style Halo (MX) components from Flex 3 use the old text engine.

As of Flex 4, we have a brand new text engine. Unfortunately, Adobe hasn’t completely written all the new components yet for Flex 4 (Spark). This means that your Flex 4 project will probably have a mix of Spark and Halo (MX) components.

One of the most frustrating things about this mix for me is working with fonts. For Spark components, you need to embed your fonts in the Compact Font Format (embedAsCFF=true). For MX components, you need to embed your fonts as non CFF (embedAsCFF=false). This could potentially mean embedding one set of fonts for your MX components and another set for your Spark Components.

Not only does this increase your application size, but it can get really hairy trying to work with styles. You need to identify all components that use Halo/MX components and tell them to use the correct font. If you go low level like label, you need to recognize that things like Alerts are MX components and they use MX labels, while if you just make a Spark label, you’ll be using Spark. So this means creating separate styles for s|Label and mx|Label.

Fortunately, Adobe made a workaround in our MX components. They allow you to use the new text engine in your MX components by checking the “Use Flash Text Engine in MX Components” option in Flash Builder.

While that’s great in Flash Builder, what if you want to use this option outside of Flash Builder? What if you want to do a command line compile, or better yet create an ANT build file.

For the command line option, information is relatively easy to find:

http://help.adobe.com/en_US/flex/using/WSda78ed3a750d6b8f-26a13bbf123c441239d-8000.html

Basically just use the theme argument and add a theme to the compiler options
-theme+=myflexhome/frameworks/projects/spark/MXFTEText.css

This is a special CSS file that Adobe made just for this purpose.

I was a little stumped on how to put it in an ANT build script, though. I tried:

<theme dir="${FLEX_HOME}/frameworks/projects/spark"> 
   <include name="MXFTEText.css" /> 
</theme>

Unfortunately, this completely replaces the theme, and doesn’t add a CSS file to your themes. It worked decently enough for my project (save all the warnings I got).

Luckily another guy on my project, TJ Downes figured out that if you use the “append=true” ANT tag option, you can make this property additive, so you end up with:

<theme dir="${FLEX_HOME}/frameworks/projects/spark" append="true>
     <include name="MXFTEText.css" /> 
</theme>

So there you have it. I couldn’t find this anywhere, so hope this helps you!

January 12, 2011

Be a Subject Expert (in something else)

Filed under: personal,projects — admin @ 1:00 am

Next week, my user interface user group, tri-me.org will be having our first meeting of the year. Rachel Nabors will be presenting on Wabi-Sabi in web design. Many times, a user group (ourselves included) will have a very technical presentation. How do you make the best MVC database application? How do you do memory management in Flex?

BLAH BLAH BLAH

I haven’t seen Rachel’s presentation yet, but it got me thinking about how inwardly focused web designers, developers, application programmers, etc are with our craft. Rachel is breaking out of this, and I think it’s a thing that some of us do to not go crazy – but I don’t think we realize how professionally useful it can be to break out of this.

Many of my friends and peers can do some amazing things behind a keyboard – both artistically and technically. We stay appraised of what Apple did to Adobe, what Google did to Apple, what the newest web framework is, and why it sucks. Our personal projects range from innovative ways to make a content management system, access a database, or log memory management in a project. We compete with each other in good AND bad ways and aspire to be the smartest kid on the block.

This is all great. We’re “keeping up with our industry”. We’re chasing technology after technology and remaining marketable. I wouldn’t have survived this long if I didn’t love the thrill of doing something innovative with new tech.

But guess what? We can’t relate to anybody but ourselves. We’re either magical in the ways we can work a computer, scam artists with how much we charge, or both to people outside our circle. This really isn’t too much of a problem professionally if you’re great at what you do, but how good is it really?

My wife is a writer, and to be honest, the profession bores me. I don’t care about grammatically correct, mapping plot points, sentence flow, etc. To her credit, though, she doesn’t write long essays about grammar. She writes things that non-writers can relate to, and she uses the knowledge of her craft to reach out to an audience that has no idea how to write.

Words are a medium to be shaped to send a message, just like pixels are. But if all we know is our craft, what kind of message can we send? Do we create a message only our peer group can understand, or do we rely on other people to tell us what message to create?

We all have hobbies – sometimes our hobbies are database programming. But other times, our hobbies are things that everyone can enjoy like wine, music, dining, television, or Japanese art history. Our hobbies can have a way of looping back into our professional lives and we suddenly have a message, and the knowhow of our craft to push those pixels and send that message.

Some examples….

My ex-employer 360KID was founded by Scott Traylor. He was interested in kids, their play, and how to use toys for education. He also happened to be a pretty decent web designer. He could have stayed a web designer, and just took whatever work came his way. Instead, he focused on children’s education and entertainment, because it was what he was interested in. Eventually 360KID was in the very unique position of being an award winning technology and design company with intimate knowledge of how to push those pixels so kids could learn. Since he was a subject expert (in something else), we were one of the best companies in the country to go to if you wanted to design something educational for kids.

Tom McCay, a NC local who I’ve met on several occasions, works at a company called rPath. They create build systems for programmers like me. BUT, Tom is passionate and knowledgeable about wine and beer. This led him to create winebythebar.com – a unique rating, review, and organizational mobile tool for wine that you can use by scanning bar codes. Tom is a subject expert (in something else). He’s a smart guy, and could get hired at any tech savvy software development company he wanted to work at. But on top of that his expertise make give him an extremely unique skillset that most likely only he possesses. He can go after these kinds of specific business opportunities (and no doubt win them) because he turned a hobby into something more.

Me, I like music. I’ve been in bands, play the keyboard, and listen to bands you’ve never heard of. I’m not particularly good at any of this, but I am a web developer as well. As a result, I’m now writing music apps, music tech articles, and doing cool stuff that nobody else does – because I’ve turned myself into a subject expert (in something else).

I think in the end, our unique qualities keep us sane and give us perspective. The more we keep ourselves pointed inward, the less unique we are. There’s only so many directions we can go in our design/development fields, and we become worth a dime a dozen because there’s hundreds of other people out there that can structure a database AND make cool icons.

The farther we reach outside our professional zone, the better chance we have of being unique and gaining this perspective. Sometimes having this unique perspective is the THING that makes us marketable regardless if this unique perspective is in music, wine, or Japanese art.

So my advice, for whatever it’s worth, is to be a subject expert (in something else).

August 18, 2010

Sambaverse Alpha

Filed under: flash/flex,music video games,projects — admin @ 12:05 am

Finally! I’m at the point where I have a demo-able application. Sambaverse is the first phase of my master plan for Blastanova.

Quite some time ago, I got interested in creating music based Flash games. I created a prototype, and it worked pretty well – but it was severely lacking in one aspect. That aspect was the ability to look ahead in music, to know what’s coming up 5 seconds from now or even 5 milliseconds from now. Or to be able to look back at what happened in the music before. Or to be a little smarter about logical song breaks.

While Flash has some decent realtime audio processing capabilities, realtime just wasn’t good enough – my music games needed mystical, physic powers…to be able to see into the future and know how the musician who composed the piece thinks.

In real terms, I needed a tool that was smart enough to load an MP3, take a good stab at automatically detecting beats, break, loud sections, and different sections of the song – like verses and choruses.

So, I set out to create Sambaverse. A person using this application loads up an MP3. Right away the gears start turning – an audio waveform and song navigator is brought up in a few seconds. You can browse around, and listen to snippets. You can properly visualize your song. That’s not very special though….you can do that in any audio editor. This is why I made some music visualization modes…

using the application
At the upper right of the application is the “Playback Visualization” menu. You can choose one or multiple options here. You have the ability to view the song beats, look where the quiet periods of the song are, where the most intense portions of the song are, and find logical breaks (sections) in the music like where a chorus ends and the bridge begins.

Unfortunately, automatically finding logical breaks in the song was a little tough, and it will never be perfect. I’d describe my break detection as a good start, I have quite a ways to go. This is why I’ve added two drop down options. The first is the “custom song sections”. This mirrors the automatically detected segments, until you drag them around to place them where you want. You can fully customize and refine these segments.

Likewise, I added a song exclamation overlay. Using this mode will place little stars on the timeline. The purpose of these stars is to signify special musical events that occur very rarely. Like when that singer does that one scream in the song “YAHHHHHH!” or there’s a sudden gong sound effect…just something wacky that occurs in the song against the norm.

So you have a few view modes at your disposal. Even more than that, pressing the orange “Analyzation Settings” button will bring up the settings window. Here you can change how quiet your quiet breaks need to be or how long they are in order for them to be identified as quiet breaks.

Theres a few settings for every view mode. I’ll be tweaking these as I go along to see what works best, and probably change names and defaults.

Finally, you can save this data as XML. Actually – I’m not really saving it yet….just posting it to a webpage, and you can save it if you want. Whatever. I know I’m psyched to play around with my new app, tweak some songs, and make some cool games.

Under the Hood

I learned a lotta cool stuff making this. The biggest thing of course was how to manipulate and use audio for my bidding in Flash. I used Pixel Bender to get a huge speed increase when loading the sound initially. That was a lot of fun once I figured out how to get it working.

Although a little annoying at the time, I was using the latest releases of Flex and Swiz. As I’ve been working on this app for a little while, I had to go through and upgrade from Flex Gumbo to Beta 1 to Beta 2 to RC to Release. Stuff changed every time. Likewise, I had an annoying evening transitioning from Swiz 0.64 to 1.0.

But I’m happy I did this. Flex skinning is awesome. Swiz allowed some nice shortcuts and organization in my application. I even tapped into Flash Catalyst for the initial application design. Catalyst seemed a little limiting after the initial graphics dump. After that for quicky and dirty graphics, I just cracked open Illustrator and exported to FXG which I then copied as text into my MXML skins.

future improvements

I’m going to take a break from this application for a bit and focus on some games. I’m sure I’ll tweak as I go along and I find limitations that I need to solve as I’m working on the games. One huge feature I’d like to add when I get back to this is to be able to run a Fast-Fourier Transform on the audio and drop frequencies I don’t care about. This way, I can zero in on the drums or the bass guitar and only analyze those. In rock music especially, the frequencies are all over the place at different tempos and can make it a little hard to find a rhythm.

I’d also like to make it so you can load in a previously saved XML file to get all your work back for a particular song.

Lastly I’d like to get a sample game/animation you can preview as you work to visualize the beats better than the Flash lights I have.

So that’s Sambaverse! Check it out! (but be nice, it’s only an alpha) .

June 23, 2010

A Custom Multi-Selection Spark DropDownList

Filed under: flash/flex — admin @ 11:00 pm

I just had to add a drop down list into my Flex 4 project, but I wanted to be able to multi-select the items, and add some custom logic.  It was sort of a pain in the butt figuring out how to do this, so I thought I’d share!

So, first of all, I created a new class that overrode DropDownList.  I added this “MultiSelectionDropDownList” to my application.  I set the width to 300, set “requireSelection” to false (since we’re allowing checkbox selection which the DDList wouldn’t know about), and set up a data provider with a few items in my list.

All normal everyday Spark DropDownList stuff so far, right?  OK, next, lets create a skin for the list.  The only reason I do this is to remove the “maxHeight” for the list.  I don’t want my list to scroll….because frankly, this isn’t the greatest example and will break when my list items are outside the viewable area.

Next I’ll add an item renderer.  The item renderer will be a normal background, and a HGroup on top of it which shows a label and a checkbox (needs an ID for access).  Pretty simple stuff so far!

Now, I have my multiselection dropdown in my application like so:

<local:MultiSelectionDropDown
		width="300"
		requireSelection="false"
		itemRenderer="MultiSelectItemRenderer"
		skinClass="MultiSelectionDropDownListSkin"
		dataProvider="{menu}"
	/>

At this point we should have a functional drop down showing some items with checkboxes. Of course those checkboxes don’t work yet, and we’ll need to add some logic.

Let’s dive into the class we created that overrides the DropDownList – the “MultiSelectionDropDown”.

The first thing I want to do is in the constructor – I want to add an event listener for when the list opens:

		public function MultiSelectionDropDown()
		{
			super();
			this.addEventListener(DropDownEvent.OPEN, onOpen, false, 0, true);
		}
 
		public function onOpen(event:DropDownEvent):void {
			activateAllCheckBoxes();
		}

So now, whenever someone clicks and opens the list, we’ll be “activating” all of our check boxes. What does this mean? Well, we’re going to loop through all of our list items, and add some event listeners to these checkboxes.

		protected function activateAllCheckBoxes():void {
			for (var c:int = 0; c < dataGroup.numElements; c++) {
				var obj:MultiSelectItemRenderer = dataGroup.getElementAt(c) as MultiSelectItemRenderer;
 
				obj.checkbox.addEventListener(MouseEvent.MOUSE_DOWN, mouseCheckBox, false, 0, true);
				obj.checkbox.addEventListener(MouseEvent.MOUSE_UP, mouseCheckBox, false, 0, true);
				obj.checkbox.addEventListener(Event.CHANGE, changeCheckBoxSelection, false, 0, true); 
}
 
		protected function mouseCheckBox(event:Event):void {
			event.stopImmediatePropagation();
		}

OK, so we’ve added some event listeners. I’ll get to the actual code to change the checkbox selection in a sec – but check out “mouseCheckBox”. Here we’re grabbing the event, and stopping it’s propagation. This means that the buck stops here when you click a check box. The event won’t make it’s way down to your item, and won’t select your item in the list and close the dropdown. Great! Cause, we want to make multiple selections without having to bother with opening the list over and over again.

Now, lets focus on the actual clicking of the checkbox:

		protected function changeCheckBoxSelection(event:Event):void {
			currentlySelectedCheckBoxes = selectedCheckboxes;
 
			// turn on multi-view mode
			if (event.currentTarget.selected == true ) {
				selectedIndex = -1;
				labelDisplay.text = "multiple selections";
			}
 
			dispatchEvent(new Event("selectionChange"));
		}
 
		protected function get selectedCheckboxes():Array {
			var returnList:Array = new Array();
			for (var c:int = 0; c < dataGroup.numElements; c++) {
				var obj:MultiSelectItemRenderer = dataGroup.getElementAt(c) as MultiSelectItemRenderer;
				if (obj && obj.checkbox.selected) {
					returnList.push(obj.data);
				}
			}
			return returnList;
		}

So here everytime I click a checkbox, if I turned it on (or checked it), I change the label display to indicate that I’m making multiple selections and set the selected item index to -1. This will make the list indicate to the user that nothing is selected, but we’re in “multiple selection mode”.

Additionally, I’m saving the array of my “currentlySelectedCheckBoxes”. I get this array by looping through my items again, and looking at the checkbox testing if it’s selected.

Once this is saved, I dispatch a “selectionChange” event. What’s this? Well, I made it up, and I’m only using it internally in this class. Who’s listening? This guy….

[Bindable(event="selectionChange")]
	public function get selectedViews():Array {
		var multiSelect:Array = selectedCheckboxes;
		if (multiSelect.length > 0) {
			return multiSelect;
		} else {
			return [selectedItem];
		}
	}

This is a bindable function to provided external access to the array of selected items in the list, whether they are selected through the checkboxes, or just selected in non-multi-selection mode just by clicking on the list item.

Let’s also improve the experience by using our saved array of “currentlySelectedCheckBoxes” and using that to populate the list when the list opens. You see, when our list closes, our items sorta….disappear. It’s sort of annoying, but pretty nice for memory management. Anyway as we go through and add our event listeners when the list opens, we can check if the checkbox is in our saved list, and force it to check itself:

protected function activateAllCheckBoxes():void {
	for (var c:int = 0; c < dataGroup.numElements; c++) {
		var obj:MultiSelectItemRenderer = dataGroup.getElementAt(c) as MultiSelectItemRenderer;
			if (obj) {
				// no check box here, this playback mode only is a solo view
				if (dataProvider.getItemAt(c) == "Select This Item Only" ) {
					obj.checkbox.visible = false;
				}
 
				// find and check of previously checked boxes
				if ( currentlySelectedCheckBoxes.indexOf(dataProvider.getItemAt(c)) != -1 ) {
					obj.checkbox.selected = true;
				}
 
				obj.checkbox.addEventListener(MouseEvent.MOUSE_DOWN, mouseCheckBox, false, 0, true);
				obj.checkbox.addEventListener(MouseEvent.MOUSE_UP, mouseCheckBox, false, 0, true);
				obj.checkbox.addEventListener(Event.CHANGE, changeCheckBoxSelection, false, 0, true);
			}
		}
	}

But that’s not all! I’ve added a “solo-selection” click in my dropdown. Clicking this “solo-selection” will clear out all checkmarks and start from scratch – exiting the multiple selection mode. You might notice that I snuck in a line to hide the checkbox if the list item is that “Select this item only” item. That’s because it’s SOLO selection and it doesn’t get a checkbox so we can select it and other items.

override protected function item_mouseDownHandler(event:MouseEvent):void {
	// launch solo view mode when clicking "playback only"
	if ( event.currentTarget.data == "Select This Item Only" ) {
		deselectAllCheckBoxes();
	}
 
	if (selectedCheckboxes.length == 0) {
		super.item_mouseDownHandler(event);	
		dispatchEvent(new Event("selectionChange"));
	} else {
		closeDropDown(false);
	}
}
 
protected function deselectAllCheckBoxes():void {
	currentlySelectedCheckBoxes = [];
	for (var c:int = 0; c < dataGroup.numElements; c++) {
		var obj:MultiSelectItemRenderer = dataGroup.getElementAt(c) as MultiSelectItemRenderer;
		if (obj) {
			obj.checkbox.selected = false;
		}
	}
}

To accomplish this, I overrode the “item_mouseDownHandler”. Inside here, I said, “if you’re that solo button, let’s deselect all the checkboxes”. To deselect, I simply went through each item in the datagroup, accessed it’s checkbox and turned off the selected property.

I also used this function to determine if we were in multiple selection mode – if so, then close the dropdown list without committing and values. If I wasn’t in multiple selection mode, then just do what the list does normally – except dispatch that change event so our custom bindable property updates any outside listeners that our selection has changed (even though it’s just a single item now in the array).

Finally the very last thing I did….

protected override function dropDownController_closeHandler(event:DropDownEvent):void
{
	if (currentlySelectedCheckBoxes.length > 0) {
		// if checkboxes are selected prevent the default behavior,
		// which is to set a selection index
		event.preventDefault();
	}
	super.dropDownController_closeHandler(event);
}

When closing the list, normal behavior is to commit a selection. I actually don’t know the details of what it was doing – but it was grabbing a selection from somewhere and producing some funky results I didn’t want if I had multiple checkboxes selected. It would update the label property and selected item with the last thing I selected in my normal list (even if it was a while ago). It’s like the list was remembering something from somewhere and competing with me! Not good.

So, I overrode the close handler. If I had multiple things selected, I stole the event, and called preventDefault(). In the DropDownListBase class, it sees this default is prevented and doesn’t try to commit whatever the heck it’s trying to commit. It’s just lets it be, and closes the list and doesn’t change anything.

That’s it! You can see the list in action, and right click to view the source here:

Multi-Select DropDown

It’s such a tiny thing, but ended up being sort of a pain to figure out how to do – so hopefully I save someone else the pain. Oh and one more thing…I don’t know that I’m necessarily advocating this kind of component yet. Might be kinda complicated in terms of useability. I’ll find out as I use it in my project and tweak it.

May 23, 2010

Music Visualization and Papervision

Filed under: flash/flex,music video games,personal — admin @ 8:46 pm

After watching Simon Free’s Papervision demo at NCDevCon today, I just had to post an old demo I did when I first got my hands on Papervision a couple of years ago. It’s a really cool project (Papervision that is), and I hope to play with it again real soon.
In this demo, you drive around a submarine through the sea, and various fish come into your view. But then a James Brown song comes on, and all the fish start dancing to the music. Different fish listen to different frequencies. It’s fun, but driving the submarine is very hard, because you can lose it off screen….

Anyway – spacebar to make the submarine go, and arrow keys to make it turn (arrow keys are relative to which way the sub is facing)

Papervision Beats (with Dancing Fish)

(by the way, thanks to Troy and Iris Stratton for getting me started with PV3D and Blender!)

May 22, 2010

Do You Belieeeeve in Flash Autotuning

Filed under: flash/flex,music video games,projects — admin @ 2:52 am

No, I didn’t accomplish the mythical Flash autotune in time for my “Audio Manipulation in Flash and Flex” presentation for NCDevCon this Sunday.  But I don’t see why it’s not possible in the least

I didn’t accomplish it, not because of Flash, but because of my lack of digital audio experience.

About a month ago, I was thinking what effect I could dazzle my attendees with, and ONE thought popped into my head:  AUTOTUNE!!!  This led me down the rabbit hole of crazy amounts of math, signal processing theory, code optimization, and more.  I even started reading a free PDF 600 page book by Stephen Smith http://www.dspguide.com/.

I’ve learned about what Fourier Transforms REALLY are, that they are really useful beyond getting frequency data, how to do low pass filters, and tons of other stuff.

I really didn’t think it would be this involved.  This is like an entire field of expertise.  I honestly thought I could steal some algorithm online and be good to go.  Nope, I’m swimming with using the FFT, then back with an iFFT, convolving signals together, and all this crazy stuff.  It’s pretty awesome though, the things you can do and learn from audio signals, and any signal in general.  I’ll definitely be finishing that 600 page book (probably 3-4 times over so I understand it).

Anyway, I was able to accomplish pitch shifting in a couple different ways, and riding a voice on top of another tone (which sounds really close to autotune if you could just get rid of that damn tone!)

I didn’t even think that one of the things that autotuning did was to detect the frequency of a sung note, and step it up or down to the correct frequency.  I thought at the beginning that when T-Pain did his thing, he just sung whatever, and the software would push the voice to whatever the producer wanted.

With Flash, I’ve seen the Audio Processing Library for Flash detect notes in a sound, and as I’ve said, I can now pitch shift!  In truth, the real Autotune by Antares Audio Technologies is said to use a “phase vocoder”, which I’m still not up on my theory enough to know what it is.  It’s probably a combination of smart pitch-shifting coupled with a flange like effect to go all robot sounding.

I finally downloaded 10.1 and got my microphone working – I recorded in from the microphone, and played back via the sound buffer (all through the sample data event).  I pitchshifted first by just speeding up the tempo so things got all high pitched and fast.  But then I grabbed a PitchShifting class by Stephan M. Bernsee  that was ported to Actionscript from C# by Arnaud Gatouillat.  Using this was VERY processor intensive.  In fact, in debug mode, my entire computer was overheating, and it was a crapshoot whether the sound would actually come out right.  And then of course the Windows sound buffer kept doing weird snapping/popping noises every so often until I restarted.  However, running as a release build seems to work just fine.  I now know why Andre Michelle’s AudioTool has a warning against using the debug player.

But all in all, it was a great learning experience.  I’m embedding the demo I’ll be showing at NCDevCon on Sunday.  And of course I’ll keep learning to become a DSP master, and someday get my Autotune working (hopefully someone beats me to it, and shares the code).

Autotune Attempt – You Need Flash 10.1 and a Microphone

May 17, 2010

NCDevCon

Filed under: Uncategorized — admin @ 11:06 pm

Just wanted to post a quick update that I’ll be speaking at NCDevCon with a hands on Flex 4 session on Saturday with Adrian Pomilio, and then on Sunday with a Flash Audio session. Adrian and I will also be pushing our new group Tri-Me! Check out ncdevcon.com for more info on the conference! It’s a great free CF, Flash, Flex, and Web conference in Raleigh

April 8, 2010

Custom Events in Actionscript 3

Filed under: flash/flex — Tags: , , , , — admin @ 1:04 am

A friend of mine recently tweeted that found that he could create custom events for his project, but he found it rather mysterious – and he didn’t really understand what he was doing even though he followed the instructions and was able to make them work in his project.

Since I use nothing but custom events in my projects, I thought they’d be a good blog post subject.

So…..custom events.  Why do you want them?

Well, first lets think about the everyday Flash events. I use flash.events.Event and flash.events.MouseEvent all the time. There are the obvious uses, like when you click on something – you listen for the “click” event type by saying addEventListener(“click”, onClick).

But, how do you know it’s “click”? It could be anything. I hate looking at the docs for every little thing. So, it’s much easier to type “MouseEvent.” into my code editor and watch it auto suggest stuff for me. You can look through and see MOUSE_DOWN, MOUSE_UP, CLICK, etc, all as options.

So I end up with addEventListener(MouseEvent.CLICK, onClick). It’s easy, there’s no chance I mistyped it because I didn’t type it – my code editor did.

But then you might be moving on to creating your own events. Say you’re loading a file. Whether the file loads, fails, the user cancels, you want to know when it’s done – so you might dispatchEvent(new Event(Event.Complete)) to let other parts of your project know that the operation is complete regardless of how it completes, just so those other parts know the file loading is done and they can move on with whatever they’re doing. Event.COMPLETE is handy for whenever you want to wire up completion events – just like Event.CLOSE is great for when you have stuff that’s closing and Event.INIT is great when you have stuff that you’re finished initializing.

What about other types of events? What about events that are specific to your project? For example, say I’m making a game. In the game, I have a rubber ball that the user can interact with. The user can bounce the ball, roll the ball, throw the ball, and spin the ball. The logistics of the user interaction to the ball doesn’t matter here, we’re just worried about the events that get dispatched.

Let’s name our events:

1. “bounceBall”
2. “rollBall”
3. “throwBall”
4. “spinBall”

Now, I could simply just use a normal Flash event. My ball dispatches events in the fashion of “dispatchEvent(new Event(“bounceBall”));”

…and I listen to my ball bouncing by adding an event listener: “ball.addEventListener(“bounceBall”, onBounceBall);”

That would work OK, but I’d have to keep looking up what I named the different events, and make sure they match in all parts of my project. God forbid I make a typo – I could be stuck for hours with a “bounceeBall” event not knowing that I accidentally typed an extra “e” in my dispatcher.

No – I like to name my events in static constants, and put them inside the event. That way I can start typing “BallEvent…”, and my code editor will autofill the rest.

So the first step is to make a BallEvent by overriding the Flash Event class:

package events
{
	import flash.events.Event;
 
	public class BallEvent extends Event
	{
		public static const BOUNCE_BALL:String = "bounceBall";
		public static const THROW_BALL:String = "throwBall";
		public static const ROLL_BALL:String = "rollBall";
		public static const SPIN_BALL:String = "spinBall";
 
		public function BallEvent(type:String, bubbles:Boolean=false, cancelable:Boolean=false)
		{
			super(type, bubbles, cancelable);
		}
	}

So I have a nice little BallEvent class. Now there’s no chance of typos. I can dispatchEvent(new BallEvent(BallEvent.ROLL_BALL)) and addEventListener(BallEvent.ROLL_BALL, onRollBall)); and if I DO make a typo, my code compiler will complain and tell me where I went wrong before my SWF ever gets created.

One thing worth mentioning is the “public static const”. If you aren’t aware of this syntax – a static is a variable that is accessed as a singleton. That is, you don’t need to create a new BallEvent object in memory to use it – just say BallEvent.ROLL_BALL, and you’ll get the value: “rollBall”. Also, a “const” is a constant value – it’s something that never changes and you hardcode it ahead of time and don’t set it at runtime.

So that’s one advantage of custom events – and a good one. Things are readable and typos don’t exist. Furthermore, things can be grouped pretty well. My BallEvent class can have all my BallEvents whereas my FrisbeeEvent class can have all my Frisbee events…. it just leads to good organized code.

Now that’s all well and good – but maybe you said to yourself “Self, it’s great I know that my Ball is bouncing, but what time did it bounce, and at what angle, and at what velocity?

Well, I could receive the Ball bounce event, and say:
var bounceAngle = ball.bounceAngle;

But what good is that? The angle of the bounce is a snapshot of a moment in time. Maybe the angle has changed in the time between when I got the event and when I’m asking the ball what the angle is. All these things can change – ESPECIALLY when you change them yourself. What if you had special code to increase the velocity of the ball when it bounces off something? Well, guess what? You could increase the velocity on the ball bounce event, but then later on in your code, you could try to get the velocity. These order of event things can get confusing. Are you getting the velocity before the you changed it? Or is it after you changed it? It can get downright undependable and muddled.

However, if you included these values in your event, you can insure that you are grabbing these properties at the moment in time that they happened, whenever you want rather than relying on guesswork.

So let’s add a couple properties to the BallEvent. I want to track velocity, time, and angle.

public static const SPIN_BALL:String = “spinBall”;

                public var time:int;
                public var velocity:int;
                public var angle:int;
 
		public function BallEvent(type:String, eventTime:int, eventVelocity:int, eventAngle:int, bubbles:Boolean=false, cancelable:Boolean=false)
		{
                        time = eventTime;
                        velocity = eventVelocity;
                        angle = eventAngle;
			super(type, bubbles, cancelable);
		}
	}

So here, I just added the properties I want to the event. When I dispatch the event, I pass them in: dispatchEvent(new BallEvent(BallEvent.ROLL_BALL, 50, 21, 90));

And then when I get receive the event, I can get all the properties I need!

public function onBallRoll(event:BallEvent):void {
var myAngle:int = event.angle;
}

That’s the basics of the custom event – but there are some best practices that folks like to follow….

The first one is to implement a “clone” method. Let’s say that you listened and caught the event, but you really just want to pass it on to another class by dispatching the event from the class that caught it. Well you can call event.clone(), and you’ll get a copied event. Unfortunately, those extra properties you added won’t be cloned. So let’s look at a sample overridden clone method:

	public override function clone():Event {
		var event:BallEvent = new BallEvent(this.type, this.time, this.velocity, this.angle, this.bubbles, this.cancellable);
		return event;
	}

So here you’re actually creating a new BallEvent in this method and returning it when you clone. You’re using your public properties that you set when you originally made the event, and just rolling them into this new event. The only thing a little unintuitive here is that the return type is a normal Flash Event. Well.. it needs to be, because you’re overriding the Flash Event’s clone function (Flash will complain otherwise), but don’t worry – when you receive the event it will be cast as the BallEvent with all the properties you need.

For example, in:

public function onBallRoll(event:BallEvent):void

The event, if it originally started off as a BallEvent (even if it got casted to Event through cloning or otherwise), will still come through here as a proper BallEvent.

Honestly, sometimes I get lazy and don’t implement a clone event. Then several weeks later I need to clone the event and I curse my laziness. But sometimes that never happens. So it’s up to you if you think you need it.

Another “pro-tip” is to use getters and setters. Remember the concept of your properties, and how they represent a snapshot in time of the Ball? Well, some people feel that this isn’t good enough. Say I’m an evil programmer, intent to sabotage you and your events. I could listen for your event, program a line that says

event.time = 9999;

….and then clone and dispatch your event. You’d never catch on to my evil attempts. I’ve changed a property on the event – and then I passed the event on like nothing happened. It no longer represents a true snapshot in time – more like a doctored snapshot in time.

So, some folks try to stop this. They create getters but no setters. That way if an external class tries to change the property, it will cause your compiler to throw errors, thus saving you from malicious programming.

Let’s take a peek at a prim and proper finished BallEvent:

package events
{
	import flash.events.Event;
 
	public class BallEvent extends Event
	{
		public static const BOUNCE_BALL:String = "bounceBall";
		public static const THROW_BALL:String = "throwBall";
		public static const ROLL_BALL:String = "rollBall";
		public static const SPIN_BALL:String = "spinBall";
 
                protected var _time:int;
                protected var _velocity:int;
                protected var _angle:int;
 
                public function get time():int {
                    return _time;
                }
 
                public function get velocity():int {
                    return _velocity;
                }
 
                public function get angle():int {
                    return _angle;
                }
		public function BallEvent(type:String, eventTime:int, eventVelocity:int, eventAngle:int, bubbles:Boolean=false, cancelable:Boolean=false)
		{
                        _time = eventTime;
                        _velocity = eventVelocity;
                        _angle = eventAngle;
			super(type, bubbles, cancelable);
		}
 
	        public override function clone():Event {
		     var event:BallEvent = new BallEvent(this.type, _time, _velocity, _angle, _bubbles, _cancellable);
		     return event;
	        }
	}

Well, OK maybe if I was being all proper, I’d throw some ASDoc or some other type of comments in there – but you get the picture.

But my last point here is that I’ve set all my variables as protected (and I usually put an underscore in front of my protected variables because of a habit I have). The protected variables can’t be get or set except by the BallEvent class or anything that extends it (like a RubberBallEvent class or a BaseBallEvent class). So these properties are completely locked in and can’t be changed by other classes. But I make them accessible through my getters.

There are other little tricks I sometimes do too. Like in a current project I have a dashboard full of buttons. When I click my dashboard, I dispatch a Dashboard.CLICK event type. But then the actual button clicked is a property called ID, or maybe it’s a reference to the actual button.

This saves me a step. Instead of adding an event listener for each and every button in my dashboard, I can simply add one event listener to my dashboard for clicking an event. And then I can have logic in my event handler for deciding what to do based on what event was clicked.

Events are usually pretty simple too, but sometimes you can make them more complex. Like say you could dispatch an event with a time property, but then have several methods to format that time property however you like. Maybe your time is originally a huge number like 22531245665.

Getting this time might be useless to display to normal people. Maybe you want to include a formattedTime function in your event. Instead of getting event.time, you could get event.formattedTime and get “April 17, 2010 2:03PM”.

So that’s how to do custom events! Hope it helps.

Older Posts »

Powered by WordPress