/* JRG, the Resource Geology Seismic Processing System for Java, and Viewmat for Java are Copyright 1998-2001 by John N. Louie The software and methods here are the subject of academic research, not commercial products. I would like to know what use you make of my methods, and have your feedback on their success or failure. Also, by letting me know who you are, I can inform you if bugs or errors are discovered. Please send me an email message with: your name and email address; whether you are a student or faculty member, consultant or employee; the name of your university or company, and department; and a sentence or two describing what use you intend to make of these methods. Send your message to louie@seismo.unr.edu */ import java.io.*; class testaif { public static void main(String args[]) { AifFileHead hd = new AifFileHead(1000, 5000); hd.createAifFileHead("1000Hz.aif"); } } public class AifFileHead { public byte b00; /* F - Begins IFF files */ public byte b01; /* O */ public byte b02; /* R */ public byte b03; /* M */ public int fbytes; /* number of bytes to follow in file */ public byte b08; /* A */ public byte b09; /* I */ public byte b10; /* F */ public byte b11; /* F */ public byte b12; /* C - Begins Common Chunk */ public byte b13; /* O */ public byte b14; /* M */ public byte b15; /* M */ public int commbytes; /* 18 - number of bytes to follow in Common chunk */ public short chan; /* 2 - number of channels */ public int frames; /* number of sample frames */ public short bits; /* 16 - bits per sample */ public short w28; /* 16398 - begins IEEE 754 80-bit float */ public int Hz; /* 44100 - samples per second - unsigned short in file */ public short w32; /* 0 */ public short w34; /* 0 */ public short w36; /* 0 */ public byte b38; /* S - Begins Sound Data Chunk */ public byte b39; /* S */ public byte b40; /* N */ public byte b41; /* D */ public int sdcbytes; /* number of bytes to follow in Sound Data Chunk, including data */ public int boffset; /* 0 - byte offset of first sample frame in data blocks */ public int bsize; /* 0 - sound data block size, alignment not needed */ public static final int AIFFILEHEADBYTES = 54; /* Constructor */ public AifFileHead() { setvals(); } public void setvals() { b00 = 70; /* F */ b01 = 79; /* O */ b02 = 82; /* R */ b03 = 77; /* M */ fbytes = AIFFILEHEADBYTES - 8; /* number of bytes to follow in file */ b08 = 65; /* A */ b09 = 73; /* I */ b10 = 70; /* F */ b11 = 70; /* F */ b12 = 67; /* C */ b13 = 79; /* O */ b14 = 77; /* M */ b15 = 77; /* M */ commbytes = 18; /* 18 - number of bytes to follow in Common chunk */ chan = (short)(2); /* 2 - number of channels */ frames = 0; /* number of sample frames */ bits = (short)(16); /* 16 - bits per sample */ w28 = (short)(16398); /* 16398 - begins IEEE 754 80-bit float */ Hz = 44100; /* 44100 - samples per second */ w32 = (short)(0); /* 0 */ w34 = (short)(0); /* 0 */ w36 = (short)(0); /* 0 */ b38 = 83; /* S */ b39 = 83; /* S */ b40 = 78; /* N */ b41 = 68; /* D */ sdcbytes = 8; /* number of bytes to follow in Sound Data Chunk, including data */ boffset = 0; /* 0 - byte offset of first sample frame in data blocks */ bsize = 0; /* 0 - sound data block size, alignment not needed */ setsizes(0); } /* sets byte sizes according to number of samples per channel */ public void setsizes(int frames) { int databytes; this.frames = frames; databytes = frames*this.chan*(this.bits/8); this.sdcbytes = databytes + 8; this.fbytes = AIFFILEHEADBYTES - 8 + databytes; } /* Construct a copy */ public AifFileHead(AifFileHead fhd) { setvals(); this.chan = fhd.chan; this.frames = fhd.frames; this.bits = fhd.bits; this.Hz = fhd.Hz; this.boffset = 0; this.bsize = 0; this.setsizes(fhd.frames); } /* Construct with Hz and samples/chan */ public AifFileHead(int Hz, int frames) { setvals(); this.Hz = Hz; this.setsizes(frames); } public String toString() { return "AIFF " + (fbytes+8) + " bytes: " + chan + " chan " + frames + " samp/chan of " + bits + " bits/samp at " + Hz + " samp/second"; } public boolean isValid() { if (Hz < 1 || b00 != 70 || bits != 16 || chan != 2) return false; return true; } public void readAifFileHead(String name) { FileInputStream fis = null; BufferedInputStream bis = null; DataInputStream dis = null; System.out.println("readAifFileHead: reading header from file " + name + " ... "); try {fis = new FileInputStream(name); } catch(FileNotFoundException fnf) { System.out.println(fnf.toString()); return; } bis = new BufferedInputStream(fis); dis = new DataInputStream(bis); try {read(dis); } catch(IOException ioe) { System.out.println("Could not read AIFF header from file " + name); return; } try {fis.close(); } catch(IOException ioe) { System.out.println(ioe.toString()); return; } System.out.println("readAifFileHead done."); } public void read(DataInputStream dis) throws IOException { b00 = dis.readByte(); /* F */ b01 = dis.readByte(); /* O */ b02 = dis.readByte(); /* R */ b03 = dis.readByte(); /* M */ fbytes = dis.readInt(); /* number of bytes to follow in file */ b08 = dis.readByte(); /* A */ b09 = dis.readByte(); /* I */ b10 = dis.readByte(); /* F */ b11 = dis.readByte(); /* F */ b12 = dis.readByte(); /* C */ b13 = dis.readByte(); /* O */ b14 = dis.readByte(); /* M */ b15 = dis.readByte(); /* M */ commbytes = dis.readInt(); /* 18 - number of bytes to follow in Common chunk */ chan = (short)(dis.readUnsignedShort()); /* 2 - number of channels */ frames = dis.readInt(); /* number of sample frames */ bits = (short)(dis.readUnsignedShort()); /* 16 - bits per sample */ w28 = (short)(dis.readUnsignedShort()); /* 16398 - begins IEEE 754 80-bit float */ Hz = dis.readUnsignedShort(); /* 44100 */ w32 = (short)(dis.readUnsignedShort()); /* 0 */ w34 = (short)(dis.readUnsignedShort()); /* 0 */ w36 = (short)(dis.readUnsignedShort()); /* 0 */ b38 = dis.readByte(); /* S */ b39 = dis.readByte(); /* S */ b40 = dis.readByte(); /* N */ b41 = dis.readByte(); /* D */ sdcbytes = dis.readInt(); /* number of bytes to follow in Sound Data Chunk, including data */ boffset = dis.readInt(); /* 0 - byte offset of first sample frame in data blocks */ bsize = dis.readInt(); /* 0 - sound data block size, alignment not needed */ } public void write(DataOutputStream dos) throws IOException { dos.writeByte(b00); /* F */ dos.writeByte(b01); /* O */ dos.writeByte(b02); /* R */ dos.writeByte(b03); /* M */ dos.writeInt(fbytes); /* number of bytes to follow in file */ dos.writeByte(b08); /* A */ dos.writeByte(b09); /* I */ dos.writeByte(b10); /* F */ dos.writeByte(b11); /* F */ dos.writeByte(b12); /* C */ dos.writeByte(b13); /* O */ dos.writeByte(b14); /* M */ dos.writeByte(b15); /* M */ dos.writeInt(commbytes); /* 18 - number of bytes to follow in Common chunk */ dos.writeShort(chan); /* 2 */ dos.writeInt(frames); /* number of sample frames */ dos.writeShort(bits); /* 16 - bits per sample */ dos.writeShort(w28); /* 16398 - starts 10-byte IEEE 754 float */ dos.writeByte(Hz >> 8); /* write Hz as an unsigned short */ dos.writeByte(Hz - ((Hz >> 8) << 8)); dos.writeShort(w32); /* 0 */ dos.writeShort(w34); /* 0 */ dos.writeShort(w36); /* 0 */ dos.writeByte(b38); /* S */ dos.writeByte(b39); /* S */ dos.writeByte(b40); /* N */ dos.writeByte(b41); /* D */ dos.writeInt(sdcbytes); /* number of bytes to follow in Sound Data Chunk, including data */ dos.writeInt(boffset); /* 0 - byte offset of first sample frame in data blocks */ dos.writeInt(bsize); /* 0 - sound data block size, alignment not needed */ } public void createAifFileHead(String name) { int i, bytes; FileOutputStream fos = null; BufferedOutputStream bos = null; DataOutputStream dos = null; System.out.print("createAifFileHead: writing " + AIFFILEHEADBYTES + "-byte AIFF header to file " + name + "... "); try {fos = new FileOutputStream(name); } catch(IOException ioe) { System.out.println("Can't create file " + name); System.out.println(ioe.toString()); return; } bos = new BufferedOutputStream(fos); dos = new DataOutputStream(bos); try { this.write(dos); } catch(IOException ioe) { System.out.println("Could not write " + AIFFILEHEADBYTES + " bytes to file " + name); return; } try { dos.flush(); bos.flush(); fos.close(); } catch(IOException ioe) { System.out.println(ioe.toString()); return; } System.out.println("done."); } }