tuttelikz

HearingFrequenciesFragment [Higher Only Working]

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