Press "Enter" to skip to content

Tag: java

“Sonifying Processing: The Beads Tutorial” Introduces Sound Art Creation In Processing

Over the past year, I’ve had the pleasure of discovering Oliver Bown’s wonderful sound art library, Beads. Beads is a library for creating and analyzing audio in Processing or Java, and it is head-and-shoulders above the other sound libraries that are available for Processing. From the ground up, Beads is made for musicians and sound artists. It takes ideas popularized by CSound, Max and other popular sound art environments, and intuitively wraps them in the comfort of the Processing programming language.

Today I’m proud to announce the release of my free ebook on sound art in Processing, Sonifying Processing: The Beads Tutorial. Also available in print and Kindle editions from

The cover of the print edition of my book on sound art in the processing programming language.

The book covers all of the standard sound-art topics in straightforward tutorial style. Each chapter addresses a basic topic, then demonstrates it in code. Topics covered include Additive Synthesis, Frequency Modulation, Sampling, Granular Synthesis, Filters, Compression, Input/Output, MIDI, Analysis and everything else an artist may need to bring
their Processing sketches to life.

It’s true that these topics are well-covered by other environments in other places. There are a plethora of sound art platforms these days. I love Pure Data, Max, SuperCollider and even Tassman and Reaktor. But there are a million people out there making visual art in Processing who don’t have a good way of exploring multimedia in the environment in which they’re comfortable. This tutorial is aimed at Processing programmers who think that sound art is a bridge too far.

In fact, Beads makes sound art incredibly easy while staying within the comfortable confines of Processing. So stop reading blog posts. Download the Beads Library. Download the book. Download the source code. And get to it!!

Cloth-based Computer Music

This is such a bizarre, unique little project that I had to post it. The sound in the video is really quiet, so turn your speakers up.

Each point on the clothe is responsible for one sine wave instrument. It’s position and movement cause the pitch of that particular instrument to change. The counter at the bottom is how many xmlHttpRequests have been sent. The cloth demo was stolen from and Source code: [1]

Black Allegheny, Swarm Generated Music

Black Allegheny is one of the first albums made up entirely of swarm generated music. The album was created using a swarm-controlled sampler called Becoming, which was programmed by the composer.

<a href="">Imperceptible Time by Evan X. Merz</a>

Becoming is an algorithmic composition program written in java, that builds upon some of John Cage’s frequently employed compositional processes. Cage often used the idea of a “gamut” in his compositions. A gamut could be a collection of musical fragments, or a collection of sounds, or a collection of instruments. Often, he would arrange the gamut visually on a graph, then use that graph to piece together the final output of a piece. Early in his career, he often used a set of rules or equations to determine how the output would relate to the graph. Around 1949, during the composition of the piano concerto, he began using chance to decide how music would be assembled from the graph and gamut.

In Becoming, I directly borrow Cage’s gamut and graph concepts; however, the software assembles music using concepts from the AI subfield of swarm intelligence. I place a number of agents on the graph and, rather than dictating their motions from a top-down rule-based approach, the music grows in a bottom-up fashion based on local decisions made by each agent. Each agent has preferences that determine their movement around the graph. These values dictate how likely the agent is to move toward food, how likely the agent is to move toward the swarm, and how likely the performer is to avoid the predator.

Yes, this is my new album! Thanks for reading and listening!

On CDM, with a great comments thread
On Make Online
Swarm Sampler On MatrixSynth
On Noise for Airports (a great intellectual music blog!)
On Califaudio

With the Blurred Vision of a Newborn

This piece unites Cage’s conception of graph music with ideas from the field of swarm intelligence. The software uses a graph of notated musical fragments to generate a score in real-time, for live performance. It does this by allowing a swarm of virtual insects to crawl over the graph, choosing new fragments with each move.

Reading and Writing WAV Files in Java

Earlier this year, we looked at how to read and write wav files in C++ and VB.Net, and C/C++ is really the natural choice for this kind of file IO (for a number of reasons that we won’t go into here). Nonetheless, not everyone uses C or C++ for computer music software. Increasingly, programmers are turning to Java.

There are a number of libraries available for reading and writing wav files, however, to really understand the format, it’s necessary to get your hands dirty. So in this article, we are looking at reading and writing wav files in Java, which is not as easy as it may seem.

Java Numbers

The problem is that Java has no unsigned types. In other words, all numerical types in java can hold either positive or negative numbers. There are no types that are designed for only positive numbers.

This doesn’t sound like a big deal unless you know a little bit about how negative numbers are represented in most programming languages. The most important thing to know is that most languages represent negative numbers, in a way that is incompatible with the way they represent only positive numbers. More specifically, wav file headers represent numbers as unsigned bytes, whereas java represents numbers using a format called Two’s Complement.

The practical implications of this are simple: you can’t use java’s built-in IO functions for reading wav headers. You have to read the data as bytes, then do the conversion in a subroutine.

Understanding this problem is hard, but luckily, working around it is relatively easy. In our solution, we made 2 changes. First, we stored all of our numbers with more bytes than are necessary in other languages (to ensure that we are storing the correct numbers). So for each 2-byte number, which would usually be stored by a short, we used an int, and for each 4-byte number, we used a long. Then we added 4 extra subroutines to do the conversions for us. These routines are at the bottom of

// these two routines convert a byte array to a unsigned short
public static int byteArrayToInt(byte[] b)

  int start = 0;
  int low = b[start] & 0xff;
  int high = b[start+1] & 0xff;
  return (int)( high << 8 | low );

// these two routines convert a byte array to an unsigned integer

public static long byteArrayToLong(byte[] b)
  int start = 0;
  int i = 0;
  int len = 4;
  int cnt = 0;
  byte[] tmp = new byte[len];
  for (i = start; i < (start + len); i++)

    tmp[cnt] = b[i];
  long accum = 0;
  i = 0;
  for ( int shiftBy = 0; shiftBy < 32; shiftBy += 8 )

    accum |= ( (long)( tmp[i] & 0xff ) ) << shiftBy;
  return accum;

// returns a byte array of length 4

private static byte[] intToByteArray(int i)
  byte[] b = new byte[4];
  b[0] = (byte) (i & 0x00FF);
  b[1] = (byte) ((i >> 8) & 0x000000FF);
  b[2] = (byte) ((i >> 16) & 0x000000FF);

  b[3] = (byte) ((i >> 24) & 0x000000FF);
  return b;

// convert a short to a byte array
public static byte[] shortToByteArray(short data)

  return new byte[]{(byte)(data & 0xff),(byte)((data >>> 8) & 0xff)};

WAV File Specification

Again, we are using a VERY basic version of the wav file format for this example. This won’t read many wav files, it will only read what are referred to here as canonical wave files. Basically, old fashioned wav files (before the chaos introduced by 24 and 32 bit recording).

If you want to read more complicated wav files, you may want to check out what’s written here and here, although you really need to look at the actual specification (and you had better have some free time).

The Source Code

To use the source code, just unzip all the files into the same directory, and then use the batch files I created called compile.bat and run.bat. Of course, I am assuming that you have the latest version of java installed.

Download the source code for reading and writing wav files in java here.