tuttelikz

MainActivity [v9+]

Oct 1st, 2017
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.53 KB | None | 0 0
  1. package com.example.iptea.audio22;
  2.  
  3. import android.media.AudioFormat;
  4. import android.media.AudioManager;
  5. import android.media.AudioTrack;
  6. import android.media.Image;
  7. import android.os.CountDownTimer;
  8. import android.support.v7.app.AppCompatActivity;
  9. import android.os.Bundle;
  10. import android.view.MotionEvent;
  11. import android.view.View;
  12. //import android.widget.Button;
  13. import android.view.animation.AnimationUtils;
  14. import android.widget.ImageView;
  15. import android.widget.SeekBar;
  16. import android.widget.TextView;
  17.  
  18. import org.w3c.dom.Text;
  19. import android.view.View;
  20.  
  21. import com.hanks.htextview.base.HTextView;
  22. import com.skyfishjy.library.RippleBackground;
  23.  
  24. public class MainActivity extends AppCompatActivity {
  25.  
  26.     Thread t; //audio processing thread
  27.     int sr = 44100; //sampling rate
  28.     boolean isRunning = true; //audio on off
  29.  
  30.     SeekBar fSlider;
  31.     double sliderval;
  32.     //private static TextView text_view;
  33.     private static TextView text_view_max_freq;
  34.  
  35.     //Button buttonClicked;
  36.     //Button stop;
  37.     public double fr = 440.f;
  38.     public int counter = 0;
  39.     public int int_count = 0;
  40.     public double max_frequency = 0;
  41.     @Override
  42.  
  43.     protected void onCreate(Bundle savedInstanceState) {
  44.         super.onCreate(savedInstanceState);
  45.         setContentView(R.layout.activity_main);
  46.  
  47.         final RippleBackground rippleBackground=(RippleBackground)findViewById(R.id.contents);
  48.         //final HTextView animatedText = (HTextView)findViewById(R.id.fadetext);
  49.  
  50.         ImageView kulak=(ImageView)findViewById(R.id.centerImage);
  51.         //ImageView kulak = (ImageView)findViewById(R.id.imageView);
  52.  
  53.  
  54.         //Button b = (Button)findViewById(R.id.button);
  55.         text_view_max_freq = (TextView)findViewById(R.id.textView4);
  56.         //text_view = (TextView)findViewById(R.id.textView2);  //////////////////////////////////
  57.         //text_view.setText("Your highest hearing frequency is: " + String.valueOf(max_frequency) + "Hz");
  58.         //fSlider.getThumb().mutate().setAlpha(0);
  59.         //kulak.setOnTouchListener(new View.OnTouchListener() {
  60.         kulak.setOnTouchListener(new View.OnTouchListener() {
  61.             @Override
  62.             public boolean onTouch(View v, MotionEvent event) {
  63.                 //String both = "Highest frequency: " + String.valueOf(max_frequency) + " Hz";
  64.                 String hfreq;
  65.                 String both;
  66.                 //String hfreq = String.valueOf(max_frequency);
  67.                 //text_view_max_freq.setText(both);
  68.  
  69.                 //text_view_max_freq.setText("Highest hearing frequency : " + String.valueOf(max_frequency) + "Hz");
  70.                 //text_view.setText("Frequency: 0 Hz");
  71.                 //text_view_max_freq.startAnimation(AnimationUtils.loadAnimation(MainActivity.this));
  72.                 //animatedText.animateText();
  73.  
  74.                 switch (event.getAction()) {
  75.                     case MotionEvent.ACTION_DOWN:
  76.  
  77.                         isRunning = true;
  78.                         fSlider = (SeekBar) findViewById(R.id.frequency);
  79.  
  80.  
  81.  
  82.                         rippleBackground.startRippleAnimation();
  83.                         t = new Thread() {
  84.                             public void run() {
  85.                                 int buffsize = AudioTrack.getMinBufferSize(sr,
  86.                                         AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT);
  87.                                 AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,
  88.                                         sr, AudioFormat.CHANNEL_OUT_MONO,
  89.                                         AudioFormat.ENCODING_PCM_16BIT, buffsize,
  90.                                         AudioTrack.MODE_STREAM);
  91.  
  92.                                 short samples[] = new short[buffsize];  //audio synthesis
  93.                                 int amp = 10000;
  94.  
  95.                                 double twopi = 8.*Math.atan(1.);
  96.                                 //double fr = 440.f;
  97.                                 double ph = 0.0;
  98.                                 // start audio
  99.                                 audioTrack.play();      //audio running
  100.                                 // synthesis loop
  101.                                 while(isRunning){   //synthesis loop
  102.  
  103.  
  104.                                     sliderval = counter*0.5 / fSlider.getMax();
  105.                                     //sliderval = fSlider.getProgress() / fSlider.getMax();
  106.                                     fr =  440 + 440*sliderval;
  107.  
  108.                                     for(int i=0; i < buffsize; i++){
  109.                                         samples[i] = (short) (amp*Math.sin(ph));
  110.                                         ph += twopi*fr/sr;
  111.                                     }
  112.                                     audioTrack.write(samples, 0, buffsize);
  113.  
  114.  
  115.                                     runOnUiThread(new Runnable() {
  116.                                         @Override
  117.                                         public void run() {
  118. /*                                            if (int_count == 0) {
  119.                                                 text_view.setText("Frequency: 0 Hz");
  120.                                             }*/
  121.                                             if (( counter % 10 ) == 0) {
  122.                                                 //text_view.setText("Frequency: " + String.valueOf(counter*1.5) + "Hz");
  123.                                                 //text_view.setText("Frequency: " + String.valueOf(fr) + "Hz"); //////////////////////////////////
  124.  
  125.                                                 fSlider.setProgress(counter/89);  // counter/100
  126.                                                 counter = counter + 100;
  127.                                             }
  128.                                         }
  129.                                     });
  130.  
  131.                                     //if (fr > max_frequency) {
  132.                                     //    max_frequency = fr;
  133.                                     //}
  134.  
  135.                                     if (fr > 20000) {
  136.                                         counter = 0;
  137.                                     }
  138.                                 }
  139.                                 //isRunning = false;
  140.                                 counter = 0;
  141.  
  142.                                 audioTrack.stop();
  143.                                 audioTrack.release();
  144.  
  145.                             }
  146.                         };
  147.  
  148.                         t.start();
  149.                         return true;
  150.  
  151.                     case MotionEvent.ACTION_UP:
  152.  
  153.                         if (fr > max_frequency) {
  154.                             max_frequency = fr;
  155.                             both = "Highest frequency: " + String.valueOf(max_frequency) + " Hz";
  156.                             text_view_max_freq.setText(both);
  157.                             hfreq = String.valueOf(max_frequency);
  158.                             //animatedText.animateText(hfreq);
  159.                         }
  160.                         isRunning = false;
  161.                         counter = 0;
  162.                         t = null;
  163.                         rippleBackground.stopRippleAnimation();
  164.  
  165.                         return true;
  166.                 }
  167.  
  168.                 return false;
  169.             }
  170.         });
  171.     }
  172.  
  173.     //public void buttonStopClick(View v) {
  174.  
  175.     //}
  176.  
  177.     public void onDestroy(){
  178.         super.onDestroy();
  179.         isRunning = false;
  180.         try {
  181.             t.join();
  182.         } catch (InterruptedException e) {
  183.             e.printStackTrace();
  184.         }
  185.         t = null;
  186.     }
  187. }
Advertisement
Add Comment
Please, Sign In to add comment