39 [WIP] cleans up the reusable buffers and creates both real and

complex variants.
This commit is contained in:
Denny
2018-03-25 09:13:03 -04:00
parent c36f6901af
commit 60ffb18cd9
70 changed files with 1211 additions and 728 deletions

View File

@@ -1,5 +1,6 @@
package io.github.dsheirer.sample;
import io.github.dsheirer.sample.buffer.ReusableBuffer;
import io.github.dsheirer.sample.real.RealBuffer;
import java.nio.ByteBuffer;
@@ -11,61 +12,71 @@ import java.nio.ShortBuffer;
*/
public class ConversionUtils
{
public static float[] convertFromSigned16BitSamples( byte[] bytes )
public static float[] convertFromSigned16BitSamples(byte[] bytes)
{
return convertFromSigned16BitSamples( ByteBuffer.wrap( bytes ) );
return convertFromSigned16BitSamples(ByteBuffer.wrap(bytes));
}
/**
* Converts the byte buffer containing 16-bit samples into a float array
*/
public static float[] convertFromSigned16BitSamples( ByteBuffer buffer )
public static float[] convertFromSigned16BitSamples(ByteBuffer buffer)
{
ShortBuffer byteBuffer = buffer.order( ByteOrder.LITTLE_ENDIAN ).asShortBuffer();
float[] samples = new float[ buffer.limit() / 2 ];
for( int x = 0; x < samples.length; x++ )
{
samples[ x ] = (float)byteBuffer.get() / (float)Short.MAX_VALUE;
}
ShortBuffer byteBuffer = buffer.order(ByteOrder.LITTLE_ENDIAN).asShortBuffer();
return samples;
float[] samples = new float[buffer.limit() / 2];
for(int x = 0; x < samples.length; x++)
{
samples[x] = (float)byteBuffer.get() / (float)Short.MAX_VALUE;
}
return samples;
}
/**
* Converts the float samples into a little-endian 16-bit sample byte buffer.
*
* @param samples - float array of sample data
* @return - little-endian 16-bit sample byte buffer
*/
public static ByteBuffer convertToSigned16BitSamples( float[] samples )
public static ByteBuffer convertToSigned16BitSamples(float[] samples)
{
ByteBuffer converted = ByteBuffer.allocate( samples.length * 2 );
converted.order( ByteOrder.LITTLE_ENDIAN );
ByteBuffer converted = ByteBuffer.allocate(samples.length * 2);
converted.order(ByteOrder.LITTLE_ENDIAN);
for( float sample: samples )
{
converted.putShort( (short)( sample * Short.MAX_VALUE ) );
}
return converted;
for(float sample : samples)
{
converted.putShort((short)(sample * Short.MAX_VALUE));
}
return converted;
}
/**
* Converts the float samples in a complex buffer to a little endian 16-bit
* buffer
*/
public static ByteBuffer convertToSigned16BitSamples( Buffer buffer )
{
return convertToSigned16BitSamples( buffer.getSamples() );
}
/**
* Converts the float samples in a complex buffer to a little endian 16-bit
* buffer
*/
public static ByteBuffer convertToSigned16BitSamples( RealBuffer buffer )
{
return convertToSigned16BitSamples( buffer.getSamples() );
}
/**
* Converts the float samples in a complex buffer to a little endian 16-bit
* buffer
*/
public static ByteBuffer convertToSigned16BitSamples(Buffer buffer)
{
return convertToSigned16BitSamples(buffer.getSamples());
}
/**
* Converts the float samples in a complex buffer to a little endian 16-bit
* buffer
*/
public static ByteBuffer convertToSigned16BitSamples(RealBuffer buffer)
{
return convertToSigned16BitSamples(buffer.getSamples());
}
/**
* Converts the float samples in a complex buffer to a little endian 16-bit
* buffer
*/
public static ByteBuffer convertToSigned16BitSamples(ReusableBuffer buffer)
{
return convertToSigned16BitSamples(buffer.getSamples());
}
}