Beispiel #1
0
        public override int Read(float[] buffer, int offset, int count)
        {
            //if (phase > 1.0f)
            //{
            //    //Console.WriteLine("Phase reset {0}", phase);
            //    phase = phase % 1.0f;
            //}

            float phaseinc  = (1.0f / _samplerate);
            float phaseinc2 = 0;
            float t         = 0;
            float sine      = 0;

            float[] frequencysamples   = new float[buffer.Length];
            float[] amplitudesamples   = new float[buffer.Length];
            float[] phaseoffsetsamples = new float[buffer.Length];
            float[] originsamples      = new float[buffer.Length];
            Frequency.Read(frequencysamples, offset, count);
            Amplitude.Read(amplitudesamples, offset, count);
            PhaseOffset.Read(phaseoffsetsamples, offset, count);
            Origin.Read(originsamples, offset, count);
            float freq        = 0;
            float amp         = 0;
            float offsetphase = 0;
            float origin      = 0;

            float value = 0;
            float twopi = (float)Math.PI * 2;

            for (int i = offset; i < count; i++)
            {
                freq = frequencysamples[i];
                amp  = amplitudesamples[i];
                //if (phase > freq)
                //{
                //    phase = phase % (1.0f / freq);
                //}

                phaseinc2 = (1.0f / (_samplerate)) * freq;

                offsetphase = (phase + phaseoffsetsamples[i]);

                origin = originsamples[i];

                switch (this.waveform)
                {
                case WaveType.SineWave:
                    value = (float)(Math.Sin(offsetphase * twopi));
                    break;

                case WaveType.SquareWave:
                    t = (float)((offsetphase));
                    if (t < 0.5)
                    {
                        value = -1.0f;
                    }
                    else
                    {
                        value = 1.0f;
                    }
                    break;

                case WaveType.TriangleWave:
                    t = (float)((offsetphase));
                    if (t <= 0.5)
                    {
                        value = -1.0f + (4 * t);
                    }
                    else
                    {
                        value = 1.0f - (4 * (t - 0.5f));
                    }

                    break;

                case WaveType.SawtoothWave:
                    t     = (float)((offsetphase));
                    value = -1.0f + (2 * t);
                    break;

                case WaveType.InverseSawtoothWave:
                    t     = (float)((offsetphase));
                    value = 1.0f - (2 * t);
                    break;

                case WaveType.PulseWaveHalf:
                    t = (float)((offsetphase));
                    if (t < 0.25)
                    {
                        value = -1.0f;
                    }
                    else
                    {
                        value = 1.0f;
                    }
                    break;

                case WaveType.PulseWaveQuarter:
                    t = (float)((offsetphase));
                    if (t < 0.125)
                    {
                        value = -1.0f;
                    }
                    else
                    {
                        value = 1.0f;
                    }
                    break;

                default:
                    value = 0;
                    break;
                }
                buffer[i] = origin + (amp * value);
                phase     = (phase + phaseinc2) % 1.0f;
            }

            return(count);
        }