tuttelikz

HearingFrequenciesFragment [+]

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