tuttelikz

Android _ v6 [+]

Sep 25th, 2017
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.11 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.os.CountDownTimer;
  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.Button;
  12. import android.widget.SeekBar;
  13. import android.widget.TextView;
  14.  
  15. import org.w3c.dom.Text;
  16. import android.view.View;
  17. public class MainActivity extends AppCompatActivity {
  18.  
  19.     Thread t; //audio processing thread
  20.     int sr = 44100; //sampling rate
  21.     boolean isRunning = true; //audio on off
  22.  
  23.     SeekBar fSlider;
  24.     double sliderval;
  25.     private static TextView text_view;
  26.     private static TextView text_view_max_freq;
  27.     Button buttonClicked;
  28.     Button stop;
  29.     public int counter = 0;
  30.     public int max_frequency = 0;
  31.     @Override
  32.  
  33.     protected void onCreate(Bundle savedInstanceState) {
  34.         super.onCreate(savedInstanceState);
  35.         setContentView(R.layout.activity_main);
  36.  
  37.         Button b = (Button)findViewById(R.id.button);
  38.         text_view_max_freq = (TextView)findViewById(R.id.textView4);
  39.         text_view = (TextView)findViewById(R.id.textView2);
  40.  
  41.  
  42.         //text_view.setText("Your highest hearing frequency is: " + String.valueOf(max_frequency) + "Hz");
  43.  
  44.         b.setOnTouchListener(new View.OnTouchListener() {
  45.             @Override
  46.             public boolean onTouch(View v, MotionEvent event) {
  47.  
  48.                 text_view_max_freq.setText("Your highest hearing frequency is: " + String.valueOf(max_frequency) + "Hz");
  49.                 text_view.setText("Frequency: 0 Hz");
  50.  
  51.                 switch (event.getAction()) {
  52.                     case MotionEvent.ACTION_DOWN:
  53.                         isRunning = true;
  54.                         fSlider = (SeekBar) findViewById(R.id.frequency);
  55.  
  56.  
  57.                         t = new Thread() {
  58.                             public void run() {
  59.                                 int buffsize = AudioTrack.getMinBufferSize(sr,
  60.                                         AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT);
  61.                                 AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,
  62.                                         sr, AudioFormat.CHANNEL_OUT_MONO,
  63.                                         AudioFormat.ENCODING_PCM_16BIT, buffsize,
  64.                                         AudioTrack.MODE_STREAM);
  65.  
  66.                                 short samples[] = new short[buffsize];  //audio synthesis
  67.                                 int amp = 10000;
  68.                                 double twopi = 8.*Math.atan(1.);
  69.                                 double fr = 440.f;
  70.                                 double ph = 0.0;
  71.                                 // start audio
  72.                                 audioTrack.play();      //audio running
  73.                                 // synthesis loop
  74.                                 while(isRunning){   //synthesis loop
  75.  
  76.  
  77.                                     sliderval = counter / fSlider.getMax();
  78.                                     //sliderval = fSlider.getProgress() / fSlider.getMax();
  79.                                     fr =  440 + 440*sliderval;
  80.  
  81.                                     for(int i=0; i < buffsize; i++){
  82.                                         samples[i] = (short) (amp*Math.sin(ph));
  83.                                         ph += twopi*fr/sr;
  84.                                     }
  85.                                     audioTrack.write(samples, 0, buffsize);
  86.  
  87.                                     if (( counter % 10 ) == 0) {
  88.                                         runOnUiThread(new Runnable() {
  89.                                             @Override
  90.                                             public void run() {
  91.                                                 text_view.setText("Frequency: " + String.valueOf(counter) + "Hz");
  92.                                                 fSlider.setProgress(counter/10);
  93.                                             }
  94.                                         });
  95.  
  96.                                     max_frequency = counter;
  97.                                     counter = counter + 10;
  98.  
  99.                                     }
  100.  
  101.  
  102.                                 }
  103.                                 audioTrack.stop();
  104.                                 audioTrack.release();
  105.                             }
  106.                         };
  107.  
  108.                         t.start();
  109.                         return true;
  110.  
  111.                     case MotionEvent.ACTION_UP:
  112.                         isRunning = false;
  113.                         counter = 0;
  114.                         t = null;
  115.                         return true;
  116.                 }
  117.  
  118.  
  119.  
  120.                 return false;
  121.             }
  122.         });
  123.  
  124.     }
  125.  
  126.     public void buttonStopClick(View v) {
  127.  
  128.     }
  129.  
  130.     public void onDestroy(){
  131.         super.onDestroy();
  132.         isRunning = false;
  133.         try {
  134.             t.join();
  135.         } catch (InterruptedException e) {
  136.             e.printStackTrace();
  137.         }
  138.         t = null;
  139.     }
  140.  
  141.  
  142. }
Advertisement
Add Comment
Please, Sign In to add comment