tuttelikz

Untitled

Sep 25th, 2017
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.66 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. protected void onCreate(Bundle savedInstanceState) {
  33. super.onCreate(savedInstanceState);
  34. setContentView(R.layout.activity_main);
  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. isRunning = true;
  45. fSlider = (SeekBar) findViewById(R.id.frequency);
  46. text_view = (TextView)findViewById(R.id.textView2);
  47.  
  48. new CountDownTimer(10000,100) {
  49. //double fr2 = 440.f;
  50. //double max_freq;
  51. public void onTick(long millisUntilFinished) {
  52. counter++;
  53. text_view.setText(String.valueOf(counter));
  54.  
  55. t = new Thread() {
  56. public void run() {
  57. int buffsize = AudioTrack.getMinBufferSize(sr,
  58. AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT);
  59. AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,
  60. sr, AudioFormat.CHANNEL_OUT_MONO,
  61. AudioFormat.ENCODING_PCM_16BIT, buffsize,
  62. AudioTrack.MODE_STREAM);
  63.  
  64. short samples[] = new short[buffsize]; //audio synthesis
  65. int amp = 10000;
  66. double twopi = 8.*Math.atan(1.);
  67. double fr = 440.f;
  68. double ph = 0.0;
  69. // start audio
  70. audioTrack.play(); //audio running
  71. // synthesis loop
  72. while(isRunning){ //synthesis loop
  73. sliderval = counter / fSlider.getMax();
  74. //sliderval = fSlider.getProgress() / fSlider.getMax();
  75. fr = 440 + 440*sliderval;
  76.  
  77. for(int i=0; i < buffsize; i++){
  78. samples[i] = (short) (amp*Math.sin(ph));
  79. ph += twopi*fr/sr;
  80. }
  81. audioTrack.write(samples, 0, buffsize);
  82. }
  83. audioTrack.stop();
  84. audioTrack.release();
  85. }
  86. };
  87.  
  88. t.start();
  89. }
  90.  
  91. public void onFinish() {
  92.  
  93. }
  94.  
  95. }.start();
  96.  
  97. return true;
  98.  
  99. case MotionEvent.ACTION_UP:
  100. isRunning = false;
  101. return true;
  102. }
  103.  
  104.  
  105. return false;
  106. }
  107. });
  108.  
  109. }
  110.  
  111.  
  112.  
  113. public void buttonStopClick(View v) {
  114.  
  115. }
  116.  
  117. public void onDestroy(){
  118. super.onDestroy();
  119. isRunning = false;
  120. try {
  121. t.join();
  122. } catch (InterruptedException e) {
  123. e.printStackTrace();
  124. }
  125. t = null;
  126. }
  127.  
  128.  
  129. }
Advertisement
Add Comment
Please, Sign In to add comment