Exemple #1
0
        /// <summary>
        /// ONLY WORKS WITH FLOAT DATA, AND 2 CHANNELS
        /// </summary>
        /// <param name="input"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public byte[] inputResample(byte[] input, int count)
        {
            //provider = new RawSourceWaveStream(input, 0, count, inputFormat);
            provider.AddSamples(input, 0, input.Length);

            int outSamples = (int)Math.Floor(((double)outputFormat.SampleRate / (double)inputFormat.SampleRate) * count);

            outFloat = new float[outSamples];
            resampler.Read(outFloat, 0, outFloat.Length);
            return(floats2bytes(outFloat));
        }
        public void CanResampleUpAndDown(int from, int to)
        {
            var channels  = 1;
            var offset    = CreateSignalGenerator(@from, channels);
            var resampler = new WdlResamplingSampleProvider(offset, to);
            //string fileName = "From {0}"
            //WaveFileWriter.CreateWaveFile16(;
            var buffer = new float[to * channels];

            Debug.WriteLine(String.Format("From {0} to {1}", from, to));
            for (int n = 0; n < 10; n++)
            {
                var read = resampler.Read(buffer, 0, buffer.Length);
                Debug.WriteLine(String.Format("read {0}", read));
            }
        }
Exemple #3
0
        public CachedVorbisSound(string audioFileName)
        {
            using (var audioFileReader = new VorbisWaveReader(audioFileName))
            {
                var resample = new WdlResamplingSampleProvider(audioFileReader, 44100);

                WaveFormat = resample.WaveFormat;
                var wholeFile  = new List <float>();
                var readBuffer = new float[resample.WaveFormat.SampleRate * resample.WaveFormat.Channels];
                int samplesRead;
                while ((samplesRead = resample.Read(readBuffer, 0, readBuffer.Length)) > 0)
                {
                    wholeFile.AddRange(readBuffer.Take(samplesRead));
                }
                AudioData = wholeFile.ToArray();
            }
        }
Exemple #4
0
        public CachedSound(string audioFileName)
        {
            using (var audioFileReader = new AudioFileReader(audioFileName))
            {
                int outRate   = 44100;
                var resampler = new WdlResamplingSampleProvider(audioFileReader, outRate);

                WaveFormat = resampler.WaveFormat;
                var wholeFile  = new List <float>((int)(audioFileReader.Length / 4));
                var readBuffer = new float[WaveFormat.SampleRate * WaveFormat.Channels];
                int samplesRead;
                while ((samplesRead = resampler.Read(readBuffer, 0, readBuffer.Length)) > 0)
                {
                    wholeFile.AddRange(readBuffer.Take(samplesRead));
                }
                AudioData = wholeFile.ToArray();
            }
        }
Exemple #5
0
        private static Sound LoadSoundFromFile(string filePath, bool loop)
        {
            using var audioFileReader = new AudioFileReader(filePath);
            var   resampler = new WdlResamplingSampleProvider(audioFileReader, 44100);
            Sound snd       = new Sound
            {
                WaveFormat = resampler.WaveFormat
            };
            List <float> wholeFile = new List <float>((int)(audioFileReader.Length / 4));

            float[] readBuffer = new float[resampler.WaveFormat.SampleRate * resampler.WaveFormat.Channels];
            int     samplesRead;

            while ((samplesRead = resampler.Read(readBuffer, 0, readBuffer.Length)) > 0)
            {
                wholeFile.AddRange(readBuffer.Take(samplesRead));
            }
            snd.AudioData = wholeFile.ToArray();
            snd.Loop      = loop;
            return(snd);
        }
        private void FetchAudioFromInputBuffer()
        {
            if (inputBuffer != null)
            {
                double samplesToFetch = (inputBuffer.BufferedDuration.TotalMilliseconds - 50d) * ((internalFormatStereo.SampleRate * internalFormatStereo.Channels) / 1000d);

                // CHECK FOR WHOLE NUMBER
                if (samplesToFetch - (int)samplesToFetch != 0)
                {
                    throw new ApplicationException("FetchAudioFromInputBuffer cannot fetch fractional samples. Adjust either internal sample rate or main server interval.");
                }

                // ONLY FETCH AUDIO IF BUFFER CONAINS ENOUGH
                if (inputBuffer.BufferedBytes > samplesToFetch && inputBuffer.BufferedDuration.TotalMilliseconds > 100)
                {
                    float[] input = new float[(int)samplesToFetch];
                    inputResampler.Read(input, 0, (int)samplesToFetch);

                    initialAudioProccessing(Converters.floats2bytes(input));
                }
            }
        }
Exemple #7
0
        public static void MixToFile(IList <Channel> channels, string filename, bool applyReplayGain)
        {
            Console.WriteLine("Mixing per-channel data...");

            // We make new readers...
            var readers = new List <WaveFileReader>();

            try
            {
                readers.AddRange(channels.Select(c => new WaveFileReader(c.Filename)));

                if (applyReplayGain)
                {
                    Console.WriteLine("Computing ReplayGain...");
                    // We read it in a second at a time, to calculate Replay Gains
                    var       mixer      = new MixingSampleProvider(readers.Select(x => x.ToSampleProvider().ToStereo()));
                    const int sampleRate = 44100;
                    var       resampler  = new WdlResamplingSampleProvider(mixer, sampleRate);
                    // We use a 1s buffer...
                    var buffer     = new float[sampleRate * 2]; // *2 for stereo
                    var replayGain = new TrackGain(sampleRate);
                    for (;;)
                    {
                        int numRead = resampler.Read(buffer, 0, buffer.Length);
                        if (numRead == 0)
                        {
                            break;
                        }

                        // And analyze
                        replayGain.AnalyzeSamples(buffer, numRead);
                    }

                    // The +3 is to make it at "YouTube loudness", which is a lot louder than ReplayGain defaults to.
                    // TODO make this configurable?
                    var gain = replayGain.GetGain() + 3;

                    Console.WriteLine($"Applying ReplayGain ({gain:N} dB) and saving to {filename}");

                    // Reset the readers
                    foreach (var reader in readers)
                    {
                        reader.Position = 0;
                    }

                    // We make a new mixer just in case resetting the previous one is problematic...
                    mixer = new MixingSampleProvider(readers.Select(x => x.ToSampleProvider().ToStereo()));
                    var amplifier = new VolumeSampleProvider(mixer)
                    {
                        Volume = (float)Math.Pow(10, gain / 20)
                    };
                    WaveFileWriter.CreateWaveFile(filename, amplifier.ToWaveProvider());
                }
                else
                {
                    var mixer = new MixingSampleProvider(readers.Select(x => x.ToSampleProvider().ToStereo()));
                    WaveFileWriter.CreateWaveFile(filename, mixer.ToWaveProvider());
                }
            }
            finally
            {
                foreach (var reader in readers)
                {
                    reader.Dispose();
                }
            }
        }