Results 1 to 1 of 1
  1. #1
    JavaProphet's Avatar
    JavaProphet is offline Comma Freak
    Join Date
    Jun 2013
    Posts
    2
    Rep Power
    0

    Default Custom wave file not compiling properly?

    So, I wanted a method to transform a byte array into a samplable file(.wav / WAVE). Here's my code:
    Java Code:
    package cipherstream.audio;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.nio.ByteBuffer;
    
    public class WAVConverter {
    	public byte[] file = null;
    	public byte[] wav = null;
    	public boolean isWav = false;
    	
    	public WAVConverter(InputStream stream, boolean isWav) {
    		try {
    			file = new byte[stream.available()];
    			stream.read(isWav ? wav : file);
    		}catch (IOException e) {
    			e.printStackTrace();
    		}
    		this.isWav = isWav;
    	}
    	
    	public WAVConverter(File fi, boolean isWav) {
    		try {
    			InputStream stream = new FileInputStream(fi);
    			file = new byte[stream.available()];
    			stream.read(isWav ? wav : file);
    			stream.close();
    		}catch (IOException e) {
    			e.printStackTrace();
    		}
    		this.isWav = isWav;
    	}
    	
    	public WAVConverter(File fi) {
    		boolean isWav = fi.getName().endsWith(".wav");
    		try {
    			InputStream stream = new FileInputStream(fi);
    			file = new byte[stream.available()];
    			stream.read(isWav ? wav : file);
    			stream.close();
    		}catch (IOException e) {
    			e.printStackTrace();
    		}
    		this.isWav = isWav;
    	}
    	
    	public void genWav(int samplerate, short channels, int bytespersample) {
    		if (file != null) {
    			int flen = file.length + (file.length % (bytespersample * channels));
    			int size = flen + 44;
    			wav = new byte[size];
    			System.arraycopy("RIFX".getBytes(), 0, wav, 0, 4);
    			ByteBuffer bb4 = ByteBuffer.allocate(4);
    			bb4.putInt(0, 36 + flen);
    			bb4.position(0);
    			bb4.get(wav, 4, 4);
    			System.arraycopy("WAVE".getBytes(), 0, wav, 8, 4);
    			System.arraycopy("fmt ".getBytes(), 0, wav, 12, 4);
    			bb4.clear();
    			bb4.putInt(0, 16);
    			bb4.position(0);
    			bb4.get(wav, 16, 4);
    			ByteBuffer bb2 = ByteBuffer.allocate(2);
    			bb2.putShort(0, (short)1);
    			bb2.position(0);
    			bb2.get(wav, 20, 2);
    			bb2.clear();
    			bb2.putShort(0, channels);
    			bb2.position(0);
    			bb2.get(wav, 22, 2);
    			bb4.clear();
    			bb4.putInt(0, samplerate);
    			bb4.position(0);
    			bb4.get(wav, 24, 4);
    			bb4.clear();
    			bb4.putInt(0, samplerate * channels * bytespersample);
    			bb4.position(0);
    			bb4.get(wav, 28, 4);
    			bb2.clear();
    			bb2.putShort(0, (short)(channels * bytespersample));
    			bb2.position(0);
    			bb2.get(wav, 32, 2);
    			bb2.clear();
    			bb2.putShort(0, (short)(bytespersample * 8));
    			bb2.position(0);
    			bb2.get(wav, 34, 2);
    			System.arraycopy("data".getBytes(), 0, wav, 36, 4);
    			bb4.clear();
    			bb4.putInt(0, flen);
    			bb4.position(0);
    			bb4.get(wav, 40, 4);
    			System.arraycopy(file, 0, wav, 44, file.length);
    		}
    	}
    	
    	public byte[] getWav() {
    		if (wav == null) {
    			genWav(8000, (short)2, 2);
    		}
    		return wav;
    	}
    }
    EDIT: updated code to fix a smaller bug, problem still unresolved.

    My problem: When I compile a .wav from the bytecode, it has a 44-byte header, I'm using RIFX vs. RIFF, so I can use big-endian numbers. As for signed/unsigned, all of my values are correct. I cannot figure out why in VLC, my wav file is only 0:00 seconds long. My documentation: https://ccrma.stanford.edu/courses/4...ts/WaveFormat/

    And help is greatly appreciated!
    Last edited by JavaProphet; 07-03-2013 at 03:04 AM.

Similar Threads

  1. Properly buffering a custom JButton
    By Reskaillev in forum New To Java
    Replies: 2
    Last Post: 04-08-2012, 08:32 PM
  2. Replies: 3
    Last Post: 10-07-2011, 12:39 AM
  3. How to create wave file from the datainputstream
    By rajan701v in forum Advanced Java
    Replies: 15
    Last Post: 06-05-2011, 11:41 AM
  4. Play wave file out of a jar file
    By schliz in forum Advanced Java
    Replies: 2
    Last Post: 12-03-2009, 01:37 PM
  5. Compiling and using jar file for custom classes
    By MAILMIRZA in forum New To Java
    Replies: 3
    Last Post: 01-12-2009, 04:56 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •