Advertisement
tuttelikz

MainActivity [v14+] with Circular

Oct 1st, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.93 KB | None | 0 0
  1. package com.example.iptea.audio22;
  2.  
  3. import android.graphics.Color;
  4. import android.media.AudioFormat;
  5. import android.media.AudioManager;
  6. import android.media.AudioTrack;
  7. import android.support.v7.app.AppCompatActivity;
  8. import android.os.Bundle;
  9. import android.view.MotionEvent;
  10. import android.view.View;
  11. import android.widget.ImageView;
  12. import android.widget.SeekBar;
  13.  
  14. import com.hanks.htextview.base.HTextView;
  15. import com.sdsmdg.harjot.crollerTest.Croller;
  16. import com.skyfishjy.library.RippleBackground;
  17.  
  18. public class MainActivity extends AppCompatActivity {
  19.  
  20.     Thread t; //audio processing thread
  21.     int sr = 44100; //sampling rate
  22.     boolean isRunning = true; //audio on off
  23.     //SeekBar fSlider;
  24.     Croller croller;
  25.     double sliderval;
  26.     public double fr = 440.f;
  27.     public int counter = 0;
  28.     public double max_frequency = 0;
  29.     @Override
  30.  
  31.     protected void onCreate(Bundle savedInstanceState) {
  32.         super.onCreate(savedInstanceState);
  33.         setContentView(R.layout.activity_main);
  34.  
  35.         final RippleBackground rippleBackground=(RippleBackground)findViewById(R.id.contents);
  36.         final HTextView animText = (HTextView)findViewById(R.id.animatedText);
  37.         final HTextView animText2 = (HTextView)findViewById(R.id.animatedText2);
  38.  
  39.         ImageView kulak=(ImageView)findViewById(R.id.centerImage);
  40.  
  41.         croller = (Croller) findViewById(R.id.croller);
  42.  
  43.         kulak.setOnTouchListener(new View.OnTouchListener() {
  44.             @Override
  45.             public boolean onTouch(View v, MotionEvent event) {
  46.                 String both;
  47.                 String both2;
  48.  
  49.                 switch (event.getAction()) {
  50.                     case MotionEvent.ACTION_DOWN:
  51.  
  52.                         isRunning = true;
  53.  
  54.                         rippleBackground.startRippleAnimation();
  55.                         t = new Thread() {
  56.                             public void run() {
  57.                                 int buffsize = AudioTrack.getMinBufferSize(sr,
  58.                                         AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT);
  59.                                 AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,
  60.                                         sr, AudioFormat.CHANNEL_OUT_MONO,
  61.                                         AudioFormat.ENCODING_PCM_16BIT, buffsize,
  62.                                         AudioTrack.MODE_STREAM);
  63.  
  64.                                 short samples[] = new short[buffsize];  //audio synthesis
  65.                                 int amp = 10000;
  66.  
  67.                                 double twopi = 8.*Math.atan(1.);
  68.                                 double ph = 0.0;
  69.                                 audioTrack.play();      //audio running
  70.  
  71.                                 while(isRunning){   //synthesis loop
  72.  
  73.                                     sliderval = counter*0.5 / croller.getMax();
  74.                                     fr =  440 + 440*sliderval;
  75.  
  76.                                     for(int i=0; i < buffsize; i++){
  77.                                         samples[i] = (short) (amp*Math.sin(ph));
  78.                                         ph += twopi*fr/sr;
  79.                                     }
  80.                                     audioTrack.write(samples, 0, buffsize);
  81.  
  82.                                     runOnUiThread(new Runnable() {
  83.                                         @Override
  84.                                         public void run() {
  85.                                             if (( counter % 10 ) == 0) {
  86.                                                 //fSlider.setProgress(counter/89);  // counter/100
  87.                                                 croller.setProgress(counter/89);
  88.                                                 counter = counter + 100;
  89.                                             }
  90.                                         }
  91.                                     });
  92.  
  93.                                     if (fr > 20000) {
  94.                                         counter = 0;
  95.                                     }
  96.                                 }
  97.                                 counter = 0;
  98.  
  99.                                 audioTrack.stop();
  100.                                 audioTrack.release();
  101.                             }
  102.                         };
  103.  
  104.                         t.start();
  105.                         return true;
  106.  
  107.                     case MotionEvent.ACTION_UP:
  108.  
  109.                         if (fr > max_frequency) {
  110.                             max_frequency = fr;
  111.                             if (max_frequency < 10000) {
  112.                                 both = "You can hear up to: " + String.valueOf(max_frequency) + " Hz";
  113.                                 both2 = "Try again :)";
  114.                             }
  115.                             else if (max_frequency > 10000 && max_frequency <15000) {
  116.                                 both = "You can hear up to: " + String.valueOf(max_frequency) + " Hz";
  117.                                 both2 = "Time to see a doctor :)";
  118.                             }
  119.                             else {
  120.                                 both = "You can hear up to: " + String.valueOf(max_frequency) + " Hz";
  121.                                 both2 = "Not bad :)";
  122.                             }
  123.                             animText.animateText(both);
  124.                             animText2.animateText(both2);
  125.                         }
  126.                         isRunning = false;
  127.                         counter = 0;
  128.                         t = null;
  129.                         rippleBackground.stopRippleAnimation();
  130.  
  131.                         return true;
  132.                 }
  133.  
  134.                 return false;
  135.             }
  136.         });
  137.     }
  138.  
  139.     public void onDestroy(){
  140.         super.onDestroy();
  141.         isRunning = false;
  142.         try {
  143.             t.join();
  144.         } catch (InterruptedException e) {
  145.             e.printStackTrace();
  146.         }
  147.         t = null;
  148.     }
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement