This project has moved and is read-only. For the latest updates, please go here.

karplus-strong algorithm in c# with Naudio

Oct 16, 2014 at 10:22 PM
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 class :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NAudio.Wave;

namespace test
{
    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 long Position
        {
            get
            {
                // we'll just return the number of bytes read so far
                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)
        {
         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;
    }
    
    }
}
am I doing something wrong ?
Oct 18, 2014 at 11:38 PM
mark can you help?
Oct 23, 2014 at 2:16 PM
Sorry, I'm afraid I am not familiar with the karplus-strong algorithm. You may find it easier to write your code implementing an ISampleProvider instead of waveStream though. Then you can deal exclusively with floating point samples, and don't need the BitConverter
Nov 2, 2014 at 5:38 AM
mark
do you know anyone that might be able to help me ?
Nov 11, 2014 at 5:06 PM
I'd recommend asking on signal processing forums.