tuttelikz

MainActivity.java [-=--=-=]

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