tuttelikz

Activity_main [v13+]

Oct 24th, 2017
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 14.06 KB | None | 0 0
  1. package com.example.iptea.audio22;
  2.  
  3. import android.content.Intent;
  4. import android.graphics.Color;
  5. import android.media.AudioFormat;
  6. import android.media.AudioManager;
  7. import android.media.AudioTrack;
  8. import android.support.v7.app.AppCompatActivity;
  9. import android.os.Bundle;
  10. import android.util.Log;
  11. import android.view.MotionEvent;
  12. import android.view.View;
  13. import android.widget.ImageView;
  14. import android.widget.SeekBar;
  15.  
  16. import com.hanks.htextview.base.HTextView;
  17. import com.sdsmdg.harjot.crollerTest.Croller;
  18. import com.skyfishjy.library.RippleBackground;
  19.  
  20. public class MainActivity extends AppCompatActivity {
  21.  
  22.     Thread t; //audio processing thread
  23.     int sr = 44100; //sampling rate
  24.     boolean isRunning = true; //audio on off
  25.     //SeekBar fSlider;
  26.     Croller croller;
  27.     double sliderval = 0;
  28.     int crolprogress = 0;
  29.     int trackButPress = 0;
  30.     /*public double fr = 440.f;*/
  31.     public double fr;
  32.     public int counter = 0;
  33.     public double max_frequency = 0;
  34.     ImageView finishBut;
  35.     @Override
  36.  
  37.     protected void onCreate(Bundle savedInstanceState) {
  38.         super.onCreate(savedInstanceState);
  39.         setContentView(R.layout.activity_main);
  40.  
  41.         final RippleBackground rippleBackground=(RippleBackground)findViewById(R.id.contents);
  42.         final HTextView animText = (HTextView)findViewById(R.id.animatedText);
  43.         final HTextView animText2 = (HTextView)findViewById(R.id.animatedText2);
  44.  
  45.         finishBut = (ImageView) findViewById(R.id.finishButton);
  46.         ImageView kulak=(ImageView)findViewById(R.id.centerImage);
  47.  
  48.         croller = (Croller) findViewById(R.id.croller);
  49.         /*croller.setProgress(crolprogress);*/
  50.         croller.setProgress(0);
  51.         kulak.setOnTouchListener(new View.OnTouchListener() {
  52.             @Override
  53.             public boolean onTouch(View v, MotionEvent event) {
  54.                 String both;
  55.                 String both2;
  56.  
  57.                 switch (event.getAction()) {
  58.                     case MotionEvent.ACTION_DOWN:
  59.                         if ((trackButPress % 2) == 0) {
  60.  
  61.                             isRunning = true;
  62.                             counter = 0;
  63.                             rippleBackground.startRippleAnimation();
  64.                             t = new Thread() {
  65.                                 public void run() {
  66.                                     int buffsize = AudioTrack.getMinBufferSize(sr,
  67.                                             AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT);
  68.                                     AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,
  69.                                             sr, AudioFormat.CHANNEL_OUT_MONO,
  70.                                             AudioFormat.ENCODING_PCM_16BIT, buffsize,
  71.                                             AudioTrack.MODE_STREAM);
  72.  
  73.                                     short samples[] = new short[buffsize];  //audio synthesis
  74.                                     int amp = 10000;
  75.  
  76.                                     double twopi = 8.*Math.atan(1.);
  77.                                     double ph = 0.0;
  78.                                     audioTrack.play();      //audio running
  79.                             /*counter = 0;*/
  80.                                     while(isRunning){   //synthesis loop
  81.  
  82.                                         /*sliderval = counter;
  83.                                         fr =  100*sliderval;
  84.                                         */
  85.                                         //sliderval = counter;
  86.                                         fr =  100*counter;
  87.  
  88.  
  89.  
  90.                                     /*sliderval = counter*0.5 / croller.getMax();*/
  91.  
  92.  
  93.                                     /*Log.d("ADebugTag", "counter: " + Integer.toString(counter));*/
  94.                                     /*Log.d("ADebugTag", "fr: " + Double.toString(sliderval));*/
  95.  
  96.                                     /*fr =  440 + 440*sliderval;*/
  97.  
  98.                                     /*fr =  100 + 100*sliderval;*/
  99.  
  100.  
  101. /*                                    crolprogress =  fr/22000;
  102.                                     croller.setProgress((int) crolprogress);*/
  103.  
  104.                                     /*fr =  440 + 440*sliderval;*/
  105.                                         Log.d("ADebugTag", "fr: " + Double.toString(fr));
  106.                                     /*Log.d("ADebugTag", "Value: " + Double.toString(fr));*/
  107.  
  108.                                         for(int i=0; i < buffsize; i++){
  109.                                             samples[i] = (short) (amp*Math.sin(ph));
  110.                                             ph += twopi*fr/sr;
  111.                                         /*Log.d("ADebugTag", "ph: " + Double.toString(ph));*/
  112.  
  113.                                         /*Log.d("ADebugTag", "Value: HELLO WORLD");*/
  114.                                         }
  115.                                         audioTrack.write(samples, 0, buffsize);
  116.                                     /*Log.d("ADebugTag", "counter: " + Integer.toString(counter));*/
  117.  
  118.                                         runOnUiThread(new Runnable() {
  119.                                             @Override
  120.                                             public void run() {
  121. /*                                            if (( counter % 10 ) == 0) {
  122.                                                 //fSlider.setProgress(counter/89);  // counter/100
  123.                                                 croller.setProgress(counter/89);
  124.                                                 counter = counter + 100;
  125.                                             }*/
  126.  
  127.  
  128.  
  129.                                                 /*crolprogress = counter/2;*/
  130.                                                 croller.setProgress(counter/2);
  131. /*                                            if (counter > 2) {
  132.                                                 crolprogress = counter/2;
  133.                                                 croller.setProgress(crolprogress);
  134.                                             }*/
  135.  
  136.                                             /*croller.setProgress(counter/89);*/
  137.                                                 Log.d("ADebugTag", "counter: " + Integer.toString(counter));
  138.                                             }
  139.                                         });
  140.                                         counter++;
  141.                                         if (fr > 20000) {
  142.                                             counter = 0;
  143.                                         }
  144.                                     }
  145.                                 /*counter = 0;*/
  146.  
  147.                                     audioTrack.stop();
  148.                                     audioTrack.release();
  149.                                 }
  150.                             };
  151.  
  152.                             t.start();
  153.                             return true;
  154.                         }
  155.  
  156.                         else {
  157.                             isRunning = true;
  158.                             /*counter = 0;*/
  159.                             rippleBackground.startRippleAnimation();
  160.                             t = new Thread() {
  161.                                 public void run() {
  162.                                     int buffsize = AudioTrack.getMinBufferSize(sr,
  163.                                             AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT);
  164.                                     AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,
  165.                                             sr, AudioFormat.CHANNEL_OUT_MONO,
  166.                                             AudioFormat.ENCODING_PCM_16BIT, buffsize,
  167.                                             AudioTrack.MODE_STREAM);
  168.  
  169.                                     short samples[] = new short[buffsize];  //audio synthesis
  170.                                     int amp = 10000;
  171.  
  172.                                     double twopi = 8.*Math.atan(1.);
  173.                                     double ph = 0.0;
  174.                                     audioTrack.play();      //audio running
  175.                             /*counter = 0;*/
  176.                                     while(isRunning){   //synthesis loop
  177.  
  178.                                         sliderval = counter;
  179.                                     /*sliderval = counter*0.5 / croller.getMax();*/
  180.  
  181.  
  182.                                     /*Log.d("ADebugTag", "counter: " + Integer.toString(counter));*/
  183.                                     /*Log.d("ADebugTag", "fr: " + Double.toString(sliderval));*/
  184.  
  185.                                     /*fr =  440 + 440*sliderval;*/
  186.  
  187.                                     /*fr =  100 + 100*sliderval;*/
  188.  
  189.                                         fr =  100*sliderval;
  190. /*                                    crolprogress =  fr/22000;
  191.                                     croller.setProgress((int) crolprogress);*/
  192.  
  193.                                     /*fr =  440 + 440*sliderval;*/
  194.                                         Log.d("ADebugTag", "fr: " + Double.toString(fr));
  195.                                     /*Log.d("ADebugTag", "Value: " + Double.toString(fr));*/
  196.  
  197.                                         for(int i=0; i < buffsize; i++){
  198.                                             samples[i] = (short) (amp*Math.sin(ph));
  199.                                             ph += twopi*fr/sr;
  200.                                         /*Log.d("ADebugTag", "ph: " + Double.toString(ph));*/
  201.  
  202.                                         /*Log.d("ADebugTag", "Value: HELLO WORLD");*/
  203.                                         }
  204.                                         audioTrack.write(samples, 0, buffsize);
  205.                                     /*Log.d("ADebugTag", "counter: " + Integer.toString(counter));*/
  206.  
  207.                                         runOnUiThread(new Runnable() {
  208.                                             @Override
  209.                                             public void run() {
  210. /*                                            if (( counter % 10 ) == 0) {
  211.                                                 //fSlider.setProgress(counter/89);  // counter/100
  212.                                                 croller.setProgress(counter/89);
  213.                                                 counter = counter + 100;
  214.                                             }*/
  215.  
  216.  
  217.                                                 croller.setProgress(crolprogress);
  218.                                                 crolprogress = counter/2;
  219.  
  220. /*                                            if (counter > 2) {
  221.                                                 crolprogress = counter/2;
  222.                                                 croller.setProgress(crolprogress);
  223.                                             }*/
  224.  
  225.                                             /*croller.setProgress(counter/89);*/
  226.                                                 Log.d("ADebugTag", "counter: " + Integer.toString(counter));
  227.                                             }
  228.                                         });
  229.                                         counter++;
  230.                                         if (fr > 20000) {
  231.                                             counter = 0;
  232.                                         }
  233.                                     }
  234.                                 /*counter = 0;*/
  235.  
  236.                                     audioTrack.stop();
  237.                                     audioTrack.release();
  238.                                 }
  239.                             };
  240.  
  241.                             t.start();
  242.                             return true;
  243.                         }
  244.  
  245.                     //m, k
  246.                     case MotionEvent.ACTION_UP:
  247.  
  248.                         if (fr > max_frequency) {
  249.                             max_frequency = fr;
  250.                             if (max_frequency < 10000) {
  251.                                 both = "You can hear up to: " + String.valueOf(max_frequency) + " Hz";
  252.                                 both2 = "Try again :)";
  253.                             }
  254.                             else if (max_frequency > 10000 && max_frequency <15000) {
  255.                                 both = "You can hear up to: " + String.valueOf(max_frequency) + " Hz";
  256.                                 both2 = "Time to see a doctor :)";
  257.                             }
  258.                             else {
  259.                                 both = "You can hear up to: " + String.valueOf(max_frequency) + " Hz";
  260.                                 both2 = "Not bad :)";
  261.                             }
  262.                             animText.animateText(both);
  263.                             animText2.animateText(both2);
  264.                         }
  265.                         isRunning = false;
  266.                         /*counter = 0;*/
  267.                         t = null;
  268.                         rippleBackground.stopRippleAnimation();
  269.  
  270.                         return true;
  271.                 }
  272.  
  273.                 return false;
  274.             }
  275.         });
  276.     }
  277.     public void lastpage(View v) {
  278.         //String strname = urname.getText().toString();
  279.         //String stremail = uremail.getText().toString();
  280.  
  281. /*        if (strname.equals("")||stremail.equals("")) {
  282.             Toast.makeText(getBaseContext(), "Enter both name and email", Toast.LENGTH_SHORT).show();
  283.         }else {
  284.             Intent in = new Intent(LoginActivity.this, VoiceActivity.class);
  285.             Bundle bundle = new Bundle();
  286.             bundle.putString("urname",strname);
  287.             bundle.putString("uremail",stremail);
  288.             in.putExtras(bundle);
  289.             startActivity(in);
  290.         }*/
  291.         Intent initi = new Intent(MainActivity.this, ResultActivity.class);
  292. /*        Bundle bundle = new Bundle();
  293.         bundle.putString("urname",strname);
  294.         bundle.putString("uremail",stremail);
  295.         in.putExtras(bundle);*/
  296.         startActivity(initi);
  297.     }
  298. /*    public void onDestroy(){
  299.         super.onDestroy();
  300.         isRunning = false;
  301.         try {
  302.             t.join();
  303.         } catch (InterruptedException e) {
  304.             e.printStackTrace();
  305.         }
  306.         t = null;
  307.     }*/
  308. }
Advertisement
Add Comment
Please, Sign In to add comment