Alan N. Shapiro, Autonomy in the Digital Society

Blog and project archive about media theory, science fiction theory, and creative coding

Generating Sound Art from Java Code, by Anna Semykina

Comments Off on Generating Sound Art from Java Code, by Anna Semykina

My generative art project is written in Java code. I want to generate sound from mathematics. I want to read a graphics file pixel by pixel, get the RGB values of each pixel, and generate sound from that. In a second iteration, I will move beyond the pixel by pixel concept to a coded schema which is a layer of interpretation between the image itself and a concept of categorization of images according to the organization of an image library or archive. In this first iteration of the project, the code produces an output.mp3 file of artistic sound generated from numbers. There are two input files: goove.mp3 and silence.mp3, which both contribute to the final result.

I used the Eclipse Java Integrated Development Environment (IDE).

MenuActions.java

package superobjects.ui;
import javax.swing.*;
import java.io.*
import java.awt.*;
import java.awt.event.*;

public class MenuActions extends JFrame implements ActionListener
{

// Attribute
private JButton Knopf1;
private JButton Knopf2;
private JButton Knopf3;
private JButton Knopf4;
private JButton Knopf5;
private JButton Knopf6;
private JButton Knopf7;
private JButton Knopf8;
private JButton Knopf9;

// Konstruktor

public MenuActions()
{
super (“Make Sound from RGB Value of Pixels in Images”);

JPanel Platte = new JPanel ();

Platte.setLayout (new GridLayout(9,1));

// Buttons erzeugen
Knopf1 = new JButton (“Set Creative Space Parameters”);
Knopf2 = new JButton (“Set SuperObject Parameters”);
Knopf3 = new JButton (“Generate Turing-Compliant Code That Will Generate Quasi-Code”);
Knopf4 = new JButton (“Generate Quasi-Code”);
Knopf5 = new JButton (“Run Slowed-Down Conventional Clock Code”);
Knopf6 = new JButton (“Run Superfast Clock Code”);
Knopf7 = new JButton (“Run Variable Time Code”);
Knopf8 = new JButton (“Run Three Clocks Code in Sync”);
Knopf9 = new JButton (“Evaluate Output”);

// Schriftart und -größe

Knopf1.setFont(new Font(“Arial”, 0, 20));
Knopf2.setFont(new Font(“Arial”, 0, 20));
Knopf3.setFont(new Font(“Arial”, 0, 20));
Knopf4.setFont(new Font(“Arial”, 0, 20));
Knopf5.setFont(new Font(“Arial”, 0, 20));
Knopf6.setFont(new Font(“Arial”, 0, 20));
Knopf7.setFont(new Font(“Arial”, 0, 20));
Knopf8.setFont(new Font(“Arial”, 0, 20));
Knopf9.setFont(new Font(“Arial”, 0, 20));

// Buttons mit Ereignissen verknöpfen
Knopf1.addActionListener (this);
Knopf2.addActionListener (this);
Knopf3.addActionListener (this);
Knopf4.addActionListener (this);
Knopf5.addActionListener (this);
Knopf6.addActionListener (this);
Knopf7.addActionListener (this);
Knopf8.addActionListener (this);
Knopf9.addActionListener (this);

// Alles einsetzen

Platte.add (Knopf1);
Platte.add (Knopf2);
Platte.add (Knopf3);
Platte.add (Knopf4);
Platte.add (Knopf5);
Platte.add (Knopf6);

Screen Shot 2014-09-03 at 2.57.13 PM

 

 

Screen Shot 2014-09-03 at 2.55.34 PM
Platte.add (Knopf7);
Platte.add (Knopf8);
Platte.add (Knopf9);

setContentPane (Platte);
}

 

 

public void actionPerformed (ActionEvent Ereignis)
{
Object Quelle = Ereignis.getSource();

if (Quelle.equals(Knopf1)) {
JOptionPane.showMessageDialog (null, “Set Creative Space Parameters”);
}

if (Quelle == Knopf2) {
JOptionPane.showMessageDialog (null, “Set SuperObject Parameters”);
}

if (Quelle == Knopf3) {
JOptionPane.showMessageDialog (null, “Generate Turing-Compliant Code That Will Generate Quasi-Code”);
}

if (Quelle == Knopf4) {
JOptionPane.showMessageDialog (null, “Generate Quasi-Code”);
}

if (Quelle == Knopf5) {
JOptionPane.showMessageDialog (null, “Run Slowed-Down Conventional Clock Code”);
}

if (Quelle == Knopf6) {

JOptionPane.showMessageDialog (null, “Run Superfast Clock Code”);

}

if (Quelle == Knopf7) {
JOptionPane.showMessageDialog (null, “Run Variable Time Code”);
}

if (Quelle == Knopf8) {

JOptionPane.showMessageDialog (null, “Run Three Clocks Code in Sync”);

}

if (Quelle == Knopf9) {
JOptionPane.showMessageDialog (null, “Evaluate Output”);
}

}

public static void main(String[] args)
{
MenuActions Rahmen = new MenuActions();
Rahmen.setSize (604,300);
Rahmen.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
Rahmen.setVisible(true);
}

}

HalloFenster.java

package einaus4;

import javax.swing.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;

public class HalloFenster extends JFrame implements ActionListener
{

static int INTERFERENCE = 10;
static int RANDOMBYTE = 5;

// Attribute

private JButton Knopf1;
private JButton Knopf2;
private JButton Knopf3;
private JButton Knopf4;
private JButton Knopf5;
private JButton Knopf6;

// Konstruktor

public HalloFenster ()
{

super (“Make Sound from RGB Value of Pixels in Images”);
JPanel Platte = new JPanel ();
Platte.setLayout (new GridLayout(6,1));

// Buttons erzeugen
Knopf1 = new JButton (“Read RGB Values from Image”);
Knopf2 = new JButton (“Read MP3 File”);
Knopf3 = new JButton (“Make Sound Test 1”);
Knopf4 = new JButton (“Make Sound Test 2”);
Knopf5 = new JButton (“Make Sound Test 3”);
Knopf6 = new JButton (“Make Sound Test 4”);

// Schriftart und -größe

Knopf1.setFont(new Font(“Arial”, 0, 20));
Knopf2.setFont(new Font(“Arial”, 0, 20));
Knopf3.setFont(new Font(“Arial”, 0, 20));
Knopf4.setFont(new Font(“Arial”, 0, 20));
Knopf5.setFont(new Font(“Arial”, 0, 20));
Knopf6.setFont(new Font(“Arial”, 0, 20));

// Buttons mit Ereignissen verkn¸pfen
Knopf1.addActionListener (this);
Knopf2.addActionListener (this);
Knopf3.addActionListener (this);
Knopf4.addActionListener (this);
Knopf5.addActionListener (this);
Knopf6.addActionListener (this);

// Alles einsetzen

Platte.add (Knopf1);
Platte.add (Knopf2);
Platte.add (Knopf3);
Platte.add (Knopf4);
Platte.add (Knopf5);
Platte.add (Knopf6);

setContentPane (Platte);
}

public void actionPerformed (ActionEvent Ereignis)
{
Object Quelle = Ereignis.getSource();

if (Quelle == Knopf1) {
JOptionPane.showMessageDialog (null, “Read RGB Values From Image”);
}

if (Quelle == Knopf2) {
JOptionPane.showMessageDialog (null, “Read MP3 File”);
}

if (Quelle == Knopf3) {
JOptionPane.showMessageDialog (null, “Make Sound Test 1”);
}

if (Quelle == Knopf4) {
JOptionPane.showMessageDialog (null, “Make Sound Test 2”);
}

if (Quelle == Knopf5) {
JOptionPane.showMessageDialog (null, “Make Sound Test 3”);
}

if (Quelle == Knopf6) {
JOptionPane.showMessageDialog (null, “Make Sound Test 4”);

try {
File song = new File(“silence.mp3”);
FileInputStream file = new FileInputStream(song);
File trashedSong = new File(“output.mp3”);
FileOutputStream trash = new FileOutputStream(trashedSong);

//byte[] b1 = {65,66,67,68,69,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90};
//byte[] b2 = {75,76,77,78,79};

int RGB[][] = new int[8000][6];

// pixel #0
for (int xx = 0; xx < 8000; xx++) {
RGB[xx][0] = 45;
RGB[xx][1] = 55;
RGB[xx][2] = 65;
RGB[xx][3] = 75;
RGB[xx][4] = 85;
RGB[xx][5] = 95;
}

boolean eof = false;
int count = 0;
System.out.println(“Creating file …”);

int RGBObjectNum = 0;
int RedGreenOrBlueValue = 0; // 0 = red;< 1 = green; 2 = blue

while (!eof) {
int input = file.read();

if (input == -1)
eof = true;

else {
count++;

if (count % INTERFERENCE == 0) {

if (RGBObjectNum < 4000) {
trash.write(RGB[RGBObjectNum][RedGreenOrBlueValue]);

if (RedGreenOrBlueValue == 0)
RedGreenOrBlueValue = 1;
else if (RedGreenOrBlueValue == 1)
RedGreenOrBlueValue = 2;
else
RedGreenOrBlueValue = 0;
}
} else
trash.write(input);
}
}

file.close();
trash.close();

System.out.println(“Done”);
} catch (Exception e) {
System.out.println(“Error — ” + e.toString());
}
}
}

public static void main(String[] args)
{
HalloFenster Rahmen = new HalloFenster ();
Rahmen.setSize (400,300);
Rahmen.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
Rahmen.setVisible(true);
}
}

Comments are closed.