lib/minim/src/ddf/minim/javasound/FloatSampleTools.java
/**
	 * Central conversion function from normalized float array to a byte array.
	 * In order to accomodate interleaved and non-interleaved samples, this
	 * method takes outByteStep as parameter which can be used to flexibly
	 * convert the data.
	 * <p>
	 * E.g.:<br>
	 * mono->mono: outByteStep=format.getFrameSize()<br>
	 * interleaved stereo->interleaved stereo:
	 * outByteStep=format.getFrameSize()/2, sampleCount*2<br>
	 * 2 mono arrays->stereo:<br>
	 * ---inOffset=0, outByteOffset=0, outByteStep=format.getFrameSize()<br>
	 * ---inOffset=1, outByteOffset=format.getFrameSize()/2,
	 * outByteStep=format.getFrameSize()<br>
	 */
Central conversion function from normalized float array to a byte array.
In order to accomodate interleaved and non-interleaved samples, this
method takes outByteStep as parameter which can be used to flexibly
convert the data.
Central conversion function from normalized float array to a byte array . In order to accomodate interleaved and non - interleaved samples this method takes outByteStep as parameter which can be used to flexibly convert the data .
static void float2byteGeneric(float[] input, int inOffset, byte[] output,
			int outByteOffset, int outByteStep, int sampleCount,
			int formatType, float ditherBits) {
		if (inOffset < 0 || inOffset + sampleCount > input.length
				|| sampleCount < 0) {
			throw new IllegalArgumentException("invalid input index: "
					+ "input.length=" + input.length + " inOffset=" + inOffset
					+ " sampleCount=" + sampleCount);
		}
		if (outByteOffset < 0
				|| outByteOffset + (sampleCount * outByteStep) >= (output.length + outByteStep)
				|| outByteStep < getSampleSize(formatType)) {
			throw new IllegalArgumentException("invalid output index: "
					+ "output.length=" + output.length + " outByteOffset="
					+ outByteOffset + " outByteStep=" + outByteStep
					+ " sampleCount=" + sampleCount + " format="
					+ formatType2Str(formatType));
		}
		if (ditherBits != 0.0f && random == null) {
			random = new Random();
		}
		int endSample = inOffset + sampleCount;
		int iSample;
		int outIndex = outByteOffset;
		for (int inIndex = inOffset; inIndex < endSample; inIndex++, outIndex += outByteStep) {
			switch (formatType) {
			case CT_8S:
				output[outIndex] = quantize8(input[inIndex] * twoPower7,
						ditherBits);
				break;
			case CT_8U:
				output[outIndex] = (byte) (quantize8(
						(input[inIndex] * twoPower7), ditherBits) + 128);
				break;
			case CT_16SB:
				iSample = quantize16(input[inIndex] * twoPower15, ditherBits);
				output[outIndex] = (byte) (iSample >> 8);
				output[outIndex + 1] = (byte) (iSample & 0xFF);
				break;
			case CT_16SL:
				iSample = quantize16(input[inIndex] * twoPower15, ditherBits);
				output[outIndex + 1] = (byte) (iSample >> 8);
				output[outIndex] = (byte) (iSample & 0xFF);
				break;
			case CT_24_3SB:
				iSample = quantize24(input[inIndex] * twoPower23, ditherBits);
				output[outIndex] = (byte) (iSample >> 16);
				output[outIndex + 1] = (byte) ((iSample >>> 8) & 0xFF);
				output[outIndex + 2] = (byte) (iSample & 0xFF);
				break;
			case CT_24_3SL:
				iSample = quantize24(input[inIndex] * twoPower23, ditherBits);
				output[outIndex + 2] = (byte) (iSample >> 16);
				output[outIndex + 1] = (byte) ((iSample >>> 8) & 0xFF);
				output[outIndex] = (byte) (iSample & 0xFF);
				break;
			case CT_24_4SB:
				iSample = quantize24(input[inIndex] * twoPower23, ditherBits);
				output[outIndex + 0] = 0;
				output[outIndex + 1] = (byte) (iSample >> 16);
				output[outIndex + 2] = (byte) ((iSample >>> 8) & 0xFF);
				output[outIndex + 3] = (byte) (iSample & 0xFF);
				break;
			case CT_24_4SL:
				iSample = quantize24(input[inIndex] * twoPower23, ditherBits);
				output[outIndex + 3] = (byte) (iSample >> 16);
				output[outIndex + 2] = (byte) ((iSample >>> 8) & 0xFF);
				output[outIndex + 1] = (byte) (iSample & 0xFF);
				output[outIndex + 0] = 0;
				break;
			case CT_32SB:
				iSample = quantize32(input[inIndex] * twoPower31, ditherBits);
				output[outIndex] = (byte) (iSample >> 24);
				output[outIndex + 1] = (byte) ((iSample >>> 16) & 0xFF);
				output[outIndex + 2] = (byte) ((iSample >>> 8) & 0xFF);
				output[outIndex + 3] = (byte) (iSample & 0xFF);
				break;
			case CT_32SL:
				iSample = quantize32(input[inIndex] * twoPower31, ditherBits);
				output[outIndex + 3] = (byte) (iSample >> 24);
				output[outIndex + 2] = (byte) ((iSample >>> 16) & 0xFF);
				output[outIndex + 1] = (byte) ((iSample >>> 8) & 0xFF);
				output[outIndex] = (byte) (iSample & 0xFF);
				break;
			default:
				throw new IllegalArgumentException("unsupported format="
						+ formatType2Str(formatType));
			}
		}
	}
static void float2byteGeneric ( float [ ] input , int inOffset , byte [ ] output , int outByteOffset , int outByteStep , int sampleCount , int formatType , float ditherBits ) { if ( inOffset < 0 || inOffset + sampleCount > input . length || sampleCount < 0 ) { throw new IllegalArgumentException ( "invalid input index: " + "input.length=" + input . length + " inOffset=" + inOffset + " sampleCount=" + sampleCount ) ; } if ( outByteOffset < 0 || outByteOffset + ( sampleCount * outByteStep ) >= ( output . length + outByteStep ) || outByteStep < getSampleSize ( formatType ) ) { throw new IllegalArgumentException ( "invalid output index: " + "output.length=" + output . length + " outByteOffset=" + outByteOffset + " outByteStep=" + outByteStep + " sampleCount=" + sampleCount + " format=" + formatType2Str ( formatType ) ) ; } if ( ditherBits != 0.0f && random == null ) { random = new Random ( ) ; } int endSample = inOffset + sampleCount ; int iSample ; int outIndex = outByteOffset ; for ( int inIndex = inOffset ; inIndex < endSample ; inIndex ++ , outIndex += outByteStep ) { switch ( formatType ) { case CT_8S : output [ outIndex ] = quantize8 ( input [ inIndex ] * twoPower7 , ditherBits ) ; break ; case CT_8U : output [ outIndex ] = ( byte ) ( quantize8 ( ( input [ inIndex ] * twoPower7 ) , ditherBits ) + 128 ) ; break ; case CT_16SB : iSample = quantize16 ( input [ inIndex ] * twoPower15 , ditherBits ) ; output [ outIndex ] = ( byte ) ( iSample >> 8 ) ; output [ outIndex + 1 ] = ( byte ) ( iSample & 0xFF ) ; break ; case CT_16SL : iSample = quantize16 ( input [ inIndex ] * twoPower15 , ditherBits ) ; output [ outIndex + 1 ] = ( byte ) ( iSample >> 8 ) ; output [ outIndex ] = ( byte ) ( iSample & 0xFF ) ; break ; case CT_24_3SB : iSample = quantize24 ( input [ inIndex ] * twoPower23 , ditherBits ) ; output [ outIndex ] = ( byte ) ( iSample >> 16 ) ; output [ outIndex + 1 ] = ( byte ) ( ( iSample >>> 8 ) & 0xFF ) ; output [ outIndex + 2 ] = ( byte ) ( iSample & 0xFF ) ; break ; case CT_24_3SL : iSample = quantize24 ( input [ inIndex ] * twoPower23 , ditherBits ) ; output [ outIndex + 2 ] = ( byte ) ( iSample >> 16 ) ; output [ outIndex + 1 ] = ( byte ) ( ( iSample >>> 8 ) & 0xFF ) ; output [ outIndex ] = ( byte ) ( iSample & 0xFF ) ; break ; case CT_24_4SB : iSample = quantize24 ( input [ inIndex ] * twoPower23 , ditherBits ) ; output [ outIndex + 0 ] = 0 ; output [ outIndex + 1 ] = ( byte ) ( iSample >> 16 ) ; output [ outIndex + 2 ] = ( byte ) ( ( iSample >>> 8 ) & 0xFF ) ; output [ outIndex + 3 ] = ( byte ) ( iSample & 0xFF ) ; break ; case CT_24_4SL : iSample = quantize24 ( input [ inIndex ] * twoPower23 , ditherBits ) ; output [ outIndex + 3 ] = ( byte ) ( iSample >> 16 ) ; output [ outIndex + 2 ] = ( byte ) ( ( iSample >>> 8 ) & 0xFF ) ; output [ outIndex + 1 ] = ( byte ) ( iSample & 0xFF ) ; output [ outIndex + 0 ] = 0 ; break ; case CT_32SB : iSample = quantize32 ( input [ inIndex ] * twoPower31 , ditherBits ) ; output [ outIndex ] = ( byte ) ( iSample >> 24 ) ; output [ outIndex + 1 ] = ( byte ) ( ( iSample >>> 16 ) & 0xFF ) ; output [ outIndex + 2 ] = ( byte ) ( ( iSample >>> 8 ) & 0xFF ) ; output [ outIndex + 3 ] = ( byte ) ( iSample & 0xFF ) ; break ; case CT_32SL : iSample = quantize32 ( input [ inIndex ] * twoPower31 , ditherBits ) ; output [ outIndex + 3 ] = ( byte ) ( iSample >> 24 ) ; output [ outIndex + 2 ] = ( byte ) ( ( iSample >>> 16 ) & 0xFF ) ; output [ outIndex + 1 ] = ( byte ) ( ( iSample >>> 8 ) & 0xFF ) ; output [ outIndex ] = ( byte ) ( iSample & 0xFF ) ; break ; default : throw new IllegalArgumentException ( "unsupported format=" + formatType2Str ( formatType ) ) ; } } }
Central conversion function from normalized float array to a byte array.
Central conversion function from normalized float array to a byte array .
// if (TDebug.TraceAudioConverter) { // TDebug.out("FloatSampleBuffer.float2byteGeneric, formatType=" // +"formatType2Str(formatType)); // } // create the random number generator for dithering // do conversion // TODO: verify // TODO: verify