コード例 #1
0
ファイル: NoiseForm.cs プロジェクト: zlwind/NWaves
        private void loadnoiseToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var ofd = new OpenFileDialog
            {
                InitialDirectory = @"D:\Docs\Research\DATABASE\Various\Фоновые звуки"
            };

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

            using (var stream = new FileStream(ofd.FileName, FileMode.Open))
            {
                var waveFile = new WaveFile(stream);
                _bitDepth = waveFile.WaveFmt.BitsPerSample;
                _noise    = waveFile[Channels.Average];
            }

            _noise.Amplify(0.2f);

            if (_noise.SamplingRate != _signal.SamplingRate)
            {
                _noise = Operation.Resample(_noise, _signal.SamplingRate);
            }

            noisePlot.Signal = _signal + _noise;
        }
コード例 #2
0
ファイル: BasicSynth.cs プロジェクト: Connor14/SimpleSynth
        protected override DiscreteSignal Render(NoteSegment segment)
        {
            DiscreteSignal mainSignal = null;

            if (segment is PercussionNoteSegment percussionNoteSegment)
            {
                mainSignal = GetPercussionSignal(percussionNoteSegment.PercussionType, percussionNoteSegment.DurationSamples);
            }
            else if (segment is MelodicNoteSegment melodicNoteSegment)
            {
                // Combine a Sine wave and a Square wave
                mainSignal = SignalHelper.GetSine(melodicNoteSegment.Frequency, melodicNoteSegment.DurationSamples);
                mainSignal.CombineAdd(SignalHelper.GetSquare(melodicNoteSegment.Frequency, melodicNoteSegment.DurationSamples));
            }

            float velocityMultiplier = segment.Velocity / 127f; // Velocity ranges from 0 - 127

            // A simple way of doing an equalizer
            float balanceMultiplier = BalanceProvider.GetMultiplier(segment);

            // Scale the signals based on their velocity and balance multipliers
            mainSignal.Amplify(velocityMultiplier * balanceMultiplier);
            mainSignal.ApplyAdsr(AdsrEnvelopeProvider.CreateEnvelope(segment));

            return(mainSignal);
        }
コード例 #3
0
        // Make the maximum amplitudes equal to the given maximum amplitude
        // Theoretically, float values can range from [-1, 1] though at one point (for some reason) I was under the impressing that 1 would clip to 0
        // I think that was according to this: https://stackoverflow.com/questions/12112945/audio-units-samples-value-range
        public static void ScaleAmplitude(this DiscreteSignal mainSignal, float maximumAmplitude = 1f)
        {
            if (maximumAmplitude < -1 || maximumAmplitude > 1)
            {
                throw new ArgumentOutOfRangeException("Amplitude must be in range [-1f, 1f].");
            }

            if (mainSignal.Samples.Length == 0)
            {
                throw new ArgumentException("Signal has no samples.");
            }

            // Amplify based on the maximum / minimum amplitues so that the new max/min are 1
            float currentMax = mainSignal.Samples.Max();
            float currentMin = mainSignal.Samples.Min();

            float greater    = Math.Max(currentMax, Math.Abs(currentMin));
            float multiplier = maximumAmplitude / greater;

            mainSignal.Amplify(multiplier);

            // Check the new minimums and maximums. This might reveal issues with the floating point math
            float postAmplifyMax = mainSignal.Samples.Max();
            float postAmplifyMin = mainSignal.Samples.Min();

            if (postAmplifyMin < -1 || postAmplifyMax > 1)
            {
                throw new Exception(
                          string.Format(
                              "Maximum or minimum amplitude after amplification was not in the valid range of [-1, 1]. Maximum amplitude: {0}; Minimum amplitude: {1}",
                              postAmplifyMax,
                              postAmplifyMin
                              )
                          );
            }
        }