Initial code check in continued

This commit is contained in:
dennis.sheirer
2014-04-26 19:48:24 +00:00
parent 960a6f9952
commit 3dfedcf8d1
474 changed files with 76613 additions and 0 deletions

View File

@@ -0,0 +1,107 @@
/*******************************************************************************
* SDR Trunk
* Copyright (C) 2014 Dennis Sheirer
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
******************************************************************************/
package buffer;
import java.util.Arrays;
/**
* Circular Buffer - implements a circular buffer with a method to get the
* current head of the buffer, and put a new value in its place at the same
* time
*
* Also provides averaging over the elements in the buffer
*/
public class AveragingCircularBooleanBuffer {
private boolean[] mBuffer;
private int mBufferPointer;
public AveragingCircularBooleanBuffer( int length )
{
mBuffer = new boolean[ length ];
//Preload the array with false values
Arrays.fill( mBuffer, false );
}
public boolean get( boolean newValue )
{
//Fetch current boolean value
boolean retVal = mBuffer[ mBufferPointer ];
//Load the new value into the buffer
put( newValue );
return retVal;
}
/**
* Loads the newValue into this buffer and adjusts the buffer pointer
* to prepare for the next get/put cycle
*/
private void put( boolean newValue )
{
//Store the new value to the buffer
mBuffer[ mBufferPointer ] = newValue;
//Increment the buffer pointer
mBufferPointer++;
//Wrap the buffer pointer around to 0 when necessary
if( mBufferPointer >= mBuffer.length )
{
mBufferPointer = 0;
}
}
/**
* Loads the newValue into the buffer, calculates the average
* and returns that average from this method
*
* This effectively performs low-pass filtering
*/
public boolean getAverage( boolean newValue )
{
//Load the new value into the buffer
put( newValue );
int trueCount = 0;
for( int x = 0; x < mBuffer.length; x++ )
{
if( mBuffer[ x ] )
{
trueCount++;
}
}
/**
* If the number of true values in the buffer is
* more than half, return true, otherwise, false
*/
if( trueCount > (int)( mBuffer.length / 2 ) )
{
return true;
}
else
{
return false;
}
}
}

View File

@@ -0,0 +1,33 @@
/*******************************************************************************
* SDR Trunk
* Copyright (C) 2014 Dennis Sheirer
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
******************************************************************************/
package buffer;
public class BufferOverflowException extends Exception
{
private static final long serialVersionUID = 1L;
public BufferOverflowException( String text )
{
super( text );
}
public BufferOverflowException( String text, Throwable exc )
{
super( text, exc );
}
}

View File

@@ -0,0 +1,23 @@
/*******************************************************************************
* SDR Trunk
* Copyright (C) 2014 Dennis Sheirer
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
******************************************************************************/
package buffer;
public interface BufferOverflowListener
{
public void overflow( Object source, String message );
}

View File

@@ -0,0 +1,60 @@
/*******************************************************************************
* SDR Trunk
* Copyright (C) 2014 Dennis Sheirer
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
******************************************************************************/
package buffer;
public class ByteArrayDelayBuffer
{
private int mBufferSize;
private byte[][] mBuffer = null;
private int mBufferPointer = 0;
/**
* Implements a delay buffer for byte arrays. As a new byte[] is placed
* into the buffer, the oldest byte[] is returned
* @param bufferSize - number of byte arrays to buffer
*/
public ByteArrayDelayBuffer( int bufferSize )
{
mBufferSize = bufferSize;
}
/**
* Inserts new bytes into the buffer
*
* @return - oldest byte[] from the buffer, or null if the buffer is not
* yet full
*/
public byte[] get( byte[] bytes )
{
if( mBuffer == null )
{
mBuffer = new byte[ mBufferSize ][ bytes.length ];
}
byte[] returnBytes = mBuffer[ mBufferPointer ];
mBuffer[ mBufferPointer++ ] = bytes;
if( mBufferPointer >= mBufferSize )
{
mBufferPointer = 0;
}
return returnBytes;
}
}

View File

@@ -0,0 +1,23 @@
/*******************************************************************************
* SDR Trunk
* Copyright (C) 2014 Dennis Sheirer
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
******************************************************************************/
package buffer;
public interface ByteArrayListener
{
public void receive( byte[] data );
}

View File

@@ -0,0 +1,25 @@
/*******************************************************************************
* SDR Trunk
* Copyright (C) 2014 Dennis Sheirer
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
******************************************************************************/
package buffer;
import java.nio.ByteBuffer;
public interface ByteBufferListener
{
public void receive( ByteBuffer buffer );
}

View File

@@ -0,0 +1,33 @@
/*******************************************************************************
* SDR Trunk
* Copyright (C) 2014 Dennis Sheirer
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
******************************************************************************/
package buffer;
public interface ByteBufferProvider
{
/**
* Adds a sample listener to receive samples
*/
public void addByteBufferListener( ByteBufferListener listener );
/**
* Removes the sample listener, if it is currently registered as a listener
*/
public void removeByteBufferListener( ByteBufferListener listener );
}

View File

@@ -0,0 +1,91 @@
/*******************************************************************************
* SDR Trunk
* Copyright (C) 2014 Dennis Sheirer
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
******************************************************************************/
package buffer;
import java.nio.ByteBuffer;
import java.util.Enumeration;
import sample.complex.ComplexSample;
/**
* Provides a complex sample enumeration wrapper around a byte array containing
* interleaved byte-length I & Q sample values in the range of 0 - 255, converting
* those samples into complex samples with value range of -128 to 0 to 127.
*/
public class ComplexByteBuffer implements Enumeration<ComplexSample>
{
private static float[] mValues;
/**
* Creates a lookup table that converts the 8-bit valued range from 0 - 255
* into scaled float values of -128 to 0 to 127
*/
static
{
mValues = new float[ 256 ];
float scale = (float)( 1.0f / 127.0f );
for( int x = 0; x < 256; x++ )
{
mValues[ x ] = (float)( x - 128 ) * scale;
}
}
private ByteBuffer mBytes;
// private byte[] mBytes;
// private int mPointer;
public ComplexByteBuffer( byte[] bytes )
{
mBytes = ByteBuffer.wrap( bytes );
// mBytes = bytes;
// mPointer = 0;
}
public byte[] toArray()
{
// return mBytes;
return mBytes.array();
}
/**
* Indicates if there are more complex samples remaining.
*/
@Override
public boolean hasMoreElements()
{
// return mPointer < mBytes.length;
return mBytes.hasRemaining();
}
/**
* Returns the next complex sample.
*
* Note: does not check for buffer overrun. Use the hasMoreElements()
* method to ensure you do not read past the end of the buffer.
*/
@Override
public ComplexSample nextElement()
{
// return new ComplexSample( mValues[ ( mBytes[ mPointer++ ] & 0xFF ) ],
// mValues[ ( mBytes[ mPointer++ ] & 0xFF ) ] );
return new ComplexSample( mValues[ ( mBytes.get() & 0xFF ) ],
mValues[ ( mBytes.get() & 0xFF ) ] );
}
}

View File

@@ -0,0 +1,23 @@
/*******************************************************************************
* SDR Trunk
* Copyright (C) 2014 Dennis Sheirer
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
******************************************************************************/
package buffer;
public interface ComplexByteBufferListener
{
public void receive( ComplexByteBuffer buffer );
}

View File

@@ -0,0 +1,90 @@
/*******************************************************************************
* SDR Trunk
* Copyright (C) 2014 Dennis Sheirer
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
******************************************************************************/
package buffer;
import java.util.Arrays;
import sample.complex.ComplexSample;
/**
* Circular sample buffer - allocates a buffer and stores samples in a circular
* fashion, overwriting older samples with newly arrived samples. Initially
* fills buffer with 0-valued samples
*
* Can be used as a delay-type buffer, to delay samples by the 'size' amount
*/
public class ComplexCircularBuffer
{
ComplexSample[] mBuffer;
int mBufferPointer = 0;
public ComplexCircularBuffer( int size )
{
mBuffer = new ComplexSample[ size ];
Arrays.fill( mBuffer, new ComplexSample( 1.0f, 1.0f ) );
}
/**
* Returns the sample currently pointed to in the buffer and stores the
* new sample in its place
* @param sample
* @return
*/
public ComplexSample get( ComplexSample sample )
{
ComplexSample retVal = mBuffer[ mBufferPointer ];
put( sample );
return retVal;
}
public void put( ComplexSample sample )
{
mBuffer[ mBufferPointer ] = sample;
mBufferPointer++;
if( mBufferPointer >= mBuffer.length )
{
mBufferPointer = 0;
}
}
public ComplexSample[] get( int count )
{
ComplexSample[] samples = new ComplexSample[ count ];
int tempCounter = mBufferPointer;
for( int x = 0; x < count; x++ )
{
samples[ x ] = mBuffer[ tempCounter ];
tempCounter++;
if( tempCounter > mBuffer.length - 1 )
{
tempCounter = 0;
}
}
return samples;
}
}

View File

@@ -0,0 +1,73 @@
/*******************************************************************************
* SDR Trunk
* Copyright (C) 2014 Dennis Sheirer
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
******************************************************************************/
package buffer;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Arrays;
import sample.complex.ComplexSample;
public class ComplexSampleBufferAssembler
{
private ByteBuffer mByteBuffer;
private int mSizeInBytes;
public ComplexSampleBufferAssembler( int size )
{
mSizeInBytes = size * 4;
//Allocate a byte buffer with 4 bytes per sample (ie 2 x 16-bit samples)
mByteBuffer = ByteBuffer.allocate( mSizeInBytes );
mByteBuffer.order( ByteOrder.LITTLE_ENDIAN );
}
/**
* Load the sample's bytes, left then right, little endian, into the
* backing byte array
*/
public void put( ComplexSample sample )
{
mByteBuffer.putShort( (short)sample.left() );
mByteBuffer.putShort( (short)sample.right() );
}
/**
* Indicates if there is room for more samples in the buffer, or if it is full
*/
public boolean hasRemaining()
{
return mByteBuffer.hasRemaining();
}
/**
* Returns a copy of the byte array backing this buffer
*/
public ByteBuffer get()
{
return ByteBuffer.wrap( Arrays.copyOf( mByteBuffer.array(), mSizeInBytes ) );
}
/**
* Clear the bytes from the underlying buffer and reset the pointer to 0.
*/
public void clear()
{
mByteBuffer.clear();
}
}

View File

@@ -0,0 +1,87 @@
/*******************************************************************************
* SDR Trunk
* Copyright (C) 2014 Dennis Sheirer
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
******************************************************************************/
package buffer;
import sample.complex.ComplexSample;
/**
* Implements a FIFO circular buffer for complex samples that provides access
* to each complex sample by index number, where index 0 is the newest/latest
* complex sample and index (length - 1) is the oldest sample in the buffer.
*/
public class ComplexTappedCircularBuffer
{
private ComplexSample[] mBuffer;
private int mBufferPointer = 0;
public ComplexTappedCircularBuffer( int length )
{
mBuffer = new ComplexSample[ length ];
//Pre-load the buffer with 0-valued samples
for( int x = 0; x < length; x++ )
{
mBuffer[ x ] = new ComplexSample( 0, 0 );
}
}
/**
* Adds the sample to the buffer and removes the oldest sample
* @param sample
*/
public void add( ComplexSample sample )
{
mBuffer[ mBufferPointer++ ] = sample;
//Reset the buffer pointer once it points past the end of the buffer
if( mBufferPointer >= mBuffer.length )
{
mBufferPointer = 0;
}
}
/**
* Retrieves a sample currently in the buffer by index position.
*
* Index 0 is the newest/latest sample in the buffer and index (length - 1)
* is the oldest.
*
* Note: the buffer is preloaded with complex samples of 0,0 value.
*
* @param index where 0 is the newest/latest sample and length-1 is the oldest
* @return complex sample from the buffer.
* @throws ArrayIndexOutOfBoundsException if you attempt to access a sample
* outside of the buffer size
*/
public ComplexSample get( int index )
{
/* Since the samples are stored in a forward fashion, we have to access
* the samples in reverse to comply with the access contract, where
* index 0 is the newest and index (length -1) is the oldest, all
* relative to the current buffer pointer */
int pointer = mBuffer.length - 1 + mBufferPointer - index;
if( pointer >= mBuffer.length )
{
pointer -= mBuffer.length;
}
return mBuffer[ pointer ];
}
}

