Beispiel #1
0
        static public bool Shift(byte[] waveData, float pitchShiftCoeff)
        {
            // pitchShiftCoeff: 0.5f - 2f
            // 0.5f - на одну октаву ниже
            // 2f - на одну октаву выше

            WavePCMParser.WaveInfo waveInfo;

            if (!WavePCMParser.GetWaveInfo(waveData, out waveInfo))
            {
                return(false);
            }

            // преобразования только для 16 бит
            if (waveInfo.bytesPerSample != 2)
            {
                return(false);
            }

            float[][] channelsData = ConvertWaveDataToFloatArrays(waveData, ref waveInfo);

            foreach (var channelData in channelsData)
            {
                PitchShifter.PitchShift(pitchShiftCoeff, waveInfo.samplesCount, 1024, 10, waveInfo.sampleRate, channelData);
            }

            ConvertFloatArraysToWaveData(waveData, ref waveInfo, channelsData);

            return(true);
        }
        //+makeLower(): returns a string for a lower pitch version of the file
        public String makeLower(String file, String newFile, float pitchShift)
        {
            // Read header, data and channels as separated data

            // Normalized data stores. Store values in the format -1.0 to 1.0
            byte[] waveheader = null;
            byte[] wavedata   = null;

            int sampleRate = 0;

            float[] in_data_l = null;
            float[] in_data_r = null;

            GetWaveData(file, out waveheader, out wavedata, out sampleRate, out in_data_l, out in_data_r);

            //
            // Apply Pitch Shifting
            //

            if (in_data_l != null)
            {
                PitchShifter.PitchShift(pitchShift, in_data_l.Length, (long)1024, (long)10, sampleRate, in_data_l);
            }

            if (in_data_r != null)
            {
                PitchShifter.PitchShift(pitchShift, in_data_r.Length, (long)1024, (long)10, sampleRate, in_data_r);
            }

            //
            // Time to save the processed data
            //

            // Backup wave data
            byte[] copydata = new byte[wavedata.Length];
            Array.Copy(wavedata, copydata, wavedata.Length);

            GetWaveData(in_data_l, in_data_r, ref wavedata);

            // Save modified wavedata

            string targetFilePath = newFile;

            if (File.Exists(targetFilePath))
            {
                File.Delete(targetFilePath);
            }

            using (BinaryWriter writer = new BinaryWriter(File.OpenWrite(targetFilePath)))
            {
                writer.Write(waveheader);
                writer.Write(wavedata);
            }

            return(newFile);
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="reverb"></param>
        public void ApplyPitch(float pitch)
        {
            if (_waveSource == null)
            {
                return;
            }

            var pitchPass = new PitchShifter(_waveSource.ToSampleSource());

            pitchPass.PitchShiftFactor = pitch;

            InitPlayback(pitchPass.ToWaveSource());
        }
        /// <summary>
        /// Bake a new AudioClip from the original source with the given parameters
        /// </summary>
        /// <returns>The new AudioClip baked with the given parameters</returns>
        public AudioClip Bake()
        {
            // Create a new AudioClip
            AudioClip ajusted = AudioClip.Create("Footstep", source.samples, source.channels, source.frequency, false);

            // Read the samples from the source clip
            float[] samples = new float[source.samples];
            source.GetData(samples, 0);

            // Pitch the samples acording to the given pitch
            Profiler.BeginSample("Bake single AudioClip");
            PitchShifter.PitchShift(pitch, samples.Length, 32, 32, ajusted.frequency, samples);
            Profiler.EndSample();

            // Normalize the audio
            AudioNormalizer.Normalize(samples);

            // Write the ajusted samples to the new AudioClip
            ajusted.SetData(samples, 0);

            return(ajusted);
        }
Beispiel #5
0
        public void Open(string filename, MMDevice device)
        {
            CleanupPlayback();

            _peakMeter = CodecFactory.Instance.GetCodec(filename)
                         .ToSampleSource()
                         .AppendSource(Equalizer.Create10BandEqualizer, out _equalizer)
                         .ToMono()
                         .AppendSource(src => new PeakMeter(src));
            _pitchShifter = _peakMeter
                            .AppendSource(src => new PitchShifter(src));

            _sampleSource = _pitchShifter
                            .AppendSource(x => new BiQuadFilterSource(x));
            _soundOut = new WasapiOut()
            {
                Latency = 100, Device = device
            };
            _soundOut.Initialize(_sampleSource.ToWaveSource());
            if (PlaybackStopped != null)
            {
                _soundOut.Stopped += PlaybackStopped;
            }
        }