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.*;
public class SpiralWindow extends JFrame {
public static void main(String[] args) {
JFrame f = new JFrame("Natalie's Fractal Window");
JPanel pan = new JPanel();
for(int i=0; i<1;i++) {
SpiralCanvas fc = new SpiralCanvas();
pan.add(fc);
}
f.setContentPane(pan);
f.pack();
f.setVisible(true);
new SpiralMusic();
}
}
|
Spiral Graphics
import java.awt.*;
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;
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);
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;
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);
oldx = x;
oldy = y;
}
s.addPart(p);
View.show(s, 250,0);
QTUtil qtu = new QTUtil();
qtu.playback(s);
Write.midi(s, "SpiralMusic.mid");
}
}
|
Try changing the tempo and instrument to
get different effects.
|