Example #1
0
        // it'd be hard to keep track of if anything changed (without checking timestamp)
        public WaveAudio Generate(double[] freqs, double[] weights, double fSeconds, bool bUseFFT, bool bRandomPhases)
        {
            WaveAudio result;

            if (!bUseFFT)
            {
                if (bRandomPhases)
                {
                    SineWaveSumNormalizeAfterwardsRandomPhase synth = new SineWaveSumNormalizeAfterwardsRandomPhase(freqs, weights, 0.7);
                    result = synth.CreateWaveAudio(fSeconds);
                }
                else
                {
                    SineWaveSumNormalizeAfterwards synth = new SineWaveSumNormalizeAfterwards(freqs, weights, 0.7);
                    result = synth.CreateWaveAudio(fSeconds);
                }
            }
            else
            {
                int      len = 1024 * 16; // and interpolate more? As of now we only have per/pixel resolution.
                double[] imgarr = new double[len]; double[] realarr = new double[len];
                Random   r = null; if (bRandomPhases)
                {
                    r = new Random();
                }

                double dScaledown = (len / 2) / (44100.0 / 2); // map 0-22050.0 to 0-512
                for (int i = 0; i < freqs.Length; i++)
                {
                    int    index     = (int)(freqs[i] * dScaledown);
                    double amplitude = weights[i] * 200;
                    if (!bRandomPhases)
                    {
                        realarr[index] += amplitude; /*realarr[len - index] += amplitude;*/
                    }
                    else
                    {
                        double phase = r.NextDouble() * 2.0 * Math.PI;
                        realarr[index] += amplitude * Math.Cos(phase); //realarr[len - index] += amplitude * Math.Cos(phase);
                        imgarr[index]  += amplitude * Math.Sin(phase); //imgarr[len - index] += amplitude * Math.Sin(phase);
                    }
                    // and, add the mirror for negative frequency
                }

                double[] samples = new double[len];
                Fourier.RawFrequencyToSamples(out samples, realarr, imgarr);
                // now I guess repeat these samples for some time...

                // hacky estimate of time, instead of real math
                int nRepeats = (int)(fSeconds / (len / 44100.0));
                result = new WaveAudio(1);
                result.LengthInSamples = nRepeats * len;
                for (int b = 0; b < nRepeats; b++)
                {
                    Array.Copy(samples, 0, result.data[0], b * samples.Length, samples.Length);
                }
            }
            return(result);
        }
Example #2
0
        // it'd be hard to keep track of if anything changed (without checking timestamp)
        public WaveAudio Generate(double[] freqs, double[] weights, double fSeconds, bool bUseFFT, bool bRandomPhases)
        {
            WaveAudio result;
            if (!bUseFFT)
            {
                if (bRandomPhases)
                {
                    SineWaveSumNormalizeAfterwardsRandomPhase synth = new SineWaveSumNormalizeAfterwardsRandomPhase(freqs, weights, 0.7);
                    result = synth.CreateWaveAudio(fSeconds);
                }
                else
                {
                    SineWaveSumNormalizeAfterwards synth = new SineWaveSumNormalizeAfterwards(freqs, weights, 0.7);
                    result = synth.CreateWaveAudio(fSeconds);
                }
            }
            else
            {
                int len = 1024 * 16; // and interpolate more? As of now we only have per/pixel resolution.
                double[] imgarr = new double[len]; double[] realarr = new double[len];
                Random r = null; if (bRandomPhases) r= new Random();

                double dScaledown = (len / 2) / (44100.0 / 2); // map 0-22050.0 to 0-512
                for (int i = 0; i < freqs.Length; i++)
                {
                    int index = (int) (freqs[i] * dScaledown);
                    double amplitude = weights[i] * 200;
                    if (!bRandomPhases)
                    { realarr[index] += amplitude; /*realarr[len - index] += amplitude;*/ }
                    else
                    {
                        double phase = r.NextDouble() * 2.0 * Math.PI;
                        realarr[index] += amplitude * Math.Cos(phase); //realarr[len - index] += amplitude * Math.Cos(phase);
                        imgarr[index] += amplitude * Math.Sin(phase); //imgarr[len - index] += amplitude * Math.Sin(phase);
                    }
                    // and, add the mirror for negative frequency
                }

                double[] samples = new double[len];
                Fourier.RawFrequencyToSamples(out samples, realarr, imgarr);
                // now I guess repeat these samples for some time...

                // hacky estimate of time, instead of real math
                int nRepeats = (int)(fSeconds / (len / 44100.0));
                result = new WaveAudio(1);
                result.LengthInSamples = nRepeats * len;
                for (int b=0; b<nRepeats; b++)
                    Array.Copy(samples, 0, result.data[0], b*samples.Length, samples.Length);
                
            }
            return result;
        }