Skip to content

Instantly share code, notes, and snippets.

@ohmygodwin
Created November 5, 2013 05:03
Show Gist options
  • Save ohmygodwin/7314145 to your computer and use it in GitHub Desktop.
Save ohmygodwin/7314145 to your computer and use it in GitHub Desktop.
import ddf.minim.*;
import ddf.minim.ugens.*;
Minim minim;
AudioOutput out;
String[] metamorphosis;
int counter = 1509;
String delimiters = " ,.?!;:[]";
// the Oscil we use for modulating frequency.
Oscil fm;
// setup is run once at the beginning
void setup()
{
// initialize the drawing window
size( 662, 250, P3D );
//The Metamorphosis
String url = "http://www.gutenberg.org/files/5200/5200.txt";
//Frankenstein
//String url = "http://www.gutenberg.org/files/84/84.txt";
//Encyclopedia of Needlework
//String url = "http://www.gutenberg.org/files/20776/20776.txt";
String[] rawtext = loadStrings(url);
String everything = join(rawtext, "" );
metamorphosis = splitTokens(everything,delimiters);
minim = new Minim( this );
out = minim.getLineOut();
// arguments are frequency, amplitude, and waveform
Oscil wave = new Oscil( 200, 0.8, Waves.TRIANGLE );
fm = new Oscil( 10, 2, Waves.SINE );
// set the offset of fm so that it generates values centered around 200 Hz
fm.offset.setLastValue( 200 );
// patch it to the frequency of wave so it controls it
fm.patch( wave.frequency );
// and patch wave to the output
wave.patch( out );
}
void draw()
{
background( 255 );
stroke( 0 );
String theword = metamorphosis[counter];
// Count how many times that word appears in text
int total = 0;
int wordLength = 0;
for (int i = 0; i < metamorphosis.length; i ++ ) {
if (theword.equals(metamorphosis[i])) {
total++;
}
// measure word length
wordLength = theword.length();
}
// draw the waveforms
for( int i = 0; i < out.bufferSize() - 1; i++ )
{
// find the x position of each buffer value
float x1 = map( i, 0, out.bufferSize(), 0, width );
float x2 = map( i+1, 0, out.bufferSize(), 0, width );
// draw a line from one buffer position to the next for both channels
line( x1, 100 + out.left.get(i)*50, x2, 100 + out.left.get(i+1)*50);
line( x1, 150 + out.right.get(i)*50, x2, 150 + out.right.get(i+1)*50);
}
counter = (counter + 1) % metamorphosis.length;
float modulateAmount = map( total, 0, 11000, 220, 1 );
float modulateFrequency = map( wordLength, 0, 20, 0.1, 100 );
fm.frequency.setLastValue( modulateFrequency );
fm.amplitude.setLastValue( modulateAmount );
println(total + ", " + wordLength);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment