Advertisement
Guest User

Untitled

a guest
Jun 21st, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.12 KB | None | 0 0
  1. package com.example.thomas.android_ss1;
  2.  
  3. /**
  4. * Created by thomas on 21.06.18.
  5. * Übernommen und angepasst von http://masterex.github.io/archive/2012/05/28/android-audio-synthesis.html
  6. *
  7. */
  8.  
  9. import android.os.Handler;
  10. import android.os.Message;
  11. import android.util.Log;
  12.  
  13. public class Metronome extends Thread {
  14.  
  15. public double bpm = 120;
  16. private int beat;
  17. private int noteValue;
  18. private int silence;
  19.  
  20. private double beatSound = 800;
  21. private double sound = 400;
  22. private final int tick = 1000; // samples of tick
  23.  
  24. public boolean play = false;
  25.  
  26. private AudioGenerator audioGenerator = new AudioGenerator(8000);
  27.  
  28. public Metronome() {
  29. audioGenerator.createPlayer();
  30. }
  31.  
  32. public void calcSilence() {
  33. silence = (int) (((60/bpm)*8000)-tick);
  34. }
  35.  
  36. public void startPlayingMetronome() {
  37. play = true;
  38. calcSilence();
  39. double[] tick =
  40. audioGenerator.getSineWave(this.tick, 8000, beatSound);
  41. double[] tock =
  42. audioGenerator.getSineWave(this.tick, 8000, sound);
  43. double silence = 0;
  44. double[] sound = new double[8000];
  45. int t = 0,s = 0,b = 0;
  46. do {
  47. for(int i=0;i<sound.length&&play;i++) {
  48. if(t<this.tick) {
  49. if(b == 0)
  50. sound[i] = tock[t];
  51. else
  52. sound[i] = tick[t];
  53. t++;
  54. } else {
  55. sound[i] = silence;
  56. s++;
  57. if(s >= this.silence) {
  58. t = 0;
  59. s = 0;
  60. b++;
  61. if(b > (this.beat-1))
  62. b = 0;
  63. }
  64. }
  65. }
  66. audioGenerator.writeSound(sound);
  67. } while(play);
  68. }
  69.  
  70. public void stopPlayingMetronome() {
  71. play = false;
  72. audioGenerator.destroyAudioTrack();
  73.  
  74. }
  75. public void destroyMetronome(){
  76.  
  77. }
  78.  
  79.  
  80.  
  81.  
  82. /* Getters and Setters ... */
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement