This project has moved. For the latest updates, please go here.

static noise

Nov 2, 2014 at 4:44 AM
so sometime when i play a note I get static noise
and do not know why...
i would said 6 out of 14 times which is high...
how so I troubleshot that ?

this s my code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NAudio.Wave;

namespace Music
{
    class Guitar : WaveStream
    {
        public Guitar()
        {
            Frequency = 0;
            n2 = 0;
            n3 = 0;
            n4 = 0;
            rnd1 = new System.Random();
            t = 0;
        }
        public double Frequency { get; set; }
        public double Amplitude { get; set; }
        static int n2;
        static int n3;
        static int n4;
        public long Bufferlength { get; set; }
        private long position;
        float[] X;
        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;
            }
        }

        // buffer size is 52920 and so is sampleCount
        // oddset is 0
        public override int Read(byte[] buffer, int offset, int sampleCount)
        {
            // Bufferlength set to the min I want the sound to play for

            int N = (int)(44100D / Frequency); // size of buffer for the karplus-strong algorithm
            float sum;// temp holding place
            // below only happens once 
            // it make the karplus-strong algorithm buffer
            if (n2 == 0) // flag
            {
                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));// random number from -0.5 - 0.5
                  //  X[i] = (float)(0.5 * Math.Sin(i * Frequency * Math.PI / 44100D));
                }
                n2 = 1; // flag  
            }
            // karplus-strong algorithm low pass filer
            for (int r = 0; r < sampleCount / 4; r++)
            {
                if (t >= (N - 1))
                {
                    t = 0;
                    n4 = 1;
                }
                if (n3 >= Bufferlength && t == 0)
                {
                    Dispose(); // stop playing
                    return 0; // stop playing
                }
                if (n4 == 0)
                {
                    sum = X[t];
                    temp[t] = sum;
                }
                else
                {
                    sum = (float)(X[t] + ((temp[t] + temp[t + 1])*0.5* 0.998));// low pass filter and decay factor of 0,996
                    temp[t] = sum;

                }
                t++; // counter this is a  static  counter
             
                // send sum to the butter that so it can be played later
                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;// end of Read sound start playing now
        }

    }
}
...
   private void button13_Click(object sender, EventArgs e)
        {
            guitar = new Guitar();
            guitar.Frequency = 130.8;
            guitar.Bufferlength = 44100 * 2;
            waveout = new WaveOut();
            WaveChannel32 temp = new WaveChannel32(guitar);
            temp.PadWithZeroes = false;
            waveout.Init(guitar);
            waveout.Play();
        }
...
Coordinator
Nov 11, 2014 at 3:53 PM
best way to trouble-shoot this type of code is not to play the audio, but write a few seconds of audio to a WAV file, then look at the results in an audio editor like Audacity