Media Art, User Experience Design, and Interface Development

Communication between AS3 and Max/MSP/Jitter

Posted on 2010-05-27

Communication between AS3 and Max/MSP/Jitter

A while back, I thought of adding a touch screen film onto my laptop’s screen, so I could use Max/MSP/Jitter‘s incredible sound processing tools and interface more easily than keyboard and mouse in the context of an installation or a performance. It turns out that a couple of years later, screens are touchable more easily, although I have yet to find a tablet that will be interesting enough for me to buy and want to use with Max.

Nevertheless, I had fiddled around with AS3 to communicate with Max. I had planned for a while to write a post about this, but I thought it was more important to present how to start and manage a project. What I will present here is how to communicate data between the two software, and since Max is a desktop installation, I will create a pure AS3 Air project.

Just to give credit where credit is due, steps to create the Air project are mostly taken from here. Let’s have at it now!

Create an Air Project

Create an Air Project (step 1)
Create an Air Project (step 1)

Let’s first create a Flex project in Flash Builder 4. Right-click in the package explorer window, select “New > Flex Project”. Make sure that you check the “Desktop (runs in Adobe Air)” option in the “Application type” subsection, you do not want to make a web project.

Create an Air Project (step 2)
Create an Air Project (step 2)

In the next window, leave the “Output folder” field as is and click “Next”. Then, in the “Main application” field, change the .MXML extension to .AS and click “Finish.”

As a habit, I always have a initStage() function. In Air, you need to spawn an actual window, something we never do for the web. This is the perfect place to do so.

private function initStage():void
	// first, spawn a window
	// then set stage params
	stage.align = StageAlign.TOP_LEFT;
	stage.scaleMode = StageScaleMode.NO_SCALE;

In order to communicate with Max, we need to create an XMLSocket.

private var _socket:XMLSocket;
private var _host:String = "";
private var _port:uint = 8080;
private function initSocket():void
	_socket = new XMLSocket();
	_socket.addEventListener(IOErrorEvent.IO_ERROR, xmlSocketEventsHandler);
	_socket.addEventListener(Event.CONNECT, xmlSocketEventsHandler);
	_socket.addEventListener(Event.CLOSE, xmlSocketEventsHandler);
	_socket.addEventListener(DataEvent.DATA, xmlSocketDataHandler);
	_socket.connect(_host, _port);

private function xmlSocketEventsHandler(event:Event):void
	trace("\n", this, "--- xmlSocketEventsHandler ---");
		case "ioError":
			trace("\tcould not connect");

		case "connect":
			trace("\tsocket connected");

		case "close":
			trace("\tsocket closed");

			trace("\t" + event.type);

There are a couple of things to go through here. At the top of this code block, I instantiate the XMLSocket. I also create variables for the host and the port, which are both necessary to connect the XMLSocket.

Then, the initSocket() function takes care of initializing the XMLSocket, also adding some listeners to it and finally connecting it. For the connection, you may want to read about your platform (whether it’s Windows, Mac OS or other) to see what is the host IP and what ports can be used locally. You may also read up a bit on the Flash Player security, in case you need a cross domain XML file, although since this project is desktop-based and not going through a browser, I did not need it.

The xmlSocketEventsHandler() simply traces what events the XMLSocket receives.

Create a Max patch

Max files are called patches. It goes back to patching cords, but I won’t go too deep into the Max language, you may read all about it yourself on the Internet.

By default, Max does not communicate with Flash, but just like AS3, there are some “libraries” that can be added to Max, they are called externals. A couple of years back, the Flashserver external was easy to obtain from the author’s website or from the Max Objects Database, but it is sadly not the case anymore. I still have it and it is bundled with the source files for this tutorial, I hope I am not infringing anyone’s copyrights by making it available again.

Then let’s create a new patch and add the Flashserver object. This post is unfortunately not about how to use Max, thus I will not explain how to use the software here. I may in some future post, but this is way out of scope for this post.

Max patch
Max patch

Simply look at the help for the Flashserver object if you need to, but quickly, after the name is the argument for what port to connect to (“8080”) , and the last argument (“256”) is the amount of clients or Flash applications that can connect to this Max patch.

Talking with Max

When Flash needs to communicate with other languages and software, the other software usually has the role of a server, therefore it needs to be opened before the Flash/Air app. Thus, make sure you open the Max patch before attempting to communicate with Flash.

First, let’s look at receiving what Max may send to Flash. Earlier, there was one event handler that I did not define, here it is:

private function xmlSocketDataHandler(event:DataEvent):void

So the data is actually what Max sends to Flash. To send multiple values to Flash, you’ll need to create a pack in Max and send it as a string and then parse it in Flash.

Let’s look at the code to send data to Max:

_socket.send("hello max;");

It’s as simple as that! Note that the data always has to end with a semicolon (“;”). In the case where there are multiple values that need to be sent, separate them all with a semicolon, like so:

_socket.send("hello max; from flash;");

On Max’s side, simply use the unpack object to be able to parse the values into your patch.

Source files

As promised, here are the files that I used to create this project. The Air project was created in Eclipse with the Flash Builder 4 plugin, and the Max patch with Max 5, with the Flashserver external installed.

6 responses to “Communication between AS3 and Max/MSP/Jitter”

  1. mat janson blanchet says:

    I have to say that this tutorial is basically a port of the example provided with the Flashserver external, which was in AS2, to an AS3 version. I did not research the difference between the XMLSocket class and the Socket class for this tutorial, it could be interesting to look into.

  2. ubi de feo says:

    since you’re in AS3, why do you use XMLSocket rather than Socket?
    just a curiousity.
    I used XMLSocket in AS2 extensively, all the Nokia Flagship Store stuff ran off our very own C++ socket server, but when I ported stuff to AS3 I started using Socket.
    I liked it.

  3. sosofresh says:

    is it possible to use max to do audio processing and for max to send the audio to flash?

    i basically want to have a flash interface on a website that is able to control the sound they are hearing. and max will do all of that audio processing determined by the controls on the flash interface.

    how do you send the audio stream to flash?

  4. As far as I know, you can only communicate with Flash through simple data, not complex sound streams. You might want to look into OpenSoundControl, with something like flosc, or maybe SoundFlower. However, I do not think there is an input for sound in Flash. You may also want to look into AudioTool and learn how they synthesized sound directly in AS3.

  5. Florian says:

    Hey I wrote an Adobe Air application, which communicates without Flosc to MaxMSP. It uses the direct BinarySocket and the UDP Datagramm Object. The performance is really good for some real-time data.

    Maybe it is interesting for some of you. Here is my code

  6. @Florian: I think the way you work may be a lot better than what I present here, I had done this some 4 or 5 years ago. An Air app makes more sense now, thanks for the link!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.