Audio Programming Intro

UPDATE: After writing this intro post I spent the next few weeks writing posts about the ins and outs of making noise with AS3/Flash. You can view the whole list of posts about audio programming here: http://labs.makemachine.net/category/audio/. Also, remember to check back. I’ve got a lot more to learn and I plan to keep sharing it with you kind reader.



The notion of writing algorithms that are used to create sound is a pretty abstract concept, one I have been wanting to understand for some time now. I’ve been working with digital audio tools for over ten years and programming software for nearly six. I have the next few weeks open so it seems like a great time for these two interests to finally merge.


To gain a basic understanding of some of the principles, I’ve been experimenting with the dynamic audio capabilities of Actionscript 3.0. It’s a very simple API but I feel this is a good thing as the limitations of the language narrow the scope of distraction. Eventually, it would be great to write this kind of code in Python or C++. I might even look into writing something audio related for Android in the future. For now Flash is a great medium because it will run on the web and it is great for displaying concepts graphically.


Ultimately I’d like to build an online synthesizer that has a mobile or desktop counterpart. This would include writing an open source library for making audio programs. As I explore, I’ll be posting tutorials and snippets on how to create waveforms, filters, envelopes, modulation and other effects. In the meantime here are a few resources that I’ve found helpful.

Posted in Actionscript, Audio | Tagged , , , , , , | Leave a comment

Property Names From JsonObject

Here is a quick snippet for finding the property names of an com.google.gson.JsonObject.

Set<Map.Entry<String, JsonElement>> map = myJObj.entrySet();
Iterator<Map.Entry<String, JsonElement>> iterator = map.iterator();
int size = map.size();for( int k = 0; k < size; k++ )
 {
        // .getKey() returns the name of the property
        String key = iterator.next().getKey();
        iterator.remove();
}
Posted in Java | Tagged , , | Leave a comment

Android AsyncTask Example

Today I was working on a loading screen for an Android app. The purpose of the loading screen is to report to the user which boot up processes were happening as well as provide a nice looking intro screen for the app. The U.I. layer of an Android app runs in a single thread. You can think of a thread as a stack of function calls. Each function in the list executes only after the one before it has completed. This is a very simplistic view of a thread but for the sake of this post it will work. With multiple processor intensive functions stacked up the U.I. layer can become inactive and appear to hang while waiting for the stack to finish. This problem can be solved by splitting up the processes into separate threads. By doing so your U.I. layer can update concurrent with the other more intensive processes.

The Android operating system provides a couple of solutions to this problem. The solution I implemented for the loading screen was a class called AsyncTask. It’s a wrapper for a threaded operation that provides some really convenient callbacks for updating progress information and handling task completion.

» Download complete example code here

Let’s look at how to go about instantiating an AsyncTask. First off, you must sub-class AsyncTask and provide three parameters. These three parameters indicate the types of objects that are passed to three callbacks you can override in your sub-class. You can set these parameter to any type that suits your application. In the following example I am using a Context object and two Integer objects.

protected class InitTask extends AsyncTask<Context, Integer, Integer>
In the line above we define our sub-class and the three parameters that will be passed to the callbacks. The callbacks look like this:

doInBackground()

@Override
protected Integer doInBackground( Context... params )  {
     return super.doInBackground( params )
}

Anything processed in this method is handled in a sperate thread. Note that the data type of the return value is an Integer and corresponds to the type third parameter in the class definition. This value returned from this method is passed to the onPostExecute() method when this thread completes.

onProgressUpdate()

@Override
protected void onProgressUpdate(Integer... values)  {
     super.onProgressUpdate(values);
}

Report progress of async operations via this method. Note the param with datatype Integer. This corresponds to the second parameter in the class definition. This callback can be triggered from within the body of the doInBackground() method by calling publishProgress()

onPostExecute()

@Override
protected void onPostExecute( Integer result )  {
      super.onPostExecute(result);
}

This method is called when the thread has completed successfully. Note that the datatype passed to this parameter matches the third parameter in the method definition.

The AsyncTask class provides to other helpful callback methods onCancelled() which handles the cancellation of the thread and onPreExecute() which is called just before the thread calls doInBackground().

A complete example project using AsyncTask is available from the makemachine code repository.

Posted in Android | 18 Comments

Newness

The past couple of weeks have been pretty busy for me. I moved from Germany to a middle of nowhere Smithsonian research station in Maryland. All the moving around and travel has brought on a random ping pong like exploration in my coding. I’m not sure if there is even a common thread between all of the half finished ventures I’ve been working on. Soon I will be settling into yet another new living arrangement. It should last about six months. I’m looking for my next project. It would be great to work on one project for six months. Perhaps itemizing the seemingly aimless options will aid in shaping my direction. Here are some possibilities.

C/C++

Starting from the ground up. Learn the most rudimentary lessons of programming. Develop an understanding of memory usage, pointers, compilers and binding C to other languages.

Cinder

Cinder is a very new completely open source framework for “creative coding”. Similar to Processing and openFrameworks, Cinder is designed by the The Barbarian Group and seems to be the next cool language for building interactive and rich graphical applications.

Linux

I’ve set up VMWare on my machine. Now I can run Linux and OSX at the same time. Thinking about setting up a dedicated machine just Linux. Hmm, this could go a million places in itself.

PyThreading/EventDispatcher

Working on an event and async operations lib for Python. So far I’ve completed an EventDispatcher and some simple asynchronous operations.

OLPC

I have been an One Laptop Per Child enthusiast for quite a while now. Perhaps it’s time to start writing apps for the Sugar platform. Sugar is the open source operating system that runs on the OLPC laptops. The really nice thing about programming for Sugar is that Python is the main scripting language. Of all the languages I know of, Python gets the gold star. It’s flexible, portable and fast (enough). There is a whole list of ideas for possible Sugar apps at the OLPC wiki. I could see myself getting involved in several of these.

Sugar Music Collaboration

Along the same line of thought as the OLPC. I’ve always been interested in the idea of creating a music collaboration tool. Perhaps some sort of real-time digital orchestra where learners could play along using digital sheet music. Each player could choose an instrument and follow along using the keys of the keyboard as a piano of sorts.

Writing

I have an idea for a science fiction/historical thriller novel. It could be interesting to make it an online experience and release it in monthly episodes for six months. Not sure where this came from… I haven’t written anything other than technical documentation for a long time… could be fun though…

Graphs & Charts

Would be great fun to build a library for creating rich statistical data visualizations.

U.I. Toolkit

Components are fun to build. I started working on a set of simple components for Flash a while back. Then I found that Keith Peters had updated his excellent minimalcomps component set. That discovery promptly ended my Flash component project. Why re-invent the button right? I could write something similar for Python or C++.

In Summary

I am looking for a new project. Something that can consume hour upon hour and in the end be really useful. Things are pretty wide open at this point. I have about a week and half before I move again. In which time I hope to nail down an idea. Perhaps someone out there would like to collaborate or has some suggestions…

Posted in General | 2 Comments