tuttelikz

HearingFrequenciesFragment [Working Up and Down]

Nov 27th, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 17.21 KB | None | 0 0
  1. package com.soyalab.askaruly.lor;
  2.  
  3. import android.content.Context;
  4. import android.content.DialogInterface;
  5. import android.content.Intent;
  6. import android.graphics.Color;
  7. import android.media.AudioFormat;
  8. import android.media.AudioManager;
  9. import android.media.AudioTrack;
  10. import android.os.Bundle;
  11. import android.support.annotation.Nullable;
  12. import android.support.v4.app.Fragment;
  13. import android.support.v7.app.AlertDialog;
  14. import android.util.Log;
  15. import android.view.LayoutInflater;
  16. import android.view.MotionEvent;
  17. import android.view.View;
  18. import android.view.ViewGroup;
  19. import android.widget.Button;
  20. import android.widget.ImageView;
  21. import android.widget.SeekBar;
  22.  
  23. import com.google.firebase.auth.FirebaseAuth;
  24. import com.hanks.htextview.base.HTextView;
  25. import com.sdsmdg.harjot.crollerTest.Croller;
  26. import com.skyfishjy.library.RippleBackground;
  27.  
  28.  
  29. public class HearingFrequencyFragment extends Fragment {
  30.  
  31.     public static HearingFrequencyFragment newInstance() {
  32.         return new HearingFrequencyFragment();
  33.     }
  34.  
  35.     Thread t; //audio processing thread
  36.     int sr = 44100; //sampling rate
  37.     boolean isRunning = true; //audio on off
  38.     //private SeekBar volumeSeekbar = null;
  39.     private SeekBar volumeSeekbar = null;
  40.     private AudioManager audioManager = null;
  41.     Croller croller;
  42.     int sliderval = 0;
  43.     int crolprogress = 0;
  44.     int trackButPress = 0;
  45.     boolean test1going = true;
  46.     boolean test2going = false;
  47.     boolean noReset = true;
  48.  
  49.     /*public double fr = 440.f;*/
  50.     public double fr;
  51.     public int frHigher;
  52.     public int frLower;
  53.     public int counter = 0;
  54.     public int frCounter = 0;
  55.     public int highCount = 0;
  56.     public int max_frequency = 0;
  57.     public int min_frequency = 0;
  58.     ImageView finishBut;
  59.     Button logoutButton;
  60.     Button resetButton;
  61.     FirebaseAuth mAuth;
  62.     FirebaseAuth.AuthStateListener mAuthListener;
  63.  
  64.     boolean testFinished = false;
  65.  
  66.     @Nullable
  67.     @Override
  68.     public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
  69.         View view = inflater.inflate(R.layout.fragment_hearing_frequency, container, false);
  70.  
  71.         return view;
  72.     }
  73.  
  74.     @Override
  75.     public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
  76.         super.onViewCreated(view, savedInstanceState);
  77.  
  78.         resetButton = (Button)view.findViewById(R.id.resetBtn);
  79.         finishBut = (ImageView)view.findViewById(R.id.finishButton);
  80.         croller = (Croller)view.findViewById(R.id.croller);
  81.         volumeSeekbar = (SeekBar)view.findViewById(R.id.volumeControl);
  82.         audioManager = (AudioManager)getActivity().getSystemService(Context.AUDIO_SERVICE);
  83.  
  84.  
  85.         final RippleBackground rippleBackground=(RippleBackground)view.findViewById(R.id.contents);
  86.         final HTextView animText = (HTextView)view.findViewById(R.id.animatedText);
  87.         final HTextView animText2 = (HTextView)view.findViewById(R.id.animatedText2);
  88.         final HTextView instructionText = (HTextView) getActivity().findViewById(R.id.instruction_text);
  89.         final ImageView kulak=(ImageView)view.findViewById(R.id.centerImage);
  90.  
  91.         croller.setProgress(0);
  92.         volumeSeekbar.setMax(audioManager
  93.                 .getStreamMaxVolume(AudioManager.STREAM_MUSIC));
  94.         volumeSeekbar.setProgress(audioManager
  95.                 .getStreamVolume(AudioManager.STREAM_MUSIC));
  96.  
  97.         resetButton.setOnClickListener(new View.OnClickListener() {
  98.             @Override
  99.             public void onClick(View view) {
  100.                 counter = 0;
  101.                 croller.setProgress(counter);
  102.                 //trackButPress = trackButPress-1;
  103.  
  104.                 test1going = true;
  105.                 test2going = false;
  106.                 instructionText.animateText("Test 1. Press until start hearing");
  107.                 kulak.setEnabled(true);
  108.  
  109.                 animText.animateText("");
  110.                 animText2.animateText("");
  111.  
  112.             }
  113.         });
  114.  
  115.         kulak.setOnTouchListener(new View.OnTouchListener() {
  116.             @Override
  117.             public boolean onTouch(View v, MotionEvent event) {
  118.                 String both;
  119.                 String both2;
  120.  
  121.                 switch (event.getAction()) {
  122.                     case MotionEvent.ACTION_DOWN:
  123.  
  124.                         if (test1going == true && test2going == false) {
  125.  
  126.                                 isRunning = true;
  127.                                 counter = 0;
  128.                                 croller.setProgressPrimaryColor(Color.parseColor("#42f4d4"));
  129.  
  130.                                 croller.setStartOffset(0);
  131.                                 croller.setProgress(0);
  132.  
  133.                                 rippleBackground.startRippleAnimation();
  134.                                 t = new Thread() {
  135.                                     public void run() {
  136.                                         int buffsize = AudioTrack.getMinBufferSize(sr,
  137.                                                 AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT);
  138.                                         AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,
  139.                                                 sr, AudioFormat.CHANNEL_OUT_MONO,
  140.                                                 AudioFormat.ENCODING_PCM_16BIT, buffsize,
  141.                                                 AudioTrack.MODE_STREAM);
  142.  
  143.                                         short samples[] = new short[buffsize];  //audio synthesis
  144.                                         int amp = 10000;
  145.  
  146.                                         double twopi = 8.*Math.atan(1.);
  147.                                         double ph = 0.0;
  148.                                         audioTrack.play();      //audio running
  149.                                         while(isRunning){   //synthesis loop
  150.                                             //frHigher = 2*counter;
  151.                                             frHigher = 2*frCounter;
  152.  
  153.                                             Log.d("ADebugTag", "highCount/2: " + Double.toString(frHigher));
  154.  
  155.                                             for(int i=0; i < buffsize; i++){
  156.                                                 samples[i] = (short) (amp*Math.sin(ph));
  157.  
  158.                                                 ph += twopi*frHigher/sr;
  159.  
  160.                                                 volumeSeekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener()
  161.                                                 {
  162.                                                     @Override
  163.                                                     public void onStopTrackingTouch(SeekBar arg0)
  164.                                                     {
  165.                                                     }
  166.  
  167.                                                     @Override
  168.                                                     public void onStartTrackingTouch(SeekBar arg0)
  169.                                                     {
  170.                                                     }
  171.  
  172.                                                     @Override
  173.                                                     public void onProgressChanged(SeekBar arg0, int progress, boolean arg2)
  174.                                                     {
  175.                                                         audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
  176.                                                                 progress, 0);
  177.  
  178.                                                     }
  179.                                                 });
  180.                                             }
  181.                                             audioTrack.write(samples, 0, buffsize);
  182.  
  183.                                             getActivity().runOnUiThread(new Runnable() {
  184.                                                 @Override
  185.                                                 public void run() {
  186.  
  187.                                                     croller.setProgress(counter);
  188.                                                 }
  189.                                             });
  190.                                             counter++;
  191.                                             if (counter %10 == 0) {
  192.                                                 frCounter = counter;
  193.                                             }
  194.  
  195.                                             if (frHigher >= 200) {
  196.                                                 isRunning = false;
  197.                                                 return;
  198.                                             }
  199.                                         }
  200.                                         highCount = counter;
  201.  
  202.                                         audioTrack.stop();
  203.                                         audioTrack.release();
  204.  
  205.                                     }
  206.                                 };
  207.  
  208.                                 t.start();
  209.  
  210.                                 trackButPress++;
  211.  
  212.                                 test1going = false;
  213.                                 test2going = true;
  214.  
  215.                                 return true;
  216.  
  217.                         }
  218.  
  219.  
  220.                         else if (test2going == true && test1going == false){
  221.  
  222.  
  223.                             testFinished = true;
  224.                             isRunning = true;
  225.                             Log.d("ADebugTag", "highCount/2: " + Double.toString(highCount/2));
  226.  
  227.                             highCount = counter;
  228.                             Log.d("ADebugTag", "highCount: " + Double.toString(highCount));
  229.                             counter = 0;
  230.  
  231.                             croller.setProgressPrimaryColor(Color.parseColor("#ffb3ff"));
  232.                             //croller.setProgress(crolprogress);
  233.                             croller.setProgress(0);
  234.                             croller.setStartOffset(0);
  235.                             rippleBackground.startRippleAnimation();
  236.  
  237.  
  238.                             t = new Thread() {
  239.                                 public void run() {
  240.                                     int buffsize = AudioTrack.getMinBufferSize(sr,
  241.                                             AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT);
  242.                                     AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,
  243.                                             sr, AudioFormat.CHANNEL_OUT_MONO,
  244.                                             AudioFormat.ENCODING_PCM_16BIT, buffsize,
  245.                                             AudioTrack.MODE_STREAM);
  246.  
  247.                                     short samples[] = new short[buffsize];  //audio synthesis
  248.                                     int amp = 10000;
  249.  
  250.                                     double twopi = 8.*Math.atan(1.);
  251.                                     double ph = 0.0;
  252.                                     audioTrack.play();      //audio running
  253.  
  254.                                     while(isRunning){   //synthesis loop
  255.  
  256.                                         sliderval = counter;
  257.                                         frLower =  100*sliderval;
  258.  
  259.                                         for(int i=0; i < buffsize; i++){
  260.                                             samples[i] = (short) (amp*Math.sin(ph));
  261.                                             ph += twopi*frLower/sr;
  262.                                             volumeSeekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener()
  263.                                             {
  264.                                                 @Override
  265.                                                 public void onStopTrackingTouch(SeekBar arg0)
  266.                                                 {
  267.                                                 }
  268.  
  269.                                                 @Override
  270.                                                 public void onStartTrackingTouch(SeekBar arg0)
  271.                                                 {
  272.                                                 }
  273.  
  274.                                                 @Override
  275.                                                 public void onProgressChanged(SeekBar arg0, int progress, boolean arg2)
  276.                                                 {
  277.                                                     audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
  278.                                                             progress, 0);
  279.  
  280.                                                 }
  281.                                             });
  282.                                         }
  283.                                         audioTrack.write(samples, 0, buffsize);
  284.  
  285.                                         getActivity().runOnUiThread(new Runnable() {
  286.                                             @Override
  287.                                             public void run() {
  288.  
  289.                                                 crolprogress = counter/2;
  290.                                                 croller.setProgress(crolprogress);
  291.                                             }
  292.                                         });
  293.                                         counter++;
  294.                                         highCount++;
  295.                                         if (frLower >= 20000) {
  296.                                             //counter = 0;
  297.                                             isRunning = false;
  298.                                             //rippleBackground.stopRippleAnimation();
  299.                                             return;
  300.                                         }
  301.                                     }
  302.  
  303.                                     audioTrack.stop();
  304.                                     audioTrack.release();
  305.  
  306.                                 }
  307.                             };
  308.  
  309.                             t.start();
  310.                             trackButPress++;
  311.  
  312.  
  313.                             test1going = true;
  314.                             test2going = false;
  315.  
  316.                             testFinished = true;
  317.                             return true;
  318.                         }
  319.  
  320.                         else {
  321.  
  322.                         }
  323.  
  324.                     case MotionEvent.ACTION_UP:
  325.                         rippleBackground.stopRippleAnimation();
  326.                         if (test1going == false && test2going == true) {
  327.                             instructionText.animateText("Test 2. Press until stop hearing");
  328.                         }
  329.                         else if (testFinished == true) {
  330. /*                            if (testFinished == true) {
  331.                                 instructionText.animateText("Test is complete.");
  332.                                 kulak.setEnabled(false);
  333.                                 testFinished = true;
  334.                             }*/
  335.  
  336.                             instructionText.animateText("Test is complete.");
  337.                             kulak.setEnabled(false);
  338.                             testFinished = true;
  339.  
  340. /*                            else {
  341.                                 instructionText.animateText("Test 1. Press until start hearing");
  342.                             }*/
  343.  
  344.  
  345.  
  346.                         }
  347.  
  348.                         else {
  349.  
  350.                         }
  351.  
  352.                         max_frequency = frLower;
  353.                         min_frequency = frHigher;
  354.                         if (test2going == false && test1going == true) {
  355.  
  356.                             both2 = "Min: " + String.valueOf(min_frequency) + " Hz";
  357.                             both = "Max: " + String.valueOf(max_frequency) + " Hz";
  358.  
  359.                             animText.animateText(both2);
  360.                             animText2.animateText(both);
  361.                         }
  362.  
  363.                         isRunning = false;
  364.                         t = null;
  365.  
  366.  
  367.                         return true;
  368.                 }
  369.  
  370.                 return false;
  371.             }
  372.         });
  373.  
  374.         finishBut.setOnClickListener(new View.OnClickListener(){
  375.             public void onClick(View view) {
  376.                 if (testFinished == true) {
  377.                     String maxfreq = String.valueOf(max_frequency);
  378.                     String minfreq = String.valueOf(min_frequency);
  379.                     Intent intent = new Intent(getActivity(),ResultActivity.class);
  380.                     Bundle bundle = new Bundle();
  381.                     bundle.putString("maxfreq",maxfreq);
  382.                     bundle.putString("minfreq",minfreq);
  383.                     intent.putExtras(bundle);
  384.                     startActivity(intent);
  385.                 }
  386.                 else {
  387.                     AlertDialog alertDialog = new AlertDialog.Builder(getActivity()).create();
  388.                     alertDialog.setTitle("Alert");
  389.                     alertDialog.setMessage("Please finish both tests");
  390.                     alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
  391.                             new DialogInterface.OnClickListener() {
  392.                                 public void onClick(DialogInterface dialog, int which) {
  393.                                     dialog.dismiss();
  394.                                 }
  395.                             });
  396.                     alertDialog.show();
  397.                 }
  398.             }
  399.         });
  400.  
  401.     }
  402. }
Add Comment
Please, Sign In to add comment