tuttelikz

Android _ v4

Sep 25th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.12 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.  
  27.     Button buttonClicked;
  28.     Button stop;
  29.     public int counter = 0;
  30.     @Override
  31.  
  32.  
  33.     protected void onCreate(Bundle savedInstanceState) {
  34.         super.onCreate(savedInstanceState);
  35.         setContentView(R.layout.activity_main);
  36.         //seebBar();
  37.  
  38.         Button b = (Button)findViewById(R.id.button);
  39.  
  40.         b.setOnTouchListener(new View.OnTouchListener() {
  41.             @Override
  42.             public boolean onTouch(View v, MotionEvent event) {
  43.  
  44.                 switch (event.getAction()) {
  45.                     case MotionEvent.ACTION_DOWN:
  46.                         isRunning = true;
  47.                         fSlider = (SeekBar) findViewById(R.id.frequency);
  48.                         text_view = (TextView)findViewById(R.id.textView2);
  49.  
  50.                         new CountDownTimer(10000,100) {
  51.                             //double fr2 = 440.f;
  52.                             //double max_freq;
  53.                             public void onTick(long millisUntilFinished) {
  54.                                 counter++;
  55.                                 //String.valueOf(counter)
  56.                                 //double incrementPitch;
  57.  
  58.                                 //text_view.setText("Progress: " + fSlider.getProgress() + " / " + fSlider.getMax());
  59.                                 //text_view.setText("Progress: " + counter + " / " + fSlider.getMax());
  60.                                 text_view.setText(String.valueOf(counter));
  61.                                 //int progress_value;
  62.  
  63.                                 //incrementPitch = fSlider.setProgress(fSlider.getProgress() + 20);
  64.                                 //sliderval = fSlider.getProgress() / fSlider.getMax();   //pick slider value
  65.                                 //sliderval = counter / fSlider.getMax();   //pick slider value
  66.                                 //fr2 =  440 + 440*sliderval;
  67.                                 //max_freq = 440 + 440;
  68.                                 //text_view.setText("Progress: " + fr2 + " / " + max_freq); //fSlider.getMax()
  69.  
  70.  
  71.                                 t = new Thread() {
  72.                                     public void run() {
  73.                                         // set process priority
  74.                                         //setPriority(Thread.MAX_PRIORITY);       //instantiate thread object with max priority
  75.                                         // set the buffer size
  76.                                         int buffsize = AudioTrack.getMinBufferSize(sr,  //setting the buffer size for output audio
  77.                                                 AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT);
  78.                                         // create an audiotrack object
  79.                                         AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,   //instantiate audioTrack object
  80.                                                 sr, AudioFormat.CHANNEL_OUT_MONO,
  81.                                                 AudioFormat.ENCODING_PCM_16BIT, buffsize,
  82.                                                 AudioTrack.MODE_STREAM);
  83.  
  84.                                         short samples[] = new short[buffsize];  //audio synthesis
  85.                                         int amp = 10000;
  86.                                         double twopi = 8.*Math.atan(1.);
  87.                                         double fr = 440.f;
  88.                                         double ph = 0.0;
  89.  
  90.                                         // start audio
  91.                                         audioTrack.play();      //audio running
  92.  
  93.                                         // synthesis loop
  94.                                         while(isRunning){   //synthesis loop
  95.                                             sliderval = counter / fSlider.getMax();
  96.                                             //sliderval = fSlider.getProgress() / fSlider.getMax();
  97.                                             fr =  440 + 440*sliderval;
  98.  
  99.                                             for(int i=0; i < buffsize; i++){
  100.                                                 samples[i] = (short) (amp*Math.sin(ph));
  101.                                                 ph += twopi*fr/sr;
  102.                                             }
  103.                                             audioTrack.write(samples, 0, buffsize);
  104.                                         }
  105.                                         audioTrack.stop();
  106.                                         audioTrack.release();
  107.                                     }
  108.                                 };
  109.  
  110.                                 t.start();
  111.  
  112.  
  113.  
  114.  
  115.                             }
  116.  
  117.                             public void onFinish() {
  118.  
  119.                             }
  120.                         }.start();
  121.  
  122.                         return true;
  123.  
  124.                     case MotionEvent.ACTION_UP:
  125.                         isRunning = false;
  126.                         return true;
  127.                 }
  128.  
  129.  
  130.                 return false;
  131. /*                if(event.getAction() == MotionEvent.ACTION_DOWN) {
  132.  
  133.                 }
  134.  
  135.  
  136.                 else if (event.getAction() == MotionEvent.ACTION_UP) {
  137.                     isRunning = false;
  138.                 }*/
  139.  
  140.  
  141.  
  142.             }
  143.         });
  144.  
  145.     }
  146.  
  147.  
  148.  
  149.  
  150. /*    public void buttonStartClick(View v) {
  151.         //Button button = (Button) v;
  152.         //final int progress_value;
  153.  
  154.  
  155.  
  156.  
  157.     }*/
  158.  
  159.     public void buttonStopClick(View v) {
  160.  
  161.     }
  162.  
  163.     public void onDestroy(){
  164.         super.onDestroy();
  165.         isRunning = false;
  166.         try {
  167.             t.join();
  168.         } catch (InterruptedException e) {
  169.             e.printStackTrace();
  170.         }
  171.         t = null;
  172.     }
  173.  
  174.     /*public void seebBar() {
  175.         final int progress_value;
  176.  
  177.         fSlider = (SeekBar) findViewById(R.id.frequency);
  178.         text_view = (TextView)findViewById(R.id.textView2);
  179.         text_view.setText("Progress: " + fSlider.getProgress() + " / " + fSlider.getMax());
  180.  
  181.         SeekBar.OnSeekBarChangeListener listener = new SeekBar.OnSeekBarChangeListener() {
  182.             double fr2 = 440.f;
  183.             double max_freq;
  184.             int progress_value;
  185.             public void onStopTrackingTouch(SeekBar seekBar) { }
  186.             public void onStartTrackingTouch(SeekBar seekBar) { }
  187.             public void onProgressChanged(SeekBar seekBar,
  188.                                           int progress,
  189.                                           boolean fromUser) {
  190.  
  191.                 if(fromUser) sliderval = progress / (double)seekBar.getMax();   //pick slider value
  192.                 fr2 =  440 + 440*sliderval;
  193.                 max_freq = 440 + 440;
  194.                 text_view.setText("Progress: " + fr2 + " / " + max_freq); //fSlider.getMax()
  195.  
  196.             }
  197.         };
  198.  
  199.         fSlider.setOnSeekBarChangeListener(listener);
  200.  
  201.  
  202.         t = new Thread() {
  203.             public void run() {
  204.                 // set process priority
  205.                 //setPriority(Thread.MAX_PRIORITY);       //instantiate thread object with max priority
  206.                 // set the buffer size
  207.                 int buffsize = AudioTrack.getMinBufferSize(sr,  //setting the buffer size for output audio
  208.                         AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT);
  209.                 // create an audiotrack object
  210.                 AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,   //instantiate audioTrack object
  211.                         sr, AudioFormat.CHANNEL_OUT_MONO,
  212.                         AudioFormat.ENCODING_PCM_16BIT, buffsize,
  213.                         AudioTrack.MODE_STREAM);
  214.  
  215.                 short samples[] = new short[buffsize];  //audio synthesis
  216.                 int amp = 10000;
  217.                 double twopi = 8.*Math.atan(1.);
  218.                 double fr = 440.f;
  219.                 double ph = 0.0;
  220.  
  221.                 // start audio
  222.                 audioTrack.play();      //audio running
  223.  
  224.                 // synthesis loop
  225.                 while(isRunning){   //synthesis loop
  226.  
  227.                     fr =  440 + 440*sliderval;
  228.  
  229.                     for(int i=0; i < buffsize; i++){
  230.                         samples[i] = (short) (amp*Math.sin(ph));
  231.                         ph += twopi*fr/sr;
  232.                     }
  233.                     audioTrack.write(samples, 0, buffsize);
  234.                 }
  235.                 audioTrack.stop();
  236.                 audioTrack.release();
  237.             }
  238.         };
  239.  
  240.         t.start();
  241.     }*/
  242.  
  243.  
  244. }
Advertisement
Add Comment
Please, Sign In to add comment