- #1
btb4198
- 572
- 10
Mod note: Added [ code ] tags to the following code.
I wrote the karplus-strong algorithm in c# using Naudio. I plays the sound, but it does not stop. It should get to 0 and you should not hear anything.
but I code get point where it just repeats the same value over and over. so I when in and did the math but hand and I see why.
well I kind of see why
array[90] = -0.19160895
array[91] = - 0.193147987
0.996 * 0.5 * ( array[90] + array[91] ) = -0.191608954626
array[90] = -0.19160895
see is is the same number!
do the sound does not get to 0 and it does not stop.
array[91] = - 0.193147987
array[92] = -0.194699377
0.996 * 0.5 * ( array[91] + array[92] ) = -0.193147987
arra[91] = -0.193147987272
this is my code :
I wrote the karplus-strong algorithm in c# using Naudio. I plays the sound, but it does not stop. It should get to 0 and you should not hear anything.
but I code get point where it just repeats the same value over and over. so I when in and did the math but hand and I see why.
well I kind of see why
array[90] = -0.19160895
array[91] = - 0.193147987
0.996 * 0.5 * ( array[90] + array[91] ) = -0.191608954626
array[90] = -0.19160895
see is is the same number!
do the sound does not get to 0 and it does not stop.
array[91] = - 0.193147987
array[92] = -0.194699377
0.996 * 0.5 * ( array[91] + array[92] ) = -0.193147987
arra[91] = -0.193147987272
this is my code :
Code:
class Guitar : WaveStream
{
public Guitar()
{
Frequency = 0;
n2 = 0;
rnd1 = new System.Random();
t = 0;
}
public double Frequency { get; set; }
public double Amplitude { get; set; }
static int n2;
public long Bufferlength { get; set; }
private long position;
float[] temp;
public override long Length { get { return Bufferlength; } }
public override WaveFormat WaveFormat { get { return WaveFormat.CreateIeeeFloatWaveFormat(44100, 2); } }
Random rnd1;
static int t;
public override int Read(byte[] buffer, int offset, int sampleCount)
{
int N = (int)(44100D/Frequency);
float sum;
if (n2 ==0)
{
temp = new float[N];
for (int i = 0; i < N; i++)
{
temp[I] = (float)(0.1 * rnd1.Next(-5, 5));
byte[] bytes = BitConverter.GetBytes(temp[I]);
buffer[i * 4 + 0] = bytes[0];
buffer[i * 4 + 1] = bytes[1];
buffer[i* 4 + 2] = bytes[2];
buffer[i * 4 + 3] = bytes[3];
}
n2 = 1;
for (int r = N; r < sampleCount / 4; r++)
{
if (t == (N-1))
t = 0;
sum = (float)(0.996 * 0.5 * (temp[t] + temp[t + 1]));
temp[t] = sum;
t++;
byte[] bytes = BitConverter.GetBytes(sum);
buffer[r * 4 + 0] = bytes[0];
buffer[r * 4 + 1] = bytes[1];
buffer[r * 4 + 2] = bytes[2];
buffer[r * 4 + 3] = bytes[3];
}
return sampleCount;
}
for (int r =0; r < sampleCount/4; r++)
{
if (t == (N-1))
t = 0;
sum = (float)(0.996 *0.5 *( temp[t] + temp[t + 1]));
temp[t] = sum;
t++;
byte[] bytes = BitConverter.GetBytes(sum);
buffer[r * 4 + 0] = bytes[0];
buffer[r * 4 + 1] = bytes[1];
buffer[r * 4 + 2] = bytes[2];
buffer[r * 4 + 3] = bytes[3];
}
return sampleCount;
}
}
Last edited by a moderator: