Spiral Music

The visualisation and sonification of maths is demonstrated in this class. A simple siral is both drawn as graphics and 'drawn' as a score. Spirals are one of the simplest examples of a fractal. The pattern of a spiral moves both forward and backward in time. Therefore the sonification of the spiral in the way it's done here would be impossible in real-time (you can't go back in time), but because jMusic can 'render' the music offline we can skip around in time as we compose in jMusic.

This is what the result sounds like.

The source files.

SpiralWindow.java - SpiralCanvas.java - SpiralMusic.java

The SpiralWindow class has the main method. it opens a frame and inserts the SpiralCanval instance in it and calls the SpiralMusic class. The SpiralCanvas class is responsible for the drawing of a siral. The SpiralMusic class is responsible for creating a jMusic score based on the spiral math.

Spiral Window

import java.awt.*;
import javax.swing.*;
 
/**
* A graphic algortihm sonification program based on Fractal math
* @author Natalie Rouillon and Andrew Brown
*/
 
public class SpiralWindow extends JFrame {
    public static void main(String[] args) {
        // create a new frame
        JFrame f = new JFrame("Natalie's Fractal Window");
        
        // create a panel to put canvases onto
        JPanel pan = new JPanel();
        // create multiple canvases and add to the panel
        for(int i=0; i<1;i++) {
            SpiralCanvas fc = new SpiralCanvas();
            pan.add(fc);
        }
        // add the panel to the frame and display it
        f.setContentPane(pan);
        f.pack();
        f.setVisible(true);
        
        // do music
        new SpiralMusic();
    }
}

Spiral Graphics

import java.awt.*;
 
// Draws a spiral - the simplest fractal interation
 
class SpiralCanvas extends Canvas {
	
    public SpiralCanvas() {
        super();
        this.setSize(200,200);
    }
 
 
    public void paint(Graphics g) {
        double r;
        int x;
        int y;
        int centreOffset = 100;
        int oldx = centreOffset;
        int oldy = centreOffset;
        double PI = 3.141593;
        double scaleFactor = 2.0;
        // colour
        g.setColor(Color.red);
        
        for(double i=0; i<16*PI; i += 0.1) {
            r = scaleFactor * i;
            x = (int)(r * Math.cos(i)) + centreOffset;
            y = (int)(r * Math.sin(i)) + centreOffset;
            g.drawLine(oldx,oldy,x,y);
            // update previous location
            oldx = x;
            oldy = y;
        }	
    }	
}

Spiral Music

import java.awt.Point;
import jm.music.data.*;
import qt.*;
import jm.util.*;
 
class SpiralMusic {
    public SpiralMusic() {
        Score s = new Score();
        Part p = new Part();
        int x;
        int y;
        int centreOffset = 100;
        int oldx = centreOffset;
        int oldy = centreOffset;
        double r;
        double PI = 3.141593;
        double scaleFactor = 2.0;
        for(double i=0; i<16*PI; i += 0.1) {
            r = scaleFactor * i;
            x = (int)(r * Math.cos(i)) + centreOffset;
            y = (int)(r * Math.sin(i)) + centreOffset;
            // do music
            Phrase phr = new Phrase();
            phr.setStartTime((double)x / 16.0);
            Note n = new Note(127-y/2, 0.125, 100);
            n.setDuration(1.0/(double)y);
            phr.addNote(n);
            p.addPhrase(phr);
            // update previous location
            oldx = x;
            oldy = y;
        }
        s.addPart(p);
        
        View.show(s, 250,0);
 
        // play back with QuickTime immediatly
        QTUtil qtu = new QTUtil();
        qtu.playback(s);
 
        // save MIDI file also
        Write.midi(s, "SpiralMusic.mid");
    }
}

Try changing the tempo and instrument to get different effects.


jMusic Tutorial Index

© 2002 Andrew R Brown