Skip to content
This repository has been archived by the owner on May 18, 2023. It is now read-only.

Commit

Permalink
Improove detection
Browse files Browse the repository at this point in the history
  • Loading branch information
kmmbvnr committed Mar 11, 2012
1 parent 2f32063 commit da3d231
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 56 deletions.
1 change: 1 addition & 0 deletions CryDetector/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

<uses-sdk android:minSdkVersion="10" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"/>

<application
android:icon="@drawable/ic_launcher"
Expand Down
90 changes: 44 additions & 46 deletions CryDetector/res/layout/main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,48 +5,6 @@
android:background="@drawable/app_background"
android:orientation="vertical" >

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="54dp"
android:layout_marginTop="40dp"
android:text="@string/samples"
android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
android:id="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/samples"
android:layout_alignParentLeft="true"
android:layout_marginLeft="58dp"
android:text="@string/hits"
android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
android:id="@+id/hits"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/samples"
android:layout_alignBottom="@+id/samples"
android:layout_alignLeft="@+id/TextView01"
android:text="0"
android:textSize="70dp" />

<TextView
android:id="@+id/samples"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginRight="19dp"
android:layout_marginTop="28dp"
android:text="0"
android:textSize="70dp" />

<LinearLayout
android:id="@+id/linearLayout2"
android:layout_width="wrap_content"
Expand All @@ -57,26 +15,26 @@

<Button
android:id="@+id/button_nocry1"
android:layout_height="wrap_content"
android:layout_width="76dp"
android:layout_height="wrap_content"
android:text="@string/nocry1" />

<Button
android:id="@+id/button_nocry2"
android:layout_height="wrap_content"
android:layout_width="76dp"
android:layout_height="wrap_content"
android:text="@string/nocry2" />

<Button
android:id="@+id/button_nocry3"
android:layout_height="wrap_content"
android:layout_width="76dp"
android:layout_height="wrap_content"
android:text="@string/nocry3" />

<Button
android:id="@+id/button_nocry4"
android:layout_height="wrap_content"
android:layout_width="76dp"
android:layout_height="wrap_content"
android:text="@string/nocry4" />
</LinearLayout>

Expand Down Expand Up @@ -123,4 +81,44 @@
android:layout_marginBottom="36dp"
android:text="@string/microphone" />

<TextView
android:id="@+id/hits"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignRight="@+id/button_mic"
android:layout_marginRight="91dp"
android:text="0"
android:textSize="65dp" />

<TextView
android:id="@+id/samples"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/hits"
android:layout_below="@+id/hits"
android:layout_marginTop="29dp"
android:text="0"
android:textSize="65dp" />

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/samples"
android:layout_alignLeft="@+id/button_mic"
android:layout_marginBottom="27dp"
android:text="@string/samples"
android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/hits"
android:layout_alignLeft="@+id/textView2"
android:layout_marginBottom="25dp"
android:text="@string/hits"
android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>
Binary file added CryDetector/res/raw/cry5.mp3
Binary file not shown.
Binary file added CryDetector/res/raw/nocry5.mp3
Binary file not shown.
5 changes: 3 additions & 2 deletions CryDetector/src/cc/wthr/crydetector/CryDetector.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,15 @@ private void addSample(Boolean sample) {
if(sample) {
mCryFilterCount ++;
}

mCryFilter.offer(sample);
if(mCryFilter.size() > 4) {
if(mCryFilter.size() > 10) {
Boolean prev = mCryFilter.poll();
if(prev) {
mCryFilterCount--;
}
}
if(mCryFilterCount==4) {
if(mCryFilterCount>=6) {
if(mCryListener != null) {
mCryListener.onCryReceived();
mCryFilter.clear();
Expand Down
46 changes: 38 additions & 8 deletions CryDetector/src/cc/wthr/crydetector/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener, ICryListener, OnCompletionListener {
private Button mButtonCry1;
Expand All @@ -21,14 +24,22 @@ public class MainActivity extends Activity implements OnClickListener, ICryListe
private Button mButtonNocry4;
private Button mButtonMic;
private CryDetector mCryDetector;
private int mTotalCries = 0;
private int mTotalSamples = 0;
private MediaPlayer mPlayer;
private Handler mUpdateUIHandler;
private TextView mHits;
private TextView mSamples;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

mHits = (TextView)findViewById(R.id.hits);
mSamples = (TextView)findViewById(R.id.samples);

mButtonCry1 = (Button)findViewById(R.id.button_cry1);
mButtonCry1.setOnClickListener(this);
mButtonCry2 = (Button)findViewById(R.id.button_cry2);
Expand All @@ -50,9 +61,20 @@ public void onCreate(Bundle savedInstanceState) {

mCryDetector = new CryDetector();
mCryDetector.setCryListener(this);

mUpdateUIHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
mHits.setText(String.format("%d", mTotalCries));
mSamples.setText(String.format("%d", mTotalSamples));
}
};
}

public void onClick(View view) {
mTotalCries = 0;
mTotalSamples = 0;
mUpdateUIHandler.sendEmptyMessage(0);
if(mPlayer != null) {
Log.d("MainActivity", "Release playing session");
onCompletion(mPlayer);
Expand All @@ -70,30 +92,37 @@ public void onClick(View view) {
} else if(view == mButtonCry3) {
soundId = R.raw.cry3;
} else if(view == mButtonCry4) {
soundId = R.raw.cry4;
soundId = R.raw.cry5;
} else if(view == mButtonNocry1) {
soundId = R.raw.nocry1;
} else if(view == mButtonNocry2) {
soundId = R.raw.nocry2;
} else if(view == mButtonNocry3) {
soundId = R.raw.nocry3;
} else if(view == mButtonNocry4) {
soundId = R.raw.nocry4;
soundId = R.raw.nocry5;
}

mPlayer = MediaPlayer.create(this, soundId);
mCryDetector.link(mPlayer.getAudioSessionId());
mPlayer.setOnCompletionListener(this);
mPlayer.start();
try {
mPlayer = MediaPlayer.create(this, soundId);
mCryDetector.link(mPlayer.getAudioSessionId());
mPlayer.setOnCompletionListener(this);
mPlayer.start();
} catch(Throwable e) {
Log.d("MainActivity", "Link visualizer error", e);
}
}
}

public void onCryReceived() {
Log.d("CRY", "Got it");
Log.d("CRY", "Got cry");
mTotalCries++;
mUpdateUIHandler.sendEmptyMessage(0);
}

public void onSampleReceived() {

mTotalSamples++;
mUpdateUIHandler.sendEmptyMessage(0);
}

public synchronized void onCompletion(MediaPlayer mp) {
Expand All @@ -102,6 +131,7 @@ public synchronized void onCompletion(MediaPlayer mp) {
if(mPlayer != null) {
mPlayer = null;
mCryDetector.unlink();
mp.stop();
mp.release();
}
} catch(Throwable e) {
Expand Down

0 comments on commit da3d231

Please sign in to comment.