Advertisement
tuttelikz

Android _ v1

Sep 24th, 2017
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.04 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.View;
  9. import android.widget.Button;
  10. import android.widget.SeekBar;
  11. import android.widget.TextView;
  12.  
  13. import org.w3c.dom.Text;
  14.  
  15. public class MainActivity extends AppCompatActivity {
  16.  
  17.     Thread t; //audio processing thread
  18.     int sr = 44100; //sampling rate
  19.     boolean isRunning = true; //audio on off
  20.  
  21.     SeekBar fSlider;
  22.     double sliderval;
  23.     private static TextView text_view;
  24.  
  25.     Button start;
  26.     Button stop;
  27.  
  28.     @Override
  29.  
  30.     protected void onCreate(Bundle savedInstanceState) {
  31.         super.onCreate(savedInstanceState);
  32.         setContentView(R.layout.activity_main);
  33.         seebBar();
  34.  
  35.     }
  36.  
  37.     public void buttonStartClick(View v) {
  38.         //Button button = (Button) v;
  39.  
  40.     }
  41.  
  42.     public void buttonStopClick(View v) {
  43.  
  44.     }
  45.  
  46.     public void onDestroy(){
  47.         super.onDestroy();
  48.         isRunning = false;
  49.         try {
  50.             t.join();
  51.         } catch (InterruptedException e) {
  52.             e.printStackTrace();
  53.         }
  54.         t = null;
  55.     }
  56.  
  57.     public void seebBar() {
  58.         final int progress_value;
  59.  
  60.         fSlider = (SeekBar) findViewById(R.id.frequency);
  61.         text_view = (TextView)findViewById(R.id.textView2);
  62.         text_view.setText("Progress: " + fSlider.getProgress() + " / " + fSlider.getMax());
  63.  
  64.         SeekBar.OnSeekBarChangeListener listener = new SeekBar.OnSeekBarChangeListener() {
  65.             double fr2 = 440.f;
  66.             double max_freq;
  67.             int progress_value;
  68.             public void onStopTrackingTouch(SeekBar seekBar) { }
  69.             public void onStartTrackingTouch(SeekBar seekBar) { }
  70.             public void onProgressChanged(SeekBar seekBar,
  71.                                           int progress,
  72.                                           boolean fromUser) {
  73.  
  74.                 if(fromUser) sliderval = progress / (double)seekBar.getMax();   //pick slider value
  75.                 fr2 =  440 + 440*sliderval;
  76.                 max_freq = 440 + 440;
  77.                 text_view.setText("Progress: " + fr2 + " / " + max_freq); //fSlider.getMax()
  78.  
  79.             }
  80.         };
  81.  
  82.         fSlider.setOnSeekBarChangeListener(listener);
  83.  
  84.  
  85.         t = new Thread() {
  86.             public void run() {
  87.                 // set process priority
  88.                 setPriority(Thread.MAX_PRIORITY);       //instantiate thread object with max priority
  89.                 // set the buffer size
  90.                 int buffsize = AudioTrack.getMinBufferSize(sr,  //setting the buffer size for output audio
  91.                         AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT);
  92.                 // create an audiotrack object
  93.                 AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,   //instantiate audioTrack object
  94.                         sr, AudioFormat.CHANNEL_OUT_MONO,
  95.                         AudioFormat.ENCODING_PCM_16BIT, buffsize,
  96.                         AudioTrack.MODE_STREAM);
  97.  
  98.                 short samples[] = new short[buffsize];  //audio synthesis
  99.                 int amp = 10000;
  100.                 double twopi = 8.*Math.atan(1.);
  101.                 double fr = 440.f;
  102.                 double ph = 0.0;
  103.  
  104.                 // start audio
  105.                 audioTrack.play();      //audio running
  106.  
  107.                 // synthesis loop
  108.                 while(isRunning){   //synthesis loop
  109.  
  110.                     fr =  440 + 440*sliderval;
  111.  
  112.                     for(int i=0; i < buffsize; i++){
  113.                         samples[i] = (short) (amp*Math.sin(ph));
  114.                         ph += twopi*fr/sr;
  115.                     }
  116.                     audioTrack.write(samples, 0, buffsize);
  117.                 }
  118.                 audioTrack.stop();
  119.                 audioTrack.release();
  120.             }
  121.         };
  122.  
  123.         t.start();
  124.     }
  125.  
  126.  
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement