Example #1
0
        public static double[] WindowedSinc(int zeroCrossings, int overSampling)
        {
            int n = (zeroCrossings * 2 * overSampling) + 1;

            double[] buffer1 = new double[n];
            double[] buffer2 = new double[n];

            // Generate Sinc
            double a = (double)-zeroCrossings;
            double b = (double)zeroCrossings;

            double r;

            for (int i = 0; i < n; i++)
            {
                r          = ((double)i) / ((double)(n - 1));
                buffer1[i] = MinBleps.Sinc(a + (r * (b - a)));
            }

            // Window Sinc
            MinBleps.BlackmanWindow(n, buffer2);

            for (int i = 0; i < n; i++)
            {
                buffer1[i] *= buffer2[i];
            }

            return(buffer1);
        }
Example #2
0
        public static void Main(Stream outStream)
        {
            var sinGen = new SinGenerator2(44100);

            double phase = 0;

            for (int i = 0; i < 44100 << 2; i++)
            {
                //outStream.Write(BitConverter.GetBytes(sinGen.Sample(phase)));
                outStream.Write(BitConverter.GetBytes(MinBleps.Sinc(phase)));

                phase += 0.001;
            }
        }
Example #3
0
        public static void Main(Stream outStream)
        {
            var minBlep = MinBleps.GenerateMinBlep(16, 64);

            double[] minSquare = new double[minBlep.Length * 2];

            // for(int i = 0 ; i < minBlep.Length ; i++)
            // {
            //     minBlep[i] = (minBlep[i]/* * 0.8*/) * 2 - 1;
            // }

            for (int i = 0; i < minBlep.Length; i++)
            {
                minSquare[i] = minBlep[i] - 0.5;
                minSquare[i + minBlep.Length] = (1 - minBlep[i]) - 0.5;
            }

            // for(int i = 0 ; i < minSquare.Length ; i++)
            // {
            //     minSquare[i] = (minSquare[i] * 0.8) * 2 - 1;
            // }

            var integrated     = Integrate(minSquare, 1);
            var differentiated = Differentiate(minSquare);

            var wSinc = WindowedSinc(16, 1);

            var integratedWSinc = Integrate(wSinc, 1);

            var saw = Program.GenerateSaw(8192);

            var convolved = Convolve(saw, wSinc);

            for (int i = 0; i < minBlep.Length << 4; i++)
            {
                //outStream.Write(BitConverter.GetBytes(minBlep[i % minBlep.Length] * 0.5));
                //outStream.Write(BitConverter.GetBytes(minSquare[i % minSquare.Length] * 0.5));
                //outStream.Write(BitConverter.GetBytes(integrated[i % integrated.Length] * 0.001));
                //outStream.Write(BitConverter.GetBytes(differentiated[i % differentiated.Length] * 200));
                //outStream.Write(BitConverter.GetBytes(wSinc[i % wSinc.Length]));
                //outStream.Write(BitConverter.GetBytes(integratedWSinc[i % integratedWSinc.Length] * 0.01));
                //outStream.Write(BitConverter.GetBytes(saw[i % saw.Length]));
                outStream.Write(BitConverter.GetBytes(convolved[i % convolved.Length] * 0.5));
            }
        }
Example #4
0
        public static void Main(Stream outStream)
        {
            // double[] minBlep = MinBleps.GenerateMinBlep(6, 4);

            // for(int i = 0 ; i < minBlep.Length ; i++)
            // {
            //     outStream.Write(BitConverter.GetBytes((Int16)((minBlep[i] - 1) * 0.7 * Int16.MaxValue)));
            // }

            int minBlepOversample    = 1024;
            int minBlepZeroCrossings = 7;

            double[] minBlepA = MinBleps.GenerateMinBlep(minBlepZeroCrossings, minBlepOversample);
            double[] minBlep  = new double[minBlepA.Length - 1];
            Array.Copy(minBlepA, 1, minBlep, 0, minBlepA.Length - 1);

            int    sampleRate          = 44100;
            double phase               = 0;
            double frequency           = 440 << 2;
            double freqMult            = 1.0594631;
            int    minBlepBufferLength = minBlep.Length * 3;

            double[] minBlepBuffer      = new double[minBlepBufferLength];
            int      minBlepBufferIndex = 0;
            int      counter            = 0;

            while (frequency < 17000)
            {
                double sample = ((phase - (frequency / sampleRate)) * 2) - 1;

                double minBleppedSample = (minBlepBuffer[minBlepBufferIndex] - (frequency / 22050)) + sample;

                minBlepBuffer[minBlepBufferIndex] = 0;

                outStream.Write(BitConverter.GetBytes(minBleppedSample * 0.8));

                minBlepBufferIndex++;

                if (minBlepBufferIndex >= minBlepBufferLength)
                {
                    minBlepBufferIndex = 0;
                }

                phase += frequency / sampleRate;

                if (phase >= 1)
                {
                    phase -= (int)phase;

                    double stepSample     = (sampleRate / frequency) * phase * minBlepOversample;
                    double exactCrossTime = 1.0 - (((frequency / sampleRate) - phase) / (frequency / sampleRate));
                    double stepSample2    = exactCrossTime * minBlepOversample;

                    for (int j = 0; j < (minBlep.Length / minBlepOversample); j++)
                    {
                        // double minBlep1 = minBlep[(int)(stepSample + j * minBlepOversample)];
                        // double minBlep2 = minBlep[(int)(stepSample + j * minBlepOversample) + 1];
                        // double minBlepSample = (minBlep1 * (1 - stepSample)) + (minBlep2 * stepSample);

                        //minBlepBuffer[(minBlepBufferIndex + j) % minBlepBufferLength] += -((minBlep[(int)(stepSample + j * minBlepOversample)] * 2) - 2);

                        int    tempIndex = (int)(exactCrossTime * minBlepOversample + (/*Utility.NextDitherGaussian()*/ 0)) + (j * minBlepOversample);
                        double minBlep1  = minBlep[tempIndex];
                        double minBlep2  = tempIndex < minBlep.Length - 1 ? minBlep[tempIndex + 1] : minBlep1;
                        double blep      = minBlep1 * (1 - exactCrossTime) + minBlep2 * (exactCrossTime);
                        minBlepBuffer[(minBlepBufferIndex + j) % minBlepBufferLength] += -((blep * 2) - 2);
                        //minBlepBuffer[(minBlepBufferIndex + j) % minBlepBufferLength] += -((minBlep[tempIndex] * 2) - 2);
                    }
                }

                counter++;

                if (counter > 10000)
                {
                    counter = 0;

                    frequency *= freqMult;
                }
            }
        }