Пример #1
0
        public static void PreprocessWithAbsEnvelope(string filePath)
        {
            Console.WriteLine($"Pre-processing with Abs and Envelope of file {filePath}");

            using (var afr = new FileStream(filePath, FileMode.Open))
            {
                // create a signal file
                var waveFile = new WaveFile(afr);

                DiscreteSignal signal = waveFile[Channels.Left];
                for (int i = 0; i < signal.Length; i++)
                {
                    signal.Samples[i] = Math.Abs(signal.Samples[i]);
                }

                // smooth signal with envelope
                DiscreteSignal envelope = Operation.Envelope(signal);

                using (var stream = new FileStream($"{DATA_PATH}/abs-envelope.wav", FileMode.Create))
                {
                    var signalFile = new WaveFile(envelope);
                    signalFile.SaveTo(stream);
                }
            }
        }
Пример #2
0
        private void SaveToFile()
        {
            using (var tempStream = new FileStream(TempFileName, FileMode.Open))
                using (var br = new BinaryReader(tempStream))
                {
                    var samples = new float[tempStream.Length / sizeof(float)];

                    for (var i = 0; i < samples.Length; i++)
                    {
                        samples[i] = br.ReadSingle();
                    }

                    var waveFile = new WaveFile(new DiscreteSignal(_samplingRate, samples));

                    using (var outputStream = new FileStream(OutputFileName, FileMode.Create))
                    {
                        waveFile.SaveTo(outputStream);
                    }
                }

            using (var file = new Java.IO.File(TempFileName))
            {
                file.Delete();
            }
        }
Пример #3
0
        private async void playToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (_waveFileName == null || _isPaused)
            {
                return;
            }

            // Play after filter
            string wavePlay = "wavPlay.wav";

            try
            {
                using (var stream = new FileStream(wavePlay, FileMode.Create))
                {
                    var waveFile = new WaveFile(_signal);
                    waveFile.SaveTo(stream);
                }
            }
            catch (IOException ex)
            {
                Console.WriteLine("Error reading from {0}. Message = {1}", wavePlay, ex.Message);
            }

            _hasStartedPlaying = true;

            await _player.PlayAsync(wavePlay);

            _hasStartedPlaying = false;
        }
Пример #4
0
        public static void PreprocessWithEnvelope(string filePath)
        {
            Console.WriteLine($"Pre-processing for word count of file {filePath}");

            using (var afr = new FileStream(filePath, FileMode.Open))
            {
                // create a signal file
                var            waveFile = new WaveFile(afr);
                DiscreteSignal signal   = waveFile[Channels.Left];

                // smooth signal with envelope
                DiscreteSignal envelope = Operation.Envelope(signal);

                // instance of SimpleGate
                SimpleGate sg = new SimpleGate(30, 30, envelope.SamplingRate);

                for (int i = 0; i < envelope.Length; i++)
                {
                    double inValue = envelope.Samples[i];

                    sg.Process(ref inValue);

                    envelope.Samples[i] = (float)inValue;
                }

                using (var stream = new FileStream($"{DATA_PATH}/envelope.wav", FileMode.Create))
                {
                    var signalFile = new WaveFile(envelope);
                    signalFile.SaveTo(stream);
                }
            }
        }
Пример #5
0
        public static void PreprocessForWordCount(string filePath)
        {
            Console.WriteLine($"Pre-processing for word count of file {filePath}");

            using (var afr = new FileStream(filePath, FileMode.Open))
            {
                // create a signal file
                var            waveFile = new WaveFile(afr);
                DiscreteSignal signal   = waveFile[Channels.Left];

                // smooth signal via moving average filter
                var            maFilter       = new MovingAverageFilter(19);
                DiscreteSignal smoothedSignal = maFilter.ApplyTo(signal);

                // instance of SimpleGate
                SimpleGate sg = new SimpleGate(30, 30, smoothedSignal.SamplingRate);

                for (int i = 0; i < smoothedSignal.Length; i++)
                {
                    double inValue = smoothedSignal.Samples[i];

                    sg.Process(ref inValue);

                    smoothedSignal.Samples[i] = (float)inValue;
                }

                using (var stream = new FileStream($"{DATA_PATH}/preprocessed.wav", FileMode.Create))
                {
                    var signalFile = new WaveFile(smoothedSignal);
                    signalFile.SaveTo(stream);
                }
            }
        }
Пример #6
0
        public async void PlayAsync(EffectsProperties properties)
        {
            if (waveFile != null)
            {
                // Convert wave file to discrete signal
                var signal = delay.WaveToSignal(waveFile);

                // Process delay
                DiscreteSignal delayProcessed = delay.Process(
                    signal,
                    properties.DelaySamples, properties.DelayGain,
                    properties.DelayVolume, properties.DelayBypass);

                // Process flanger
                DiscreteSignal flangerProcessed = flanger.Process(
                    delayProcessed,
                    properties.FlangerSamples, properties.FlangerGain,
                    properties.FlangerVolume, properties.FlangerSpeed,
                    properties.FlangerBypass);

                // Process chorus
                DiscreteSignal chorusProcessed = chorus.Process(
                    flangerProcessed,
                    properties.ChorusMiliseconds, properties.ChorusGain1,
                    properties.ChorusGain2, properties.ChorusVolume,
                    properties.ChorusBypass);

                // Process distortion
                DiscreteSignal distProcessed = distortion.Process(
                    chorusProcessed,
                    properties.DistGain, properties.DistDistortion,
                    properties.DistVolume, properties.DistBypass
                    );

                // Redirect last proccesed effect to output
                var output = distProcessed;

                if (audioPlayer == null)
                {
                    audioPlayer = new MciAudioPlayer();
                }

                audioPlayer.Stop();

                // Save processed file to stream
                var processedFileName = string.Format("{0}.wav", Guid.NewGuid());
                using (var stream = new FileStream(processedFileName, FileMode.Create))
                {
                    var waveFile = new WaveFile(output);
                    waveFile.SaveTo(stream);
                }

                await audioPlayer.PlayAsync(processedFileName);

                // cleanup temporary file
                File.Delete(processedFileName);
            }
        }
Пример #7
0
        public static void TestSimpleGate(string filePath)
        {
            Console.WriteLine($"Testing 'SingleGate' class with file {filePath}");

            using (var afr = new FileStream(filePath, FileMode.Open))
            {
                // create a signal file
                var            waveFile = new WaveFile(afr);
                DiscreteSignal signal   = waveFile[Channels.Left];

                List <Tuple <int, int> > attackReleaseValues = new List <Tuple <int, int> > {
                    new Tuple <int, int>(5, 5),
                    new Tuple <int, int>(5, 10),
                    new Tuple <int, int>(10, 5),
                    new Tuple <int, int>(10, 10),
                    new Tuple <int, int>(10, 15),
                    new Tuple <int, int>(15, 10),
                    new Tuple <int, int>(15, 15),
                    new Tuple <int, int>(15, 20),
                    new Tuple <int, int>(20, 15),
                    new Tuple <int, int>(20, 20),
                    new Tuple <int, int>(20, 25),
                    new Tuple <int, int>(25, 20),
                    new Tuple <int, int>(25, 25),
                };

                // for each attack/release pair combination
                for (int i = 0; i < attackReleaseValues.Count; i++)
                {
                    int attack, release;
                    attack  = attackReleaseValues[i].Item1;
                    release = attackReleaseValues[i].Item2;

                    // instance of SimpleGate
                    SimpleGate sg = new SimpleGate(attack, release, signal.SamplingRate);

                    for (int j = 0; j < signal.Length; j++)
                    {
                        double inValue = signal.Samples[j];
                        sg.Process(ref inValue);
                        signal.Samples[j] = (float)inValue;
                    }

                    using (var stream = new FileStream($"{DATA_PATH}/gate-{attack}-{release}.wav", FileMode.Create))
                    {
                        var signalFile = new WaveFile(signal);
                        signalFile.SaveTo(stream);
                    }
                }
            }
        }
Пример #8
0
        public async Task PlayAsync(DiscreteSignal signal, int startPos = 0, int endPos = -1, short bitDepth = 16)
        {
            var stream = new MemoryStream();
            var wave   = new WaveFile(signal, bitDepth);

            wave.SaveTo(stream);

            stream = new MemoryStream(stream.ToArray());

            _player?.Dispose();
            _player = new SoundPlayer(stream);
            _player.Stream.Seek(0, SeekOrigin.Begin);
            _player.Play();
        }
Пример #9
0
        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var sfd = new SaveFileDialog();

            if (sfd.ShowDialog() != DialogResult.OK)
            {
                return;
            }

            using (var stream = new FileStream(sfd.FileName, FileMode.Create))
            {
                var waveFile = new WaveFile(_signal2);
                waveFile.SaveTo(stream);
            }
        }
Пример #10
0
        public static void WordCountSignalPreprocessing(string filePath)
        {
            Console.WriteLine($"Pre-processing for word count of file {filePath}");

            using (var afr = new FileStream(filePath, FileMode.Open))
            {
                // create a signal file
                var            waveFile = new WaveFile(afr);
                DiscreteSignal signal   = waveFile[Channels.Left];

                DiscreteSignal preprocessed = ManualWordCount.PreprocessAudio(signal);

                using (var stream = new FileStream($"{DATA_PATH}/word-count-preprocessed.wav", FileMode.Create))
                {
                    var signalFile = new WaveFile(preprocessed);
                    signalFile.SaveTo(stream);
                }
            }
        }
Пример #11
0
        public MemoryStream GenerateWAV()
        {
            var signal = GetSignal();

            stopwatch.Restart();

            MemoryStream output;

            using (MemoryStream stream = new MemoryStream())
            {
                var waveFile = new WaveFile(signal, 32);
                waveFile.SaveTo(stream);
                output = new MemoryStream(stream.ToArray());
            }

            OnProgressChanged(new GenerationProgressChangedEventArgs(5, 5, "MemoryStream writing", stopwatch.Elapsed));
            stopwatch.Stop();

            return(output);
        }
Пример #12
0
        private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var sfd = new SaveFileDialog()
            {
                Filter       = "wav | *.wav | mp3 | *.mp3",
                AddExtension = true,
                DefaultExt   = "wav"
            };

            if (sfd.ShowDialog() != DialogResult.OK)
            {
                return;
            }

            using (var stream = new FileStream(sfd.FileName, FileMode.Create))
            {
                var waveFile = new WaveFile(_signal * 2);
                waveFile.SaveTo(stream);
            }
        }
Пример #13
0
        public static DiscreteSignal PreprocessAudio(DiscreteSignal signal)
        {
            // smooth signal via moving average filter
            var maFilter = new MovingAverageFilter(19);
            DiscreteSignal smoothedSignal = maFilter.ApplyTo(signal);



            using (var stream = new FileStream("smoothedSignal.wav", FileMode.Create))
            {
                var signalFile = new WaveFile(smoothedSignal);
                signalFile.SaveTo(stream);
            }



            // pre-process signal with SimpleGate
            SimpleGate sg = new SimpleGate(30, 30, smoothedSignal.SamplingRate);

            // apply for each measured sample
            for (int i = 0; i < smoothedSignal.Length; i++)
            {
                double inValue = smoothedSignal.Samples[i];

                sg.Process(ref inValue);

                smoothedSignal.Samples[i] = (float)inValue;
            }

            // apply envelope operation
            DiscreteSignal envelopeSignal = Operation.Envelope(smoothedSignal);

            using (var stream = new FileStream("envelopeSignal.wav", FileMode.Create))
            {
                var signalFile = new WaveFile(envelopeSignal);
                signalFile.SaveTo(stream);
            }

            return envelopeSignal;
        }
Пример #14
0
        public static void TestMovingAverageFilter(string filePath)
        {
            Console.WriteLine($"Testing 'MovingAverage' filters on file {filePath}");

            using (var afr = new FileStream(filePath, FileMode.Open))
            {
                // create a signal file
                var            waveFile = new WaveFile(afr);
                DiscreteSignal signal   = waveFile[Channels.Left];

                int[] wSizes = new int[] { 3, 7, 11, 15, 19, 23, 27, 31, 35, 39, 43, 47, 51 };

                for (int i = 0; i < wSizes.Length; i++)
                {
                    // build moving average filter of specified window size
                    int wSize    = wSizes[i];
                    var maFilter = new MovingAverageFilter(wSize);

                    // apply moving average filter
                    DiscreteSignal smoothed = maFilter.ApplyTo(signal);

                    // compute signal differences
                    DiscreteSignal diff = Operation.SpectralSubtract(signal, smoothed);

                    using (var stream = new FileStream($"{DATA_PATH}/moving-average-{wSize}.wav", FileMode.Create))
                    {
                        var smoothedFile = new WaveFile(smoothed);
                        smoothedFile.SaveTo(stream);
                    }

                    using (var stream = new FileStream($"{DATA_PATH}/diff-original-smoothed-{wSize}.wav", FileMode.Create))
                    {
                        var diffFile = new WaveFile(diff);
                        diffFile.SaveTo(stream);
                    }
                }
            }
        }