コード例 #1
0
        public static WaveAudio Tremolo(WaveAudio w, double tremfreq, double amp)
        {
            WaveAudio res = new WaveAudio(w.getSampleRate(), w.getNumChannels());

            res.LengthInSamples = w.LengthInSamples;
            double tremeloFreqScale = 2.0 * Math.PI * tremfreq / (double)w.getSampleRate();

            for (int ch = 0; ch < w.data.Length; ch++)
            {
                for (int i = 0; i < w.data[ch].Length; i++)
                {
                    double val = w.data[ch][i] * (1 + amp * Math.Sin(tremeloFreqScale * i));
                    if (val > 1.0)
                    {
                        val = 1.0;
                    }
                    else if (val < -1.0)
                    {
                        val = -1.0;
                    }
                    res.data[ch][i] = val;
                }
            }
            return(res);
        }
コード例 #2
0
        // These work by shifting the signal until it seems to correlate with itself.
        // In other words if the signal looks very similar to (signal shifted 200 samples) than the fundamental period is probably 200 samples
        // Note that the algorithm only works well when there's only one prominent fundamental.
        // This could be optimized by looking at the rate of change to determine a maximum without testing all periods.
        private static double[] detectPitchCalculation(WaveAudio w, double minHz, double maxHz, int nCandidates, int nResolution, PitchDetectAlgorithm algorithm)
        {
            // note that higher frequency means lower period
            int nLowPeriodInSamples = hzToPeriodInSamples(maxHz, w.getSampleRate());
            int nHiPeriodInSamples = hzToPeriodInSamples(minHz, w.getSampleRate());
            if (nHiPeriodInSamples <= nLowPeriodInSamples) throw new Exception("Bad range for pitch detection.");
            if (w.getNumChannels() != 1) throw new Exception("Only mono supported.");
            double[] samples = w.data[0];
            if (samples.Length < nHiPeriodInSamples) throw new Exception("Not enough samples.");

            // both algorithms work in a similar way
            // they yield an array of data, and then we find the index at which the value is highest.
            double[] results = new double[nHiPeriodInSamples - nLowPeriodInSamples];

            if (algorithm == PitchDetectAlgorithm.Amdf)
            {
                for (int period = nLowPeriodInSamples; period < nHiPeriodInSamples; period += nResolution)
                {
                    double sum = 0;
                    // for each sample, see how close it is to a sample n away. Then sum these.
                    for (int i = 0; i < samples.Length - period; i++)
                        sum += Math.Abs(samples[i] - samples[i + period]);

                    double mean = sum / (double)samples.Length;
                    mean *= -1; //somewhat of a hack. We are trying to find the minimum value, but our findBestCandidates finds the max. value.
                    results[period - nLowPeriodInSamples] = mean;
                }
            }
            else if (algorithm == PitchDetectAlgorithm.Autocorrelation)
            {
                for (int period = nLowPeriodInSamples; period < nHiPeriodInSamples; period += nResolution)
                {
                    double sum = 0;
                    // for each sample, find correlation. (If they are far apart, small)
                    for (int i = 0; i < samples.Length - period; i++)
                        sum += samples[i] * samples[i + period];

                    double mean = sum / (double)samples.Length;
                    results[period - nLowPeriodInSamples] = mean;
                }
            }

            // find the best indices
            int[] bestIndices = findBestCandidates(nCandidates, ref results); //note findBestCandidates modifies parameter
            // convert back to Hz
            double[] res = new double[nCandidates];
            for (int i=0; i<nCandidates;i++)
                res[i] = periodInSamplesToHz(bestIndices[i]+nLowPeriodInSamples, w.getSampleRate());
            return res;
        }
コード例 #3
0
        public static WaveAudio Vibrato(WaveAudio wave, double freq, double width)
        {
            if (width < 0)
            {
                throw new ArgumentException("Factor must >= 0");
            }
            WaveAudio newwave = new WaveAudio(wave.getSampleRate(), wave.getNumChannels());

            // do operation for all channels
            for (int i = 0; i < wave.getNumChannels(); i++)
            {
                newwave.data[i] = vibratoChannel(wave.data[i], wave.getSampleRate(), width, freq);
            }

            return(newwave);
        }
コード例 #4
0
            public WaveAudio doModify(WaveAudio src, int bufsize)
            {
                WaveAudio wout = new WaveAudio(src.getSampleRate(), 1);

                wout.LengthInSamples = src.LengthInSamples;

                //reuse the buffers.
                double[] freqRealIn  = new double[bufsize / 2], freqRealOut = new double[bufsize / 2];
                double[] freqImagIn  = new double[bufsize / 2], freqImagOut = new double[bufsize / 2];
                double[] fbuffertime = new double[bufsize];
                for (int partnum = 0; partnum < src.LengthInSamples / bufsize; partnum++)
                {
                    //copy into buffer.
                    Array.Copy(src.data[0], partnum * bufsize, fbuffertime, 0, bufsize);
                    double[] ffreqreal, ffreqimag;
                    Fourier.RawSamplesToFrequency(fbuffertime, out ffreqreal, out ffreqimag);
                    //we only care about the first half of these results.
                    Array.Copy(ffreqreal, freqRealIn, bufsize / 2);
                    Array.Copy(ffreqimag, freqImagIn, bufsize / 2);
                    this.modifyRectangular(freqRealIn, freqImagIn, freqRealOut, freqImagOut);
                    Array.Copy(freqRealOut, ffreqreal, bufsize / 2);
                    Array.Copy(freqImagOut, ffreqimag, bufsize / 2);
                    double[] fbufout;
                    Fourier.RawFrequencyToSamples(out fbufout, ffreqreal, ffreqimag);
                    Array.Copy(fbufout, 0, wout.data[0], partnum * bufsize, bufsize);
                }
                return(wout);
            }
