1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
| import java.io.*;
public class Bin2Wav {
static int []head= { 0x52, 0x49, 0x46, 0x46, 0xD4, 0x2D, 0x11, 0x02, 0x57, 0x41, 0x56, 0x45,
0x66, 0x6D, 0x74, 0x20, 0x10, 0x00, 0x00, 0x00, 0x01, 0x00, 0x02, 0x00, 0x44, 0xAC, 0x00, 0x00, 0x10, 0xB1, 0x02, 0x00, 0x04, 0x00, 0x10, 0x00,
0x64, 0x61, 0x74, 0x61 };
public static void main(String[] args) throws IOException { try { if (args.length == 0) throw new IllegalArgumentException("Wrong number of arguments");
File inFile=new File(args[0]); File outFile; if(args.length==1) outFile=new File(args[0]+".wav"); else outFile=new File(args[1]);
long len=inFile.length();
FileInputStream inStream=new FileInputStream(inFile); DataOutputStream outStream=new DataOutputStream(new FileOutputStream(outFile));
System.out.println(inFile.getAbsolutePath() + " has " + len + " bytes.\n");
System.out.println("writing header"); for(int i=0;i<head.length;i++) { outStream.writeByte(head[i]); } outStream.writeInt(endian((int)len)); outStream.flush();
System.out.println("copying data (samples)"); copy(inStream, outStream); System.out.println("\nk. done."); } catch (Exception e) { System.err.println(e); System.err.println("Usage: java Bin2Wav infile.bin [outfile.wav]"); } }
public static int endian( int x ) { int a = x >>> 24 ; int b = ( x >>> 16 ) & 0xff ; int c = ( x >>> 8 ) & 0xff ; int d = x & 0xff ; x = ( d << 24 ) | ( c << 16 ) | ( b << 8 ) | a ; return x ; }
static void copy( InputStream fis, OutputStream fos ) { try { byte buffer[] = new byte[0xffff]; int nbytes;
while ( (nbytes = fis.read(buffer)) != -1 ) fos.write( buffer, 0, nbytes ); } catch( IOException e ) { System.err.println( e ); } finally { if ( fis != null ) try { fis.close(); } catch ( IOException e ) {}
try { if ( fos != null ) fos.close(); } catch ( IOException e ) {} } } } |