tuttelikz

Android _ v2

Sep 24th, 2017
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.34 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 buttonClicked;
  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.  
  38.  
  39.  
  40. public void buttonStartClick(View v) {
  41. //Button button = (Button) v;
  42. //final int progress_value;
  43. double incrementPitch;
  44. fSlider = (SeekBar) findViewById(R.id.frequency);
  45. text_view = (TextView)findViewById(R.id.textView2);
  46. text_view.setText("Progress: " + fSlider.getProgress() + " / " + fSlider.getMax());
  47. double fr2 = 440.f;
  48. double max_freq;
  49. int progress_value;
  50. incrementPitch = fSlider.setProgress(fSlider.getProgress() + 20);
  51. sliderval = fSlider.getProgress() / fSlider.getMax(); //pick slider value
  52. fr2 = 440 + 440*sliderval;
  53. max_freq = 440 + 440;
  54. text_view.setText("Progress: " + fr2 + " / " + max_freq); //fSlider.getMax()
  55.  
  56.  
  57. t = new Thread() {
  58. public void run() {
  59. // set process priority
  60. //setPriority(Thread.MAX_PRIORITY); //instantiate thread object with max priority
  61. // set the buffer size
  62. int buffsize = AudioTrack.getMinBufferSize(sr, //setting the buffer size for output audio
  63. AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT);
  64. // create an audiotrack object
  65. AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, //instantiate audioTrack object
  66. sr, AudioFormat.CHANNEL_OUT_MONO,
  67. AudioFormat.ENCODING_PCM_16BIT, buffsize,
  68. AudioTrack.MODE_STREAM);
  69.  
  70. short samples[] = new short[buffsize]; //audio synthesis
  71. int amp = 10000;
  72. double twopi = 8.*Math.atan(1.);
  73. double fr = 440.f;
  74. double ph = 0.0;
  75.  
  76. // start audio
  77. audioTrack.play(); //audio running
  78.  
  79. // synthesis loop
  80. while(isRunning){ //synthesis loop
  81.  
  82. fr = 440 + 440*sliderval;
  83.  
  84. for(int i=0; i < buffsize; i++){
  85. samples[i] = (short) (amp*Math.sin(ph));
  86. ph += twopi*fr/sr;
  87. }
  88. audioTrack.write(samples, 0, buffsize);
  89. }
  90. audioTrack.stop();
  91. audioTrack.release();
  92. }
  93. };
  94.  
  95. t.start();
  96.  
  97. }
  98.  
  99. public void buttonStopClick(View v) {
  100.  
  101. }
  102.  
  103. public void onDestroy(){
  104. super.onDestroy();
  105. isRunning = false;
  106. try {
  107. t.join();
  108. } catch (InterruptedException e) {
  109. e.printStackTrace();
  110. }
  111. t = null;
  112. }
  113.  
  114. /*public void seebBar() {
  115. final int progress_value;
  116.  
  117. fSlider = (SeekBar) findViewById(R.id.frequency);
  118. text_view = (TextView)findViewById(R.id.textView2);
  119. text_view.setText("Progress: " + fSlider.getProgress() + " / " + fSlider.getMax());
  120.  
  121. SeekBar.OnSeekBarChangeListener listener = new SeekBar.OnSeekBarChangeListener() {
  122. double fr2 = 440.f;
  123. double max_freq;
  124. int progress_value;
  125. public void onStopTrackingTouch(SeekBar seekBar) { }
  126. public void onStartTrackingTouch(SeekBar seekBar) { }
  127. public void onProgressChanged(SeekBar seekBar,
  128. int progress,
  129. boolean fromUser) {
  130.  
  131. if(fromUser) sliderval = progress / (double)seekBar.getMax(); //pick slider value
  132. fr2 = 440 + 440*sliderval;
  133. max_freq = 440 + 440;
  134. text_view.setText("Progress: " + fr2 + " / " + max_freq); //fSlider.getMax()
  135.  
  136. }
  137. };
  138.  
  139. fSlider.setOnSeekBarChangeListener(listener);
  140.  
  141.  
  142. t = new Thread() {
  143. public void run() {
  144. // set process priority
  145. //setPriority(Thread.MAX_PRIORITY); //instantiate thread object with max priority
  146. // set the buffer size
  147. int buffsize = AudioTrack.getMinBufferSize(sr, //setting the buffer size for output audio
  148. AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT);
  149. // create an audiotrack object
  150. AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, //instantiate audioTrack object
  151. sr, AudioFormat.CHANNEL_OUT_MONO,
  152. AudioFormat.ENCODING_PCM_16BIT, buffsize,
  153. AudioTrack.MODE_STREAM);
  154.  
  155. short samples[] = new short[buffsize]; //audio synthesis
  156. int amp = 10000;
  157. double twopi = 8.*Math.atan(1.);
  158. double fr = 440.f;
  159. double ph = 0.0;
  160.  
  161. // start audio
  162. audioTrack.play(); //audio running
  163.  
  164. // synthesis loop
  165. while(isRunning){ //synthesis loop
  166.  
  167. fr = 440 + 440*sliderval;
  168.  
  169. for(int i=0; i < buffsize; i++){
  170. samples[i] = (short) (amp*Math.sin(ph));
  171. ph += twopi*fr/sr;
  172. }
  173. audioTrack.write(samples, 0, buffsize);
  174. }
  175. audioTrack.stop();
  176. audioTrack.release();
  177. }
  178. };
  179.  
  180. t.start();
  181. }*/
  182.  
  183.  
  184. }
Advertisement
Add Comment
Please, Sign In to add comment