Ejemplo n.º 1
0
        static void Main2(string[] args)
        {
            using (var stdout = /*Console.OpenStandardOutput()*/ File.Open("./out2.raw", FileMode.Create))
            {
                int      sampleRate          = 44100;
                int      waveformSampleCount = 44100 << 10;
                double[] waveform            = GenerateSaw(waveformSampleCount);
                //double[] waveform2 = GenerateSaw(waveformSampleCount/2);
                //double[] waveform3 = GenerateSaw(waveformSampleCount/4);
                //double[] waveform4 = GenerateSaw(waveformSampleCount/8);

                SampleFilter sf = new SampleFilter();

                double phase     = 0;
                double frequency = 110;
                double volume    = 1f;
                bool   increased = false;
                double freqMult  = 1.00001f;
                //double freqMult = 1.1f;
                int run = 0;

                while (frequency < 20000)
                {
                    //var sample = BitConverter.GetBytes((Int16)(waveform[(int)(phase * waveformSampleCount)] * volume));
                    double sample;

                    double realIndex = (phase * waveformSampleCount) + (NextDither() * 1000000);
                    int    intIndex  = (int)Math.Round(realIndex);

                    if (intIndex < 0)
                    {
                        intIndex = -intIndex;
                    }

                    intIndex %= waveformSampleCount;
                    int intIndexNext = (intIndex + 1) % waveformSampleCount;


                    sample = waveform[intIndex];
                    var sample2 = waveform[intIndexNext];

                    double interpolationPercent = (realIndex - (int)realIndex);
                    double delta     = sample2 - sample;
                    double increment = delta * interpolationPercent;

                    sample = sample + increment;

                    var dither = NextDither();

                    // if(frequency < sampleRate / 2)
                    //     sample = waveform[(int)(phase * waveformSampleCount)] * volume;
                    // else if(frequency < (sampleRate / 2) + (sampleRate / 4))
                    //     sample = waveform2[(int)(phase * waveformSampleCount / 2)] * volume;
                    // else if(frequency < (sampleRate / 2) + (sampleRate / 4) + (sampleRate / 8))
                    //     sample = waveform3[(int)(phase * waveformSampleCount / 4)] * volume;
                    // else
                    //     sample = waveform4[(int)(phase * waveformSampleCount / 8)] * volume;

                    //if(run > 2185)
                    //    run = run;

                    //FirFilter.Put(sf, sample);

                    //sample = FirFilter.Get(sf) * 0.8;

                    stdout.Write(BitConverter.GetBytes((Int16)(((sample * volume) * Int16.MaxValue) + dither)), 0, 2);
                    stdout.Write(BitConverter.GetBytes((Int16)(((sample * volume) * Int16.MaxValue) + dither)), 0, 2);
                    //stdout.Write(BitConverter.GetBytes(sample), 0, 8);
                    //stdout.Write(BitConverter.GetBytes(sample), 0, 8);
                    //Console.WriteLine(waveform[(int)(phase * waveformSampleCount)]);

                    phase += frequency / sampleRate;

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

                    //if(phase >= 1)
                    //    phase = 0;

                    //if(run > 0 && (run % 44100) == 0)
                    frequency *= freqMult;

                    //if(frequency >= 19000 || frequency <= 5)
                    //    freqMult = 1/freqMult;

                    /*if(!increased && frequency > 5000)
                     * {
                     *  volume = 1;
                     *  increased = true;
                     * }*/

                    run++;
                }

                // int sampleRate = 44100;

                // float phaseL = 0;
                // float frequencyL = (float)(20 * 2 * Math.PI);
                // float phaseR = 0;
                // float frequencyR = (float)(20 * 2 * Math.PI);

                // while(true)
                // {
                //     stdout.Write(BitConverter.GetBytes((Int16)(Math.Sin(phaseL) * Int16.MaxValue)), 0, 2);
                //     stdout.Write(BitConverter.GetBytes((Int16)(Math.Sin(phaseR) * Int16.MaxValue)), 0, 2);

                //     phaseL += (frequencyL / sampleRate);
                //     phaseR += (frequencyR / sampleRate);

                //     if(phaseL >= 2 * Math.PI)
                //         phaseL -= (float)(2 * Math.PI);

                //     if(phaseR >= 2 * Math.PI)
                //         phaseR -= (float)(2 * Math.PI);

                //     frequencyL += 0.009f;
                //     frequencyR += 0.005f;
                // }
            }
        }