Archive for the ‘Programming’ Category

Max Tap Tempo Calculator Extended

Saturday, April 4th, 2009

Max Tap Tempo Calculator ExtendedAfter a little bit more work in Max, I’ve extended the tap tempo calculator to diferentiate between semibreves, minims, crotchets, quavers and semi-quavers rather than just displaying the crotchet timing. I have left the big LED in so that it can be compared.

Basically the difference is that now the crotchets are split down into semi-quavers, and from there the metro goes into a counter and to the LED’s which are lit up as the counter counts through them on each beat of the metronome.

Aside from that it remains very simple in what it does – just does it in a slightly more interesting way. I’ll put up some more interesting things done in Max/MSP/Jitter soon, promise.

You can download the Max patch here.

Max Tap Tempo Calculator

Friday, March 27th, 2009

Max tap tempo calculatorThis semester at uni we are looking at graphical programming with Max/MSP/Jitter. This simple little patch is the first one we created as homework. The aim is to take a pair of taps on the bang object and work out the timing between them. The LED object will then flash on every beat.

It is also manually adjustable by changing the BPM or milliseconds in the two float boxes. You can also stop the flashing with the “Stop” button.

It’s very simple and straightforward, but it is a great little program to create as an introduction to graphical programming in Max and getting an idea of how it functions.

You can download the Max patch here.

Mandelbrot Player

Wednesday, November 5th, 2008

The mandelbrot before audio playback commencesI’ve built on my previous Frequency Mandelbrot to add basic audio playback controls to it. It no longer starts automatically upon loading, you now have to press ‘P‘ on your keyboard to begin playback and there are a number of other hot keys for different playback options.

An audio file can be played, paused, rewound, and fast-forwarded. The left and right audio channels as well as beats and the overall volume are taken and used to colour a mandelbrot fractal. The left and right audio channels generate a series of lines that change speed and direction based on the audio peaks. The centre of the fractal has a constraint on it based on the beats, so as the amount of beats increase, it will freeze and stay still while the outer area of the fractal, which appears as a background continues to animate around it. And yes, it is meant to have the lines through it.

The keyboard controls for playback are:

Play: P
Pause: S
Fast-Forward: F
Rewind: R
Restart: H
Stop: Q

Mandelbrot during audio playbackA note on these playback options, at the moment, ‘Q‘ will actually close the link to the song file, so this should not be used unless it is needed. Attempting to start the song again with ‘P‘ after ‘Q‘ has been used will cause Processing to error. I haven’t gotten around this as of yet unfortunately.

To replay a song from the start, press ‘H‘ and this will return the play head to the start of the file.

Rewind ‘R‘ and fast forward ‘F‘ skip the track back and forward by 500 milliseconds, so ‘R‘ should be used in place of ‘H‘ if you don’t want to return to the start of the song.

The source code is a little bit different to previous versions as I have added a class for keyPressed to determine what to do if the control keys are pressed:


// Create the controls.
void keyPressed()
{
if ( key == 'p' ) song.play(); // Press P to play the song.
if ( key == 's' ) song.pause(); // Press S to pause playing.
if ( key == 'h' ) song.rewind(); // Press H to reset to the start.
if ( key == 'f' ) song.skip(500); // Press F to fast forward.
if ( key == 'r' ) song.skip(-500); // Press R to rewind.
if ( key == 'q' ) song.close(); // Press Q to quit playing.
}

It’s all fairly straight forward, the Minim library is very simple to use and there are some great tools in it that you can play with. I highly recommend it if you are working with audio in Processing.

You can have a look at it in action here. The song used in this piece is “Freedom (Waking Mix)” by vo1k1 2008 – Licensed under Creative Commons Attribution Noncommercial (3.0).

This is fairly CPU intensive, so many computers will have trouble running this, it is also about 9mb to load so you probably don’t want to try it on a low speed connection.

The full source code is available from the same page as the applet, or you can see it after the jump:

(more…)

Frequency Mandelbrot

Sunday, September 21st, 2008

Frequency Mandelbrot 1Where I had been experimenting with just the outsides of the mandelbrot before, I’ve started having a look at modifying the inside of the fractal as well. I’ve continued to use the frequency spectrum for the effects simply because it goes with the frequency spectrum used in the background.

import ddf.minim.*;
import ddf.minim.analysis.*;

FFT fft;

AudioPlayer song;
BeatDetect beat;

float fSize;

void setup()
{
size(300, 300);

// always start Minim first!
Minim.start(this);

// specify 512 for the length of the sample buffers
// the default buffer size is 1024
song = Minim.loadFile("freedom.mp3", 512);
song.play();
beat = new BeatDetect();

float fSize = 20;
// an FFT needs to know how
// long the audio buffers it will be analyzing are
// and also needs to know
// the sample rate of the audio it is analyzing is
fft = new FFT(song.bufferSize(), song.sampleRate());
}

void draw()
{
background(0);
// first perform a forward fft on one of song's buffers
// I'm using the mix buffer
//  but you can use any one you like
fft.forward(song.mix);
fill(255);

loadPixels();

// Maximum number of iterations for each point on the complex plane
float maxiterations = 200;
float xmin = -2.5;
float ymin = -2;
float wh = 2;

beat.detect(song.mix);
if ( beat.isOnset() ) fSize += 0.1;

fSize = constrain(fSize, -10, 10);

ymin = -1;
xmin = -1.5;

maxiterations = maxiterations + fSize;

// x goes from xmin to xmax
float xmax = xmin + wh;
// y goes from ymin to ymax
float ymax = ymin + wh;

// Calculate amount we increment x,y for each pixel
float dx = (xmax - xmin) / (width);
float dy = (ymax - ymin) / (height);

// Start y
float y = ymin;
for(int j = 0; j < height; j++) {
// Start x
float x = xmin;
for(int i = 0;  i < width; i++) {

// Now we test, as we iterate z = z^2 + cm does z tend towards infinity?
float a = x;
float b = y;
int n = 0;
while (n < maxiterations) {
float aa = a * a;
float bb = b * b;
float twoab = 2.0 * a * b;
a = aa - bb + x;
b = twoab + y;
// Infinty in our finite world is simple, let's just consider it 16
if(aa + bb > 16.0f) {
break;         }
n++;
}

// We color each pixel based on how long it takes to get to infinity
// If we never got there, let's pick the color black
if (n == maxiterations) pixels[i+j*width] = 0;
else pixels[i+j*width] = color(n*(song.right.get(i)*50) % 255, n*(song.left.get(i)*50) % 200, n*(song.right.get(i)*30));
x += dx;
}
y += dy;
}
updatePixels();
}

void stop()
{
song.close();
super.stop();
}

Frequency Mandelbrot 2In this version I have used the BeatDetect class and another variable, fSize with a constraint on it and then added to the maximum iterations. This allows the frequency spectrum to spread through the mandelbrot fractal, or appear to anyway. It is actually just using the frequency spectrum to colour the fractal, and this makes it appear like spectrum is forming the fractal.

You can have a look at it in action here. The song used in this piece is “Freedom (Waking Mix)” by vo1k1 2008 – Licensed under Creative Commons Attribution Noncommercial (3.0).

This is fairly CPU intensive, so many computers will have trouble running this, it is also about 13mb to load so you probably don’t want to try it on a low speed connection.

Minim and Beat Detection

Wednesday, September 17th, 2008

While I was looking through the Minim documentation, I came across the built in BeatDetect class that analyses the audio input for beats. My understanding is that it looks for patterns in the rhythm to determine individual beats of a particular type, be it the bass drum, snare, cymbals etc. It has a couple of modes for detecting beats, one is sound energy mode and the other is frequency energy mode.

Sound energy mode just looks for spikes in the audio mix levels and returns a boolean response of true when a spike occurs.

Frequency energy mode allows you to look at particular frequency bands and use their output the same way you would the output of sound energy mode.

I decided to have a play with the sample code using frequency energy mode that is provided in the documentation to figure out how to control it and so on.

The sample code checks the frequency bands for the snare, kick/bass drum and high hats and then has text for each one that responds to the audio input for their particular band.

I didn’t do anything overly complex with this as of yet, just changed the objects that are manipulated to boxes rather than text so I could get an idea of of what does what.

Here is the code that I ended up with after my changes:

import ddf.minim.*;
import ddf.minim.analysis.*;

class BeatListener implements AudioListener
{
private BeatDetect beat;
private AudioPlayer source;

BeatListener(BeatDetect beat, AudioPlayer source)
{
this.source = source;
this.source.addListener(this);
this.beat = beat;
}

void samples(float[] samps)
{
beat.detect(source.mix);
}

void samples(float[] sampsL, float[] sampsR)
{
beat.detect(source.mix);
}
}

AudioPlayer song;
BeatDetect beat;
BeatListener bl;

float kickSize, snareSize, hatSize;

void setup()
{
size(512, 200);
smooth();
Minim.start(this);
song = Minim.loadFile("song.mp3", 1024);
song.play();
// a beat detection object that is FREQ_ENERGY mode that
// expects buffers the length of song's buffer size
// and samples captured at songs's sample rate
beat = new BeatDetect(song.bufferSize(), song.sampleRate());
beat.setSensitivity(300);
kickSize = snareSize = hatSize = 16;
// make a new beat listener, so that we won't miss any buffers for the analysis
bl = new BeatListener(beat, song);
}

void draw()
{
background(0);
fill(255);
if ( beat.isKick() ) kickSize = 32;
if ( beat.isSnare() ) snareSize = 32;
if ( beat.isHat() ) hatSize = 32;
stroke(255);
strokeWeight(kickSize);
rect(width/4, height/2, 20, 20);
stroke(255);
strokeWeight(snareSize);
rect(width/2, height/2, 20, 20);
stroke(255);
strokeWeight(hatSize);
rect(3*width/4, height/2, 20, 20);
kickSize = constrain(kickSize * 0.95, 16, 32);
snareSize = constrain(snareSize * 0.95, 16, 32);
hatSize = constrain(hatSize * 0.95, 16, 32);
}

void stop()
{
// always close Minim audio classes when you are finished with them
song.close();
// always stop Minim before exiting
Minim.stop();

// this closes the sketch
super.stop();
}

For this to work, make sure you have an MP3 or Wav file in a data folder within the sketch directory for this sketch and change song.mp3 to the filename of your song.