Note that there are some explanatory texts on larger screens.

plurals
  1. PORetrieving Array from onActivityResult
    primarykey
    data
    text
    <p>I am using FFT on an Android project and need for the audioRecord to write to the buffer for only 3 seconds. Since, I do not need any updating during the capture I am not using AsyncTask.</p> <p>When I go to start the processing I get errors for the toTransform length and expression type. What am I missing to make these parts work or did I go about this the wrong way?</p> <p>P.S. This all works as an Asynctask.</p> <pre><code>package com.example.learnfft; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.util.Calendar; import java.util.Timer; import java.util.TimerTask; import android.app.Activity; import android.content.Intent; import android.media.AudioFormat; import android.media.AudioRecord; import android.media.MediaRecorder; import android.os.Bundle; import android.os.Handler; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; //import android.widget.Toast; import ca.uol.aig.fftpack.RealDoubleFFT; public class Main extends Activity implements OnClickListener { public int channelConfiguration = AudioFormat.CHANNEL_IN_MONO; public int audioEncoding = AudioFormat.ENCODING_PCM_16BIT; //FFT private RealDoubleFFT transformer; int blockSize = 1024; Button startStopButton; int frequency = 44100; int requestcode; boolean started = false; double freq; double[] magnitude; short[] buffer = new short[blockSize]; double[] toTransform = new double[blockSize]; //Create thread Handler Handler handler = new Handler(); int bufferSize = AudioRecord.getMinBufferSize(frequency, channelConfiguration, audioEncoding); Timer timer = new Timer(); AudioRecord audioRecord = new AudioRecord( MediaRecorder.AudioSource.MIC, frequency, channelConfiguration, audioEncoding, bufferSize); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button button = (Button) findViewById(R.id.takeTemp); button.setOnClickListener(this); } @Override public void onClick(View v) { Intent i = new Intent(this,recordAudio.class); startActivityForResult(i,requestcode); //recordAudio(toTransform); FindFrequency(); } public void FindFrequency() { try { audioRecord.startRecording(); int bufferReadResult = audioRecord.read(buffer, 0, blockSize); for (int i = 0; i &lt; blockSize &amp;&amp; i &lt; bufferReadResult; i++) { toTransform[i] = (double) buffer[i] / 32768.0; // signed // 16 // bit } timer.schedule(new TimerTask() { public void run() { handler.post(new Runnable() { public void run() { audioRecord.stop(); } }); } }, 3000); //Stop after 3 seconds } catch (Throwable t) { Log.e("AudioRecord", "Recording Failed"); } File freqFile = new File("/mnt/sdcard/Test APKs/frequency.file"); File magFile = new File("/mnt/sdcard/Test APKs/magnitude.file"); int blockSize = 256; double[] audioDataDoubles = new double[(blockSize*2)]; String mydate = java.text.DateFormat.getDateTimeInstance().format(Calendar.getInstance().getTime()); transformer.ft(toTransform); for ( int x = 0; x &lt; toTransform[0].length; x++) { &lt;-- Length error audioDataDoubles[2*x] = toTransform[0][x]; &lt;-- is array but it must resolve to double. audioDataDoubles[(2*x)+1] = 0.0; } double[] re = new double[blockSize]; double[] im = new double[blockSize]; double[] magnitude = new double[blockSize]; // Calculate the Real and imaginary and Magnitude. for(int i = 0; i &lt; blockSize; i++){ // real is stored in first part of array re[i] = audioDataDoubles[i*2]; // imaginary is stored in the sequential part im[i] = audioDataDoubles[(2*i)+1]; // magnitude is calculated by the square root of (imaginary^2 + real^2) magnitude[i] = Math.sqrt((re[i] * re[i]) + (im[i]*im[i])); } double peak = -1.0; // Get the largest magnitude peak for(int i = 0; i &lt; blockSize; i++){ if(peak &lt; magnitude[i]) peak = magnitude[i]; String magValue = String.valueOf(peak); try { BufferedWriter buf = new BufferedWriter(new FileWriter(magFile, true)); buf.append(mydate + " - " + magValue); buf.newLine(); buf.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } // calculated the frequency freq = (frequency * peak)/blockSize; String freqValue = String.valueOf(freq); //Toast.makeText(Main.this, freqValue, Toast.LENGTH_SHORT).show(); try { BufferedWriter buf = new BufferedWriter(new FileWriter(freqFile, true)); buf.append(mydate + " - " + freqValue); buf.newLine(); buf.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload