Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2017
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.86 KB | None | 0 0
  1. import java.util.LinkedList;
  2. import java.util.Queue;
  3.  
  4. /**
  5. * Implements your code here.
  6. * Change comments
  7. * @Author Your Name
  8. * @Date
  9. */
  10. public class KPGuitarString implements GuitarString{
  11.  
  12. private Queue<Double> ringBuffer;
  13. // private RingBuffer ring;
  14. private int strlen;
  15. private int time;
  16. private static final int SAMPLE = 44100;
  17. private static final double ENERGY = .996;
  18.  
  19.  
  20. public KPGuitarString(double frequency)
  21. {
  22. time = 0;
  23. int length = (int) Math.ceil(SAMPLE / frequency);
  24. strlen = length;
  25. //ring = new RingBuffer(length);
  26. ringBuffer = new LinkedList<>();
  27. for (int i = 0; i < length; i++) {
  28. ringBuffer.add(0.0);
  29. }
  30. // while(!ring.isFull())
  31. // ring.enqueue(0);
  32.  
  33. }
  34.  
  35. public KPGuitarString(double[] init)
  36. {
  37. time = 0;
  38. ringBuffer = (Queue<Double>) new RingBuffer(init.length);
  39. for(int i = 0; i < init.length; i++)
  40. {
  41. // ring.enqueue(init[i]);
  42. ringBuffer.add(init[i]);
  43. }
  44.  
  45. }
  46. /**
  47. * Implement your code here
  48. * Change comments
  49. */
  50. @Override
  51. public void pluck(){
  52. for(int i = 0; i < strlen; i++)
  53. {
  54. double rnd = Math.random() - 0.5;
  55. // ring.enqueue(rnd);
  56. ringBuffer.add(rnd);
  57. }
  58. }
  59.  
  60. /**
  61. * Implement your code here
  62. * Change comments
  63. */
  64. @Override
  65. public void tic(){
  66. // double first = ring.dequeue();
  67. double first = ringBuffer.remove();
  68. double second = sample();
  69. ringBuffer.add(ENERGY * (first + second) / 2 );
  70. time++;
  71. }
  72.  
  73. /**
  74. * Implement your code here
  75. * Change comments
  76. */
  77. @Override
  78. public double sample()
  79. {
  80. return ringBuffer.peek();
  81. }
  82.  
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement