This blog has moved

This blog is now at http://www.celesteh.com/blog

Saturday 29 November 2014

Broken bmp converter

I've spent all evening trying to convert from .au files to bmp, and I've realised I'm really doing this the wrong way. This realisation partly brought about becase the conversion is obviously not working. The images are kind of pretty, though, so I thought I'd post the script. It runs in Processing, which is a simplified version of java.

How this code works is that it opens a file dialogue, in which the user is expected to select an .au audio file, but it doesn't enforce this, so try data bending any file if you want. It then prints the file out onto the screen as if it were made up of integers giving pixel colours. Then, it saves that as a bmp.

What it should do is just put a bmp header at the top of an existing file, but I thought I was being smart.

the code is below. This was developed for a new commission I'm trying to get done. Hopefully, I'll sort this out properly tomorrow, or if not, it will have to wait for the next digital commission (which could be yours!).

Commissions make excellent gifts! Order now and delivery is guaranteed in time for Christmas or Hanukkah.


// look at au header
// get the offset (32 bit word #1 (starting from 0))
// get the size (32 bit word #2) -- nevermind that
// set the sketch to that size, roughly square
// write from the the offset to the end to all the pixels
// save as bmp


import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.DataInputStream;

int x, y;
long length;

DataInputStream din;
FileInputStream fin;

String name;


void setup()
{
  x = 100; y = 100;
  size(4410, 600); // 1 minute
  selectInput("Select a file to process:", "fileSelected");
  frame.setResizable(true);
  noLoop();

}

void fileSelected(File file){
  
    try {
    //File file = new File("/home/celesteh/Documents/inProgress/shorts/rh/auToBmp/data/sine.au");
    name = file.getName();
    
    fin = new FileInputStream(file);
    din = new DataInputStream(fin);
    din.readInt(); // magic number
    int offset = din.readInt();
    System.out.println(offset);
    int skipTo = 16;//offset-8;// (32 bits + 32 bits) / 8
    din.skipBytes(skipTo);
    length = file.length() /*864*/ - skipTo;
    length = length /4; // 32 bit ints
    System.out.println(length);
    float root = sqrt(length);
    x = ceil(root);
    y = floor(root);
    while( (x * y) < length)
      x++;
    System.out.println(x);
    frame.setSize(x, y);
    redraw();
  } catch (IOException fe)
  {  exit();
  }
  

  
  //redraw();
  //redraw();
}

void draw() {
    int b;
    
    frame.setSize(x, y);
    
    if (din != null) {
      try {
        loadPixels();
        for (int i = 0; (i < length) && (i < pixels.length); i++)  {
          pixels[i] = din.readInt(); //readByte();
          //din.readByte(); din.readByte();
        }
        //updatePixels();
        din.close(); 
      } catch (IOException fe) {
        
      } finally {
        updatePixels();
        saveFrame(name.concat(".bmp"));
        
      }
              
     

    }
}

No comments:

Commission Music

Commission Music
Bespoke Noise!!