locked
synthesize Guitar sound RRS feed

  • Question

  • 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[i] = (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;
            }
               
        }

    Friday, June 26, 2020 2:54 PM

All replies

  • Karplus-Strong simulates the general effect of a plucked or hammered string.  It doesn't make any attempt to simulate particular KINDS of strings.  Getting a true guitar sound is incredibly complicated, because it involves not only the shape, length, tension, and material of the string, but also the shape, size and material of the guitar body.  That's why almost every modern keyboard is sampled, not synthesized.

    You're using a simple sine wave to simulate the sound of the string, which Karplus-Strong then plucks.  There are probably web sites where you can find algorithms to generate an original sound that is closer to a true guitar.


    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

    Monday, June 29, 2020 5:22 AM
  • I have been looking but could not find one..

    lol that why is I posted this question 

    Monday, June 29, 2020 7:22 AM
  • I have been looking but could not find one..

    lol that why is I posted this question 

    The first Google link gives me :

    KarplusStrong.h

    Monday, June 29, 2020 7:28 AM