コード例 #5
0
        /// <summary>
        /// Create a new sound by changing the speed/pitch of the old sound. 2.0 = an octave up, 1.0 = the same, 0.5 = down an octave
        /// </summary>
        public static WaveAudio ScalePitchAndDuration(WaveAudio w, double factor)
        {
            if (factor < 0) throw new ArgumentException("Factor must >= 0");
            WaveAudio res = new WaveAudio(w.getSampleRate(), w.getNumChannels());

            // do operation for all channels
            for (int i = 0; i < w.getNumChannels(); i++)
                res.data[i] = scalePitchAndDurationChannel(w.data[i], factor);

            return res;
        }
コード例 #6
0
 public static WaveAudio GetSliceSample(WaveAudio wthis, int nStart, int nEnd)
 {
     WaveAudio slice = new WaveAudio(wthis.getSampleRate(), wthis.getNumChannels());
     if (nEnd <= nStart || nEnd > wthis.LengthInSamples || nStart < 0) throw new Exception("Invalid slice");
     for (int ch = 0; ch < slice.data.Length; ch++)
     {
         slice.data[ch] = new double[nEnd - nStart];
         Array.Copy(wthis.data[ch], nStart, slice.data[ch], 0, nEnd - nStart);
     }
     return slice;
 }
コード例 #7
0
ファイル: ConstructionUtil.cs プロジェクト: Alexrerx/yaalp
 public static WaveAudio hiPassFilter(WaveAudio w, double factor) //0.5
 {
     WaveAudio ret = new WaveAudio(w.getSampleRate(), w.getNumChannels());
     ret.LengthInSamples = w.LengthInSamples;
     for (int ch = 0; ch < w.getNumChannels(); ch++)
     {
         ret.data[ch][0] = w.data[ch][0];
         for (int i = 1; i < ret.data[ch].Length; i++)
             ret.data[ch][i] = factor * ret.data[ch][i - 1] + (factor) * (w.data[ch][i] - w.data[ch][i-1]);
     }
     return ret;
 }
コード例 #8
0
ファイル: ConstructionUtil.cs プロジェクト: Alexrerx/yaalp
        //could also get continuous with window.


        public static WaveAudio lowPassFilter(WaveAudio w, double factor) //0.5
        {
            //http://en.wikipedia.org/wiki/Low-pass_filter
            WaveAudio ret = new WaveAudio(w.getSampleRate(), w.getNumChannels());
            ret.LengthInSamples = w.LengthInSamples;
            for (int ch = 0; ch < w.getNumChannels(); ch++)
            {
                ret.data[ch][0] = w.data[ch][0];
                for (int i=1; i<ret.data[ch].Length; i++)
                    ret.data[ch][i] = (1-factor)*ret.data[ch][i-1] + (factor)*w.data[ch][i];
            }
            return ret;
        }
コード例 #9
0
        public static WaveAudio hiPassFilter(WaveAudio w, double factor) //0.5
        {
            WaveAudio ret = new WaveAudio(w.getSampleRate(), w.getNumChannels());

            ret.LengthInSamples = w.LengthInSamples;
            for (int ch = 0; ch < w.getNumChannels(); ch++)
            {
                ret.data[ch][0] = w.data[ch][0];
                for (int i = 1; i < ret.data[ch].Length; i++)
                {
                    ret.data[ch][i] = factor * ret.data[ch][i - 1] + (factor) * (w.data[ch][i] - w.data[ch][i - 1]);
                }
            }
            return(ret);
        }
コード例 #10
0
        public static WaveAudio GetSliceSample(WaveAudio wthis, int nStart, int nEnd)
        {
            WaveAudio slice = new WaveAudio(wthis.getSampleRate(), wthis.getNumChannels());

            if (nEnd <= nStart || nEnd > wthis.LengthInSamples || nStart < 0)
            {
                throw new Exception("Invalid slice");
            }
            for (int ch = 0; ch < slice.data.Length; ch++)
            {
                slice.data[ch] = new double[nEnd - nStart];
                Array.Copy(wthis.data[ch], nStart, slice.data[ch], 0, nEnd - nStart);
            }
            return(slice);
        }
コード例 #11
0
        /// <summary>
        /// Create a new sound by changing the speed/pitch of the old sound. 2.0 = an octave up, 1.0 = the same, 0.5 = down an octave
        /// </summary>
        public static WaveAudio ScalePitchAndDuration(WaveAudio w, double factor)
        {
            if (factor < 0)
            {
                throw new ArgumentException("Factor must >= 0");
            }
            WaveAudio res = new WaveAudio(w.getSampleRate(), w.getNumChannels());

            // do operation for all channels
            for (int i = 0; i < w.getNumChannels(); i++)
            {
                res.data[i] = scalePitchAndDurationChannel(w.data[i], factor);
            }

            return(res);
        }
コード例 #12
0
        //could also get continuous with window.


        public static WaveAudio lowPassFilter(WaveAudio w, double factor) //0.5
        {
            //http://en.wikipedia.org/wiki/Low-pass_filter
            WaveAudio ret = new WaveAudio(w.getSampleRate(), w.getNumChannels());

            ret.LengthInSamples = w.LengthInSamples;
            for (int ch = 0; ch < w.getNumChannels(); ch++)
            {
                ret.data[ch][0] = w.data[ch][0];
                for (int i = 1; i < ret.data[ch].Length; i++)
                {
                    ret.data[ch][i] = (1 - factor) * ret.data[ch][i - 1] + (factor) * w.data[ch][i];
                }
            }
            return(ret);
        }
コード例 #13
0
        public static WaveAudio Wahwah(WaveAudio wOriginal, double freq, double depth, double freqofs, double res)
        {
            double startphaseleft = 0;
            double startphaseright = startphaseleft + Math.PI; //note that left and right channels should start pi out of phase
            double freq_scaled = 2.0 * Math.PI * freq / (double)wOriginal.getSampleRate();

            WaveAudio w = wOriginal.Clone();
            if (w.getNumChannels() == 1)
                effect_wahwahaud_impl(w.data[0], startphaseleft, freq_scaled, depth, freqofs, res);
            else
            {
                effect_wahwahaud_impl(w.data[0], startphaseleft, freq_scaled, depth, freqofs, res);
                effect_wahwahaud_impl(w.data[1], startphaseright, freq_scaled, depth, freqofs, res);
            }
            return w;
        }
コード例 #14
0
        public static WaveAudio Phaser(WaveAudio wOriginal, double freq, double fb, int depth, int stages, int drywet)
        {
            double startphaseleft = 0;
            double startphaseright = startphaseleft + Math.PI; //note that left and right channels should start pi out of phase
            double freq_scaled = 2.0 * Math.PI * freq / (double)wOriginal.getSampleRate();

            WaveAudio w = wOriginal.Clone();
            if (w.getNumChannels() == 1)
                effect_phaseraud_impl(w.data[0], freq_scaled, startphaseleft, fb, depth, stages, drywet);
            else
            {
                effect_phaseraud_impl(w.data[0], freq_scaled, startphaseleft, fb, depth, stages, drywet);
                effect_phaseraud_impl(w.data[1], freq_scaled, startphaseright, fb, depth, stages, drywet);
            }
            return w;
        }
コード例 #15
0
        // The following create new audio without modifying original
        public static WaveAudio Concatenate(WaveAudio w1, WaveAudio w2)
        {
            // make sure sample rates match we could be nicer and convert automatically
            if (w1.m_currentSampleRate != w2.m_currentSampleRate) throw new Exception("Sample rates don't match");
            if (w2.getNumChannels() != w2.getNumChannels()) throw new Exception("Number of channels don't match");

            WaveAudio newwave = new WaveAudio(w1.getSampleRate(), w1.getNumChannels());
            newwave.LengthInSamples = w1.LengthInSamples + w2.LengthInSamples;

            for (int ch = 0; ch < w1.getNumChannels(); ch++)
            {
                //          source    sIndex, destination, destIndex,  length
                Array.Copy(w1.data[ch], 0, newwave.data[ch], 0, w1.data[ch].Length);
                Array.Copy(w2.data[ch], 0, newwave.data[ch], w1.data[ch].Length, w2.data[0].Length);
            }
            return newwave;
        }
コード例 #16
0
        // Beat detection, algorithm put together by Ben Fisher after reading some things
        // numbers here are arbitrary, based on what seemed to work ok. I'm sure it could be better.
        public static double GuessBpm(WaveAudio input)
        {
            const double minBpm = 60, maxBpm = 150;

            WaveAudio w = Effects.Derivative(input); // take the derivative of samples.

            // divide samples into chunks of size 1024.
            int    nBufsize       = 1024;
            double chunkframerate = input.getSampleRate() / nBufsize; // the chunks go by at this rate.

            // do first FFT
            double[][] frequencyData = SpectrumContentOverTime(w, 4, nBufsize);

            // create a new signal in time, consisting of the energy at lowest 1/4 freqs of the chunks.
            int slength = (int)Fourier.findNearestPowerOfTwo((uint)frequencyData.Length);

            double[] lowerdata = new double[slength];
            for (int i = 0; i < slength; i++)
            {
                lowerdata[i] = frequencyData[i][0]; // the bottom 1/4 freqs (index 0-255,0Hz to 5512.5Hz  or something ?)
            }
            // now take a second FFT on this new signal. Frequency should be range 0.6 to 2.5 Hz (40 to 150 Bpm).
            double[] reout, imgout;
            RawSamplesToFrequency(lowerdata, out reout, out imgout);

            // only keep track of output inside the range we are interested in
            int minFreqindex = (int)(reout.Length * ((minBpm / 60) / chunkframerate));
            int maxFreqindex = (int)(reout.Length * ((maxBpm / 60) / chunkframerate));

            // find the best candidate
            double highestEnergy = -1; int highestEnergyIndex = -1;

            for (int b = minFreqindex; b < maxFreqindex; b++)
            {
                double magnitude = Math.Sqrt(reout[b] * reout[b] + imgout[b] + imgout[b]);
                if (magnitude > highestEnergy)
                {
                    highestEnergyIndex = b; highestEnergy = magnitude;
                }
            }
            double freqHertz = chunkframerate * (highestEnergyIndex / (double)reout.Length);
            double freqInBpm = freqHertz * 60;

            return(freqInBpm);
        }
コード例 #17
0
ファイル: EffectsAudacity.cs プロジェクト: yazici/yaalp
        public static WaveAudio Wahwah(WaveAudio wOriginal, double freq, double depth, double freqofs, double res)
        {
            double startphaseleft  = 0;
            double startphaseright = startphaseleft + Math.PI; //note that left and right channels should start pi out of phase
            double freq_scaled     = 2.0 * Math.PI * freq / (double)wOriginal.getSampleRate();

            WaveAudio w = wOriginal.Clone();

            if (w.getNumChannels() == 1)
            {
                effect_wahwahaud_impl(w.data[0], startphaseleft, freq_scaled, depth, freqofs, res);
            }
            else
            {
                effect_wahwahaud_impl(w.data[0], startphaseleft, freq_scaled, depth, freqofs, res);
                effect_wahwahaud_impl(w.data[1], startphaseright, freq_scaled, depth, freqofs, res);
            }
            return(w);
        }
コード例 #18
0
ファイル: EffectsAudacity.cs プロジェクト: yazici/yaalp
        public static WaveAudio Phaser(WaveAudio wOriginal, double freq, double fb, int depth, int stages, int drywet)
        {
            double startphaseleft  = 0;
            double startphaseright = startphaseleft + Math.PI; //note that left and right channels should start pi out of phase
            double freq_scaled     = 2.0 * Math.PI * freq / (double)wOriginal.getSampleRate();

            WaveAudio w = wOriginal.Clone();

            if (w.getNumChannels() == 1)
            {
                effect_phaseraud_impl(w.data[0], freq_scaled, startphaseleft, fb, depth, stages, drywet);
            }
            else
            {
                effect_phaseraud_impl(w.data[0], freq_scaled, startphaseleft, fb, depth, stages, drywet);
                effect_phaseraud_impl(w.data[1], freq_scaled, startphaseright, fb, depth, stages, drywet);
            }
            return(w);
        }
コード例 #19
0
            public WaveAudio doModify(WaveAudio src, int bufsize)
            {
                WaveAudio wout = new WaveAudio(src.getSampleRate(), 1);

                wout.LengthInSamples = src.LengthInSamples;

                //reuse the buffers.
                double[] ffreqmaghalfout = new double[bufsize / 2], ffreqanghalfout = new double[bufsize / 2];
                double[] ffreqmaghalfin  = new double[bufsize / 2], ffreqanghalfin = new double[bufsize / 2];
                double[] fbuffertime     = new double[bufsize];
                for (int partnum = 0; partnum < src.LengthInSamples / bufsize; partnum++)
                {
                    //copy into buffer.
                    Array.Copy(src.data[0], partnum * bufsize, fbuffertime, 0, bufsize);
                    double[] ffreqreal, ffreqimag;
                    Fourier.RawSamplesToFrequency(fbuffertime, out ffreqreal, out ffreqimag);
                    //we only care about the first half of these results.
                    for (int i = 0; i < bufsize / 2; i++)
                    {
                        ffreqmaghalfin[i] = Math.Sqrt(ffreqreal[i] * ffreqreal[i] + ffreqimag[i] * ffreqimag[i]);
                        ffreqanghalfin[i] = Math.Atan2(ffreqimag[i], ffreqreal[i]);
                    }
                    this.modifyAngular(ffreqmaghalfin, ffreqanghalfin, ffreqmaghalfout, ffreqanghalfout);
                    if (partnum == 0)
                    {
                        this.drawPlots(ffreqmaghalfin, ffreqanghalfin, ffreqmaghalfout, ffreqanghalfout);
                    }
                    for (int i = 0; i < ffreqreal.Length / 2; i++)
                    {
                        ffreqreal[i] = ffreqmaghalfout[i] * Math.Sin(ffreqanghalfout[i]);
                        ffreqimag[i] = ffreqmaghalfout[i] * Math.Cos(ffreqanghalfout[i]);
                    }
                    for (int i = ffreqreal.Length / 2; i < ffreqreal.Length; i++)
                    {
                        ffreqreal[i] = ffreqimag[i] = 0;
                    }
                    double[] fbufout;
                    Fourier.RawFrequencyToSamples(out fbufout, ffreqreal, ffreqimag);
                    Array.Copy(fbufout, 0, wout.data[0], partnum * bufsize, bufsize);
                }
                return(wout);
            }
コード例 #20
0
            public WaveAudio doModify(WaveAudio src, int bufsize)
            {
                WaveAudio wout = new WaveAudio(src.getSampleRate(), 1);

                wout.LengthInSamples = src.LengthInSamples;

                //reuse the buffers.
                double[] ffreqmaghalfout = new double[bufsize / 2], ffreqanghalfout = new double[bufsize / 2];
                double[] ffreqmaghalfin  = new double[bufsize / 2], ffreqanghalfin = new double[bufsize / 2];
                double[] fbuffertime     = new double[bufsize];

                for (int index = 0; index < src.LengthInSamples - bufsize; index += bufsize / overlap)
                {
                    //copy into buffer.
                    Array.Copy(src.data[0], index, fbuffertime, 0, bufsize);
                    double[] ffreqreal, ffreqimag;
                    Fourier.RawSamplesToFrequency(fbuffertime, out ffreqreal, out ffreqimag);
                    //we only care about the first half of these results.
                    for (int i = 0; i < bufsize / 2; i++)
                    {
                        ffreqmaghalfin[i] = Math.Sqrt(ffreqreal[i] * ffreqreal[i] + ffreqimag[i] * ffreqimag[i]);
                        ffreqanghalfin[i] = Math.Atan2(ffreqimag[i], ffreqreal[i]);
                    }
                    this.modifyAngular(ffreqmaghalfin, ffreqanghalfin, ffreqmaghalfout, ffreqanghalfout);
                    for (int i = 0; i < ffreqreal.Length / 2; i++)
                    {
                        ffreqreal[i] = ffreqmaghalfout[i] * Math.Sin(ffreqanghalfout[i]);
                        ffreqimag[i] = ffreqmaghalfout[i] * Math.Cos(ffreqanghalfout[i]);
                    }
                    for (int i = ffreqreal.Length / 2; i < ffreqreal.Length; i++)
                    {
                        ffreqreal[i] = ffreqimag[i] = 0;
                    }
                    double[] fbufout;
                    Fourier.RawFrequencyToSamples(out fbufout, ffreqreal, ffreqimag);
                    WaveAudio ww = new WaveAudio(44100, 1);
                    ww.data[0] = fbufout;
                    //Array.Copy(fbufout, 0, wout.data[0], partnum*bufsize, bufsize);
                    ConstructionUtil.placeAudioRamp(wout, ww, index, (bufsize / overlapRamp));
                }
                return(wout);
            }
コード例 #21
0
        public static void propertytests()
        {
            // these aren't the best tests.
            WaveAudio w1 = new WaveAudio(44100, 2);
            asserteq(w1.data.Length, 2, "channels");
            asserteq(w1.getNumChannels(), 2, "channels");
            assert(w1.data[0] != null && w1.data[1] != null, "channels");
            assert(w1.data[0].Length == 1 && w1.data[1].Length == 1, "004");
            asserteq(w1.getSampleRate(), 44100, "005");

            WaveAudio w1m = new WaveAudio(22050, 1);
            asserteq(w1m.data.Length, 1, "channels");
            assert(w1m.data[0] != null, "channels");
            asserteq(w1m.data[0].Length, 1, "004");
            asserteq(w1m.getSampleRate(), 22050, "005");

            // now set some properties
            w1m.LengthInSamples = 100;
            asserteq(w1m.data[0].Length, 100);
            asserteqf(w1m.LengthInSeconds, 100 / (double)w1m.getSampleRate(), 0.001);
        }
コード例 #22
0
ファイル: WaveAudioOperations.cs プロジェクト: yazici/yaalp
        // The following create new audio without modifying original
        public static WaveAudio Concatenate(WaveAudio w1, WaveAudio w2)
        {
            // make sure sample rates match we could be nicer and convert automatically
            if (w1.m_currentSampleRate != w2.m_currentSampleRate)
            {
                throw new Exception("Sample rates don't match");
            }
            if (w2.getNumChannels() != w2.getNumChannels())
            {
                throw new Exception("Number of channels don't match");
            }

            WaveAudio newwave = new WaveAudio(w1.getSampleRate(), w1.getNumChannels());

            newwave.LengthInSamples = w1.LengthInSamples + w2.LengthInSamples;

            for (int ch = 0; ch < w1.getNumChannels(); ch++)
            {
                //          source    sIndex, destination, destIndex,  length
                Array.Copy(w1.data[ch], 0, newwave.data[ch], 0, w1.data[ch].Length);
                Array.Copy(w2.data[ch], 0, newwave.data[ch], w1.data[ch].Length, w2.data[0].Length);
            }
            return(newwave);
        }
コード例 #23
0
        public static WaveAudio Vibrato(WaveAudio wave, double freq, double width)
        {
            if (width < 0) throw new ArgumentException("Factor must >= 0");
            WaveAudio newwave = new WaveAudio(wave.getSampleRate(), wave.getNumChannels());
            
            // do operation for all channels
            for (int i = 0; i < wave.getNumChannels(); i++)
                newwave.data[i] = vibratoChannel(wave.data[i], wave.getSampleRate(), width, freq);

            return newwave;
        }
コード例 #24
0
        // These work by shifting the signal until it seems to correlate with itself.
        // In other words if the signal looks very similar to (signal shifted 200 samples) than the fundamental period is probably 200 samples
        // Note that the algorithm only works well when there's only one prominent fundamental.
        // This could be optimized by looking at the rate of change to determine a maximum without testing all periods.
        private static double[] detectPitchCalculation(WaveAudio w, double minHz, double maxHz, int nCandidates, int nResolution, PitchDetectAlgorithm algorithm)
        {
            // note that higher frequency means lower period
            int nLowPeriodInSamples = hzToPeriodInSamples(maxHz, w.getSampleRate());
            int nHiPeriodInSamples  = hzToPeriodInSamples(minHz, w.getSampleRate());

            if (nHiPeriodInSamples <= nLowPeriodInSamples)
            {
                throw new Exception("Bad range for pitch detection.");
            }
            if (w.getNumChannels() != 1)
            {
                throw new Exception("Only mono supported.");
            }
            double[] samples = w.data[0];
            if (samples.Length < nHiPeriodInSamples)
            {
                throw new Exception("Not enough samples.");
            }

            // both algorithms work in a similar way
            // they yield an array of data, and then we find the index at which the value is highest.
            double[] results = new double[nHiPeriodInSamples - nLowPeriodInSamples];

            if (algorithm == PitchDetectAlgorithm.Amdf)
            {
                for (int period = nLowPeriodInSamples; period < nHiPeriodInSamples; period += nResolution)
                {
                    double sum = 0;
                    // for each sample, see how close it is to a sample n away. Then sum these.
                    for (int i = 0; i < samples.Length - period; i++)
                    {
                        sum += Math.Abs(samples[i] - samples[i + period]);
                    }

                    double mean = sum / (double)samples.Length;
                    mean *= -1; //somewhat of a hack. We are trying to find the minimum value, but our findBestCandidates finds the max. value.
                    results[period - nLowPeriodInSamples] = mean;
                }
            }
            else if (algorithm == PitchDetectAlgorithm.Autocorrelation)
            {
                for (int period = nLowPeriodInSamples; period < nHiPeriodInSamples; period += nResolution)
                {
                    double sum = 0;
                    // for each sample, find correlation. (If they are far apart, small)
                    for (int i = 0; i < samples.Length - period; i++)
                    {
                        sum += samples[i] * samples[i + period];
                    }

                    double mean = sum / (double)samples.Length;
                    results[period - nLowPeriodInSamples] = mean;
                }
            }

            // find the best indices
            int[] bestIndices = findBestCandidates(nCandidates, ref results); //note findBestCandidates modifies parameter
            // convert back to Hz
            double[] res = new double[nCandidates];
            for (int i = 0; i < nCandidates; i++)
            {
                res[i] = periodInSamplesToHz(bestIndices[i] + nLowPeriodInSamples, w.getSampleRate());
            }
            return(res);
        }
コード例 #25
0
 public static WaveAudio Tremolo(WaveAudio w, double tremfreq, double amp)
 {
     WaveAudio res = new WaveAudio(w.getSampleRate(), w.getNumChannels());
     res.LengthInSamples = w.LengthInSamples;
     double tremeloFreqScale = 2.0 * Math.PI * tremfreq / (double)w.getSampleRate();
     for (int ch=0;ch<w.data.Length; ch++)
     {
         for (int i = 0; i < w.data[ch].Length; i++)
         {
             double val = w.data[ch][i] * (1 + amp * Math.Sin(tremeloFreqScale * i));
             if (val > 1.0) val = 1.0;
             else if (val < -1.0) val = -1.0;
             res.data[ch][i] = val;
         }
     }
     return res;
 }
コード例 #26
0
ファイル: Fourier.cs プロジェクト: Alexrerx/yaalp
        // Beat detection, algorithm put together by Ben Fisher after reading some things
        // numbers here are arbitrary, based on what seemed to work ok. I'm sure it could be better.
        public static double GuessBpm(WaveAudio input)
        {
            const double minBpm = 60, maxBpm = 150;

            WaveAudio w = Effects.Derivative(input); // take the derivative of samples.

            // divide samples into chunks of size 1024.
            int nBufsize = 1024;
            double chunkframerate = input.getSampleRate() / nBufsize; // the chunks go by at this rate.

            // do first FFT
            double[][] frequencyData = SpectrumContentOverTime(w, 4, nBufsize);

            // create a new signal in time, consisting of the energy at lowest 1/4 freqs of the chunks.
            int slength = (int)Fourier.findNearestPowerOfTwo((uint)frequencyData.Length);
            double[] lowerdata = new double[slength];
            for (int i = 0; i < slength; i++)
                lowerdata[i] = frequencyData[i][0]; // the bottom 1/4 freqs (index 0-255,0Hz to 5512.5Hz  or something ?)

            // now take a second FFT on this new signal. Frequency should be range 0.6 to 2.5 Hz (40 to 150 Bpm).
            double[] reout, imgout;
            RawSamplesToFrequency(lowerdata, out reout, out imgout);

            // only keep track of output inside the range we are interested in
            int minFreqindex = (int)(reout.Length * ((minBpm / 60) / chunkframerate));
            int maxFreqindex = (int)(reout.Length * ((maxBpm / 60) / chunkframerate));

            // find the best candidate
            double highestEnergy = -1; int highestEnergyIndex = -1;
            for (int b = minFreqindex; b < maxFreqindex; b++)
            {
                double magnitude = Math.Sqrt(reout[b] * reout[b] + imgout[b] + imgout[b]);
                if (magnitude > highestEnergy) { highestEnergyIndex = b; highestEnergy = magnitude; }
            }
            double freqHertz = chunkframerate * (highestEnergyIndex / (double)reout.Length);
            double freqInBpm = freqHertz * 60;
            return freqInBpm;
        }
コード例 #27
0
        static void iotests_perfile(string strFilename, int nBits, int nChannels, int nRate)
        {
            WaveAudio w01 = new WaveAudio(strFilename);
            asserteq(w01.getNumChannels(), nChannels);
            asserteq(w01.getSampleRate(), nRate, "011");
            asserteqf(w01.LengthInSamples, 90725 * (nRate / 22050), 1.0, "012"); //note give 1.0 tolerance
            asserteqf(w01.LengthInSeconds, 4.1145124, "013");

            asserteq(w01.data.Length, nChannels);
            asserteq(w01.data[0].Length, w01.LengthInSamples);
            for (int i = 0; i < nChannels; i++)
                asserteq(w01.data[i].Length, w01.LengthInSamples);

            // test converting to other rates / quality
            w01.SaveWaveFile("..\\..\\testout\\o_" + nRate + "_" + nBits + "_" + nRate + "_" + nChannels + ".wav", nBits);
            nBits = (nBits == 8) ? 16 : 8;
            w01.SaveWaveFile("..\\..\\testout\\ot_" + nRate + "_" + nBits + "_" + nRate + "_" + nChannels + ".wav", nBits);
        }