View File

@@ -0,0 +1,97 @@
/*******************************************************************************
* SDR Trunk
* Copyright (C) 2014 Dennis Sheirer
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
******************************************************************************/
package buffer;
import java.util.ArrayList;
import java.util.Arrays;
public class FloatArrayCircularAveragingBuffer
{
private ArrayList<float[]> mBuffer;
private int mBufferPointer;
private int mBufferSize;
private float[] mAverage;
public FloatArrayCircularAveragingBuffer( int bufferSize )
{
mBufferSize = bufferSize;
mBuffer = new ArrayList<float[]>();
}
/**
* Replaces the oldest float array element in the buffer with this new
* float array element, and then returns the average of the buffer contents
* with the newElement values included in the average array
*/
public float[] get( float[] newElement )
{
/**
* If we've never setup the average array, or the newElements length is
* different than what we've been processing, reset the buffer & averages
*/
if( mAverage == null || mAverage.length != newElement.length )
{
reset( newElement.length );
}
float[] oldElement = mBuffer.get( mBufferPointer );
for( int x = 0; x < newElement.length; x++ )
{
/**
* Change the current average value in each array element by the
* difference of the new element minus the old element, divided
* by the buffer size ... average it
*/
mAverage[ x ] +=
( ( newElement[ x ] - oldElement[ x ] ) / mBufferSize );
}
mBuffer.set( mBufferPointer, newElement );
mBufferPointer++;
if( mBufferPointer >= mBufferSize )
{
mBufferPointer = 0;
}
return mAverage;
}
/**
* Clears buffer and average and fills buffer with zero-valued float arrays
*/
private void reset( int arrayLength )
{
mAverage = new float[ arrayLength ];
Arrays.fill( mAverage, 0.0f );
mBuffer.clear();
mBufferPointer = 0;
float[] emptyArray = new float[ arrayLength ];
Arrays.fill( emptyArray, 0.0f );
for( int x = 0; x < mBufferSize; x++ )
{
mBuffer.add( emptyArray );
}
}
}

View File

@@ -0,0 +1,57 @@
/*******************************************************************************
* SDR Trunk
* Copyright (C) 2014 Dennis Sheirer
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
******************************************************************************/
package buffer;
public class FloatAveragingBuffer
{
private float[] mBuffer;
private float mAverage = 0.0f;
private int mBufferSize;
private int mBufferPointer;
public FloatAveragingBuffer( int size )
{
mBufferSize = size;
mBuffer = new float[ size ];
}
public float get( float newValue )
{
float oldValue = mBuffer[ mBufferPointer ];
if( Float.isInfinite( newValue ) || Float.isNaN( newValue ) )
{
mAverage = mAverage - ( oldValue / mBufferSize );
mBuffer[ mBufferPointer++ ] = 0.0f;
}
else
{
mAverage = mAverage - ( oldValue / mBufferSize ) + ( newValue / mBufferSize );
mBuffer[ mBufferPointer++ ] = newValue;
}
if( mBufferPointer >= mBufferSize )
{
mBufferPointer = 0;
}
return mAverage;
}
}

View File

