Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.soyalab.askaruly.lor;
- import android.content.Context;
- import android.content.Intent;
- import android.graphics.Color;
- import android.media.AudioFormat;
- import android.media.AudioManager;
- import android.media.AudioTrack;
- import android.support.annotation.NonNull;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.MotionEvent;
- import android.view.View;
- import android.widget.Button;
- import android.widget.ImageView;
- import android.widget.SeekBar;
- import com.google.firebase.auth.FirebaseAuth;
- import com.hanks.htextview.base.HTextView;
- import com.sdsmdg.harjot.crollerTest.Croller;
- import com.skyfishjy.library.RippleBackground;
- import static java.lang.Math.abs;
- public class MainActivity extends AppCompatActivity {
- Thread t; //audio processing thread
- int sr = 44100; //sampling rate
- boolean isRunning = true; //audio on off
- //private SeekBar volumeSeekbar = null;
- private SeekBar volumeSeekbar = null;
- private AudioManager audioManager = null;
- Croller croller;
- int sliderval = 0;
- int crolprogress = 0;
- int trackButPress = 0;
- /*public double fr = 440.f;*/
- public double fr;
- public int frHigher;
- public int frLower;
- public int counter = 0;
- public int frCounter = 0;
- public int highCount = 0;
- public int max_frequency = 0;
- public int min_frequency = 0;
- ImageView finishBut;
- Button logoutButton;
- Button resetButton;
- FirebaseAuth mAuth;
- FirebaseAuth.AuthStateListener mAuthListener;
- @Override
- protected void onStart() {
- super.onStart();
- mAuth.addAuthStateListener(mAuthListener);
- }
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- logoutButton = (Button)findViewById(R.id.logoutBtn);
- resetButton = (Button)findViewById(R.id.resetBtn);
- mAuth = FirebaseAuth.getInstance();
- mAuthListener = new FirebaseAuth.AuthStateListener() {
- @Override
- public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
- if (firebaseAuth.getCurrentUser() == null) {
- startActivity(new Intent(MainActivity.this,EnterActivity.class));
- }
- }
- };
- logoutButton.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- mAuth.signOut();
- }
- });
- final RippleBackground rippleBackground=(RippleBackground)findViewById(R.id.contents);
- final HTextView animText = (HTextView)findViewById(R.id.animatedText);
- final HTextView animText2 = (HTextView)findViewById(R.id.animatedText2);
- final HTextView instructionText = (HTextView)findViewById(R.id.instruction_text);
- finishBut = (ImageView) findViewById(R.id.finishButton);
- ImageView kulak=(ImageView)findViewById(R.id.centerImage);
- //final TextView instructionText = (TextView)findViewById(R.id.instruction_text);
- //initControls();
- croller = (Croller) findViewById(R.id.croller);
- croller.setProgress(0);
- resetButton.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- counter = 0;
- croller.setProgress(counter);
- //trackButPress = trackButPress-1;
- if ((trackButPress % 2) == 0) {
- //instructionText.setText(R.string.evenPress);
- instructionText.animateText("Test 1. Press until start hearing");
- }
- else {
- //instructionText.setText(R.string.oddPress);
- instructionText.animateText("Test 2. Press until stop hearing");
- }
- }
- });
- volumeSeekbar = (SeekBar)findViewById(R.id.volumeControl);
- audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
- volumeSeekbar.setMax(audioManager
- .getStreamMaxVolume(AudioManager.STREAM_MUSIC));
- volumeSeekbar.setProgress(audioManager
- .getStreamVolume(AudioManager.STREAM_MUSIC));
- kulak.setOnTouchListener(new View.OnTouchListener() {
- @Override
- public boolean onTouch(View v, MotionEvent event) {
- String both;
- String both2;
- switch (event.getAction()) {
- case MotionEvent.ACTION_DOWN:
- if ((trackButPress % 2) == 0) {
- isRunning = true;
- counter = 0;
- croller.setProgressPrimaryColor(Color.parseColor("#42f4d4"));
- croller.setStartOffset(0);
- croller.setProgress(0);
- rippleBackground.startRippleAnimation();
- t = new Thread() {
- public void run() {
- int buffsize = AudioTrack.getMinBufferSize(sr,
- AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT);
- AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,
- sr, AudioFormat.CHANNEL_OUT_MONO,
- AudioFormat.ENCODING_PCM_16BIT, buffsize,
- AudioTrack.MODE_STREAM);
- short samples[] = new short[buffsize]; //audio synthesis
- int amp = 10000;
- double twopi = 8.*Math.atan(1.);
- double ph = 0.0;
- audioTrack.play(); //audio running
- while(isRunning){ //synthesis loop
- //frHigher = 2*counter;
- frHigher = 2*frCounter;
- Log.d("ADebugTag", "highCount/2: " + Double.toString(frHigher));
- for(int i=0; i < buffsize; i++){
- samples[i] = (short) (amp*Math.sin(ph));
- ph += twopi*frHigher/sr;
- volumeSeekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener()
- {
- @Override
- public void onStopTrackingTouch(SeekBar arg0)
- {
- }
- @Override
- public void onStartTrackingTouch(SeekBar arg0)
- {
- }
- @Override
- public void onProgressChanged(SeekBar arg0, int progress, boolean arg2)
- {
- audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
- progress, 0);
- }
- });
- }
- audioTrack.write(samples, 0, buffsize);
- runOnUiThread(new Runnable() {
- @Override
- public void run() {
- croller.setProgress(counter);
- }
- });
- counter++;
- if (counter %10 == 0) {
- frCounter = counter;
- }
- if (frHigher >= 200) {
- isRunning = false;
- return;
- }
- }
- highCount = counter;
- audioTrack.stop();
- audioTrack.release();
- }
- };
- t.start();
- trackButPress++;
- return true;
- }
- else {
- isRunning = true;
- Log.d("ADebugTag", "highCount/2: " + Double.toString(highCount/2));
- highCount = counter;
- Log.d("ADebugTag", "highCount: " + Double.toString(highCount));
- counter = 0;
- croller.setProgressPrimaryColor(Color.parseColor("#ffb3ff"));
- //croller.setProgress(crolprogress);
- croller.setProgress(0);
- croller.setStartOffset(0);
- rippleBackground.startRippleAnimation();
- t = new Thread() {
- public void run() {
- int buffsize = AudioTrack.getMinBufferSize(sr,
- AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT);
- AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,
- sr, AudioFormat.CHANNEL_OUT_MONO,
- AudioFormat.ENCODING_PCM_16BIT, buffsize,
- AudioTrack.MODE_STREAM);
- short samples[] = new short[buffsize]; //audio synthesis
- int amp = 10000;
- double twopi = 8.*Math.atan(1.);
- double ph = 0.0;
- audioTrack.play(); //audio running
- while(isRunning){ //synthesis loop
- sliderval = counter;
- frLower = 100*sliderval;
- for(int i=0; i < buffsize; i++){
- samples[i] = (short) (amp*Math.sin(ph));
- ph += twopi*frLower/sr;
- volumeSeekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener()
- {
- @Override
- public void onStopTrackingTouch(SeekBar arg0)
- {
- }
- @Override
- public void onStartTrackingTouch(SeekBar arg0)
- {
- }
- @Override
- public void onProgressChanged(SeekBar arg0, int progress, boolean arg2)
- {
- audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
- progress, 0);
- }
- });
- }
- audioTrack.write(samples, 0, buffsize);
- runOnUiThread(new Runnable() {
- @Override
- public void run() {
- crolprogress = counter/2;
- croller.setProgress(crolprogress);
- }
- });
- counter++;
- highCount++;
- if (frLower >= 20000) {
- //counter = 0;
- isRunning = false;
- //rippleBackground.stopRippleAnimation();
- return;
- }
- }
- audioTrack.stop();
- audioTrack.release();
- //////////////////////////////
- //////////////////////////////
- }
- };
- t.start();
- trackButPress++;
- return true;
- }
- case MotionEvent.ACTION_UP:
- rippleBackground.stopRippleAnimation();
- if ((trackButPress % 2) == 0) {
- //instructionText.setText(R.string.evenPress);
- instructionText.animateText("Test 1. Press until start hearing");
- }
- else {
- //instructionText.setText(R.string.oddPress);
- instructionText.animateText("Test 2. Press until stop hearing");
- }
- max_frequency = frLower;
- min_frequency = frHigher;
- if (trackButPress%2 == 0) {
- both2 = "Min: " + String.valueOf(min_frequency) + " Hz";
- both = "Max: " + String.valueOf(max_frequency) + " Hz";
- animText.animateText(both2);
- animText2.animateText(both);
- }
- isRunning = false;
- t = null;
- return true;
- }
- return false;
- }
- });
- }
- /* private void initControls()
- {
- try
- {
- volumeSeekbar = (SeekBar)findViewById(R.id.volumeControl);
- audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
- volumeSeekbar.setMax(audioManager
- .getStreamMaxVolume(AudioManager.STREAM_MUSIC));
- volumeSeekbar.setProgress(audioManager
- .getStreamVolume(AudioManager.STREAM_MUSIC));
- volumeSeekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener()
- {
- @Override
- public void onStopTrackingTouch(SeekBar arg0)
- {
- }
- @Override
- public void onStartTrackingTouch(SeekBar arg0)
- {
- }
- @Override
- public void onProgressChanged(SeekBar arg0, int progress, boolean arg2)
- {
- audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
- progress, 0);
- }
- });
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- }*/
- public void lastpage(View v) {
- String maxfreq = String.valueOf(max_frequency);
- String minfreq = String.valueOf(min_frequency);
- Intent intent = new Intent(MainActivity.this,ResultActivity.class);
- Bundle bundle = new Bundle();
- bundle.putString("maxfreq",maxfreq);
- bundle.putString("minfreq",minfreq);
- intent.putExtras(bundle);
- startActivity(intent);
- /* Intent initi = new Intent(MainActivity.this, ResultActivity.class);
- startActivity(initi);*/
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment