Advertisement
tuttelikz

Android _ v3

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