@@ -0,0 +1,70 @@
/*******************************************************************************
* SDR Trunk
* Copyright (C) 2014 Dennis Sheirer
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
******************************************************************************/
package buffer;
import java.util.Arrays;
import sample.Listener;
/**
* Assembles single samples into sample blocks (ie arrays) for broadcast to
* registered SampleBlockListeners
*/
public class FloatBufferAssembler implements Listener<Float>
{
private Float[] mFloatBuffer;
private int mBufferSize;
private int mBufferPointer = 0;
private Listener<Float[]> mListener;
public FloatBufferAssembler( int bufferSize )
{
mBufferSize = bufferSize;
mFloatBuffer = new Float[ mBufferSize ];
}
/**
* Stores received samples in the sample block and manages the storage
* pointer. Sends sample blocks when full.
*/
@Override
public void receive( Float sample )
{
mFloatBuffer[ mBufferPointer++ ] = sample;
if( mBufferPointer == mBufferSize )
{
if( mListener != null )
{
mListener.receive( Arrays.copyOf( mFloatBuffer, mBufferSize ) );
}
mBufferPointer = 0;
}
}
/**
* Adds a new sampleblock listener to receive sample blocks as they are
* assembled from the sample stream
*/
public void setListener( Listener<Float[]> listener )
{
mListener = listener;
}
}

View File

@@ -0,0 +1,110 @@
/*******************************************************************************
* SDR Trunk
* Copyright (C) 2014 Dennis Sheirer
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
******************************************************************************/
package buffer;
import java.util.Arrays;
/**
* Circular sample buffer - allocates a buffer and stores samples in a circular
* fashion, overwriting older samples with newly arrived samples. Initially
* fills buffer with 0-valued samples
*
* Produces no output. Obtain the average buffer value on-demand via the
* getAverage() method
*/
public class FloatCircularAveragingBuffer
{
private float[] mBuffer;
private int mBufferPointer = 0;
private int mSampleCountForAveraging;
private int mCurrentSampleCount = 0;
private float mAverage = 0.0f;
public FloatCircularAveragingBuffer( int size, int sampleCountForAveraging )
{
mBuffer = new float[ size ];
mSampleCountForAveraging = sampleCountForAveraging;
Arrays.fill( mBuffer, 0.0f );
}
/**
* Convenience wrapper to throw away to put the new sample and throwaway
* the oldest sample
*/
public void put( float newestSample )
{
get( newestSample );
}
/**
* Loads the newest sample into the buffer and returns the oldest sample
*/
public float get( float newestSample )
{
float oldestSample = mBuffer[ mBufferPointer ];
mBuffer[ mBufferPointer ] = newestSample;
mBufferPointer++;
if( mBufferPointer >= mBuffer.length )
{
mBufferPointer = 0;
}
mCurrentSampleCount++;
if( mCurrentSampleCount >= mSampleCountForAveraging )
{
calculateAverage();
mCurrentSampleCount = 0;
}
return oldestSample;
}
/**
* Calculates the average of the maximum and minimum values present in
* the buffer
*/
private void calculateAverage()
{
float min = mBuffer[ 0 ];
float max = mBuffer[ 0 ];
for( int x = 0; x < mBuffer.length; x++ )
{
if( mBuffer[ x ] < min )
{
min = mBuffer[ x ];
}
if( mBuffer[ x ] > max )
{
max = mBuffer[ x ];
}
}
mAverage = (float)( ( min + max ) / 2.0f );
}
public float getAverage()
{
return mAverage;
}
}

View File

@@ -0,0 +1,68 @@
/*******************************************************************************
* SDR Trunk
* Copyright (C) 2014 Dennis Sheirer
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
******************************************************************************/
package buffer;
import java.util.Arrays;
/**
* Circular sample buffer - stores samples in a circular buffer
* overwriting older samples with newly arrived samples. Initially
* fills buffer with 0-valued samples
*
* Continuously calculates the average of the samples in the buffer, with the
* average available from the average() method.
*/
public class FloatCircularAveragingBuffer2
{
private float[] mBuffer;
private int mBufferPointer = 0;
private float mAverage = 0.0f;
public FloatCircularAveragingBuffer2( int size )
{
mBuffer = new float[ size ];
Arrays.fill( mBuffer, 0.0f );
}
/**
* Loads the newest sample into the buffer and returns the oldest sample
*/
public float get( float newestSample )
{
float oldestSample = mBuffer[ mBufferPointer ];
mBuffer[ mBufferPointer ] = newestSample;
mBufferPointer++;
if( mBufferPointer >= mBuffer.length )
{
mBufferPointer = 0;
}
mAverage = mAverage - ( oldestSample / mBuffer.length ) +
( newestSample / mBuffer.length );
return oldestSample;
}
public float average()
{
return mAverage;
}
}

View File

@@ -0,0 +1,108 @@
/*******************************************************************************
* SDR Trunk
* Copyright (C) 2014 Dennis Sheirer
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
******************************************************************************/
package buffer;
import java.util.Arrays;
/**
* Circular sample buffer - allocates a buffer and stores samples in a circular
* fashion, overwriting older samples with newly arrived samples. Initially
* fills buffer with 0-valued samples
*
* Can be used as a delay-type buffer, to delay samples by the 'size' amount
* @author denny
*
*/
public class FloatCircularBuffer
{
float[] mBuffer;
int mBufferPointer = 0;
public FloatCircularBuffer( int size )
{
mBuffer = new float[ size ];
Arrays.fill( mBuffer, (float)0.0 );
}
/**
* Puts the new value into the buffer and returns the oldest buffer value
* that it replaced
* @param newValue
* @return
*/
public float get( float newValue )
{
float oldestSample = mBuffer[ mBufferPointer ];
mBuffer[ mBufferPointer ] = newValue;
mBufferPointer++;
if( mBufferPointer >= mBuffer.length )
{
mBufferPointer = 0;
}
return oldestSample;
}
/**
* Convenience method to put a new value in the buffer, while discarding
* the old value
*/
public void put( float newValue )
{
get( newValue );
}
/**
* Returns an array of arraySize, loaded with sampleCount of the
* oldest samples, loaded into the array starting at index 0.
*
* This is useful for fetching an array filled half way with samples to
* use in FFT processing.
* @param arraySize - size of the array (must be larger than sampleCount)
* @param sampleCount - number of the oldest samples in the buffer to load
* @return
*/
public float[] get( int arraySize, int sampleCount )
{
float[] samples = new float[ arraySize ];
if( arraySize > sampleCount )
{
int tempCounter = mBufferPointer;
for( int x = 0; x < sampleCount; x++ )
{
samples[ x ] = mBuffer[ tempCounter ];
tempCounter++;
if( tempCounter > mBuffer.length )
{
tempCounter = 0;
}
}
}
return samples;
}
}

View File

@@ -0,0 +1,66 @@
/*******************************************************************************
* SDR Trunk
* Copyright (C) 2014 Dennis Sheirer
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
******************************************************************************/
package buffer;
import java.nio.ByteBuffer;
public class FloatSampleBuffer
{
private static final long serialVersionUID = 1L;
private int mSize;
private float[] mBuffer;
private int mBufferPointer = 0;
public FloatSampleBuffer( int size )
{
super();
mBuffer = new float[ size ];
}
public void put( float sample )
{
if( !isFull() )
{
mBuffer[ mBufferPointer++ ] = sample;
}
}
public void reset()
{
mBufferPointer = 0;
}
public boolean isFull()
{
return mBufferPointer >= mBuffer.length;
}
public byte[] getBytes()
{
ByteBuffer bb = ByteBuffer.allocate( mBuffer.length * 2 );
for( int x = 0; x < mBuffer.length; x++ )
{
bb.putShort( ((Float)mBuffer[ x ]).shortValue() );
}
return bb.array();
}
}

View File

