Thread into activity

Put your problem here if it does not fit any of the other categories.

Thread into activity

Postby arcane » Wed Mar 07, 2012 3:06 am

Can someone please help me to add the following thread in to an activity to display the value stored in the "frequency" variable?

import android.media.AudioRecord;
import android.media.MediaRecorder.AudioSource;
import android.media.AudioFormat;


class recorderThread extends Thread {
public boolean recording; //variable to start or stop recording
public int frequency; //the public variable that contains the frequency value "heard", it is updated continually while the thread is running.
public recorderThread () {
}

@Override
public void run() {
AudioRecord recorder;
int numCrossing,p;
short audioData[];
int bufferSize;

bufferSize=AudioRecord.getMinBufferSize(8000,AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCDING_PCM_16BIT)*3; //get the buffer size to use with this audio record

recorder = new AudioRecord (AudioSource.MIC,8000,AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_16BIT,bufferSize); //instantiate the AudioRecorder

recording=true; //variable to use start or stop recording
audioData = new short [bufferSize]; //short array that pcm data is put into.


while (recording) { //loop while recording is needed
if (recorder.getState()==android.media.AudioRecord.STATE_INITIALIZED) // check to see if the recorder has initialized yet.
if (recorder.getRecordingState()==android.media.AudioRecord.RECORDSTATE_STOPPED)
recorder.startRecording(); //check to see if the Recorder has stopped or is not recording, and make it record.

else {

recorder.read(audioData,0,bufferSize); //read the PCM audio data into the audioData array

//Now we need to decode the PCM data using the Zero Crossings Method

numCrossing=0; //initialize your number of zero crossings to 0
for (p=0;p<bufferSize/4;p+=4) {
if (audioData[p]>0 && audioData[p+1]<=0) numCrossing++;
if (audioData[p]<0 && audioData[p+1]>=0) numCrossing++;
if (audioData[p+1]>0 && audioData[p+2]<=0) numCrossing++;
if (audioData[p+1]<0 && audioData[p+2]>=0) numCrossing++;
if (audioData[p+2]>0 && audioData[p+3]<=0) numCrossing++;
if (audioData[p+2]<0 && audioData[p+3]>=0) numCrossing++;
if (audioData[p+3]>0 && audioData[p+4]<=0) numCrossing++;
if (audioData[p+3]<0 && audioData[p+4]>=0) numCrossing++;
}//for p

for (p=(bufferSize/4)*4;p<bufferSize-1;p++) {
if (audioData[p]>0 && audioData[p+1]<=0) numCrossing++;
if (audioData[p]<0 && audioData[p+1]>=0) numCrossing++;
}



frequency=(8000/bufferSize)*(numCrossing/2); // Set the audio Frequency to half the number of zero crossings, times the number of samples our buffersize is per second.

}//else recorder started

} //while recording

if (recorder.getState()==android.media.AudioRecord.RECORDSTATE_RECORDING) recorder.stop(); //stop the recorder before ending the thread
recorder.release(); //release the recorders resources
recorder=null; //set the recorder to be garbage collected.

}//run


}//recorderThread
arcane
Junior Developer
Junior Developer
 
Posts: 11
Joined: Thu Mar 01, 2012 7:39 am

Top

Re: Thread into activity

Postby nuetronprotron » Wed Mar 07, 2012 10:48 am

Hi arcane,
In order to make any changes to the ui should be done only by mainthread ,but you are invoking a another thread and if it is running in background then you have to post through a handler from the main thread and then post it in UI stack like handler.post(/* here display ur freq */); .Onlythen you can see in UI change.
nuetronprotron
Developer
Developer
 
Posts: 37
Joined: Tue Jan 10, 2012 10:22 am

Re: Thread into activity

Postby arcane » Fri Mar 09, 2012 4:08 am

hmm...I didn't get it...Can you please explain it by a sample code?
arcane
Junior Developer
Junior Developer
 
Posts: 11
Joined: Thu Mar 01, 2012 7:39 am

Re: Thread into activity

Postby Phyll » Fri Mar 09, 2012 2:04 pm

Hi arcane,

I guess you could do something like this:

Code: Select all
public class App3 extends Activity {

    public boolean recording;
    public int frequency = -1;
    TextView freq;


    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.main);
        freq = (TextView)findViewById(R.id.frequency);
       
        new Thread(recorderThread).start();
       
        }//oncreate


    private Runnable recorderThread = new Runnable(){

    @Override
    public void run() {
    AudioRecord recorder;
    int numCrossing,p;
    short audioData[];
    int bufferSize;

    bufferSize=AudioRecord.getMinBufferSize(8000,AudioFormat.CHANNEL_CONFIGURATION_MONO,
    AudioFormat.ENCODING_PCM_16BIT)*3; //get the buffer size to use with this audio record
   
    recorder = new AudioRecord (AudioSource.MIC,8000,AudioFormat.CHANNEL_CONFIGURATION_MONO,
    AudioFormat.ENCODING_PCM_16BIT,bufferSize); //instantiate the AudioRecorder

    recording=true; //variable to use start or stop recording
    audioData = new short [bufferSize]; //short array that pcm data is put into.


    while (recording) { //loop while recording is needed
    if (recorder.getState()==android.media.AudioRecord.STATE_INITIALIZED) // check to see if the recorder has initialized yet.
    if (recorder.getRecordingState()==android.media.AudioRecord.RECORDSTATE_STOPPED)
    recorder.startRecording(); //check to see if the Recorder has stopped or is not recording, and make it record.

    else {

    recorder.read(audioData,0,bufferSize); //read the PCM audio data into the audioData array

    //Now we need to decode the PCM data using the Zero Crossings Method

    numCrossing=0; //initialize your number of zero crossings to 0
    for (p=0;p<bufferSize/4;p+=4) {
    if (audioData[p]>0 && audioData[p+1]<=0) numCrossing++;
    if (audioData[p]<0 && audioData[p+1]>=0) numCrossing++;
    if (audioData[p+1]>0 && audioData[p+2]<=0) numCrossing++;
    if (audioData[p+1]<0 && audioData[p+2]>=0) numCrossing++;
    if (audioData[p+2]>0 && audioData[p+3]<=0) numCrossing++;
    if (audioData[p+2]<0 && audioData[p+3]>=0) numCrossing++;
    if (audioData[p+3]>0 && audioData[p+4]<=0) numCrossing++;
    if (audioData[p+3]<0 && audioData[p+4]>=0) numCrossing++;
    }//for p

    for (p=(bufferSize/4)*4;p<bufferSize-1;p++) {
    if (audioData[p]>0 && audioData[p+1]<=0) numCrossing++;
    if (audioData[p]<0 && audioData[p+1]>=0) numCrossing++;
    }



    frequency=(8000/bufferSize)*(numCrossing/2); // Set the audio Frequency to half the number of zero crossings, times the number of samples our buffersize is per second.

   message = updateFrequency.obtainMessage();
    updateFrequency.sendMessage(message);
     
    }//else recorder started

    } //while recording

    if (recorder.getState()==android.media.AudioRecord.RECORDSTATE_RECORDING) recorder.stop(); //stop the recorder before ending the thread
    recorder.release(); //release the recorders resources
    recorder=null; //set the recorder to be garbage collected.

    }//run


    };//recorderThread

    Handler updateFrequency = new Handler() {     
        public void handleMessage(Message msg) {
            freq.setText(""+frequency);
            }//handlemessage
        };//handler

    @Override
    protected void onDestroy() {
        recording = false; //be sure to kill thread before exiting app
       super.onDestroy();
        }//ondestroy       
       
    //TODO: Fill In Methods Etc.
    }//class


You also need the RECORD_AUDIO permission.

Hope this helps.

Phyll
Phyll
Master Developer
Master Developer
 
Posts: 648
Joined: Fri Oct 14, 2011 11:19 am

Re: Thread into activity

Postby arcane » Mon Mar 12, 2012 6:41 am

Hello phyll,

Thank you very much for the code you have posted. The code was working fine, but there is a small issue occurring. When I run the code (in the emulator), I can see the value at the "freq" TextView is changing, but when I put it into an android mobile phone, the value at the "freq" textview is 0 and it doesn't change.I put uses-permission as well..Can you please suggest why it is happening??
arcane
Junior Developer
Junior Developer
 
Posts: 11
Joined: Thu Mar 01, 2012 7:39 am

Re: Thread into activity

Postby Phyll » Tue Mar 13, 2012 12:22 pm

Hi arcane,

I don't have time to make it work right now but if you want to look into it yourself here's what I'm thinking.

There might be some kind of problem with the buffersize being used in the constructor and the buffersize used in the read statement. The docs say they should be different and if you look at the parameters for each, you will see that the buffersize in the constructor is in bytes while the buffersize in the read method is in shorts. I'm not sure exactly what that means but it could be the reason.

Hope this helps.

Phyll
Phyll
Master Developer
Master Developer
 
Posts: 648
Joined: Fri Oct 14, 2011 11:19 am

Top

Re: Thread into activity

Postby arcane » Tue Mar 13, 2012 5:45 pm

Hey Phyll,

Thank you very much for the guidance...I'll try to figure it out whether it is something do with the byte and short data types.
arcane
Junior Developer
Junior Developer
 
Posts: 11
Joined: Thu Mar 01, 2012 7:39 am

Re: Thread into activity

Postby Phyll » Tue Mar 13, 2012 11:15 pm

Hi arcane,

Now that I have had more time to look at it. The frequency always comes out zero because the calculation has a bunch of integers in it that kind of just does that.

This is kind of messy but it works in my phone:

double part = ((double)8000/(double)bufferSize);
frequency=(int)(part*((double)numCrossing/2.0));

Maybe you can refine it. Or see why the other one doesn't work.

Phyll
Phyll
Master Developer
Master Developer
 
Posts: 648
Joined: Fri Oct 14, 2011 11:19 am

Re: Thread into activity

Postby arcane » Wed Mar 14, 2012 4:48 am

Hi Phyll,

Thank you very much for the updated code. It works fine now, but it seems like that it is not much efficient and accurate for a guitar tuner. :( By the way, thank you very much once again.. :)
arcane
Junior Developer
Junior Developer
 
Posts: 11
Joined: Thu Mar 01, 2012 7:39 am

Re: Thread into activity

Postby Phyll » Wed Mar 14, 2012 1:54 pm

Hi arcane,

Yeah, you know, I don't know how that tutorial ever worked. I don't know exactly where he got that method. You might want to check on the FFT ways of doing it. Might be a library somwhere around that makes it easy.

Phyll
Phyll
Master Developer
Master Developer
 
Posts: 648
Joined: Fri Oct 14, 2011 11:19 am

Re: Thread into activity

Postby arcane » Wed Mar 14, 2012 4:02 pm

Hey Phyll,

Yeah, Now the only hope is to go for a FFT library. Hope I'll have enough time to do it... :(
arcane
Junior Developer
Junior Developer
 
Posts: 11
Joined: Thu Mar 01, 2012 7:39 am

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: Google [Bot] and 18 guests