活跃农民
- 积分
- 354
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2012-11-11
- 最后登录
- 1970-1-1
|
package synthesizer;
public interface BoundedQueue{
public int capacity();
public int fillCount();
public boolean isEmpty();
public boolean isFull();
public void enqueue(double x);
public double dequeue();
public double peak();
}
package synthesizer;
public abstract class AbstractBoundedQueue implements BoundedQueue{
protected int fillCount;
protected int capacity;
@Override
public int capacity(){
return capacity;
}
@Override
public int fillCount(){
return fillCount;
}
@Override
public boolean isEmpty(){
if(fillCount==0) return true;
return false;
}
@Override
public boolean isFull(){
if(fillCount==capacity) return true;
return false;
}
}
package synthesizer;
// Make sure to make this class a part of the synthesizer package
//package <package name>;
public class ArrayRingBuffer extends AbstractBoundedQueue {
/* Index for the next dequeue or peek. */
private int first;
/* Index for the next enqueue. */
private int last;
/* Array for storing the buffer data. */
private double[] rb;
/** Create a new ArrayRingBuffer with the given capacity. */
public ArrayRingBuffer(int capacity) {
// TODO: Create new array with capacity elements.
// first, last, and fillCount should all be set to 0.
// this.capacity should be set appropriately. Note that the local variable
// here shadows the field we inherit from AbstractBoundedQueue.
this.capacity=capacity;
rb=new double[this.capacity];
first=0;
last=0;
fillCount=0;
}
/** Adds x to the end of the ring buffer. If there is no room, then
* throw new RuntimeException("Ring buffer overflow")
*/
public void enqueue(double x) {
// TODO: Enqueue the item. Don't forget to increase fillCount and update last.
// is there room?
if(isFull()){
throw new RuntimeException("Ring buffer overflow");
}
rb[last]=x;
fillCount++;
last++;
last= last % capacity;
}
/** Dequeue oldest item in the ring buffer. If the buffer is empty, then
* throw new RuntimeException("Ring buffer underflow");
*/
public double dequeue() {
// TODO: Dequeue the first item. Don't forget to decrease fillCount and update first.
if(isEmpty()){
throw new RuntimeException("Ring buffer underflow");
}
double temp=rb[first];
fillCount--;
first++;
first= first % capacity;
return temp;
}
/** Return oldest item, but don't remove it. */
public double peak() {
// TODO: Return the first item. None of your instance variables should change.
return rb[first];
}
}
package synthesizer;
import org.junit.Test;
import static org.junit.Assert.*;
/** Tests the ArrayRingBuffer class.
* @author Josh Hug
*/
public class TestArrayRingBuffer {
@Test
public void someTest() {
synthesizer.ArrayRingBuffer arb = new synthesizer.ArrayRingBuffer(5);
arb.enqueue(1);
assertEquals(1,arb.peak(),0);
arb.enqueue(3);
assertEquals(1,arb.peak(),0);
arb.dequeue();
assertEquals(3,arb.peak(),0);
}
/** Calls tests for ArrayRingBuffer. */
public static void main(String[] args) {
jh61b.junit.textui.runClasses(TestArrayRingBuffer.class);
}
}
// Make sure to make this class a part of the synthesizer package
//package <package name>;
package synthesizer;
//Make sure this class is public
public class GuitarString {
/** Constants. Do not change. In case you're curious, the keyword final means
* the values cannot be changed at runtime. We'll discuss this and other topics
* in lecture on Friday. */
private static final int SR = 44100; // Sampling Rate
private static final double DECAY = .996; // energy decay factor
/* Buffer for storing sound data. */
private BoundedQueue buffer;
/* Create a guitar string of the given frequency. */
public GuitarString(double frequency) {
// TODO: Create a buffer with capacity = SR / frequency. You'll need to
// cast the result of this divsion operation into an int. For better
// accuracy, use the Math.round() function before casting.
// Your buffer should be initially filled with zeros.
int capacity=(int) Math.round(SR/frequency);
buffer= new ArrayRingBuffer(capacity);
while(!buffer.isFull()){
buffer.enqueue(0);
}
}
/* Pluck the guitar string by replacing the buffer with white noise. */
public void pluck() {
// TODO: Dequeue everything in the buffer, and replace it with random numbers
// between -0.5 and 0.5. You can get such a number by using:
// double r = Math.random() - 0.5;
//
// Make sure that your random numbers are different from each other.
while(!buffer.isEmpty()){
buffer.dequeue();
}
while(!buffer.isFull()){
double r = Math.random() - 0.5;
// if(r!=buffer.last)
buffer.enqueue(r);
}
}
/* Advance the simulation one time step by performing one iteration of
* the Karplus-Strong algorithm.
*/
public void tic() {
// TODO: Dequeue the front sample and enqueue a new sample that is
// the average of the two multiplied by the DECAY factor.
// Do not call StdAudio.play().
double m=(buffer.dequeue()+buffer.peak())*0.5*DECAY;
buffer.enqueue(m);
}
/* Return the double at the front of the buffer. */
public double sample() {
return buffer.peak();
}
}
|
|