コード例 #28
0
            public WaveAudio doModify(WaveAudio src, int bufsize)
            {
                WaveAudio wout = new WaveAudio(src.getSampleRate(), 1);
                wout.LengthInSamples = src.LengthInSamples;

                //reuse the buffers.
                double[] freqRealIn=new double[bufsize/2], freqRealOut=new double[bufsize/2];
                double[] freqImagIn=new double[bufsize/2], freqImagOut=new double[bufsize/2];
                double[] fbuffertime = new double[bufsize];
                for (int partnum=0; partnum<src.LengthInSamples/bufsize; partnum++)
                {
                    //copy into buffer.
                    Array.Copy(src.data[0], partnum*bufsize, fbuffertime, 0, bufsize);
                    double[] ffreqreal, ffreqimag;
                    Fourier.RawSamplesToFrequency(fbuffertime, out ffreqreal, out ffreqimag);
                    //we only care about the first half of these results.
                    Array.Copy(ffreqreal, freqRealIn, bufsize/2);
                    Array.Copy(ffreqimag, freqImagIn, bufsize/2);
                    this.modifyRectangular(freqRealIn, freqImagIn, freqRealOut, freqImagOut);
                    Array.Copy(freqRealOut, ffreqreal, bufsize/2);
                    Array.Copy(freqImagOut, ffreqimag, bufsize/2);
                    double[] fbufout;
                    Fourier.RawFrequencyToSamples(out fbufout, ffreqreal, ffreqimag);
                    Array.Copy(fbufout, 0, wout.data[0], partnum*bufsize, bufsize);
                }
                return wout;
            }
コード例 #29
0
            public WaveAudio doModify(WaveAudio src, int bufsize)
            {
                WaveAudio wout = new WaveAudio(src.getSampleRate(), 1);
                wout.LengthInSamples = src.LengthInSamples;

                //reuse the buffers.
                double[] ffreqmaghalfout=new double[bufsize/2], ffreqanghalfout=new double[bufsize/2];
                double[] ffreqmaghalfin=new double[bufsize/2], ffreqanghalfin=new double[bufsize/2];
                double[] fbuffertime = new double[bufsize];
                for (int partnum=0; partnum<src.LengthInSamples/bufsize; partnum++)
                {
                    //copy into buffer.
                    Array.Copy(src.data[0], partnum*bufsize, fbuffertime, 0, bufsize);
                    double[] ffreqreal, ffreqimag;
                    Fourier.RawSamplesToFrequency(fbuffertime, out ffreqreal, out ffreqimag);
                    //we only care about the first half of these results.
                    for (int i=0; i<bufsize/2; i++)
                    {
                        ffreqmaghalfin[i] = Math.Sqrt(ffreqreal[i]*ffreqreal[i]+ffreqimag[i]*ffreqimag[i]);
                        ffreqanghalfin[i] = Math.Atan2(ffreqimag[i], ffreqreal[i]);
                    }
                    this.modifyAngular(ffreqmaghalfin, ffreqanghalfin, ffreqmaghalfout, ffreqanghalfout);
                    if (partnum==0) this.drawPlots(ffreqmaghalfin, ffreqanghalfin, ffreqmaghalfout, ffreqanghalfout);
                    for (int i=0; i<ffreqreal.Length/2; i++)
                    {
                        ffreqreal[i] = ffreqmaghalfout[i]*Math.Sin(ffreqanghalfout[i]);
                        ffreqimag[i] = ffreqmaghalfout[i]*Math.Cos(ffreqanghalfout[i]);
                    }
                    for (int i=ffreqreal.Length/2; i<ffreqreal.Length; i++)
                    {
                        ffreqreal[i]=ffreqimag[i]=0;
                    }
                    double[] fbufout;
                    Fourier.RawFrequencyToSamples(out fbufout, ffreqreal, ffreqimag);
                    Array.Copy(fbufout, 0, wout.data[0], partnum*bufsize, bufsize);
                }
                return wout;
            }
コード例 #30
0
 public WaveAudio doModify(WaveAudio src, int bufsize) 
 {
     WaveAudio wout = new WaveAudio(src.getSampleRate(), 1);
     wout.LengthInSamples = src.LengthInSamples;
     
     //reuse the buffers.
     double[] ffreqmaghalfout=new double[bufsize/2], ffreqanghalfout=new double[bufsize/2];
     double[] ffreqmaghalfin=new double[bufsize/2], ffreqanghalfin=new double[bufsize/2];
     double[] fbuffertime = new double[bufsize];
     
     for (int index=0; index<src.LengthInSamples-bufsize; index+=bufsize/overlap)
     {
         //copy into buffer.
         Array.Copy(src.data[0], index, fbuffertime, 0, bufsize);
         double[] ffreqreal, ffreqimag;
         Fourier.RawSamplesToFrequency(fbuffertime, out ffreqreal, out ffreqimag);
         //we only care about the first half of these results.
         for (int i=0; i<bufsize/2; i++)
         {
             ffreqmaghalfin[i] = Math.Sqrt(ffreqreal[i]*ffreqreal[i]+ffreqimag[i]*ffreqimag[i]);
             ffreqanghalfin[i] = Math.Atan2(ffreqimag[i], ffreqreal[i]);
         }
         this.modifyAngular(ffreqmaghalfin, ffreqanghalfin, ffreqmaghalfout, ffreqanghalfout);
         for (int i=0; i<ffreqreal.Length/2; i++)
         {
             ffreqreal[i] = ffreqmaghalfout[i]*Math.Sin(ffreqanghalfout[i]);
             ffreqimag[i] = ffreqmaghalfout[i]*Math.Cos(ffreqanghalfout[i]);
         }
         for (int i=ffreqreal.Length/2; i<ffreqreal.Length; i++)
         {
             ffreqreal[i]=ffreqimag[i]=0;
         }
         double[] fbufout;
         Fourier.RawFrequencyToSamples(out fbufout, ffreqreal, ffreqimag);
         WaveAudio ww = new WaveAudio(44100, 1);
         ww.data[0] = fbufout;
         //Array.Copy(fbufout, 0, wout.data[0], partnum*bufsize, bufsize);
         ConstructionUtil.placeAudioRamp(wout, ww, index, (bufsize/overlapRamp));
     }
     return wout;
 }
コード例 #31
0
ファイル: WaveAudioOperations.cs プロジェクト: yazici/yaalp
        // Helper function. It's long and gross because either sound could be longer.
        // I could be more clever and use Math.Max / Min to have WaveAudio longer, WaveAudio shorter
        // , but at least now it is readable
        /// <summary>
        /// Element-wise combination of two audio clips. For example, adding, or modulation.
        /// </summary>
        internal static WaveAudio elementWiseCombination(WaveAudio w1, WaveAudio w2, ElementWiseCombinationFn fn)
        {
            if (w1.m_currentSampleRate != w2.m_currentSampleRate)
            {
                throw new Exception("Sample rates don't match");
            }
            if (w1.getNumChannels() != w2.getNumChannels())
            {
                throw new Exception("Number of channels don't match");
            }

            WaveAudio newwave = new WaveAudio(w1.getSampleRate(), w1.getNumChannels());

            newwave.LengthInSamples = Math.Max(w1.LengthInSamples, w2.LengthInSamples);
            double val;

            for (int ch = 0; ch < w1.getNumChannels(); ch++)
            {
                if (w1.LengthInSamples > w2.LengthInSamples)
                {
                    for (int i = 0; i < w1.LengthInSamples; i++)
                    {
                        if (i >= w2.LengthInSamples)
                        {
                            val = fn(w1.data[ch][i], 0);
                        }
                        else
                        {
                            val = fn(w1.data[ch][i], w2.data[ch][i]);
                        }

                        if (val > 1.0)
                        {
                            val = 1.0;
                        }
                        else if (val < -1.0)
                        {
                            val = -1.0;
                        }
                        newwave.data[ch][i] = val;
                    }
                }
                else
                {
                    for (int i = 0; i < w2.LengthInSamples; i++)
                    {
                        if (i >= w1.LengthInSamples)
                        {
                            val = fn(0, w2.data[ch][i]);
                        }
                        else
                        {
                            val = fn(w1.data[ch][i], w2.data[ch][i]);
                        }

                        if (val > 1.0)
                        {
                            val = 1.0;
                        }
                        else if (val < -1.0)
                        {
                            val = -1.0;
                        }
                        newwave.data[ch][i] = val;
                    }
                }
            }
            return(newwave);
        }
コード例 #32
0
        // Helper function. It's long and gross because either sound could be longer.
        // I could be more clever and use Math.Max / Min to have WaveAudio longer, WaveAudio shorter
        // , but at least now it is readable 
        /// <summary>
        /// Element-wise combination of two audio clips. For example, adding, or modulation.
        /// </summary>
        internal static WaveAudio elementWiseCombination(WaveAudio w1, WaveAudio w2, ElementWiseCombinationFn fn)
        {
            if (w1.m_currentSampleRate != w2.m_currentSampleRate) throw new Exception("Sample rates don't match");
            if (w1.getNumChannels() != w2.getNumChannels()) throw new Exception("Number of channels don't match");

            WaveAudio newwave = new WaveAudio(w1.getSampleRate(), w1.getNumChannels());
            newwave.LengthInSamples = Math.Max(w1.LengthInSamples, w2.LengthInSamples);
            double val;
            for (int ch = 0; ch < w1.getNumChannels(); ch++)
            {
                if (w1.LengthInSamples > w2.LengthInSamples)
                {
                    for (int i = 0; i < w1.LengthInSamples; i++)
                    {
                        if (i >= w2.LengthInSamples)
                            val = fn(w1.data[ch][i], 0);
                        else
                            val = fn(w1.data[ch][i], w2.data[ch][i]);

                        if (val > 1.0) val = 1.0;
                        else if (val < -1.0) val = -1.0;
                        newwave.data[ch][i] = val;
                    }
                }
                else
                {
                    for (int i = 0; i < w2.LengthInSamples; i++)
                    {
                        if (i >= w1.LengthInSamples)
                            val = fn(0, w2.data[ch][i]);
                        else
                            val = fn(w1.data[ch][i], w2.data[ch][i]);

                        if (val > 1.0) val = 1.0;
                        else if (val < -1.0) val = -1.0;
                        newwave.data[ch][i] = val;
                    }
                }
            }
            return newwave;
        }