tuttelikz

Activity_main [v15+]

Oct 25th, 2017
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 15.93 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.setProgress(counter/2);
  80.  
  81.                             rippleBackground.startRippleAnimation();
  82.                             t = new Thread() {
  83.                                 public void run() {
  84.                                     int buffsize = AudioTrack.getMinBufferSize(sr,
  85.                                             AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT);
  86.                                     AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,
  87.                                             sr, AudioFormat.CHANNEL_OUT_MONO,
  88.                                             AudioFormat.ENCODING_PCM_16BIT, buffsize,
  89.                                             AudioTrack.MODE_STREAM);
  90.  
  91.                                     short samples[] = new short[buffsize];  //audio synthesis
  92.                                     int amp = 10000;
  93.  
  94.                                     double twopi = 8.*Math.atan(1.);
  95.                                     double ph = 0.0;
  96.                                     audioTrack.play();      //audio running
  97.                                     while(isRunning){   //synthesis loop
  98.                                         frHigher = 100*counter;
  99.                                         //fr =  100*counter;
  100.  
  101.                                         for(int i=0; i < buffsize; i++){
  102.                                             samples[i] = (short) (amp*Math.sin(ph));
  103.                                             //ph += twopi*fr/sr;
  104.                                             ph += twopi*frHigher/sr;
  105.                                         }
  106.                                         audioTrack.write(samples, 0, buffsize);
  107.  
  108.                                         runOnUiThread(new Runnable() {
  109.                                             @Override
  110.                                             public void run() {
  111.                                                 //croller.setProgressPrimaryColor(R.color.cyan);
  112.                                                 croller.setProgress(counter/2);
  113.                                             }
  114.                                         });
  115.                                         counter++;
  116.                                         if (frHigher >= 20000) {
  117.                                             //counter = 0;
  118.                                             isRunning = false;
  119.                                             //rippleBackground.stopRippleAnimation();
  120.                                             return;
  121.                                         }
  122.                                     }
  123.                                     highCount = counter;
  124.  
  125.                                     audioTrack.stop();
  126.                                     audioTrack.release();
  127.                                 }
  128.                             };
  129.  
  130.                             t.start();
  131.  
  132.                             trackButPress++;
  133.  
  134.                             return true;
  135.                         }
  136.  
  137.                         else {
  138.                             isRunning = true;
  139.                             Log.d("ADebugTag", "highCount/2: " + Double.toString(highCount/2));
  140.  
  141.  
  142.                             highCount = counter;
  143.                             Log.d("ADebugTag", "highCount: " + Double.toString(highCount));
  144.                             counter = 0;
  145.  
  146.                             crolprogress = counter/2;
  147.                             //croller.setProgressPrimaryColor(Color.parseColor("#e0b3ff"));
  148.                             croller.setProgressPrimaryColor(Color.parseColor("#ffb3ff"));
  149.                             croller.setProgress(crolprogress);
  150.                             croller.setStartOffset(highCount*36/20);
  151.                             rippleBackground.startRippleAnimation();
  152.  
  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.  
  169.                                     while(isRunning){   //synthesis loop
  170.  
  171.                                         sliderval = highCount;
  172.                                         frLower =  100*sliderval;
  173.                                         //fr =  100*sliderval;
  174.  
  175.                                         for(int i=0; i < buffsize; i++){
  176.                                             samples[i] = (short) (amp*Math.sin(ph));
  177.                                             ph += twopi*frLower/sr;
  178.                                         }
  179.                                         audioTrack.write(samples, 0, buffsize);
  180.  
  181.                                         runOnUiThread(new Runnable() {
  182.                                             @Override
  183.                                             public void run() {
  184.  
  185.                                                 crolprogress = counter/2;
  186.                                                 croller.setProgress(crolprogress);
  187.                                             }
  188.                                         });
  189.                                         counter--;
  190.                                         highCount--;
  191.                                         if (frLower <= 0) {
  192.                                             //counter = 0;
  193.                                             isRunning = false;
  194.                                             //rippleBackground.stopRippleAnimation();
  195.                                             return;
  196.                                         }
  197.                                     }
  198.  
  199.                                     audioTrack.stop();
  200.                                     audioTrack.release();
  201.                                 }
  202.                             };
  203.  
  204.                             t.start();
  205.                             trackButPress++;
  206.                             return true;
  207.                         }
  208.  
  209.                     //m, k
  210.                     case MotionEvent.ACTION_UP:
  211.                         rippleBackground.stopRippleAnimation();
  212.                         if ((trackButPress % 2) == 0) {
  213.                             instructionText.setText(R.string.evenPress);
  214.                         }
  215.                         else {
  216.                             instructionText.setText(R.string.oddPress);
  217.                         }
  218.  
  219.                         max_frequency = frHigher;
  220.                         min_frequency = frLower;
  221.                         if (trackButPress%2 == 0) {
  222.                             both = "Max: " + String.valueOf(max_frequency) + " Hz";
  223.                             both2 = "Min: " + String.valueOf(min_frequency) + " Hz";
  224.  
  225.                             animText.animateText(both);
  226.                             animText2.animateText(both2);
  227.                         }
  228.                         //both = "You can hear up to: " + String.valueOf(max_frequency) + " Hz";
  229.                         //both2 = "You can hear down to: " + String.valueOf(min_frequency) + " Hz";
  230.  
  231.  
  232. /*                        if (fr > max_frequency) {
  233.                             max_frequency = fr;
  234.                             if (max_frequency < 10000) {
  235.                                 both = "You can hear up to: " + String.valueOf(max_frequency) + " Hz";
  236.                                 both2 = "Try again :)";
  237.                             }
  238.                             else if (max_frequency > 10000 && max_frequency <15000) {
  239.                                 both = "You can hear up to: " + String.valueOf(max_frequency) + " Hz";
  240.                                 both2 = "Time to see a doctor :)";
  241.                             }
  242.                             else {
  243.                                 both = "You can hear up to: " + String.valueOf(max_frequency) + " Hz";
  244.                                 both2 = "Not bad :)";
  245.                             }
  246.                             animText.animateText(both);
  247.                             animText2.animateText(both2);
  248.                         }*/
  249.                         isRunning = false;
  250.                         t = null;
  251.  
  252.  
  253.                         return true;
  254.                 }
  255.  
  256.                 return false;
  257.             }
  258.         });
  259.     }
  260.  
  261.     private void initControls()
  262.     {
  263.         try
  264.         {
  265.             volumeSeekbar = (SeekBar)findViewById(R.id.volumeControl);
  266.             audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
  267.             volumeSeekbar.setMax(audioManager
  268.                     .getStreamMaxVolume(AudioManager.STREAM_MUSIC));
  269.             volumeSeekbar.setProgress(audioManager
  270.                     .getStreamVolume(AudioManager.STREAM_MUSIC));
  271.  
  272.  
  273.             volumeSeekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener()
  274.             {
  275.                 @Override
  276.                 public void onStopTrackingTouch(SeekBar arg0)
  277.                 {
  278.                 }
  279.  
  280.                 @Override
  281.                 public void onStartTrackingTouch(SeekBar arg0)
  282.                 {
  283.                 }
  284.  
  285.                 @Override
  286.                 public void onProgressChanged(SeekBar arg0, int progress, boolean arg2)
  287.                 {
  288.                     audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
  289.                             progress, 0);
  290.                 }
  291.             });
  292.  
  293.  
  294.  
  295. /*        {
  296.             volumeSeekbar = (BubbleSeekBar) findViewById(R.id.volumeControl);
  297.             audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
  298.  
  299.             volumeSeekbar.setOnProgressChangedListener(new BubbleSeekBar.OnProgressChangedListener() {
  300.                 @Override
  301.                 public void onProgressChanged(BubbleSeekBar bubbleSeekBar, int progress, float progressFloat) {
  302.                     audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
  303.                             progress, 0);
  304.                 }
  305.  
  306.                 @Override
  307.                 public void getProgressOnActionUp(BubbleSeekBar bubbleSeekBar, int progress, float progressFloat) {
  308.  
  309.                 }
  310.  
  311.                 @Override
  312.                 public void getProgressOnFinally(BubbleSeekBar bubbleSeekBar, int progress, float progressFloat) {
  313.  
  314.                 }
  315.             });*/
  316.  
  317.  
  318.  
  319. /*            volumeSeekbar.se
  320.             volumeSeekbar.setMax(audioManager
  321.                     .getStreamMaxVolume(AudioManager.STREAM_MUSIC));
  322.             volumeSeekbar.setProgress(audioManager
  323.                     .getStreamVolume(AudioManager.STREAM_MUSIC));
  324.             volumeSeekbar.setOnScrollChangeListener(new );
  325.             volumeSeekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener()
  326.             {
  327.                 @Override
  328.                 public void onStopTrackingTouch(SeekBar arg0)
  329.                 {
  330.                 }
  331.  
  332.                 @Override
  333.                 public void onStartTrackingTouch(SeekBar arg0)
  334.                 {
  335.                 }
  336.  
  337.                 @Override
  338.                 public void onProgressChanged(SeekBar arg0, int progress, boolean arg2)
  339.                 {
  340.                     audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
  341.                             progress, 0);
  342.                 }
  343.             });*/
  344.         }
  345.         catch (Exception e)
  346.         {
  347.             e.printStackTrace();
  348.         }
  349.     }
  350.  
  351.     public void lastpage(View v) {
  352.         //String strname = urname.getText().toString();
  353.         //String stremail = uremail.getText().toString();
  354.  
  355. /*        if (strname.equals("")||stremail.equals("")) {
  356.             Toast.makeText(getBaseContext(), "Enter both name and email", Toast.LENGTH_SHORT).show();
  357.         }else {
  358.             Intent in = new Intent(LoginActivity.this, VoiceActivity.class);
  359.             Bundle bundle = new Bundle();
  360.             bundle.putString("urname",strname);
  361.             bundle.putString("uremail",stremail);
  362.             in.putExtras(bundle);
  363.             startActivity(in);
  364.         }*/
  365.         Intent initi = new Intent(MainActivity.this, ResultActivity.class);
  366. /*        Bundle bundle = new Bundle();
  367.         bundle.putString("urname",strname);
  368.         bundle.putString("uremail",stremail);
  369.         in.putExtras(bundle);*/
  370.         startActivity(initi);
  371.     }
  372. /*    public void onDestroy(){
  373.         super.onDestroy();
  374.         isRunning = false;
  375.         try {
  376.             t.join();
  377.         } catch (InterruptedException e) {
  378.             e.printStackTrace();
  379.         }
  380.         t = null;
  381.     }*/
  382. }
Advertisement
Add Comment
Please, Sign In to add comment