import java.io.*;
import java.io.File;
import javax.sound.sampled.*;
public class InOut {
public static void main(String[] args) {
try {
File srcFile = new File("earthmovers1.mp3");
File dstFile = new File("earthmovers2.mp3");
FileInputStream in = new FileInputStream(srcFile);
FileOutputStream out = new FileOutputStream(dstFile);
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();
} catch ( Exception e ) {System.out.println( e );}
}
}
Showing posts with label java. Show all posts
Showing posts with label java. Show all posts
Friday, June 10, 2011
Java: Write to File: Copy Bytes: Copy Music
Thursday, June 9, 2011
aLife cellular automata with sound output
Warning: a typo creating a bug in this, needs repair.
import javax.sound.sampled.*;
import java.util.Random;
/**
*
*/
public class Alife
{
private double frequency = 1000.0;
private double rad = 0.5 * Math.PI;
private double volume = 256.0;
private int byteSize = 10000;
public Alife(int myByteSize, double myFrequency, double myRad, double myVolume ) {
byteSize =myByteSize;
frequency = myFrequency;
rad = myRad;
volume = myVolume;
}
public void aByte() {
Random r = new Random();
try
{
AudioFormat af = new
AudioFormat( (float)byteSize, 8, 1, true, true );
DataLine.Info info = new
DataLine.Info ( SourceDataLine.class, af );
SourceDataLine source =
(SourceDataLine) AudioSystem.getLine( info );
source.open( af );
source.start();
byte[] buf = new byte[byteSize];
int bufLess = buf.length - 10;
int localPop = 0;
int deaths = 0;
int births = 0;
int currentPop = 0;
int iterations = 0;
// RULE 1 Any live cell with fewer than two live neighbours dies, as if caused by under-population.
// RULE 2 Any live cell with two or three live neighbours lives on to the next generation.
// RULE 3 Any live cell with more than three live neighbours dies, as if by overcrowding.
// RULE 4 Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.
boolean firstRun = true;
while (true) {
iterations++;
localPop = 0;
deaths = 0;
births = 0;
currentPop = 0;
rad = rad + 0.1;
if (rad>1.0) {rad=0.0;}
for ( int i=0; i<buf.length; i++ ) {
if (volume>512) {volume=0;}
if (firstRun == false) {
if (i > 10) {
if (i < bufLess) {
if (buf[i-1]>0.0) {localPop++;}
if (buf[i+1]>0.0) {localPop++;}
if (buf[i-2]>0.0) {localPop++;}
if (buf[i+2]>0.0) {localPop++;}
// GOT A LOCAL POPULATION COUNT ABOVE
if ((buf[i]<0.1) && (localPop==3)) { rad=r.nextGaussian();frequency++;volume++; } // RULE 4
else { if ( (buf[i]>0.0) && (localPop>3) ) {rad=0.0; frequency--; } // RULE 3
else { if ( (buf[i]>0.0) && (localPop<2) ) {rad=0.0;frequency--;volume--; } // RULE 1
else { rad=rad+0.1 ; volume++;frequency++; } // RULE 2
}
}
}
}
}
if (buf[i]>0) {currentPop++;} else {deaths++;}
buf[i] = (byte)( Math.sin( rad * frequency / byteSize ) * volume );
}
source.write( buf, 0, buf.length );
source.write( buf, buf.length, 0 );
source.write( buf, 0, currentPop );
source.write( buf, deaths, 0 );
firstRun = false;
System.out.println( "current pop:" + currentPop );
System.out.println( "deaths:" + deaths );
System.out.println( "iterations:" + iterations );
}
// source.drain();
// source.stop();
// source.close();
}
catch ( Exception e )
{
System.out.println( e );
}
}
public static void main(String[] args) {
int byteSize = 10000;
double frequency = 500.0;
double myRad = 0.2 * Math.PI;
double volume = 256.0;
Alife line1 = new Alife(byteSize, frequency, myRad, volume);
line1.aByte();
}
}
Sunday, May 22, 2011
Basic Tone Class and Instantiation
I'm in the process of relearning Java sound generation, and hope to write programs that output interesting soundscapes. This blog posting has very rudimentary code, and hope to continue enhancing it. I will post links here to the bigger and better code samples after they are written.
import javax.sound.sampled.*;
import java.util.Random;
/**
*
*/
public class Tone
{
private int seconds = 1 ;
private int sampleRate = 8000;
private double frequency = 1000.0;
private double rad = 0.5 * Math.PI;
private double volume = 128.0;
public Tone(int mySeconds, int mySampleRate, double myFrequency, double myRad, double myVolume ) {
seconds = mySeconds;
sampleRate = mySampleRate;
frequency = myFrequency;
rad = myRad;
volume = myVolume;
}
public void setSeconds(int newValue) {
seconds =newValue;
}
public int getSeconds() {
return seconds;
}
public void setSampleRate(int newValue) {
sampleRate = newValue;
}
public int getSampleRate() {
return sampleRate;
}
public void setFrequency(double newValue) {
frequency = newValue;
}
public double getFrequency() {
return frequency;
}
public void setRad(double newValueA, double newValueB ) {
rad = newValueA * newValueB; // e.g. 0.5 * Math.PI
}
public double getRad() {
return rad;
}
public void setVolume(double newValue) {
volume = newValue;
}
public double getVolume() {
return volume;
}
public void playTone() {
try
{
AudioFormat af = new
AudioFormat( (float)sampleRate, 8, 1, true, true );
DataLine.Info info = new
DataLine.Info ( SourceDataLine.class, af );
SourceDataLine source =
(SourceDataLine) AudioSystem.getLine( info );
source.open( af );
source.start();
byte[] buf = new byte[sampleRate * seconds];
for ( int i=0; i<buf.length; i++ )
{
buf[i] =
(byte)( Math.sin( rad * frequency / sampleRate * i ) * volume );
}
source.write( buf, 0, buf.length );
source.drain();
source.stop();
source.close();
}
catch ( Exception e )
{
System.out.println( e );
}
}
}
import javax.sound.sampled.*;
public class TestTone
{
public static void main (String[] args) {
double myRad = 0.5 * Math.PI;
Tone lineOne = new Tone(1, 8000, 1000.0, myRad, 128.0 );
lineOne.playTone();
}
}
Below is a little more interesting than a single second tone:
import javax.sound.sampled.*;
import java.util.Random;
public class TestTone
{
public static void main (String[] args) {
Random generator = new Random();
double myRad = 0.2 * Math.PI;
double volume = 128.0;
for (int i=0;i<10;i++) {
if (i % 2 == 0) {volume=volume+16; myRad=myRad + 0.2;}
else {volume = volume-8; myRad= myRad + Math.PI;}
Tone lineOne = new Tone(generator.nextInt(2), 8000, 200.0, myRad, volume );
if (i % 3 == 0) {volume=volume-16; myRad=myRad - 0.2;}
else {volume = volume+16; myRad= myRad * Math.PI;}
Tone lineTwo = new Tone(generator.nextInt(2), 8000, 500.0, myRad, volume );
lineOne.playTone();
lineTwo.playTone();
}
}
}
The coding is awful in the source code below, but the sound output is way cool. I'm posting it so I don't lose it.
import javax.sound.sampled.*;
import java.util.Random;
/**
* Generate a tone mathematically.
*
* @author this.is.lance.miller@gmail.com
*/
public class WildTone
{
/**
* Generate a tone
*
* @param args not used
*/
Random generator = new Random();
public static void main ( String[] args )
{
Random generator = new Random();
int seconds = 1;
int sampleRate = 8000;
double frequency = 4000.0;
double frequencyb = 2000.0;
double frequencyc = 500.0;
double RAD = 0.1* Math.PI/1;
double RADb = 0.1* Math.PI/1;
// double RAD = generator.nextDouble()*Math.PI/generator.nextInt(8);
int volume = 256; // generator.nextInt(512);
int looplimit = 90;
int extraInt = 1;
int bufInt = 1;
double extraDbl = 0.1;
int loopValley = generator.nextInt(sampleRate);
try
{
AudioFormat af = new
AudioFormat( (float)sampleRate, 8, 1, true, true );
DataLine.Info info = new
DataLine.Info ( SourceDataLine.class, af );
SourceDataLine source =
(SourceDataLine) AudioSystem.getLine( info );
source.open( af );
source.start();
byte[] buf = new byte[sampleRate * seconds];
byte[] bufb = new byte[(sampleRate * seconds)];
for (int j=1;j<looplimit;j++) {
for ( int i=0; i<buf.length; i++ )
{
RAD= extraDbl*Math.PI/extraInt;
RADb= (0.1*Math.PI/extraInt)*.2;
loopValley = generator.nextInt(5);
loopValley=loopValley+1000;
if (i > loopValley) {
if (i < loopValley+500) {
if (i > loopValley+600) {
if (i < loopValley+620) { RAD= generator.nextDouble()*Math.PI/2; }
else {extraDbl=0.1;}
}
else {extraDbl=0.4;}
}
else {
if (extraDbl > 0.3) {extraDbl = 0.1; frequency=4000.0;}
else { extraDbl=extraDbl+0.1; frequency=500.0; }
}
}
if (i % 2 == 0 || i % 3 == 1 ) {volume=0;}
else {
if (i < sampleRate*seconds/2 ) { volume=256;}
else {volume=512; frequency=frequencyb;}
}
buf[i] =
(byte)( Math.sin( RAD * frequency / sampleRate * i ) * volume );
if (i % 2 == 0) {
bufb[i] =
(byte)( Math.sin( RADb * (frequencyb) / sampleRate * i ) * (volume-256) );
} else {
bufb[i] =
(byte)( Math.sin( RADb * (frequencyc) / sampleRate * i ) * (volume-256) );
}
}
bufInt=generator.nextInt(2);
if (bufInt > 0 ) { source.write( bufb, 0, bufb.length/bufInt );}
else { source.write( buf, 0, buf.length );
if (bufInt == 2) { source.write( bufb, 0, bufb.length/3 );}}
} // end for j
source.drain();
source.stop();
source.close();
}
catch ( Exception e )
{
System.out.println( e );
}
System.exit( 0 );
}
}
Sunday, December 30, 2007
JAVA: Printmyself Class
import java.io.*;
import java.lang.*;
public class Printmyself {
public static void main(String[] args) {
try
{
String fileName = "Printmyself.java";
FileReader fileRdr = new FileReader(fileName);
BufferedReader bfrRdr = new BufferedReader(fileRdr);
String line = " ";
while(bfrRdr.readLine() != null) {
line = bfrRdr.readLine();
System.out.println(line);}
}
catch (IOException ioe) { System.out.println(ioe); }
}
}
>javac Printmyself.java
>java Printmyself
Wednesday, December 26, 2007
JAVA: Cellular Automata
import java.awt.*;
//John Conway's Game of Life
//altered from barry tolnas applet
public class CA1 {
public static void main(String[] args) {
int width =100;
int height=100;
int[][] CA1 = new int[width][]; //Array to hold the cell states
int[][] CA2 = new int[width][]; //Array to hold new states temporarily
for (int col=0; col<width; col++) {
CA1[col] = new int[height];
CA2[col] = new int[height];
}
//Initialize a box of cells
final int boxwidth = 100;
final int cx = width/2; //center in the x direction
final int cy = height/2; //center in the y direction
for (int y=0; y<boxwidth/2; y++) {
for(int x=0; x<boxwidth/2; x++) {
int initialState=(int)(2*Math.random()); //Get a random 1 or 0
CA1[cx-x][cy-y]=initialState; //These four lines create
CA1[cx+x][cy-y]=initialState; // a symmetrical pattern of
CA1[cx+x][cy+y]=initialState; // cells by placing four
CA1[cx-x][cy+y]=initialState; // copies of each in the
} // right places.
}
int sum;
while (true) { // infinite loop
//Update all cells
for(int y=1; y<height-1; y++) { //loop down window drawing rows
for(int x=1; x<width-1; x++) { //loop over each cell in row
//Draw the state of the current cell
if (CA1[x][y]==0) ;
else ;
//g.drawLine(x,y,x,y);
//Figure out next state
sum=0;
sum += CA1[x-1][y];
sum += CA1[x-1][y+1];
sum += CA1[x][y+1];
sum += CA1[x+1][y+1];
sum += CA1[x+1][y];
sum += CA1[x+1][y-1];
sum += CA1[x][y-1];
sum += CA1[x-1][y-1];
if (CA1[x][y]==0 && sum==3) { CA2[x][y] = 1; // cell is born
System.out.println("oh; joy; a child is");
System.out.println("born");
continue; }
if (CA1[x][y]==1 && sum<2) { CA2[x][y] = 0; //die of loneliness
System.out.println("oh; so so sad;");
System.out.println("that one died of loneliness");
System.out.println("**\n***\n****\n*****\n******\n");
continue; }
if (CA1[x][y]==1 && sum>=4) { CA2[x][y] = 0; // die of overpopulation
System.out.println("oh; sad; death by");
System.out.println("over population");
System.out.println("**\n***\n****\n*****\n******\n");
continue; }
CA2[x][y] = CA1[x][y];
System.out.println("oh, i am");
System.out.println("staying alive and doing ok");
System.out.println("**\n***\n****\n*****\n******\n");
}
}
//Copy the new states in CA2 back into CA1 for the next iteration.
for (int y=1; y<height-1; y++) {
for (int x=1; x<width-1; x++) CA1[x][y]=CA2[x][y];
}
}
}
}
- Copy the souce code above into a file called CA.java
- javac CA.java
- java CA1
Subscribe to:
Posts (Atom)