Skip to content

Instantly share code, notes, and snippets.

@joelrebel
Created October 27, 2016 05:25
Show Gist options
  • Save joelrebel/0a72004816496d2c048059cab20e4b3a to your computer and use it in GitHub Desktop.
Save joelrebel/0a72004816496d2c048059cab20e4b3a to your computer and use it in GitHub Desktop.
$ cat /tmp/j.java
@Override
protected Integer doInBackground(byte[]...arr) {
byte[] fft = arr[0];
int[] bands = new int[50];
int[] mag = new int[100];
int j=0;
//sample down fft values into magnitudes
//sample down magnitudes into bands
//this could use more tuning
//http://stackoverflow.com/questions/7651633/using-fft-in-android
for (int i=0; i < mag.length; i++){
//convert each of the values to its appropriate values
//(real * real) + (imaginary * imaginary)
mag[i] = ( (fft[2*i] * fft[2*i]) + (fft[2*i+1] * fft[2*i+1]) );
//downsample the fft values
//A better way to do this is to take the peak
//values for each band and store those
if ( ( i != 0 ) && ( (i % 2) == 0 ) ) {
bands[j] = mag[i-1] + mag[i];
j++;
}
}
byte[] bandsData = new byte[bands.length];
//convert to byte array
//
//[0, 0, 13, 121, 0, 0, 8, 72, 0, 0, 2, 122, 0, 0, 1, 126, 0, 0, 0, 7, 0, 0, 0, -31, 0, 0, 0, 12, 0, 0, 0, 17, 0, 0, 0, 70, 0, 0, 0, 18, 0, 0, 0, 6, 0, 0, 0, 11, 0, 0, 0, 2, 0, 0, 0, 1, 0, 0, 0, 5, 0, 0, 0, 4, 0, 0, 0, 3, 0, 0, 0, 3, 0, 0, 0, 3, 0, 0, 0, 2, 0, 0, 0, 6, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 1, 0, 0, 0, 6, 0, 0, 0, 5, 0, 0, 0, 6, 0, 0, 0, 10, 0, 0, 0, 17, 0, 0, 0, 10, 0, 0, 0, 14, 0, 0, 0, 8, 0, 0, 0, 11, 0, 0, 0, 5, 0, 0, 0, 5, 0, 0, 0, 7, 0, 0, 0, 4, 0, 0, 0, 2, 0, 0, 0, 6, 0, 0, 0, 6, 0, 0, 0, 6, 0, 0, 0, 2, 0, 0, 0, 2, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 0]
//[0, 0, 39, 126, 0, 0, 13, 43, 0, 0, 3, -94, 0, 0, 1, 50, 0, 0, 0, 110, 0, 0, 1, 127, 0, 0, 0, -86, 0, 0, 0, 21, 0, 0, 2, 26, 0, 0, 0, 14, 0, 0, 0, 86, 0, 0, 0, 18, 0, 0, 0, 18, 0, 0, 0, 21, 0, 0, 0, 5, 0, 0, 0, 15, 0, 0, 0, 5, 0, 0, 0, 7, 0, 0, 0, 9, 0, 0, 0, 2, 0, 0, 0, 9, 0, 0, 0, 6, 0, 0, 0, 3, 0, 0, 0, 25, 0, 0, 0, 23, 0, 0, 0, 10, 0, 0, 0, 9, 0, 0, 0, 37, 0, 0, 0, 26, 0, 0, 0, 63, 0, 0, 0, 42, 0, 0, 0, 33, 0, 0, 0, 14, 0, 0, 0, 15, 0, 0, 0, 5, 0, 0, 0, 10, 0, 0, 0, 3, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 2, 0, 0, 0, 5, 0, 0, 0, 3, 0, 0, 0, 5, 0, 0, 0, 5, 0, 0, 0, 7, 0, 0, 0, 4, 0, 0, 0, 6, 0, 0, 0, 2, 0, 0, 0, 0]
bandsData = intsToBytes(bands);
new UDPSend().execute(bandsData);
return 0;
}
public static byte[] intsToBytes(int[] ints){
ByteBuffer bb = ByteBuffer.allocate(ints.length * 4);
IntBuffer ib = bb.asIntBuffer();
for (int i: ints) ib.put(i);
return bb.array();
}
@Override
protected Integer doInBackground(byte[]...arr) {
DatagramSocket ds = null;
DatagramPacket dp;
byte[] message = new byte[arr[0].length];
message = arr[0];
int SERVER_PORT = 7777;
try {
InetAddress address = InetAddress.getByName("192.168.0.120");
ds = new DatagramSocket();
dp = new DatagramPacket(message, message.length, address, SERVER_PORT);
Log.v("WOOPWOOP", Arrays.toString(message));
ds.send(dp);
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
if (ds != null)
{
ds.close();
}
return 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment