-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGuitarString_MiS.java
More file actions
58 lines (48 loc) · 1.29 KB
/
GuitarString_MiS.java
File metadata and controls
58 lines (48 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*************************************************************************
* @mseskar
* 5/1/18
*
* Write a program to simulate plucking a guitar string using the Karplus-Strong algorithm. This algorithm played a seminal role in the emergence of physically modeled sound synthesis (where a physical description of a musical instrument is used to synthesize sound electronically) *
*************************************************************************/
public class GuitarString_MiS {
private final double DECAY_FACTOR = 0.996;
private RingBuffer rb;
private int N;
public GuitarString_MiS(double frequency)
{
N = Math.round((float)(44100/frequency));
rb = new RingBuffer(N);
for(int i=0; i<N-1; i++)
{
rb.enqueue(0.0);
}
}
public GuitarString_MiS(double[] init)
{
N = init.length;
rb = new RingBuffer(N);
for(Double values: init)
{
rb.enqueue(values);
}
System.out.println(rb);
}
public void pluck() //act of striking new note, i.e. "plucking string"
{
for(int i = 0; i < N; i++)
{
rb.enqueue(Math.random() - 0.5);
rb.dequeue();
}
}
public void tic()//delay factor to effect reverb
{
double first = rb.dequeue();
double second = rb.peek();
rb.enqueue(DECAY_FACTOR*0.5*(first+second));
}
public double sample()
{
return rb.peek();
}
}