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.DialogInterface;
- import android.content.Intent;
- import android.graphics.Color;
- import android.media.AudioFormat;
- import android.media.AudioManager;
- import android.media.AudioTrack;
- import android.os.Bundle;
- import android.support.annotation.Nullable;
- import android.support.v4.app.Fragment;
- import android.support.v7.app.AlertDialog;
- import android.util.Log;
- import android.view.LayoutInflater;
- import android.view.MotionEvent;
- import android.view.View;
- import android.view.ViewGroup;
- 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;
- public class HearingFrequencyFragment extends Fragment {
- public static HearingFrequencyFragment newInstance() {
- return new HearingFrequencyFragment();
- }
- 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;
- boolean test1going = true;
- boolean test2going = false;
- boolean noReset = true;
- /*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;
- boolean testFinished = false;
- @Nullable
- @Override
- public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
- View view = inflater.inflate(R.layout.fragment_hearing_frequency, container, false);
- return view;
- }
- @Override
- public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
- super.onViewCreated(view, savedInstanceState);
- resetButton = (Button)view.findViewById(R.id.resetBtn);
- finishBut = (ImageView)view.findViewById(R.id.finishButton);
- croller = (Croller)view.findViewById(R.id.croller);
- volumeSeekbar = (SeekBar)view.findViewById(R.id.volumeControl);
- audioManager = (AudioManager)getActivity().getSystemService(Context.AUDIO_SERVICE);
- final RippleBackground rippleBackground=(RippleBackground)view.findViewById(R.id.contents);
- final HTextView animText = (HTextView)view.findViewById(R.id.animatedText);
- final HTextView animText2 = (HTextView)view.findViewById(R.id.animatedText2);
- final HTextView instructionText = (HTextView) getActivity().findViewById(R.id.instruction_text);
- final ImageView kulak=(ImageView)view.findViewById(R.id.centerImage);
- croller.setProgress(0);
- volumeSeekbar.setMax(audioManager
- .getStreamMaxVolume(AudioManager.STREAM_MUSIC));
- volumeSeekbar.setProgress(audioManager
- .getStreamVolume(AudioManager.STREAM_MUSIC));
- resetButton.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- counter = 0;
- croller.setProgress(counter);
- //trackButPress = trackButPress-1;
- test1going = true;
- test2going = false;
- instructionText.animateText("Test 1. Press until start hearing");
- kulak.setEnabled(true);
- animText.animateText("");
- animText2.animateText("");
- }
- });
- 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 (test1going == true && test2going == false) {
- 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);
- getActivity().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++;
- test1going = false;
- test2going = true;
- return true;
- }
- else if (test2going == true && test1going == false){
- testFinished = true;
- 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);
- getActivity().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++;
- test1going = true;
- test2going = false;
- testFinished = true;
- return true;
- }
- else {
- }
- case MotionEvent.ACTION_UP:
- rippleBackground.stopRippleAnimation();
- if (test1going == false && test2going == true) {
- instructionText.animateText("Test 2. Press until stop hearing");
- }
- else if (testFinished == true) {
- /* if (testFinished == true) {
- instructionText.animateText("Test is complete.");
- kulak.setEnabled(false);
- testFinished = true;
- }*/
- instructionText.animateText("Test is complete.");
- kulak.setEnabled(false);
- testFinished = true;
- /* else {
- instructionText.animateText("Test 1. Press until start hearing");
- }*/
- }
- else {
- }
- max_frequency = frLower;
- min_frequency = frHigher;
- if (test2going == false && test1going == true) {
- 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;
- }
- });
- finishBut.setOnClickListener(new View.OnClickListener(){
- public void onClick(View view) {
- if (testFinished == true) {
- String maxfreq = String.valueOf(max_frequency);
- String minfreq = String.valueOf(min_frequency);
- Intent intent = new Intent(getActivity(),ResultActivity.class);
- Bundle bundle = new Bundle();
- bundle.putString("maxfreq",maxfreq);
- bundle.putString("minfreq",minfreq);
- intent.putExtras(bundle);
- startActivity(intent);
- }
- else {
- AlertDialog alertDialog = new AlertDialog.Builder(getActivity()).create();
- alertDialog.setTitle("Alert");
- alertDialog.setMessage("Please finish both tests");
- alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
- new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int which) {
- dialog.dismiss();
- }
- });
- alertDialog.show();
- }
- }
- });
- }
- }
Add Comment
Please, Sign In to add comment