Attachment 'PcmWaveByteArrayWriter.java'

Download

   1 package au.com.foundationstone.model.audio.jspeex;
   2 
   3 
   4 import java.io.ByteArrayOutputStream;
   5 import java.io.File;
   6 import java.io.IOException;
   7 import org.xiph.speex.AudioFileWriter;
   8 import org.xiph.speex.PcmWaveWriter;
   9 
  10 
  11 /**
  12  * Pcm Stream Writer
  13  * unfortunately cannot subclass PcmWaveWriter to get at the many private instance variables
  14  */
  15 public class PcmWaveByteArrayWriter extends AudioFileWriter {
  16 	private static final int FORTY = 40;
  17 	private static final int FORTY_ONE = 41;
  18 	private static final int FORTY_TWO = 42;
  19 	private static final int FORTY_THREE = 43;
  20 	private static final int FOUR = 4;
  21 	private static final int FIVE = 5;
  22 	private static final int SIX = 6;
  23 	private static final int SEVEN = 7;
  24 	private static final int TWENTY_FOUR = 24;
  25 	private static final int SIXTEEN = 16;
  26 	private static final int OXFF = 0xff;
  27 	private static final int EIGHT = 8;
  28 	private final ByteArrayOutputStream gBaos = new ByteArrayOutputStream();
  29 	private byte[] gResultByteArray = null;
  30 	/** Defines the sampling rate of the audio input. */
  31 	private int sampleRate;
  32 	/** Defines the number of channels of the audio input (1=mono, 2=stereo). */
  33 	private int channels;
  34 	private int size;
  35 
  36 
  37 	/**
  38 	 * Constructor.
  39 	 */
  40 	public PcmWaveByteArrayWriter() {
  41 		size = 0;
  42 	}
  43 
  44 
  45 	public PcmWaveByteArrayWriter(final int aSampleRate, final int theChannels) {
  46 		this();
  47 		setPCMFormat(aSampleRate, theChannels);
  48 	}
  49 
  50 
  51 	private void setPCMFormat(final int aSampleRate, final int theChannels) {
  52 		this.channels = theChannels;
  53 		this.sampleRate = aSampleRate;
  54 	}
  55 
  56 
  57 	/**
  58 	 * Closes the output file.
  59 	 * MUST be called to have a correct stream.
  60 	 * @exception IOException if there was an exception closing the Audio Writer.
  61 	 */
  62 	public void close() throws IOException {
  63 		gResultByteArray = gBaos.toByteArray();
  64 		gBaos.close();
  65 		int fileLength = gResultByteArray.length - EIGHT;
  66 		// write a little ending int
  67 		gResultByteArray[FOUR] = new Integer(OXFF & fileLength).byteValue();
  68 		gResultByteArray[FIVE] = new Integer(OXFF & (fileLength >>> EIGHT)).byteValue();
  69 		gResultByteArray[SIX] = new Integer(OXFF & (fileLength >>> SIXTEEN)).byteValue();
  70 		gResultByteArray[SEVEN] = new Integer(OXFF & (fileLength >>> TWENTY_FOUR)).byteValue();
  71 		// write a little ending int
  72 		gResultByteArray[FORTY] = new Integer(OXFF & size).byteValue();
  73 		gResultByteArray[FORTY_ONE] = new Integer(OXFF & (size >>> EIGHT)).byteValue();
  74 		gResultByteArray[FORTY_TWO] = new Integer(OXFF & (size >>> SIXTEEN)).byteValue();
  75 		gResultByteArray[FORTY_THREE] = new Integer(OXFF & (size >>> TWENTY_FOUR)).byteValue();
  76 	}
  77 
  78 
  79 	public void open(final File file) throws IOException {
  80 		throw new UnsupportedOperationException("you need a PcmWaveWriter");
  81 	}
  82 
  83 
  84 	public void open(final String filename) throws IOException {
  85 		throw new UnsupportedOperationException("you need a PcmWaveWriter");
  86 	}
  87 
  88 
  89 	/**
  90 	 * this is the entry point. call getBytes() to collect the results.
  91 	 */
  92 	public void open() {
  93 		size = 0;
  94 	}
  95 
  96 
  97 	public byte[] getBytes() {
  98 		return gResultByteArray;
  99 	}
 100 
 101 
 102 	public void writeHeader(final String comment) throws IOException {
 103 		/* writes the RIFF chunk indicating wave format */
 104 		byte[] chkid = "RIFF".getBytes();
 105 		gBaos.write(chkid, 0, chkid.length);
 106 		writeInt(gBaos, 0); /* total length must be blank */
 107 		chkid = "WAVE".getBytes();
 108 		gBaos.write(chkid, 0, chkid.length);
 109 		/* format subchunk: of size 16 */
 110 		chkid = "fmt ".getBytes();
 111 		gBaos.write(chkid, 0, chkid.length);
 112 		writeInt(gBaos, SIXTEEN); // Size of format chunk
 113 		writeShort(gBaos, PcmWaveWriter.WAVE_FORMAT_PCM); // Format tag: PCM
 114 		writeShort(gBaos, (short) channels); // Number of channels
 115 		writeInt(gBaos, sampleRate); // Sampling frequency
 116 		writeInt(gBaos, sampleRate * channels * 2); // Average bytes per second
 117 		writeShort(gBaos, (short) (channels * 2)); // Blocksize of data
 118 		writeShort(gBaos, (short) SIXTEEN); // Bits per sample
 119 		/* write the start of data chunk */
 120 		chkid = "data".getBytes();
 121 		gBaos.write(chkid, 0, chkid.length);
 122 		writeInt(gBaos, 0);
 123 	}
 124 
 125 
 126 	public void writePacket(final byte[] data, final int offset, final int len) throws IOException {
 127 		gBaos.write(data, offset, len);
 128 		size += len;
 129 	}
 130 }

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.

You are not allowed to attach a file to this page.