B
Btb4198
Guest
using the the Karplus-Strong algorithm, I wrote this code to synthesize Guitar sound, it works , but it still sound to fake.
does anyone know what I did wrong ?
class Guitar : WaveStream
{
public Guitar()
{
Amplitude = 1;
Frequency = 0;
n2 = 0;
n3 = 0;
n4 = 0;
rnd1 = new System.Random();
t = 0;
}
bool onetimeflag;
public bool stopflag { get; set; }
public double Frequency { get; set; }
public double Amplitude { get; set; }
public long Bufferlenght { get; set; }
public override long Length { get { return Bufferlenght; } }
public override WaveFormat WaveFormat { get { return WaveFormat.CreateIeeeFloatWaveFormat(44100, 2); } }
private long position;
static int n2;
static int n3;
static int n4;
float[] X;
float[] temp;
Random rnd1;
static int t;
public override long Position
{
get
{
return position;
}
set
{
position = value;
}
}
public override int Read(byte[] buffer, int offset, int sampleCount)
{
int N = (int)(44100D / Frequency);
float sum;
if (n2 == 0)
{
X = new float[N];
temp = new float[N + 1];
for (int i = 0; i < N; i++)
{
X = (float)(rnd1.Next(-5, 5)) + (float)(0.5 * Math.Sin(i * Frequency * Math.PI / 44100D));
}
n2 = 1;
}
for (int r = 0; r < sampleCount / 4; r++)
{
if (t >= (N - 1))
{
t = 0;
n4 = 1;
}
if (n3 >= Bufferlenght && t == 0)
{
Dispose();
return 0;
}
if (n4 == 0)
{
sum = X[t];
temp[t] = sum;
}
else
{
sum = (float)(((temp[t] + temp[t + 1]) * 0.5 * 0.998));
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];
n3++;
}
return sampleCount;
}
}
Continue reading...
does anyone know what I did wrong ?
class Guitar : WaveStream
{
public Guitar()
{
Amplitude = 1;
Frequency = 0;
n2 = 0;
n3 = 0;
n4 = 0;
rnd1 = new System.Random();
t = 0;
}
bool onetimeflag;
public bool stopflag { get; set; }
public double Frequency { get; set; }
public double Amplitude { get; set; }
public long Bufferlenght { get; set; }
public override long Length { get { return Bufferlenght; } }
public override WaveFormat WaveFormat { get { return WaveFormat.CreateIeeeFloatWaveFormat(44100, 2); } }
private long position;
static int n2;
static int n3;
static int n4;
float[] X;
float[] temp;
Random rnd1;
static int t;
public override long Position
{
get
{
return position;
}
set
{
position = value;
}
}
public override int Read(byte[] buffer, int offset, int sampleCount)
{
int N = (int)(44100D / Frequency);
float sum;
if (n2 == 0)
{
X = new float[N];
temp = new float[N + 1];
for (int i = 0; i < N; i++)
{
X = (float)(rnd1.Next(-5, 5)) + (float)(0.5 * Math.Sin(i * Frequency * Math.PI / 44100D));
}
n2 = 1;
}
for (int r = 0; r < sampleCount / 4; r++)
{
if (t >= (N - 1))
{
t = 0;
n4 = 1;
}
if (n3 >= Bufferlenght && t == 0)
{
Dispose();
return 0;
}
if (n4 == 0)
{
sum = X[t];
temp[t] = sum;
}
else
{
sum = (float)(((temp[t] + temp[t + 1]) * 0.5 * 0.998));
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];
n3++;
}
return sampleCount;
}
}
Continue reading...