@@ -0,0 +1,72 @@
/*******************************************************************************
* SDR Trunk
* Copyright (C) 2014 Dennis Sheirer
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
******************************************************************************/
package buffer;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
public class FloatSampleBufferAssembler
{
private ByteBuffer mByteBuffer;
private int mSizeInBytes;
public FloatSampleBufferAssembler( int size )
{
mSizeInBytes = size * 2;
//Allocate a byte buffer with 2 bytes per sample (ie 1 x 16-bit sample)
mByteBuffer = ByteBuffer.allocate( mSizeInBytes );
mByteBuffer.order( ByteOrder.LITTLE_ENDIAN );
}
/**
* Load the sample's bytes, left then right, little endian, into the
* backing byte array
*/
public void put( float sample )
{
mByteBuffer.putShort( Float.valueOf( sample ).shortValue() );
}
/**
* Indicates if there is room for more samples in the buffer, or if it is full
*/
public boolean hasRemaining()
{
return mByteBuffer.hasRemaining();
}
/**
* Returns a copy of the byte array backing this buffer
*/
public ByteBuffer get()
{
return mByteBuffer;
// return ByteBuffer.wrap( Arrays.copyOf( mByteBuffer.array(), mSizeInBytes ) );
}
/**
* Clear the bytes from the underlying buffer and reset the pointer to 0.
*/
public void reset()
{
// mByteBuffer.clear();
mByteBuffer = ByteBuffer.allocate( mSizeInBytes );
mByteBuffer.order( ByteOrder.LITTLE_ENDIAN );
}
}

View File

@@ -0,0 +1,86 @@
/*******************************************************************************
* SDR Trunk
* Copyright (C) 2014 Dennis Sheirer
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
******************************************************************************/
package buffer;
/**
* Implements a FIFO circular buffer for floats that provides access
* to each sample by index number, where index 0 is the newest/latest
* complex sample and index (length - 1) is the oldest sample in the buffer.
*/
public class FloatTappedCircularBuffer
{
private Float[] mBuffer;
private int mBufferPointer = 0;
public FloatTappedCircularBuffer( int length )
{
mBuffer = new Float[ length ];
//Pre-load the buffer with 0-valued samples
for( int x = 0; x < length; x++ )
{
mBuffer[ x ] = Float.valueOf( 0.0f );
}
}
/**
* Adds the sample to the buffer and removes the oldest sample
* @param sample
*/
public void add( Float sample )
{
mBuffer[ mBufferPointer++ ] = sample;
//Reset the buffer pointer once it points past the end of the buffer
if( mBufferPointer >= mBuffer.length )
{
mBufferPointer = 0;
}
}
/**
* Retrieves a sample currently in the buffer by index position.
*
* Index 0 is the newest/latest sample in the buffer and index (length - 1)
* is the oldest.
*
* Note: the buffer is preloaded with complex samples of 0,0 value.
*
* @param index where 0 is the newest/latest sample and length-1 is the oldest
* @return complex sample from the buffer.
* @throws ArrayIndexOutOfBoundsException if you attempt to access a sample
* outside of the buffer size
*/
public Float get( int index )
{
/* Since the samples are stored in a forward fashion, we have to access
* the samples in reverse to comply with the access contract, where
* index 0 is the newest and index (length -1) is the oldest, all
* relative to the current buffer pointer */
int pointer = mBuffer.length - 1 + mBufferPointer - index;
if( pointer >= mBuffer.length )
{
pointer -= mBuffer.length;
}
return mBuffer[ pointer ];
}
}

View File

@@ -0,0 +1,69 @@
/*******************************************************************************
* SDR Trunk
* Copyright (C) 2014 Dennis Sheirer
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
******************************************************************************/
package buffer;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import sample.Broadcaster;
import sample.Listener;
public class FloatToByteBufferAssembler implements Listener<Float>
{
private Broadcaster<ByteBuffer> mBroadcaster = new Broadcaster<ByteBuffer>();
private int mBufferSize = 0;
private ByteBuffer mBuffer;
public FloatToByteBufferAssembler( int bufferSize )
{
mBufferSize = bufferSize;
resetBuffer();
}
public void addByteBufferListener( Listener<ByteBuffer> listener )
{
mBroadcaster.addListener( listener );
}
public void removeByteBufferListener( Listener<ByteBuffer> listener )
{
mBroadcaster.removeListener( listener );
}
private void resetBuffer()
{
mBuffer = ByteBuffer.allocate( mBufferSize );
mBuffer.order( ByteOrder.BIG_ENDIAN );
}
@Override
public void receive( Float sample )
{
mBuffer.putShort( Float.valueOf( sample ).shortValue() );
if( mBuffer.position() >= mBufferSize )
{
//Buffer is full ... send it
mBroadcaster.receive( mBuffer );
//Reset the buffer
resetBuffer();
}
}
}

View File

@@ -0,0 +1,53 @@
/*******************************************************************************
* SDR Trunk
* Copyright (C) 2014 Dennis Sheirer
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
******************************************************************************/
package buffer;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Enumeration;
import sample.complex.ComplexSample;
public class ShortComplexByteBuffer implements Enumeration<ComplexSample>
{
private ByteBuffer mByteBuffer;
public ShortComplexByteBuffer( byte[] bytes, ByteOrder endianness )
{
mByteBuffer = ByteBuffer.wrap( bytes );
mByteBuffer.order( endianness );
}
public byte[] array()
{
return mByteBuffer.array();
}
@Override
public boolean hasMoreElements()
{
return mByteBuffer.hasRemaining();
}
@Override
public ComplexSample nextElement()
{
return new ComplexSample( (float)mByteBuffer.getShort(),
(float)mByteBuffer.getShort() );
}
}

View File

@@ -0,0 +1,59 @@
/*******************************************************************************
* SDR Trunk
* Copyright (C) 2014 Dennis Sheirer
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
******************************************************************************/
package buffer;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Enumeration;
import sample.complex.ComplexSample;
import source.mixer.MixerChannel;
public class StereoByteBuffer implements Enumeration<ComplexSample>
{
private ByteBuffer mByteBuffer;
public StereoByteBuffer( byte[] bytes, int frames )
{
mByteBuffer = ByteBuffer.wrap( bytes );
mByteBuffer.order( ByteOrder.LITTLE_ENDIAN );
}
public byte[] array()
{
return mByteBuffer.array();
}
public ByteBuffer getChannel( MixerChannel channel )
{
return StereoToMonoByteBufferConverter.getChannel( mByteBuffer, channel );
}
@Override
public boolean hasMoreElements()
{
return mByteBuffer.hasRemaining();
}
@Override
public ComplexSample nextElement()
{
return new ComplexSample( (float)mByteBuffer.getShort(),
(float)mByteBuffer.getShort() );
}
}

View File

@@ -0,0 +1,24 @@
/*******************************************************************************
* SDR Trunk
* Copyright (C) 2014 Dennis Sheirer
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
******************************************************************************/
package buffer;
public interface StereoByteBufferListener
{
public void receive( StereoByteBuffer buffer );
}

View File

@@ -0,0 +1,32 @@
/*******************************************************************************
* SDR Trunk
* Copyright (C) 2014 Dennis Sheirer
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
******************************************************************************/
package buffer;
public interface StereoByteBufferProvider
{
/**
* Adds a sample listener to receive samples
*/
public void addStereoByteBufferListener( StereoByteBufferListener listener );
/**
* Removes the sample listener, if it is currently registered as a listener
*/
public void removeStereoByteBufferListener( StereoByteBufferListener listener );
}

View File

@@ -0,0 +1,58 @@
/*******************************************************************************
* SDR Trunk
* Copyright (C) 2014 Dennis Sheirer
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
******************************************************************************/
package buffer;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import source.mixer.MixerChannel;
public class StereoToMonoByteBufferConverter
{
public static ByteBuffer getChannel( ByteBuffer buffer, MixerChannel channel )
{
byte[] channelBuffer = new byte[ buffer.capacity() / 2 ];
int channelBufferPointer = 0;
if( channel == MixerChannel.RIGHT )
{
//throw away the first two bytes
buffer.get();
buffer.get();
}
while( channelBufferPointer < channelBuffer.length )
{
if( buffer.hasRemaining() )
{
channelBuffer[ channelBufferPointer++ ] = buffer.get();
channelBuffer[ channelBufferPointer++ ] = buffer.get();
}
if( buffer.hasRemaining() )
{
buffer.get();
buffer.get();
}
}
return ByteBuffer.wrap( channelBuffer ).order( ByteOrder.LITTLE_ENDIAN );
}
}