tuttelikz

Activity_main [v16+]

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